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

add a specific requestHeaders

Open itwillrain opened this issue 1 year ago • 3 comments

Description

I'd like to add specific header such as operationId like bellow.

export const addPet = (options: Options<AddPetData>) =>
  (options?.client ?? _heyApiClient).post<AddPetResponse, AddPetError>({
    url: '/pets',
    ...options,
    headers: {
      'x-operation-id': 'addPet',
      ...(options?.headers ?? {}),
    },
  });

it seems plugin makes other file. the only way is probably change the file after generated. if there is another way, please let me know.

sorry if I miss-understand.

purpose

I wondering if there is operation Id in request header, probably easy to debug in such as cloud watch.

Thanks in advance.

itwillrain avatar Mar 30 '25 10:03 itwillrain

Hey @itwillrain, this isn't a feature today. Do you have any idea how you'd want to configure this if it existed?

mrlubos avatar Mar 30 '25 10:03 mrlubos

@mrlubos

Thanks for your reply!

I did not deep dive into heyApi code but, if it's possible, the interface like bellow could be nice. When set dependencies, intercept result and allow to update options.
If intercepter has openapi.yaml key arguments, would be perfect for me.

// config.ts

plugins: [
    '@hey-api/client-axios',
    {
      exportInlineEnums: true,
      identifierCase: 'preserve',
      enums: 'javascript',
      name: '@hey-api/typescript',
    },
    {
      name: '@hey-api/sdk',
      plugins: [
        {
          // custom plugins
          name: './add-operation-id-header',
        },
      ],
    },
  ],
// add-operation-id-header/config.ts

export const defaultConfig: Plugin.Config<Config> = {
  _dependencies: ['@hey-api/sdk'],
  _handler: handler,
  _handlerLegacy: () => {},
  myOption: false, // implements default value from types
  name: 'add-operation-id-header',
  output: 'sdk.gen.ts',  // or other flag 
};

or how about just adding options methods to sdk plugins??

options: (options, { operationId }) => {
      return {
           ...options, 
          {
             headers: {
                'x-operation-id': operationId,
                ...(options?.headers ?? {}),
             },
          }
      }
}

What do you think of it ??

itwillrain avatar Mar 30 '25 12:03 itwillrain

I have a similar issue right now. I have header parameters for which I would like to set default values based on some schema parameters, and it would be nice to have some intermediate hook that you could execute to create options for the request. By default, it will just return options as it is, and if you want to modify it, you have to call a function that passes useful context to this hook and node with option that you could change, but I'm not sure about this solution as I didn't work with anything like that, but definitely it would be nice to have.

If you have any other questions related to my use case, et,c I would be happy to answer them.

@mrlubos

mkeyy0 avatar Apr 23 '25 12:04 mkeyy0