Skip to content

Commit 2fe932d

Browse files
authored
Refacto : network package updated with axios replaced by ky (#668)
* network package updated: axios is replaced by ky * esnext removed from typescript config
1 parent 79a9ccc commit 2fe932d

File tree

9 files changed

+124
-65
lines changed

9 files changed

+124
-65
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const i18nextInstanceMock: any = {
2+
language: 'en',
3+
changeLanguage: jest.fn(() => Promise.resolve(undefined)),
4+
on: jest.fn(),
5+
use: jest.fn(() => i18nextInstanceMock),
6+
init: jest.fn(() => Promise.resolve(undefined)),
7+
};
8+
9+
function createMockKyRequestFn() {
10+
return jest.fn(() =>
11+
Promise.resolve({
12+
status: 200,
13+
json: jest.fn(() => Promise.resolve({})),
14+
}),
15+
);
16+
}
17+
18+
export = {
19+
/* Actual exports */
20+
21+
/* Mocks */
22+
get: createMockKyRequestFn(),
23+
};

packages/public/network/package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"packageManager": "yarn@3.2.4",
66
"description": "MonkJs core package used to communicate with the API",
77
"author": "monkvision",
8-
"main": "lib/index.js",
9-
"types": "lib/index.d.ts",
8+
"main": "lib/src/index.js",
9+
"types": "lib/src/index.d.ts",
1010
"files": [
1111
"package.json",
1212
"README.md",
@@ -24,6 +24,15 @@
2424
"lint": "yarn run prettier && yarn run eslint",
2525
"lint:fix": "yarn run prettier:fix && yarn run eslint:fix"
2626
},
27+
"dependencies": {
28+
"jsonwebtoken": "^9.0.2",
29+
"jwt-decode": "^4.0.0",
30+
"ky": "^1.2.0"
31+
},
32+
"peerDependencies": {
33+
"react": "^17.0.2",
34+
"react-dom": "^17.0.2"
35+
},
2736
"devDependencies": {
2837
"@monkvision/common": "4.0.0",
2938
"@monkvision/eslint-config-base": "4.0.0",
@@ -66,10 +75,5 @@
6675
"bugs": {
6776
"url": "https://github.com/monkvision/monkjs/issues"
6877
},
69-
"homepage": "https://github.com/monkvision/monkjs",
70-
"dependencies": {
71-
"axios": "^1.6.2",
72-
"jsonwebtoken": "^9.0.2",
73-
"jwt-decode": "^4.0.0"
74-
}
78+
"homepage": "https://github.com/monkvision/monkjs"
7579
}

packages/public/network/src/api/config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AxiosRequestConfig } from 'axios';
21
import packageJson from '../../package.json';
32

43
export const sdkVersion = packageJson.version;
@@ -17,15 +16,20 @@ export interface MonkAPIConfig {
1716
authToken: string;
1817
}
1918

