axios-oauth-1.0a icon indicating copy to clipboard operation
axios-oauth-1.0a copied to clipboard

Add per-request oauth and improve CJS support

Open VastBlast opened this issue 2 years ago • 1 comments

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(...);

VastBlast avatar Jul 04 '23 07:07 VastBlast

  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

dobesv avatar Jul 05 '23 22:07 dobesv