Skip to content

Commit

Permalink
fix: jsdoc for buildPath signature
Browse files Browse the repository at this point in the history
  • Loading branch information
KoichiKiyokawa committed Mar 12, 2023
1 parent 3a95dd5 commit 3238781
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,41 @@ pages/

```ts
type PathToParams = {
posts: never
"posts/[id]": { id: string | number }
"/posts": {
query?: Record<string, string | number | string[] | number[]>
hash?: string
}
"/posts/[id]": {
params: { id: string | number }
query?: Record<string, string | number | string[] | number[]>
hash?: string
}
}

/**
* @example
* buildPath('/posts/[id]', { params: { id: 1 }}) // => '/posts/1'
*/
export function buildPath<Path extends keyof PathToParams>(
path: Path,
...params: PathToParams[Path] extends never
? [
params?: {
searchParams?: Record<string, string | number>
hash?: string
}
]
: [
params: PathToParams[Path] & {
searchParams?: Record<string, string | number>
hash?: string
}
]
args: PathToParams[Path]
): string {
const [pathParams] = params
if (pathParams === undefined) return path

return (
path.replace(/\[(\w+)\]/g, (_, key) => pathParams[key]) +
(pathParams.searchParams
? "?" + new URLSearchParams(pathParams.searchParams as any).toString()
path.replace(/\[(\w+)\]/g, (_, key) => (args as any).params[key]) +
(args.query
? "?" + new URLSearchParams(args.query as any).toString()
: "") +
(pathParams.hash ? "#" + pathParams.hash : "")
(args.hash ? "#" + args.hash : "")
)
}

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

</details>
Expand All @@ -73,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]', {id:1})</code> | <code>pagesPath.posts.\_id(1).$url()</code> |
| API | <code>buildPath('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 |
4 changes: 2 additions & 2 deletions src/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test("bracket", async () => {
/**
* @example
* buildPath('/posts/[id]', { id: 1 }) // => '/posts/1'
* buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1'
*/
export function buildPath<Path extends keyof PathToParams>(
path: Path,
Expand Down Expand Up @@ -62,7 +62,7 @@ it("colon", async () => {
/**
* @example
* buildPath('/posts/[id]', { id: 1 }) // => '/posts/1'
* buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1'
*/
export function buildPath<Path extends keyof PathToParams>(
path: Path,
Expand Down
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type PathToParams = {
/**
* @example
* buildPath('/posts/[id]', { id: 1 }) // => '/posts/1'
* buildPath('/posts/[id]', { params: { id: 1 } }) // => '/posts/1'
*/
export function buildPath<Path extends keyof PathToParams>(
path: Path,
Expand Down

0 comments on commit 3238781

Please sign in to comment.