20-
export function getBaseAxiosConfig(config: MonkAPIConfig): AxiosRequestConfig {
19+
export interface KyConfig {
20+
baseUrl: string;
21+
headers: Record<string, string>;
22+
}
23+
24+
export function getKyConfig(config: MonkAPIConfig): KyConfig {
2125
const apiDomain = config.apiDomain.endsWith('/')
2226
? config.apiDomain.substring(0, config.apiDomain.length - 1)
2327
: config.apiDomain;
2428
const authorizationHeader = config.authToken.startsWith('Bearer ')
2529
? config.authToken
2630
: `Bearer ${config.authToken}`;
2731
return {
28-
baseURL: `https://${apiDomain}`,
32+
baseUrl: `https://${apiDomain}`,
2933
headers: {
3034
'Access-Control-Allow-Origin': '*',
3135
'Authorization': authorizationHeader,

packages/public/network/src/api/react.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { useMonkState } from '@monkvision/common';
1+
import {
2+
useMonkState,
3+
MonkAction,
4+
MonkActionType,
5+
MonkUpdateStateAction,
6+
} from '@monkvision/common';
27
import { Dispatch } from 'react';
3-
import { MonkAction, MonkActionType, MonkUpdateStateAction } from '@monkvision/common/src';
48
import { MonkAPIConfig } from './config';
59
import { MonkAPIRequest, MonkAPIResponse } from './requests/types';
610
import { MonkApi } from './requests';
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import axios from 'axios';
2-
import { getBaseAxiosConfig, MonkAPIConfig } from '../../config';
1+
import ky from 'ky';
2+
import { getKyConfig, MonkAPIConfig } from '../../config';
33
import { MonkAPIRequest } from '../types';
44
import { ApiInspectionGet } from '../../apiModels';
55
import { mapGetInspectionResponse } from './mappers';
@@ -11,15 +11,16 @@ export const getInspection: MonkAPIRequest<[id: string], ApiInspectionGet> = asy
1111
id: string,
1212
config: MonkAPIConfig,
1313
) => {
14-
const axiosResponse = await axios.request<ApiInspectionGet>({
15-
...getBaseAxiosConfig(config),
16-
method: 'get',
17-
url: `/inspections/${id}`,
14+
const { baseUrl, headers } = getKyConfig(config);
15+
const response = await ky.get(`${baseUrl}/inspections/${id}`, {
16+
headers,
1817
});
18+
const body = await response.json<ApiInspectionGet>();
1919
return {
2020
payload: {
21-
entities: mapGetInspectionResponse(axiosResponse.data),
21+
entities: mapGetInspectionResponse(body),
2222
},
23-
axiosResponse,
23+
response,
24+
body,
2425
};
2526
};

packages/public/network/src/api/requests/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AxiosResponse } from 'axios';
21
import { MonkUpdateStatePayload } from '@monkvision/common';
32
import { MonkAPIConfig } from '../config';
43

@@ -11,9 +10,13 @@ export interface MonkAPIResponse<T> {
1110
*/
1211
payload: MonkUpdateStatePayload;
1312
/**
14-
* The raw response object obtained from Axios when making the request.
13+
* The raw response object obtained from the fetch method when making the request.
1514
*/
16-
axiosResponse: AxiosResponse<T>;
15+
response: Response;
16+
/**
17+
* The body of the response.
18+
*/
19+
body: T;
1720
}
1821

1922
/**

packages/public/network/test/api/config.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import packageJson from '../../package.json';
2-
import { getBaseAxiosConfig, MonkAPIConfig, sdkVersion } from '../../src/api/config';
2+
import { getKyConfig, MonkAPIConfig, sdkVersion } from '../../src/api/config';
33

44
describe('Network package API global config utils', () => {
55
describe('sdkVersion global constant', () => {
@@ -8,30 +8,30 @@ describe('Network package API global config utils', () => {
88
});
99
});
1010

11-
describe('getBaseAxiosConfig function', () => {
11+
describe('getKyConfig function', () => {
1212
const baseConfig: MonkAPIConfig = {
1313
apiDomain: 'testapidomain',
1414
authToken: 'Bearer testtoken',
1515
};
1616

1717
it('should set the baseURL property', () => {
18-
expect(getBaseAxiosConfig(baseConfig)).toEqual(
18+
expect(getKyConfig(baseConfig)).toEqual(
1919
expect.objectContaining({
20-
baseURL: `https://${baseConfig.apiDomain}`,
20+
baseUrl: `https://${baseConfig.apiDomain}`,
2121
}),
2222
);
2323
});
2424

2525
it('should remove the ending slash from the baseURL property', () => {
26-
expect(getBaseAxiosConfig({ ...baseConfig, apiDomain: `${baseConfig.apiDomain}/` })).toEqual(
26+
expect(getKyConfig({ ...baseConfig, apiDomain: `${baseConfig.apiDomain}/` })).toEqual(
2727
expect.objectContaining({
28-
baseURL: `https://${baseConfig.apiDomain}`,
28+
baseUrl: `https://${baseConfig.apiDomain}`,
2929
}),
3030
);
3131
});
3232

3333
it('should set the Access-Control-Allow-Origin header', () => {
34-
expect(getBaseAxiosConfig(baseConfig)).toEqual(
34+
expect(getKyConfig(baseConfig)).toEqual(
3535
expect.objectContaining({
3636
headers: expect.objectContaining({
3737
'Access-Control-Allow-Origin': '*',
@@ -41,7 +41,7 @@ describe('Network package API global config utils', () => {
4141
});
4242

4343
it('should set the Authorization header', () => {
44-
expect(getBaseAxiosConfig(baseConfig)).toEqual(
44+
expect(getKyConfig(baseConfig)).toEqual(
4545
expect.objectContaining({
4646
headers: expect.objectContaining({
4747
Authorization: baseConfig.authToken,
@@ -52,7 +52,7 @@ describe('Network package API global config utils', () => {
5252

5353
it('should add the "Bearer " prefix to the token if it is missing', () => {
5454
const authToken = 'testtokentest';
55-
expect(getBaseAxiosConfig({ ...baseConfig, authToken })).toEqual(
55+
expect(getKyConfig({ ...baseConfig, authToken })).toEqual(
5656
expect.objectContaining({
5757
headers: expect.objectContaining({
5858
Authorization: `Bearer ${authToken}`,
@@ -62,7 +62,7 @@ describe('Network package API global config utils', () => {
6262
});
6363

6464
it('should set the X-Monk-SDK-Version header', () => {
65-
expect(getBaseAxiosConfig(baseConfig)).toEqual(
65+
expect(getKyConfig(baseConfig)).toEqual(
6666
expect.objectContaining({
6767
headers: expect.objectContaining({
6868
'X-Monk-SDK-Version': packageJson.version,

packages/public/network/test/api/requests/inspections/requests.test.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
1-
jest.mock('axios', () => ({
2-
request: jest.fn(() => Promise.resolve({ data: { test: 'hello' } })),
3-
}));
1+
import ky, { ResponsePromise } from 'ky';
2+
import { mapGetInspectionResponse } from '../../../../src/api/requests/inspections/mappers';
3+
import { getKyConfig, MonkAPIConfig } from '../../../../src/api/config';
4+
import { getInspection } from '../../../../src/api/requests/inspections';
5+
46
jest.mock('../../../../src/api/config', () => ({
5-
getBaseAxiosConfig: jest.fn(() => ({ baseUrl: 'test' })),
7+
getKyConfig: jest.fn(() => ({ baseUrl: 'test', headers: { test: 'hello' } })),
68
}));
79
jest.mock('../../../../src/api/requests/inspections/mappers', () => ({
810
mapGetInspectionResponse: jest.fn(() => ({ inspections: [{ id: 'test' }] })),
911
}));
1012

11-
import axios from 'axios';
12-
import { mapGetInspectionResponse } from '../../../../src/api/requests/inspections/mappers';
13-
import { getBaseAxiosConfig, MonkAPIConfig } from '../../../../src/api/config';
14-
import { getInspection } from '../../../../src/api/requests/inspections';
15-
1613
describe('Inspection requests', () => {
14+
afterEach(() => {
15+
jest.clearAllMocks();
16+
});
17+
1718
describe('getInspection function', () => {
18-
it('should pass the proper params to the axios request', async () => {
19+
it('should pass the proper params to the ky request', async () => {
1920
const id = 'inspection-id-test';
2021
const config: MonkAPIConfig = { apiDomain: 'test', authToken: 'wow' };
2122
await getInspection(id, config);
2223

23-
expect(getBaseAxiosConfig).toHaveBeenCalledWith(config);
24-
expect(axios.request).toHaveBeenCalledWith({
25-
...getBaseAxiosConfig(config),
26-
method: 'get',
27-
url: `/inspections/${id}`,
28-
});
24+
expect(getKyConfig).toHaveBeenCalledWith(config);
25+
expect(ky.get).toHaveBeenCalledWith(
26+
`${getKyConfig({} as MonkAPIConfig).baseUrl}/inspections/${id}`,
27+
{ headers: getKyConfig(config).headers },
28+
);
29+
});
30+
31+
it('should return the ky response', async () => {
32+
const status = 404;
33+
jest.spyOn(ky, 'get').mockImplementationOnce(
34+
() =>
35+
Promise.resolve({
36+
status,
37+
json: () => Promise.resolve({}),
38+
}) as ResponsePromise,
39+
);
40+
const result = await getInspection('test', {} as MonkAPIConfig);
41+
expect(result.response).toEqual(expect.objectContaining({ status }));
2942
});
3043

31-
it('should return the axiosResponse', async () => {
44+
it('should return the response body', async () => {
45+
const body = { hello: 'world' };
46+
jest.spyOn(ky, 'get').mockImplementationOnce(
47+
() =>
48+
Promise.resolve({
49+
json: () => Promise.resolve(body),
50+
}) as ResponsePromise,
51+
);
3252
const result = await getInspection('test', {} as MonkAPIConfig);
33-
expect(result.axiosResponse).toEqual(await (axios.request as jest.Mock)());
53+
expect(result.body).toEqual(body);
3454
});
3555

3656
it('should return the mapped entities', async () => {

yarn.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3721,7 +3721,7 @@ __metadata:
37213721
languageName: unknown
37223722
linkType: soft
37233723

3724-
"@monkvision/network@workspace:packages/public/network":
3724+
"@monkvision/network@4.0.0, @monkvision/network@workspace:packages/public/network":
37253725
version: 0.0.0-use.local
37263726
resolution: "@monkvision/network@workspace:packages/public/network"
37273727
dependencies:
@@ -3737,7 +3737,6 @@ __metadata:
37373737
"@types/node": ^18.11.9
37383738
"@typescript-eslint/eslint-plugin": ^5.43.0
37393739
"@typescript-eslint/parser": ^5.43.0
3740-
axios: ^1.6.2
37413740
eslint: ^8.29.0
37423741
eslint-config-airbnb-base: ^15.0.0
37433742
eslint-config-prettier: ^8.5.0
@@ -3751,12 +3750,16 @@ __metadata:
37513750
jest: ^29.3.1
37523751
jsonwebtoken: ^9.0.2
37533752
jwt-decode: ^4.0.0
3753+
ky: ^1.2.0
37543754
mkdirp: ^1.0.4
37553755
prettier: ^2.7.1
37563756
regexpp: ^3.2.0
37573757
rimraf: ^3.0.2
37583758
ts-jest: ^29.0.3
37593759
typescript: ^4.8.4
3760+
peerDependencies:
3761+
react: ^17.0.2
3762+
react-dom: ^17.0.2
37603763
languageName: unknown
37613764
linkType: soft
37623765

@@ -6713,17 +6716,6 @@ __metadata:
67136716
languageName: node
67146717
linkType: hard
67156718

6716-
"axios@npm:^1.6.2":
6717-
version: 1.6.2
6718-
resolution: "axios@npm:1.6.2"
6719-
dependencies:
6720-
follow-redirects: ^1.15.0
6721-
form-data: ^4.0.0
6722-
proxy-from-env: ^1.1.0
6723-
checksum: 4a7429e2b784be0f2902ca2680964391eae7236faa3967715f30ea45464b98ae3f1c6f631303b13dfe721b17126b01f486c7644b9ef276bfc63112db9fd379f8
6724-
languageName: node
6725-
linkType: hard
6726-
67276719
"axobject-query@npm:^3.1.1":
67286720
version: 3.2.1
67296721
resolution: "axobject-query@npm:3.2.1"
@@ -14134,6 +14126,13 @@ __metadata:
1413414126
languageName: node
1413514127
linkType: hard
1413614128

14129+
"ky@npm:^1.2.0":
14130+
version: 1.2.0
14131+
resolution: "ky@npm:1.2.0"
14132+
checksum: 10d436364d3e49e0c702adf90dfd0494812ba12853dfc4c1552a13150d769dfe0af339cd105f1076861f35ce1fb242e875858a2adb4fce62de0a5b5c7e9739fa
14133+
languageName: node
14134+
linkType: hard
14135+
1413714136
"language-subtag-registry@npm:~0.3.2":
1413814137
version: 0.3.22
1413914138
resolution: "language-subtag-registry@npm:0.3.22"
@@ -15216,6 +15215,7 @@ __metadata:
1521615215
"@monkvision/common-ui-web": 4.0.0
1521715216
"@monkvision/inspection-capture-web": 4.0.0
1521815217
"@monkvision/monitoring": 4.0.0
15218+
"@monkvision/network": 4.0.0
1521915219
"@monkvision/sentry": 4.0.0
1522015220
"@monkvision/sights": 4.0.0
1522115221
"@monkvision/types": 4.0.0

0 commit comments

Comments
 (0)