Skip to content
Myr

feat: Custom fetch method for Hono Client options (#920)

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

* Allow setting custom fetch method for hono client * Run yarn denoify

authored by Jonas Hiltl · committed by GitHub · parent 4c4bf2b · Browse files · 5 files+31 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+31 added−2 removed

  • tests:+27 added−0 removed
  • other:+4 added−2 removed

5 files+31 added−2 removed

modified:deno_dist/client/client.ts+1 added-1 removed
modified:deno_dist/client/types.ts+1 added
modified:src/client/client.test.ts+27 added
304 unmodified lines
305 type verify = Expect<Equal<boolean, typeof data.ok>>306 expect(data.ok).toBe(true)307 })308})309310describe('Use custom fetch method', () => {311 it('Should call the custom fetch method when provided', async () => {312 const fetchMock = jest.fn()313314 const api = new Hono().get('/search', (c) => c.jsonT({ ok: true }))315 const app = new Hono().route('/api', api)316 type AppType = typeof app317 const client = hc<AppType>('http://localhost', { fetch: fetchMock })318 await client.api.search.$get()319 expect(fetchMock).toHaveBeenCalledTimes(1)320 })321322 it('Should return Response from custom fetch method', async () => {323 const fetchMock = jest.fn()324 const returnValue = new Response(null, { status: 200 })325 fetchMock.mockReturnValueOnce(returnValue)326327 const api = new Hono().get('/search', (c) => c.jsonT({ ok: true }))328 const app = new Hono().route('/api', api)329 type AppType = typeof app330 const client = hc<AppType>('http://localhost', { fetch: fetchMock })331 const res = await client.api.search.$get()332 expect(res.ok).toBe(true)333 expect(res).toEqual(returnValue)334 })335})
modified:src/client/client.ts+1 added-1 removed
90 unmodified lines
91 methodUpperCase = this.method.toUpperCase()92 setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')9394 // Pass URL string to 1st arg for testing with MSW and node-fetch95 return fetch(url, {95 return (opt?.fetch || fetch)(url, {96 body: setBody ? this.rBody : undefined,97 method: methodUpperCase,98 headers: headers,99 })
26 unmodified lines
modified:src/client/types.ts+1 added
12 unmodified lines
13}1415export type RequestOptions = {16 headers?: Record<string, string>17 fetch?: typeof fetch18}1920type ClientRequest<S extends Data> = {21 [M in keyof S]: S[M] extends { input: infer R; output: infer O }
52 unmodified lines