panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

feat: add resetHttpOptionsDefaults for unsetting defaults #676

Closed kendlee closed 5 months ago

kendlee commented 5 months ago

Problem: Whenever custom.setHttpOptionsDefaults is called to set headers, there are no available method to remove/unset any of the added headers. This becomes problematic on the event that multiple Client instances need to be created, where the other doesn't need the preconfigured headers.

Currently, the only means to unset headers will be to override them is to call custom.setHttpOptionsDefaults, and set the same header key with a null value.

Proposal is to add a resetHttpOptionsDefaults method which unsets everything to the defaults, so that there is no need to unset any value explicitly.

Pseudo-code on usage:

custom.setHttpOptionsDefaults({
  headers: {
    Origin: 'some-origin', // required by microsoft provider
  }
});
const msclient = new issuer.Client(config);
// some code to retrieve refreshToken
const tokens = await msclient.refresh(credential.refreshToken); // uses header while performing request

// unless this is reset, all requests will include the preconfigured header
custom.resetHttpOptionsDefaults();

const anotherclient = new issuer.Client(anotherConfig);
panva commented 5 months ago

Hi @kendlee,

I recognize the many issues that come from default headers being configured on a library level.

That's why all options can be configured on a more granular bases as well. The Issuer constructor has the same issue as default library level though...

These options will be removed entirely in the next major in favour of a different API (similar to got.extend).

kendlee commented 5 months ago

@panva thanks for the information! tried out the granular config and it seems to work for my use case, closing this ticket!