Skip to content

Commit

Permalink
Allow customizing Axios client (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant authored Aug 18, 2022
1 parent ba5c377 commit 89d2954
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/common/interfaces/workos-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { AxiosRequestConfig } from 'axios';

export interface WorkOSOptions {
apiHostname?: string;
https?: boolean;
port?: number;
axios?: Omit<AxiosRequestConfig, 'baseURL'>;
}
20 changes: 20 additions & 0 deletions src/workos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ describe('WorkOS', () => {
expect(workos.baseURL).toEqual('https://localhost:4000');
});
});

describe('when the `axios` option is provided', () => {
it('applies the configuration to the Axios client', async () => {
mock.onPost().reply(200, 'OK', { 'X-Request-ID': 'a-request-id' });

const workos = new WorkOS('sk_test', {
axios: {
headers: {
'X-My-Custom-Header': 'Hey there!',
},
},
});

await workos.post('/somewhere', {});

expect(mock.history.post[0].headers).toMatchObject({
'X-My-Custom-Header': 'Hey there!',
});
});
});
});

describe('post', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/workos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ export class WorkOS {
}

this.client = axios.create({
...options.axios,
baseURL: this.baseURL,
headers: {
...options.axios?.headers,
Authorization: `Bearer ${this.key}`,
'User-Agent': `workos-node/${VERSION}`,
},
Expand Down

0 comments on commit 89d2954

Please sign in to comment.