openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Header in createClient not considered when resolving request

Open newza-fullmetal opened this issue 2 years ago • 3 comments

Description

If I add default headers in the createClient call

const sharedHeader = { workspace: env().workspace, 'x-web-version': '2' }
const client = createClient({ baseUrl: env().ENDPOINT, fetch: customFetchWithInterceptor, headers: sharedHeader })

It is not resolved when I use the client : client.GET('/internal/notifications/getuserwebnotifications', { params: { query } })

the error is : "the property header is missing in the type .... "

here the params typescript of this specific request :

    query?: {
        page?: number;
        page_size?: number;
        search?: string;
        ordering?: string;
        action?: string;
    };
    header: {
        workspace: string;
    };
}

Expected result

I would expect the header were inherited from the createClient call. But maybe I am missing something.

newza-fullmetal avatar Jan 16 '24 10:01 newza-fullmetal

That is how it should work, and we do have tests that the headers set in createClient persist. The only thing I can think of is the headers are passed along to the custom fetcher using a Headers() instance (not a plain object). Does your custom fetcher support that?

drwpow avatar Jan 16 '24 13:01 drwpow

I am facing the same issue. The value of the header is being persisted, but typescript is throwing error saying headers are not passed. What are we missing here? I was able to reproduce it here: https://stackblitz.com/edit/vitejs-vite-vgdvfq?file=src%2Fmain.ts

Running tsc on the terminal will output

❯ tsc
src/main.ts:34:3 - error TS2741: Property 'header' is missing in type '{ path: { id: string; }; }' but required in type '{ header: { 'X-ENGAGESPOT-API-KEY': string; 'X-ENGAGESPOT-USER-ID': string; 'Authentication Bearer'?: string | undefined; 'X-ENGAGESPOT-USER-SIGNATURE'?: string | undefined; }; path: { id: string; }; }'.

34   params: {
     ~~~~~~

  src/types/engagespot.v3.d.ts:240:9
    240         header: {
                ~~~~~~
    'header' is declared here.
  node_modules/openapi-fetch/dist/index.d.ts:101:9
    101     : { params: T["parameters"] }
                ~~~~~~
    The expected type comes from property 'params' which is declared here on type 'FetchOptions<{ parameters: { header: { 'X-ENGAGESPOT-API-KEY': string; 'X-ENGAGESPOT-USER-ID': string; 'Authentication Bearer'?: string | undefined; 'X-ENGAGESPOT-USER-SIGNATURE'?: string | undefined; }; path: { ...; }; }; responses: { ...; }; }>'

Mind taking a look? Thanks!

hemandev avatar Apr 11 '24 10:04 hemandev

So this is working as-intended; as @hemandev pointed out the header is being passed in runtime; it’s just the types that don’t remember what gets passed in createClient().

Would welcome a PR if someone is able to persist headers from createClient() to the individual request (each request should probably be a union of CreateClientHeaders & RequestOptions).

drwpow avatar Jun 24 '24 15:06 drwpow