Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers passed in createClient not getting omitted from the headers in Client methods. #1617

Open
1 task
hemandev opened this issue Apr 12, 2024 · 1 comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@hemandev
Copy link

Description

import createFetchClient from 'openapi-fetch';

import type { paths } from './types/engagespot.v3';

const headers: CommonHeaders = {
  'X-ENGAGESPOT-API-KEY': '6lle29ey369dif42l5a7ne',
  'X-ENGAGESPOT-USER-ID': 'hemanditwiz@gmail.com',
};
export function createApiClient() {
  const apiClient = createFetchClient<paths>({
    baseUrl: 'https://api.engagespot.co/',
    headers,
  });

  return apiClient;
}

const engagespotClient = createApiClient();

const { data, error } = await engagespotClient.GET('/v3/notifications/{id}', {
  params: {
    path: {
      id: '24702304',
    },
    header: {},
  },
});

Since I passed the headers in createClient, GET shouldn't complain about required headers again.
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: { ...; }; }>'

Reproduction

How can this be reproduced / when did the error occur? Does the issue occur in a specific browser, or all browsers?

Expected result

(in case it’s not obvious)

Checklist

@hemandev hemandev added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Apr 12, 2024
@drwpow
Copy link
Owner

drwpow commented Apr 16, 2024

You know, come to think of it, there might be a way to infer which headers you passed in, but it may come with drawbacks and have some rough edges. But by this library’s design, static types are static types, and don’t respond to dynamic input. In order to get this to work, we’d have to modify your schema types from its original version, to no longer require those specific headers on every path.

I’d be open to a PR if someone wanted to take a crack at overriding the base schema types based on the options passed to createClient, but it may be a little tricky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

2 participants