2022-03-18 10:20:03 -07:00
|
|
|
import { jest } from '@jest/globals';
|
2022-03-18 11:16:59 -07:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-05-26 10:37:22 -07:00
|
|
|
import LinkHeader from 'http-link-header';
|
2022-03-18 10:20:03 -07:00
|
|
|
|
2022-06-09 12:08:51 -07:00
|
|
|
import type { AxiosInstance, AxiosResponse } from 'axios';
|
|
|
|
|
2022-11-15 05:43:26 -08:00
|
|
|
const api = jest.requireActual('../index') as Record<string, Function>;
|
2022-03-18 10:20:03 -07:00
|
|
|
let mocks: Array<Function> = [];
|
|
|
|
|
2022-06-21 11:14:45 -07:00
|
|
|
export const __stub = (func: (mock: MockAdapter) => void) => mocks.push(func);
|
2022-03-18 10:20:03 -07:00
|
|
|
export const __clear = (): Function[] => mocks = [];
|
|
|
|
|
|
|
|
const setupMock = (axios: AxiosInstance) => {
|
2022-04-12 10:13:51 -07:00
|
|
|
const mock = new MockAdapter(axios, { onNoMatch: 'throwException' });
|
2022-03-18 10:20:03 -07:00
|
|
|
mocks.map(func => func(mock));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const staticClient = api.staticClient;
|
|
|
|
|
2022-05-26 10:37:22 -07:00
|
|
|
export const getLinks = (response: AxiosResponse): LinkHeader => {
|
|
|
|
return new LinkHeader(response.headers?.link);
|
|
|
|
};
|
|
|
|
|
2022-10-03 08:03:43 -07:00
|
|
|
export const getNextLink = (response: AxiosResponse) => {
|
|
|
|
const nextLink = new LinkHeader(response.headers?.link);
|
|
|
|
return nextLink.refs.find((ref) => ref.uri)?.uri;
|
|
|
|
};
|
|
|
|
|
2022-03-18 10:20:03 -07:00
|
|
|
export const baseClient = (...params: any[]) => {
|
|
|
|
const axios = api.baseClient(...params);
|
|
|
|
setupMock(axios);
|
|
|
|
return axios;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default (...params: any[]) => {
|
|
|
|
const axios = api.default(...params);
|
|
|
|
setupMock(axios);
|
|
|
|
return axios;
|
|
|
|
};
|