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
Provenance
- Commit
35f15b7- Tree
829f60f- Author
niko-gardenanet <115976718+niko-gardenanet@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c99f696
5 files changed+73 added−9 removed
modified:deno_dist/client/client.ts+6 added-2 removed
33 unmodified lines3435363738383940414238 unmodified lines818283848585868788899091929369 unmodified lines33 unmodified lines constructor(url: string, method: string) { this.url = url this.method = method } fetch = ( fetch = async ( args?: ValidationTargets & { param?: Record<string, string> }, opt?: ClientRequestOptions38 unmodified lines let setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')
const headerValues: Record<string, string> = { ...(args?.header ?? {}), ...(opt?.headers ? opt.headers : {}), ...(typeof opt?.headers === 'function' ? await opt.headers() : opt?.headers ? opt.headers : {}), }
if (args?.cookie) { const cookies: string[] = []69 unmodified linesmodified:deno_dist/client/types.ts+4 added-2 removed
5 unmodified lines6 type HonoRequest = (typeof Hono.prototype)['request']7 8 export 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 }19 20 export type ClientRequest<S extends Schema> = {110 unmodified linesmodified:src/client/client.test.ts+53 added-1 removed
1 unmodified line2 /* eslint-disable @typescript-eslint/no-explicit-any */3 /* eslint-disable @typescript-eslint/ban-ts-comment */4 import { rest } from 'msw'5 import { setupServer } from 'msw/node'6-import { vi, expectTypeOf } from 'vitest'6+import { expectTypeOf, vi } from 'vitest'7 import { Hono } from '../hono'8 import { parse } from '../utils/cookie'9 import type { Equal, Expect } from '../utils/types'10 import { validator } from '../validator'610 unmodified lines621 622 expect(awaited).toEqual(client)623 })624 })625+626+describe('Dynamic headers', () => {627+ const app = new Hono()628+629+ const route = app.post('/posts', (c) => {630+ return c.json({631+ requestDynamic: 'dummy',632+ })633+ })634+635+ type AppType = typeof route636+637+ 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+ )646+647+ beforeAll(() => server.listen())648+ afterEach(() => server.resetHandlers())649+ afterAll(() => server.close())650+651+ let dynamic = ''652+653+ const client = hc<AppType>('http://localhost', {654+ headers: () => ({ 'x-hono': 'hono', 'x-dynamic': dynamic }),655+ })656+657+ it('Should have "x-dynamic": "one"', async () => {658+ dynamic = 'one'659+660+ const res = await client.posts.$post()661+662+ expect(res.ok).toBe(true)663+ const data = await res.json()664+ expect(data.requestDynamic).toEqual('one')665+ })666+667+ it('Should have "x-dynamic": "two"', async () => {668+ dynamic = 'two'669+670+ const res = await client.posts.$post()671+672+ 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 lines34 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?: ClientRequestOptions38 unmodified lines81 let setBody = !(methodUpperCase === 'GET' || methodUpperCase === 'HEAD')82 83 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 }91 92 if (args?.cookie) {93 const cookies: string[] = []69 unmodified linesmodified:src/client/types.ts+4 added-2 removed
5 unmodified lines6 type HonoRequest = (typeof Hono.prototype)['request']7 8 export 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 }19 20 export type ClientRequest<S extends Schema> = {110 unmodified lines