Skip to content

[bug]: user provided headers instance ignored when generated headers existΒ #2715

@Theo-Steiner

Description

@Theo-Steiner

Description

related: #1065 #963

Motivation

using Headers with a client that has generated headers (e.g. every post request)

Issue

the generated types for the client allow for headers to either be passed as a POJO or an instance of Headers.

In certain cases, hey api generates headers to be used in addition to the user provided headers.
The desired behavior is, that those will be merged with the user provided headers.
Example:

/**
 * generated api function example (note that `Content-Type` is generated by hey SDK
 *  & will be merged with the user provided headers
 */
export const todoApiAdd = (options) => {
    return (options.client ?? _heyApiClient).post({
        url: '/todo/add',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

However, this implementation (introduced in #1065 )does not take into account that option.headers could be an instance of Headers.

Proposed fix

I believe the best path forward would be modifying the generated code to allow lossless merging of the two:

/**
 * Example of what a fix could look like
 */
export const todoApiAdd = (options) => {
    // create new headers object from headers, works with both headers instance and POJO
    const headers = new Headers(options.headers)
    // set generated headers
    headers.set('Content-Type', 'application/json')
    return (options.client ?? _heyApiClient).post({
        url: '/todo/add',
        ...options,
        headers
    });
};

Reproducible example or configuration

reproducible example

OpenAPI specification (optional)

No response

System information (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug πŸ”₯Something isn't workingclientClient package related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions