Skip to content

Commit f1dcb56

Browse files
committed
feat(history-sync): add encode interface
1 parent 3c2553a commit f1dcb56

6 files changed

Lines changed: 50 additions & 8 deletions

File tree

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` interface

docs/middleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { locales as middleware } from "nextra/locales";
2-

docs/postcss.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ module.exports = {
33
tailwindcss: {},
44
autoprefixer: {},
55
},
6-
}
6+
};
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import type { ActivityComponentType } from "@stackflow/react";
22

3+
type Params<K> = K extends ActivityComponentType<infer U>
4+
? U
5+
: Record<string, unknown>;
6+
37
export type Route<K> = {
48
path: string;
5-
decode?: (
6-
params: Record<string, string>,
7-
) => K extends ActivityComponentType<infer U> ? U : {};
9+
decode?: (params: Record<string, string>) => Params<K> | null;
10+
encode?: (params: Params<K>) => Record<string, string>;
811
};
912

1013
export type RouteLike<T> = string | string[] | Route<T> | Route<T>[];

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,37 @@ test("makeTemplate - parse with given decode function", () => {
7676
articleId: 1234,
7777
});
7878
});
79+
80+
test("makeTemplate - fill with given encode function", () => {
81+
const template = makeTemplate({
82+
path: "/articles/:articleId",
83+
encode: ({ id }) => ({
84+
articleId: String(id),
85+
}),
86+
});
87+
88+
expect(template.fill({ id: 1234 as unknown as string })).toEqual(
89+
"/articles/1234/",
90+
);
91+
});
92+
93+
test("makeTemplate - roundtrip with given encode and decode function", () => {
94+
const template = makeTemplate({
95+
path: "/articles/:articleId",
96+
encode: ({ id }) => ({
97+
articleId: String(id),
98+
}),
99+
decode: ({ articleId }) => ({
100+
id: Number(articleId),
101+
}),
102+
});
103+
104+
expect(template.fill(template.parse("/articles/1234/") ?? {})).toStrictEqual(
105+
"/articles/1234/",
106+
);
107+
expect(
108+
template.parse(template.fill({ id: 1234 as unknown as string })),
109+
).toStrictEqual({
110+
id: 1234,
111+
});
112+
});

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

Lines changed: 4 additions & 3 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);
@@ -59,10 +59,11 @@ export function makeTemplate<T>(
5959

6060
return {
6161
fill(params: { [key: string]: string | undefined }) {
62-
const pathname = pattern.stringify(params);
62+
const encodedParams = encode ? encode(params as any) : params;
63+
const pathname = pattern.stringify(encodedParams);
6364
const pathParams = pattern.match(pathname);
6465

65-
const searchParamsMap = { ...params };
66+
const searchParamsMap = { ...encodedParams };
6667

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

0 commit comments

Comments
 (0)