Skip to content

Commit

Permalink
fix: a "complete" URLObject in target should be left as it is (#529)
Browse files Browse the repository at this point in the history
* fix: a "complete" URLObject in `target` should be left as it is
  • Loading branch information
normanzb authored Nov 25, 2024
1 parent 505f64b commit 70af4ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-mugs-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frames.js": patch
---

fix: a "complete" URLObject in `target` should be left as it is
12 changes: 12 additions & 0 deletions packages/frames.js/src/core/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ describe("generateTargetURL", () => {
"http://test.com/test?test=test"
);
});

it("generates a correct URL if target is an URL object with every compulsory property", () => {
// If the user passing in a URL object with all props required to
// construct a full URL, that means they want to go straight to
// there without modification.
const baseUrl = new URL("http://test.com");
const target = new URL("http://another.com/test");

expect(generateTargetURL({ baseUrl, target }).toString()).toBe(
target.toString()
);
});
});

describe("resolveBaseUrl", () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/frames.js/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ function isValidButtonAction(action: unknown): action is ButtonActions {
return typeof action === "string" && action in buttonActionToCode;
}

function isUrlObjectComplete(urlObject: UrlObject): boolean {
return (
!!urlObject.host &&
!!urlObject.protocol &&
!!urlObject.pathname
);
}

export function generateTargetURL({
baseUrl,
target,
Expand All @@ -59,6 +67,9 @@ export function generateTargetURL({
}

if (typeof target === "object") {
if (isUrlObjectComplete(target)) {
return new URL(formatUrl(target));
}
return new URL(
formatUrl({
host: baseUrl.host,
Expand Down

0 comments on commit 70af4ca

Please sign in to comment.