Skip to content

Commit

Permalink
feat: enable to omit the second param
Browse files Browse the repository at this point in the history
  • Loading branch information
KoichiKiyokawa committed Mar 12, 2023
1 parent e4f000c commit bf4302d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm lint:check
- run: pnpm tsc --noEmit
- run: pnpm test:coverage
- run: pnpm test:e2e
- run: pnpm tsc --noEmit
- run: pnpm test:typecheck
- uses: codecov/codecov-action@v3.1.1
6 changes: 5 additions & 1 deletion e2e/generated/bracket/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ test("build path with params, query, hash", () => {
}),
).toBe("/posts/1/comments/2?q=1#section");

expect($path("/about", { query: { q: "hoge" } }));
expect($path("/about", { query: { q: "hoge" } })).toBe("/about?q=hoge");
});

test("can omit the second param", () => {
expect($path("/posts")).toBe("/posts");
});

test("$echoPath should returns the argument", () => {
Expand Down
14 changes: 10 additions & 4 deletions e2e/generated/bracket/output.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// prettier-ignore
// This file is auto generated. DO NOT EDIT

type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false;

type PathToParams = {
"/about": { query: import("../../projects/bracket/pages/about").Query; hash?: string };
"/posts": { query?: Record<string, string | number | string[] | number[]>; hash?: string };
"/posts/[id]/comments/[commentId]": {
params: { id: string | number; commentId: string | number };
query?: Record<string, string | number | string[] | number[]>;
Expand All @@ -16,12 +19,15 @@ type PathToParams = {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true
? [p?: PathToParams[Path]]
: [p: PathToParams[Path]]
): string {
const { params, query, hash } = args[0] ?? ({} as any);
return (
path.replace(/\[(\w+)\]/g, (_, key) => (args as any).params[key]) +
(args.query ? "?" + new URLSearchParams(args.query as any).toString() : "") +
(args.hash ? "#" + args.hash : "")
(params ? path.replace(/\[(\w+)\]/g, (_, key) => params[key]) : path) +
(query ? "?" + new URLSearchParams(query as any).toString() : "") +
(hash ? "#" + hash : "")
);
}

Expand Down
13 changes: 9 additions & 4 deletions e2e/generated/colon/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// prettier-ignore
// This file is auto generated. DO NOT EDIT

type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false;

type PathToParams = {
"/about": { query?: Record<string, string | number | string[] | number[]>; hash?: string };
"/posts/:id/comments/:commentId": {
Expand All @@ -16,12 +18,15 @@ type PathToParams = {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true
? [p?: PathToParams[Path]]
: [p: PathToParams[Path]]
): string {
const { params, query, hash } = args[0] ?? ({} as any);
return (
path.replace(/:(\w+)/g, (_, key) => (args as any).params[key]) +
(args.query ? "?" + new URLSearchParams(args.query as any).toString() : "") +
(args.hash ? "#" + args.hash : "")
(params ? path.replace(/:(\w+)/g, (_, key) => params[key]) : path) +
(query ? "?" + new URLSearchParams(query as any).toString() : "") +
(hash ? "#" + hash : "")
);
}

Expand Down
13 changes: 9 additions & 4 deletions e2e/generated/next-pages/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// prettier-ignore
// This file is auto generated. DO NOT EDIT

type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false;

type PathToParams = {
"/about": { query: import("../../projects/next-pages/pages/about").Query; hash?: string };
"/api/hello": { query?: Record<string, string | number | string[] | number[]>; hash?: string };
Expand All @@ -17,12 +19,15 @@ type PathToParams = {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true
? [p?: PathToParams[Path]]
: [p: PathToParams[Path]]
): string {
const { params, query, hash } = args[0] ?? ({} as any);
return (
path.replace(/\[(\w+)\]/g, (_, key) => (args as any).params[key]) +
(args.query ? "?" + new URLSearchParams(args.query as any).toString() : "") +
(args.hash ? "#" + args.hash : "")
(params ? path.replace(/\[(\w+)\]/g, (_, key) => params[key]) : path) +
(query ? "?" + new URLSearchParams(query as any).toString() : "") +
(hash ? "#" + hash : "")
);
}

Expand Down
Empty file.
26 changes: 16 additions & 10 deletions src/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ test("bracket", async () => {
"// prettier-ignore
// This file is auto generated. DO NOT EDIT
type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false
type PathToParams = {
}
Expand All @@ -27,14 +29,15 @@ test("bracket", async () => {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true ? [p?: PathToParams[Path]] : [p: PathToParams[Path]]
): string {
const { params, query, hash } = (args[0] ?? {} as any)
return (
path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => ((args as any).params)[key]) +
(args.query
? '?' + new URLSearchParams(args.query as any).toString()
(params ? path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => params[key]) : path) +
(query
? '?' + new URLSearchParams(query as any).toString()
: '') +
(args.hash ? '#' + args.hash : '')
(hash ? '#' + hash : '')
)
}
Expand All @@ -56,6 +59,8 @@ it("colon", async () => {
"// prettier-ignore
// This file is auto generated. DO NOT EDIT
type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false
type PathToParams = {
}
Expand All @@ -66,14 +71,15 @@ it("colon", async () => {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true ? [p?: PathToParams[Path]] : [p: PathToParams[Path]]
): string {
const { params, query, hash } = (args[0] ?? {} as any)
return (
path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => ((args as any).params)[key]) +
(args.query
? '?' + new URLSearchParams(args.query as any).toString()
(params ? path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => params[key]) : path) +
(query
? '?' + new URLSearchParams(query as any).toString()
: '') +
(args.hash ? '#' + args.hash : '')
(hash ? '#' + hash : '')
)
}
Expand Down
13 changes: 8 additions & 5 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export async function generate({
return `// prettier-ignore
// This file is auto generated. DO NOT EDIT
type IsAllPropertiesOptional<T> = { [K in keyof T]?: any } extends T ? true : false
type PathToParams = {
${(
await Promise.all(
Expand All @@ -78,14 +80,15 @@ type PathToParams = {
*/
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
...args: IsAllPropertiesOptional<PathToParams[Path]> extends true ? [p?: PathToParams[Path]] : [p: PathToParams[Path]]
): string {
const { params, query, hash } = (args[0] ?? {} as any)
return (
path.replace(${new RegExp(dynamicSegmentRegex, "g")}, (_, key) => ((args as any).params)[key]) +
(args.query
? '?' + new URLSearchParams(args.query as any).toString()
(params ? path.replace(${new RegExp(dynamicSegmentRegex, "g")}, (_, key) => params[key]) : path) +
(query
? '?' + new URLSearchParams(query as any).toString()
: '') +
(args.hash ? '#' + args.hash : '')
(hash ? '#' + hash : '')
)
}
Expand Down

0 comments on commit bf4302d

Please sign in to comment.