Skip to content

AuthParams are ignored in request when set.Β #3038

@cre8

Description

@cre8

Description

When setting the authParameters in

the object mutates, but the changes are not persisted, when I print out the authorization headers in setAuthParams, they are set, but not after the call.

With a workaround like this it will be sent correctly

const beforeRequest = async (options: RequestOptions) => {
  // Build a temporary options object so we can apply auth headers
  // before constructing the immutable HttpRequest. IMPORTANT: do
  // not pass a shallow spread to setAuthParams because that would
  // prevent mutations from being visible to the caller.
  const preOpts: any = {
    ..._config,
    ...options,
    headers: mergeHeaders(_config.headers, options.headers),
    httpClient: options.httpClient ?? _config.httpClient,
    serializedBody: undefined,
  };

  if (preOpts.body !== undefined && preOpts.bodySerializer) {
    preOpts.serializedBody = preOpts.bodySerializer(preOpts.body);
  }

  // remove Content-Type header if body is empty to avoid sending invalid requests
  if (preOpts.body === undefined || preOpts.serializedBody === "") {
    preOpts.headers = preOpts.headers.delete
      ? preOpts.headers.delete("Content-Type")
      : preOpts.headers;
  }

  if (preOpts.security) {
    // Pass the same object so setAuthParams can mutate it.
    await setAuthParams(preOpts);
    // Log the updated header after mutation
    console.log(preOpts.headers?.getAll("Authorization"));
  }

  // Now create the actual request using the updated headers
  const { opts, req, url } = requestOptions({ ...options, headers: preOpts.headers });

  if (opts.requestValidator) {
    await opts.requestValidator(opts);
  }

  return { opts, req, url };
};

Reproducible example or configuration

https://stackblitz.com/edit/hey-api-client-fetch-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