Skip to content
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
15 changes: 15 additions & 0 deletions apps/builder/app/builder/shared/url-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ import { expect, test } from "vitest";
import {
compilePathnamePattern,
isPathnamePattern,
matchPathnamePattern,
tokenizePathnamePattern,
validatePathnamePattern,
} from "./url-pattern";

test("decode matched params", () => {
expect(matchPathnamePattern("/blog/:slug", "/blog/привет")).toEqual({
slug: "привет",
});
expect(
matchPathnamePattern(
"/blog/:slug",
"/blog/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"
)
).toEqual({
slug: "привет",
});
});

test("check pathname is pattern", () => {
expect(isPathnamePattern("/:name")).toEqual(true);
expect(isPathnamePattern("/:slug*")).toEqual(true);
Expand Down
22 changes: 20 additions & 2 deletions apps/builder/app/builder/shared/url-pattern.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { matchPathnameParams } from "@webstudio-is/sdk";
import { URLPattern } from "urlpattern-polyfill";

export { isPathnamePattern } from "@webstudio-is/sdk";

const baseUrl = "http://url";

const tryDecode = (encoded: string) => {
try {
return decodeURIComponent(encoded);
} catch {
return encoded;
}
};

export const matchPathnamePattern = (pattern: string, pathname: string) => {
try {
return new URLPattern({ pathname: pattern }).exec({ pathname })?.pathname
.groups;
const groups = new URLPattern({ pathname: pattern }).exec({ pathname })
?.pathname.groups;
if (groups) {
const decodedGroups: Record<string, undefined | string> = {};
for (const [name, value] of Object.entries(groups)) {
if (value) {
decodedGroups[name] = tryDecode(value);
}
}
return decodedGroups;
}
} catch {
// empty block
}
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"strip-indent": "^4.0.0",
"tiny-invariant": "^1.3.3",
"title-case": "^4.3.2",
"urlpattern-polyfill": "^10.0.0",
"urlpattern-polyfill": "^10.1.0",
"use-debounce": "^10.0.4",
"valid-filename": "^4.0.0",
"warn-once": "^0.1.1",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading