-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When this library is used by other libraries to perform requests, things like custom interceptors configured in the application don't trigger on those requests. Therefor we need a way to set and use a custom axios instance. Added config to set custom axios instance used by all requests, which defaults to the global axios. Closes #60
- Loading branch information
Gido Manders
committed
May 25, 2022
1 parent
3a1eff8
commit a5ccf05
Showing
6 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
type Config = { | ||
// The fetch variant which is used to make requests. | ||
api?: AxiosInstance; | ||
}; | ||
|
||
export default Config; | ||
|
||
const config: Config = { | ||
api: undefined // by default use axios as is | ||
}; | ||
|
||
/** | ||
* Configures the MadConnect library axios instance. | ||
* | ||
* @param {AxiosInstance} api An axios instance with extra configurations like interceptors | ||
*/ | ||
export function configureApi(api: AxiosInstance): void { | ||
config.api = api; | ||
} | ||
|
||
/** | ||
* Returns the config for the mad-connect library. | ||
* | ||
* @returns The Config | ||
*/ | ||
export function getConfig(): Config { | ||
return config; | ||
} | ||
|
||
/** | ||
* Convenience function to return 'api' from the config. | ||
* | ||
* Returns either a custom axios instance configured by | ||
* the user via 'configureApi' or the default axios | ||
* implementation provided by the Axios library. | ||
* | ||
* @export | ||
* @returns {AxiosInstance} Either the default axios or the configured axios instance. | ||
*/ | ||
export function getApi(): AxiosInstance { | ||
return config.api ? config.api : axios; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { configureApi, getConfig, getApi } from '../src/config'; | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
let windowSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
test('configuration lifecycle', async () => { | ||
expect.assertions(4); | ||
|
||
// By default it should use regular axios. | ||
expect(getConfig().api).toBe(undefined); | ||
expect(getApi()).toBe(axios); | ||
|
||
// @ts-expect-error Test mock | ||
const fakeApi: AxiosInstance = { test: true }; | ||
|
||
configureApi(fakeApi); | ||
|
||
// Now we expect the config to be set. | ||
expect(getConfig().api).toBe(fakeApi); | ||
expect(getApi()).toBe(fakeApi); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters