Skip to content

Commit

Permalink
feat!: rename buildPath to $path, echoPath to $echoPath
Browse files Browse the repository at this point in the history
  • Loading branch information
KoichiKiyokawa committed Mar 12, 2023
1 parent f773bf9 commit 6ae8e65
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 93 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path extends keyof PathToParams>(
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path]
): string {
Expand All @@ -62,9 +62,9 @@ export function buildPath<Path extends keyof PathToParams>(

/**
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
* $echoPath('/posts/[id]') // => '/posts/[id]'
*/
export function echoPath<Path extends keyof PathToParams>(path: Path): string {
export function $echoPath<Path extends keyof PathToParams>(path: Path): string {
return path
}
```
Expand All @@ -77,7 +77,7 @@ https://user-images.githubusercontent.com/40315079/212696306-ed6c9f88-4641-4549-

| | This library | [pathpida](https://github.com/aspida/pathpida) |
| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| API | <code>buildPath('posts/[id]', { params: { id: 1 }})</code> | <code>pagesPath.posts.\_id(1).$url()</code> |
| API | <code>$path('posts/[id]', { params: { id: 1 }})</code> | <code>pagesPath.posts.\_id(1).$url()</code> |
| 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<br><img width="564" alt="image" src="https://user-images.githubusercontent.com/40315079/213208755-c5f80f43-d59d-4a14-be76-da7316fb58bb.png"> | 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 |
30 changes: 15 additions & 15 deletions e2e/generated/bracket/output.test-d.ts
Original file line number Diff line number Diff line change
@@ -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"));
});
10 changes: 5 additions & 5 deletions e2e/generated/bracket/output.test.ts
Original file line number Diff line number Diff line change
@@ -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]");
});
8 changes: 4 additions & 4 deletions e2e/generated/bracket/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path extends keyof PathToParams>(
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
): string {
Expand All @@ -27,8 +27,8 @@ export function buildPath<Path extends keyof PathToParams>(

/**
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
* $echoPath('/posts/[id]') // => '/posts/[id]'
*/
export function echoPath<Path extends keyof PathToParams>(path: Path): string {
export function $echoPath<Path extends keyof PathToParams>(path: Path): string {
return path;
}
30 changes: 15 additions & 15 deletions e2e/generated/colon/output.test-d.ts
Original file line number Diff line number Diff line change
@@ -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"));
});
8 changes: 4 additions & 4 deletions e2e/generated/colon/output.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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");
});

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");
});
8 changes: 4 additions & 4 deletions e2e/generated/colon/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path extends keyof PathToParams>(
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
): string {
Expand All @@ -27,8 +27,8 @@ export function buildPath<Path extends keyof PathToParams>(

/**
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
* $echoPath('/posts/[id]') // => '/posts/[id]'
*/
export function echoPath<Path extends keyof PathToParams>(path: Path): string {
export function $echoPath<Path extends keyof PathToParams>(path: Path): string {
return path;
}
30 changes: 15 additions & 15 deletions e2e/generated/next-pages/output.test-d.ts
Original file line number Diff line number Diff line change
@@ -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"));
});
12 changes: 6 additions & 6 deletions e2e/generated/next-pages/output.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
8 changes: 4 additions & 4 deletions e2e/generated/next-pages/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path extends keyof PathToParams>(
export function $path<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
): string {
Expand All @@ -28,8 +28,8 @@ export function buildPath<Path extends keyof PathToParams>(

/**
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
* $echoPath('/posts/[id]') // => '/posts/[id]'
*/
export function echoPath<Path extends keyof PathToParams>(path: Path): string {
export function $echoPath<Path extends keyof PathToParams>(path: Path): string {
return path;
}
Loading

0 comments on commit 6ae8e65

Please sign in to comment.