Skip to content

fix: a "complete" URLObject in target should be left as it is #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check if the values are set right? URLObject is just saying that there are properties but their values can be empty right?

Also wouldn't it make sense to also allow URL class instance?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tha last point about URL instance is for generateTargetUrl function. baseUrl is typed like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLObject is already compatible with URL, the user is able to pass an URL object. Also to add URL explicitly the whole call stacks will be updated. as it is gaining nothing I think let's leave that part out?

I've updated value check into isUrlObjectComplete, pls have a look @michalkvasnicak , thanks!

!!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
Loading