From 6ae8e65eb0075abb683877c1b8c5ff228e759b80 Mon Sep 17 00:00:00 2001 From: Koichi Kiyokawa Date: Sun, 12 Mar 2023 18:48:13 +0900 Subject: [PATCH] feat!: rename `buildPath` to `$path`, `echoPath` to `$echoPath` --- README.md | 10 ++++---- e2e/generated/bracket/output.test-d.ts | 30 +++++++++++------------ e2e/generated/bracket/output.test.ts | 10 ++++---- e2e/generated/bracket/output.ts | 8 +++--- e2e/generated/colon/output.test-d.ts | 30 +++++++++++------------ e2e/generated/colon/output.test.ts | 8 +++--- e2e/generated/colon/output.ts | 8 +++--- e2e/generated/next-pages/output.test-d.ts | 30 +++++++++++------------ e2e/generated/next-pages/output.test.ts | 12 ++++----- e2e/generated/next-pages/output.ts | 8 +++--- src/generate.test.ts | 24 +++++++++--------- src/generate.ts | 8 +++--- 12 files changed, 93 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 37cc21b..5c43553 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,9 @@ type PathToParams = { /** * @example - * buildPath('/posts/[id]', { params: { id: 1 }}) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 }}) // => '/posts/1' */ -export function buildPath( +export function $path( path: Path, args: PathToParams[Path] ): string { @@ -62,9 +62,9 @@ export function buildPath( /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ -export function echoPath(path: Path): string { +export function $echoPath(path: Path): string { return path } ``` @@ -77,7 +77,7 @@ https://user-images.githubusercontent.com/40315079/212696306-ed6c9f88-4641-4549- | | This library | [pathpida](https://github.com/aspida/pathpida) | | ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | -| API | buildPath('posts/[id]', { params: { id: 1 }}) | pagesPath.posts.\_id(1).$url() | +| API | $path('posts/[id]', { params: { id: 1 }}) | pagesPath.posts.\_id(1).$url() | | Bundle Size | Constant even if the number of paths increases, because it only generates few functions. | Increases as paths increase, because it generates a big object. | | For long path(e.g. `/foo/bar/baz`) | Just select one completion and we can search path like fuzzy
image | Needs to push `.` key many times for `pagesPath.foo.bar.baz.$url()` | | Supported Frameworks | Any frameworks (thanks to its flexible configuration) | Next.js, Nuxt.js | diff --git a/e2e/generated/bracket/output.test-d.ts b/e2e/generated/bracket/output.test-d.ts index 92d853e..db8a43a 100644 --- a/e2e/generated/bracket/output.test-d.ts +++ b/e2e/generated/bracket/output.test-d.ts @@ -1,47 +1,47 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { test, assertType } from "vitest"; -import { buildPath } from "./output"; +import { $path } from "./output"; test("valid", () => { - assertType(buildPath("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 } })); + assertType($path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 } })); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, query: { q: "foo" }, }), ); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, hash: "section", }), ); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, query: { q: 1 }, hash: "section", }), ); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo" }, hash: "section" })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo" }, hash: "section" })); }); test("invalid", () => { // @ts-expect-error missing all of param - assertType(buildPath("/posts/[id]/comments/[commentId]")); + assertType($path("/posts/[id]/comments/[commentId]")); // @ts-expect-error missing commentId param - assertType(buildPath("/posts/[id]/comments/[commentId]", { id: 1 })); + assertType($path("/posts/[id]/comments/[commentId]", { id: 1 })); // @ts-expect-error missing id param - assertType(buildPath("/posts/[id]/comments/[commentId]", { commentId: 1 })); + assertType($path("/posts/[id]/comments/[commentId]", { commentId: 1 })); // @ts-expect-error - assertType(buildPath("/about", { id: 1 })); + assertType($path("/about", { id: 1 })); // @ts-expect-error - assertType(buildPath("/about", { searchParams: 1 })); + assertType($path("/about", { searchParams: 1 })); // @ts-expect-error - assertType(buildPath("/about", { hash: 1 })); + assertType($path("/about", { hash: 1 })); // @ts-expect-error - assertType(buildPath("not-defined")); + assertType($path("not-defined")); }); diff --git a/e2e/generated/bracket/output.test.ts b/e2e/generated/bracket/output.test.ts index bd5ffdc..59a9671 100644 --- a/e2e/generated/bracket/output.test.ts +++ b/e2e/generated/bracket/output.test.ts @@ -1,18 +1,18 @@ import { expect, test } from "vitest"; -import { buildPath, echoPath } from "./output"; +import { $path, $echoPath } from "./output"; test("build path with params, query, hash", () => { expect( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 2 }, query: { q: 1 }, hash: "section", }), ).toBe("/posts/1/comments/2?q=1#section"); - expect(buildPath("/about", { query: { q: "hoge" } })); + expect($path("/about", { query: { q: "hoge" } })); }); -test("echoPath should returns the argument", () => { - expect(echoPath("/posts/[id]/comments/[commentId]")).toBe("/posts/[id]/comments/[commentId]"); +test("$echoPath should returns the argument", () => { + expect($echoPath("/posts/[id]/comments/[commentId]")).toBe("/posts/[id]/comments/[commentId]"); }); diff --git a/e2e/generated/bracket/output.ts b/e2e/generated/bracket/output.ts index 1a546fc..aba5a69 100644 --- a/e2e/generated/bracket/output.ts +++ b/e2e/generated/bracket/output.ts @@ -12,9 +12,9 @@ type PathToParams = { /** * @example - * buildPath('/posts/[id]', { id: 1 }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ -export function buildPath( +export function $path( path: Path, args: PathToParams[Path], ): string { @@ -27,8 +27,8 @@ export function buildPath( /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ -export function echoPath(path: Path): string { +export function $echoPath(path: Path): string { return path; } diff --git a/e2e/generated/colon/output.test-d.ts b/e2e/generated/colon/output.test-d.ts index ae0cfec..6bbb19c 100644 --- a/e2e/generated/colon/output.test-d.ts +++ b/e2e/generated/colon/output.test-d.ts @@ -1,47 +1,47 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { test, assertType } from "vitest"; -import { buildPath } from "./output"; +import { $path } from "./output"; test("valid", () => { - assertType(buildPath("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 1 } })); + assertType($path("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 1 } })); assertType( - buildPath("/posts/:id/comments/:commentId", { + $path("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 1 }, query: { q: "foo" }, }), ); assertType( - buildPath("/posts/:id/comments/:commentId", { + $path("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 1 }, hash: "section", }), ); assertType( - buildPath("/posts/:id/comments/:commentId", { + $path("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 1 }, query: { q: 1 }, hash: "section", }), ); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo", hash: "section" } })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo", hash: "section" } })); }); test("invalid", () => { // @ts-expect-error - assertType(buildPath("posts/:id/comments/:commentId")); + assertType($path("posts/:id/comments/:commentId")); // @ts-expect-error - assertType(buildPath("posts/:id/comments/:commentId", { id: 1 })); + assertType($path("posts/:id/comments/:commentId", { id: 1 })); // @ts-expect-error - assertType(buildPath("posts/:id/comments/:commentId", { commentId: 1 })); + assertType($path("posts/:id/comments/:commentId", { commentId: 1 })); // @ts-expect-error - assertType(buildPath("about", { id: 1 })); + assertType($path("about", { id: 1 })); // @ts-expect-error - assertType(buildPath("about", { query: 1 })); + assertType($path("about", { query: 1 })); // @ts-expect-error - assertType(buildPath("about", { hash: 1 })); + assertType($path("about", { hash: 1 })); // @ts-expect-error - assertType(buildPath("not-defined")); + assertType($path("not-defined")); }); diff --git a/e2e/generated/colon/output.test.ts b/e2e/generated/colon/output.test.ts index b7b9e9a..4b1174c 100644 --- a/e2e/generated/colon/output.test.ts +++ b/e2e/generated/colon/output.test.ts @@ -1,9 +1,9 @@ import { expect, test } from "vitest"; -import { buildPath, echoPath } from "./output"; +import { $path, $echoPath } from "./output"; test("build path with params, query, hash", () => { expect( - buildPath("/posts/:id/comments/:commentId", { + $path("/posts/:id/comments/:commentId", { params: { id: 1, commentId: 2 }, query: { q: 1 }, hash: "section", @@ -11,6 +11,6 @@ test("build path with params, query, hash", () => { ).toBe("/posts/1/comments/2?q=1#section"); }); -test("echoPath should returns the argument", () => { - expect(echoPath("/posts/:id/comments/:commentId")).toBe("/posts/:id/comments/:commentId"); +test("$echoPath should returns the argument", () => { + expect($echoPath("/posts/:id/comments/:commentId")).toBe("/posts/:id/comments/:commentId"); }); diff --git a/e2e/generated/colon/output.ts b/e2e/generated/colon/output.ts index 1f32827..9c843af 100644 --- a/e2e/generated/colon/output.ts +++ b/e2e/generated/colon/output.ts @@ -12,9 +12,9 @@ type PathToParams = { /** * @example - * buildPath('/posts/[id]', { id: 1 }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ -export function buildPath( +export function $path( path: Path, args: PathToParams[Path], ): string { @@ -27,8 +27,8 @@ export function buildPath( /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ -export function echoPath(path: Path): string { +export function $echoPath(path: Path): string { return path; } diff --git a/e2e/generated/next-pages/output.test-d.ts b/e2e/generated/next-pages/output.test-d.ts index 92d853e..db8a43a 100644 --- a/e2e/generated/next-pages/output.test-d.ts +++ b/e2e/generated/next-pages/output.test-d.ts @@ -1,47 +1,47 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { test, assertType } from "vitest"; -import { buildPath } from "./output"; +import { $path } from "./output"; test("valid", () => { - assertType(buildPath("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 } })); + assertType($path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 } })); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, query: { q: "foo" }, }), ); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, hash: "section", }), ); assertType( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 1 }, query: { q: 1 }, hash: "section", }), ); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo" } })); - assertType(buildPath("/about", { query: { q: "foo" }, hash: "section" })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo" } })); + assertType($path("/about", { query: { q: "foo" }, hash: "section" })); }); test("invalid", () => { // @ts-expect-error missing all of param - assertType(buildPath("/posts/[id]/comments/[commentId]")); + assertType($path("/posts/[id]/comments/[commentId]")); // @ts-expect-error missing commentId param - assertType(buildPath("/posts/[id]/comments/[commentId]", { id: 1 })); + assertType($path("/posts/[id]/comments/[commentId]", { id: 1 })); // @ts-expect-error missing id param - assertType(buildPath("/posts/[id]/comments/[commentId]", { commentId: 1 })); + assertType($path("/posts/[id]/comments/[commentId]", { commentId: 1 })); // @ts-expect-error - assertType(buildPath("/about", { id: 1 })); + assertType($path("/about", { id: 1 })); // @ts-expect-error - assertType(buildPath("/about", { searchParams: 1 })); + assertType($path("/about", { searchParams: 1 })); // @ts-expect-error - assertType(buildPath("/about", { hash: 1 })); + assertType($path("/about", { hash: 1 })); // @ts-expect-error - assertType(buildPath("not-defined")); + assertType($path("not-defined")); }); diff --git a/e2e/generated/next-pages/output.test.ts b/e2e/generated/next-pages/output.test.ts index 5982694..8194638 100644 --- a/e2e/generated/next-pages/output.test.ts +++ b/e2e/generated/next-pages/output.test.ts @@ -1,19 +1,19 @@ import { expect, test } from "vitest"; -import { buildPath, echoPath } from "./output"; +import { $path, $echoPath } from "./output"; test("build path with params, query, hash", () => { expect( - buildPath("/posts/[id]/comments/[commentId]", { + $path("/posts/[id]/comments/[commentId]", { params: { id: 1, commentId: 2 }, query: { q: 1 }, hash: "section", }), ).toBe("/posts/1/comments/2?q=1#section"); - expect(buildPath("/about", { query: { q: "hoge" } })); + expect($path("/about", { query: { q: "hoge" } })); }); -test("echoPath should returns the argument", () => { - expect(echoPath("/posts/[id]/comments/[commentId]")).toBe("/posts/[id]/comments/[commentId]"); - expect(echoPath("/api/hello")).toBe("/api/hello"); +test("$echoPath should returns the argument", () => { + expect($echoPath("/posts/[id]/comments/[commentId]")).toBe("/posts/[id]/comments/[commentId]"); + expect($echoPath("/api/hello")).toBe("/api/hello"); }); diff --git a/e2e/generated/next-pages/output.ts b/e2e/generated/next-pages/output.ts index 0f454ee..beddb32 100644 --- a/e2e/generated/next-pages/output.ts +++ b/e2e/generated/next-pages/output.ts @@ -13,9 +13,9 @@ type PathToParams = { /** * @example - * buildPath('/posts/[id]', { id: 1 }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ -export function buildPath( +export function $path( path: Path, args: PathToParams[Path], ): string { @@ -28,8 +28,8 @@ export function buildPath( /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ -export function echoPath(path: Path): string { +export function $echoPath(path: Path): string { return path; } diff --git a/src/generate.test.ts b/src/generate.test.ts index 7ec6400..f861f30 100644 --- a/src/generate.test.ts +++ b/src/generate.test.ts @@ -23,11 +23,11 @@ test("bracket", async () => { /** * @example - * buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ - export function buildPath( - path: Path, - args: PathToParams[Path], + export function $path( + path: Path, + args: PathToParams[Path], ): string { return ( path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => ((args as any).params)[key]) + @@ -40,9 +40,9 @@ test("bracket", async () => { /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ - export function echoPath(path: Path): string { + export function $echoPath(path: Path): string { return path } " @@ -62,11 +62,11 @@ it("colon", async () => { /** * @example - * buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ - export function buildPath( - path: Path, - args: PathToParams[Path], + export function $path( + path: Path, + args: PathToParams[Path], ): string { return ( path.replace(/\\\\[(\\\\w+)\\\\]/g, (_, key) => ((args as any).params)[key]) + @@ -79,9 +79,9 @@ it("colon", async () => { /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ - export function echoPath(path: Path): string { + export function $echoPath(path: Path): string { return path } " diff --git a/src/generate.ts b/src/generate.ts index 40ebb35..26181b0 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -74,9 +74,9 @@ type PathToParams = { /** * @example - * buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' + * $path('/posts/[id]', { params: { id: 1 } }) // => '/posts/1' */ -export function buildPath( +export function $path( path: Path, args: PathToParams[Path], ): string { @@ -91,9 +91,9 @@ export function buildPath( /** * @example - * echoPath('/posts/[id]') // => '/posts/[id]' + * $echoPath('/posts/[id]') // => '/posts/[id]' */ -export function echoPath(path: Path): string { +export function $echoPath(path: Path): string { return path } `;