Skip to content

Commit

Permalink
Merge pull request #34 from ordermentum/axios-cache-key
Browse files Browse the repository at this point in the history
Have the client cache use headers as a key
  • Loading branch information
sugendran authored Jun 28, 2024
2 parents bba9610 + b9ad7c0 commit 9cab454
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/axios-asap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ordermentum/axios-asap",
"version": "0.2.0",
"version": "0.3.0",
"main": "build/index.js",
"files": [
"build/*"
Expand Down
29 changes: 20 additions & 9 deletions packages/axios-asap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ export const createAsapInterceptor = (authConfig: AuthHeaderConfig) => {

export const serviceToClientMap = new Map<string, AxiosInstance>();

const getDefaultAxiosConfig = (): AxiosRequestConfig => ({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});
let httpAgent: http.Agent;
let httpsAgent: https.Agent;

const getDefaultAxiosConfig = (): AxiosRequestConfig => {
if (!httpAgent) {
httpAgent = new http.Agent({ keepAlive: true });
}
if (!httpsAgent) {
httpsAgent = new https.Agent({ keepAlive: true });
}
return { httpAgent, httpsAgent }
};

export type Options = {
issuer: string;
Expand All @@ -35,14 +43,17 @@ export type Options = {

export const createClient = (
{ issuer, service, publicKey, privateKey }: Options,
authConfig: Pick<
AuthHeaderConfig,
'insecureMode' | 'additionalClaims' | 'tokenExpiryMs'
authConfig: Partial<
Pick<
AuthHeaderConfig,
'insecureMode' | 'additionalClaims' | 'tokenExpiryMs'
>
> = {},
axiosOptions: AxiosRequestConfig = {}
) => {
const DEFAULT_TOKEN_EXPIRY_MS = 60 * 5 * 1000; // 5 minute expiry
const issuerServiceKey = `${issuer}:${service}`;
const headers = JSON.stringify(axiosOptions.headers ?? {});
const issuerServiceKey = `${issuer}:${service}:${headers}`;

// Check if cache has a client
if (!authConfig.insecureMode && serviceToClientMap.has(issuerServiceKey))
Expand All @@ -67,7 +78,7 @@ export const createClient = (
});

const client = axios.create(axiosCreateOpts);
// @ts-ignore

client.interceptors.request.use(asapInterceptor);
serviceToClientMap.set(issuerServiceKey, client);

Expand Down
92 changes: 92 additions & 0 deletions packages/axios-asap/test/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,96 @@ describe('createAuthHeaderGenerator', () => {
expect(Object.keys(res?.config?.headers ?? {})).to.include('Authorization');
expect(res?.config?.headers?.Authorization).to.include('Bearer');
});

it('the cache is aware of headers and returns different clients for them', () => {
const client1 = createClient(
{
service: 'the-keyid',
issuer: 'an-issuer',
publicKey: 'public-key',
privateKey: privateKeyPem,
},
{
insecureMode: false,
additionalClaims: {
admin: true,
userId: '123',
},
},
{
headers: {
'x-custom-header': '1',
},
}
);
const client2 = createClient(
{
service: 'the-keyid',
issuer: 'an-issuer',
publicKey: 'public-key',
privateKey: privateKeyPem,
},
{
insecureMode: false,
additionalClaims: {
admin: true,
userId: '123',
},
},
{
headers: {
'x-custom-header': '2',
},
}
);
expect(client1).to.not.equal(client2);
expect(client1.defaults.headers['x-custom-header']).to.equal('1');
expect(client2.defaults.headers['x-custom-header']).to.equal('2');
});

it('the cache returns the same client if the headers match', () => {
const client1 = createClient(
{
service: 'the-keyid',
issuer: 'an-issuer',
publicKey: 'public-key',
privateKey: privateKeyPem,
},
{
insecureMode: false,
additionalClaims: {
admin: true,
userId: '123',
},
},
{
headers: {
'x-custom-header': '1',
},
}
);
const client2 = createClient(
{
service: 'the-keyid',
issuer: 'an-issuer',
publicKey: 'public-key',
privateKey: privateKeyPem,
},
{
insecureMode: false,
additionalClaims: {
admin: true,
userId: '123',
},
},
{
headers: {
'x-custom-header': '1',
},
}
);
expect(client1).to.equal(client2);
expect(client1.defaults.headers['x-custom-header']).to.equal('1');
expect(client2.defaults.headers['x-custom-header']).to.equal('1');
})
});

0 comments on commit 9cab454

Please sign in to comment.