Skip to content
Myr

feat(client): add `param` option to `$url()` (#1885)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(client): add `param` option to `$url()` * denoify

authored by Yusuke Wada · committed by GitHub · parent 2333706 · Browse files · 5 files+40 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+40 added−2 removed

  • tests:+20 added−0 removed
  • other:+20 added−2 removed

5 files+40 added−2 removed

modified:deno_dist/client/client.ts+3 added
modified:deno_dist/client/types.ts+7 added-1 removed
21 unmodified lines
22 : (args?: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>23 : never24 : never25} & {26 $url: () => URL26 $url: (27 arg?: S[keyof S] extends { input: infer R }28 ? R extends { param: infer P }29 ? { param: P }30 : {}31 : {}32 ) => URL33}3435// eslint-disable-next-line @typescript-eslint/no-explicit-any36type BlankRecordToNever<T> = T extends any ? (keyof T extends never ? never : T) : never
84 unmodified lines
modified:src/client/client.test.ts+20 added
603 unmodified lines
604 const json = await res.json()605 expectTypeOf(json).toEqualTypeOf<{ data: string } | { message: string }>()606 })607})608609describe('$url() with a param option', () => {610 const app = new Hono().get('/posts/:id/comments', (c) => c.json({ ok: true }))611 type AppType = typeof app612 const client = hc<AppType>('http://localhost')613614 it('Should return the correct path - /posts/123/comments', async () => {615 const url = client.posts[':id'].comments.$url({616 param: {617 id: '123',618 },619 })620 expect(url.pathname).toBe('/posts/123/comments')621 })622623 it('Should return the correct path - /posts/:id/comments', async () => {624 const url = client.posts[':id'].comments.$url()625 expect(url.pathname).toBe('/posts/:id/comments')626 })627})
modified:src/client/client.ts+3 added
140 unmodified lines
141142 const path = parts.join('/')143 const url = mergePath(baseUrl, path)144 if (method === 'url') {145 if (opts.args[0] && opts.args[0].param) {146 return new URL(replaceUrlParam(url, opts.args[0].param))147 }148 return new URL(url)149 }150151 const req = new ClientRequestImpl(url, method)
7 unmodified lines
modified:src/client/types.ts+7 added-1 removed
21 unmodified lines
22 : (args?: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>23 : never24 : never25} & {26 $url: () => URL26 $url: (27 arg?: S[keyof S] extends { input: infer R }28 ? R extends { param: infer P }29 ? { param: P }30 : {}31 : {}32 ) => URL33}3435// eslint-disable-next-line @typescript-eslint/no-explicit-any36type BlankRecordToNever<T> = T extends any ? (keyof T extends never ? never : T) : never
84 unmodified lines