Skip to content

Commit 72656d6

Browse files
authored
Merge branch 'main' into main
2 parents f7d09aa + 45fb7ba commit 72656d6

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

.changeset/purple-chefs-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@stackflow/plugin-history-sync": minor
3+
---
4+
5+
Add encode option to Route interface for converting activity params to URL string params

extensions/plugin-history-sync/src/RouteLike.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export type Route<ComponentType> = {
99
decode?: (
1010
params: Record<string, string>,
1111
) => ComponentType extends ActivityComponentType<infer U> ? U : {};
12+
encode?: (
13+
params: ComponentType extends ActivityComponentType<infer U>
14+
? U
15+
: Record<string, any>,
16+
) => Record<string, string | undefined>;
1217
defaultHistory?: (
1318
params: Record<string, string>,
1419
) => HistoryEntry[] | DefaultHistoryDescriptor;

extensions/plugin-history-sync/src/makeTemplate.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,20 @@ test("makeTemplate - parse with given decode function", () => {
7676
articleId: 1234,
7777
});
7878
});
79+
80+
test("makeTemplate - fill with encode function using JSON.stringify for object params", () => {
81+
const template = makeTemplate({
82+
path: "/search",
83+
encode: (params) => ({
84+
filter: JSON.stringify(params.filter),
85+
}),
86+
});
87+
88+
expect(
89+
template.fill({
90+
filter: { category: "tech", tags: ["javascript", "react"] },
91+
}),
92+
).toEqual(
93+
"/search/?filter=%7B%22category%22%3A%22tech%22%2C%22tags%22%3A%5B%22javascript%22%2C%22react%22%5D%7D",
94+
);
95+
});

extensions/plugin-history-sync/src/makeTemplate.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface UrlPatternOptions {
4646
}
4747

4848
export function makeTemplate<T>(
49-
{ path, decode }: Route<T>,
49+
{ path, decode, encode }: Route<T>,
5050
urlPatternOptions?: UrlPatternOptions,
5151
) {
5252
const pattern = new UrlPattern(`${path}(/)`, urlPatternOptions);
@@ -58,11 +58,14 @@ export function makeTemplate<T>(
5858
: (pattern as any).names.length;
5959

6060
return {
61-
fill(params: { [key: string]: string | undefined }) {
62-
const pathname = pattern.stringify(params);
61+
fill(params: { [key: string]: any }) {
62+
const encodedParams: { [key: string]: string | undefined } = encode
63+
? encode(params as Parameters<typeof encode>[0])
64+
: params;
65+
const pathname = pattern.stringify(encodedParams);
6366
const pathParams = pattern.match(pathname);
6467

65-
const searchParamsMap = { ...params };
68+
const searchParamsMap = { ...encodedParams };
6669

6770
Object.keys(pathParams).forEach((key) => {
6871
delete searchParamsMap[key];

0 commit comments

Comments
 (0)