Skip to content
Myr

feat(client): accept a function that provides dynamic headers to hc (#2461)

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

authored by niko-gardenanet · committed by GitHub · parent c99f696 · Browse files · 5 files+73 added−9 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+73 added−9 removed

  • tests:+53 added−1 removed
  • other:+20 added−8 removed

5 files+73 added−9 removed

modified:deno_dist/client/client.ts+6 added-2 removed
modified:deno_dist/client/types.ts+4 added-2 removed
5 unmodified lines
6type HonoRequest = (typeof Hono.prototype)['request']78export type ClientRequestOptions<T = unknown> = keyof T extends never9 ? {10 headers?: Record<string, string>10 headers?:11 | Record<string, string>12 | (() => Record<string, string> | Promise<Record<string, string>>)13 fetch?: typeof fetch | HonoRequest14 }15 : {14 headers: T16 headers: T | (() => T | Promise<T>)17 fetch?: typeof fetch | HonoRequest18 }1920export type ClientRequest<S extends Schema> = {
110 unmodified lines
modified:src/client/client.test.ts+53 added-1 removed
1 unmodified line
2/* eslint-disable @typescript-eslint/no-explicit-any */3/* eslint-disable @typescript-eslint/ban-ts-comment */4import { rest } from 'msw'5import { setupServer } from 'msw/node'6import { vi, expectTypeOf } from 'vitest'6import { expectTypeOf, vi } from 'vitest'7import { Hono } from '../hono'8import { parse } from '../utils/cookie'9import type { Equal, Expect } from '../utils/types'10import { validator } from '../validator'
610 unmodified lines
621622 expect(awaited).toEqual(client)623 })624})625626describe('Dynamic headers', () => {627 const app = new Hono()628629 const route = app.post('/posts', (c) => {630 return c.json({631 requestDynamic: 'dummy',632 })633 })634635 type AppType = typeof route636637 const server = setupServer(638 rest.post('http://localhost/posts', async (req, res, ctx) => {639 const requestDynamic = req.headers.get('x-dynamic')640 const payload = {641 requestDynamic,642 }643 return res(ctx.status(200), ctx.json(payload))644 })645 )646647 beforeAll(() => server.listen())648 afterEach(() => server.resetHandlers())649 afterAll(() => server.close())650651 let dynamic = ''652653 const client = hc<AppType>('http://localhost', {654 headers: () => ({ 'x-hono': 'hono', 'x-dynamic': dynamic }),655 })656657 it('Should have "x-dynamic": "one"', async () => {658 dynamic = 'one'659660 const res = await client.posts.$post()661662 expect(res.ok).toBe(true)663 const data = await res.json()664 expect(data.requestDynamic).toEqual('one')665 })666667 it('Should have "x-dynamic": "two"', async () => {668 dynamic = 'two'669670 const res = await client.posts.$post()671672 expect(res.ok).toBe(true)673 const data = await res.json()674 expect(data.requestDynamic).toEqual('two')675 })676})
modified:src/client/client.ts+6 added-2 removed
33 unmodified lines
34 constructor(url: string, method: string) {35 this.url = url36 this.method = method37 }38 fetch = (38 fetch = async (39 args?: ValidationTargets & {40 param?: Record<string, string>41 },42 opt?: ClientRequestOptions
38 unmodified lines
81 let setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')8283 const headerValues: Record<string, string> = {84 ...(args?.header ?? {}),85 ...(opt?.headers ? opt.headers : {}),85 ...(typeof opt?.headers === 'function'86 ? await opt.headers()87 : opt?.headers88 ? opt.headers89 : {}),90 }9192 if (args?.cookie) {93 const cookies: string[] = []
69 unmodified lines
modified:src/client/types.ts+4 added-2 removed
5 unmodified lines
6type HonoRequest = (typeof Hono.prototype)['request']78export type ClientRequestOptions<T = unknown> = keyof T extends never9 ? {10 headers?: Record<string, string>10 headers?:11 | Record<string, string>12 | (() => Record<string, string> | Promise<Record<string, string>>)13 fetch?: typeof fetch | HonoRequest14 }15 : {14 headers: T16 headers: T | (() => T | Promise<T>)17 fetch?: typeof fetch | HonoRequest18 }1920export type ClientRequest<S extends Schema> = {
110 unmodified lines