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

Add per-request oauth and improve CJS support #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

VastBlast
Copy link

This PR adds improved CJS support:

// rather than having to do this:
const { default: addOAuthInterceptor } = require('axios-oauth-1.0a');

// you can now do this:
const addOAuthInterceptor = require('axios-oauth-1.0a');

It also adds the ability to add/override oauth options per individual request:

// Create a client
const client = axios.create();

// Add interceptor
addOAuthInterceptor(client);

// send a signed request
client.get('https://example.com/example', {
    algorithm: 'HMAC-SHA1',
    key: 'someKey1',
    secret: 'someSecret1',
}).then(...);

// send another signed request with a different key/secret
client.get('https://example.com/example', {
    algorithm: 'HMAC-SHA1',
    key: 'someKey2',
    secret: 'someSecret2',
}).then(...);

OR:

// Create a client
const client = axios.create();

// Add interceptor
addOAuthInterceptor(client, {
    algorithm: 'HMAC-SHA1',
    key: 'xxx',
    secret: 'yyy',
});

// send a signed request with the algorithm/key/secret set above, and token set below
client.get('https://example.com/example', {
    token: 'someToken1'
}).then(...);

// send another signed request with a different token and added tokenSecret
client.get('https://example.com/example', {
    token: 'someToken2',
    tokenSecret: 'someTokenSecret'
}).then(...);

@dobesv
Copy link
Owner

dobesv commented Jul 5, 2023

  1. Can you do this without checking in the generated resources? I am opposed to checking in built/generated files.
  2. Maybe there's a way to improve the CJS compatibility while avoiding the module.exports = hack? Maybe have seperate exports for ESM and CJS clients? like in https://antfu.me/posts/publish-esm-and-cjs

@@ -94,30 +98,43 @@ const addOAuthInterceptor = (
tokenSecret = null,
callback = null,
verifier = null,
}: OAuthInterceptorConfig
}: Partial<OAuthInterceptorConfig> = {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we lose something here by not requiring the required fields any more. Maybe this argument could be totally optional and if it's not provided then you MUST provide a per-request config. I'm a bit doubtful people will really want to provide some "defaults" here and also provide per-request "overrides". And they could manage that themselves without too much difficulty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants