Skip to content

Commit d644543

Browse files
committed
Replace axios with fetch
1 parent 22833db commit d644543

24 files changed

Lines changed: 6043 additions & 285 deletions

File tree

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"prepublishOnly": "npm run lint && npm run test"
1919
},
2020
"dependencies": {
21-
"axios": "^1.6.1",
22-
"form-data": "^4.0.0"
21+
"form-data": "^4.0.4"
2322
},
2423
"devDependencies": {
2524
"@babel/preset-env": "^7.23.3",
@@ -40,7 +39,7 @@
4039
"eslint-plugin-standard": "^5.0.0",
4140
"jest": "^29.7.0",
4241
"lodash": "^4.17.21",
43-
"nock": "^13.3.8",
42+
"nock": "^14.0.6",
4443
"prettier": "^3.0.3",
4544
"rimraf": "^5.0.5",
4645
"terser": "^5.24.0",
@@ -50,6 +49,6 @@
5049
"uuid": "^9.0.1"
5150
},
5251
"engines": {
53-
"node": ">=10"
52+
"node": ">=18"
5453
}
5554
}

src/helpers/mapKeys/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isObject } from '../isObject';
33
export const mapKeys = (
44
value: Record<string, unknown>,
55
callback: (key: string) => string,
6-
): Record<string, string | boolean | number> => {
6+
): Record<string, unknown> => {
77
if (value === undefined || !isObject(value)) {
88
return value;
99
}

src/modules/baseMessageModule/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export class BaseMessageModule extends BaseModule {
8888
const formData = this.getFormDataForVmsLocalFile(body, content);
8989

9090
const data = await this.httpClient.post<
91-
ApiMessageResponse | MessageErrorResponse,
9291
ApiMessageResponse | MessageErrorResponse
9392
>(this.endpoint, formData.getBuffer(), {
9493
headers: formData.getHeaders(),
@@ -102,7 +101,6 @@ export class BaseMessageModule extends BaseModule {
102101
}
103102

104103
const data = await this.httpClient.post<
105-
ApiMessageResponse | MessageErrorResponse,
106104
ApiMessageResponse | MessageErrorResponse
107105
>(this.endpoint, body);
108106

src/modules/baseModule/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { AxiosInstance } from 'axios';
1+
import { HttpClient } from '../../smsapi/httpClient';
22

33
export class BaseModule {
4-
protected httpClient: AxiosInstance;
4+
protected httpClient: HttpClient;
55

6-
constructor(httpClient: AxiosInstance) {
6+
constructor(httpClient: HttpClient) {
77
this.httpClient = httpClient;
88
}
99
}

src/modules/contacts/httpClient/formatResponseDates/index.spec.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { createAxiosResponse } from '../../../../testHelpers/createAxiosResponse';
2-
31
import { formatResponseDates } from '.';
42

53
describe('formatResponseDates', () => {
64
it('should format dates for ApiCollection<Group>', () => {
75
// given
8-
const response = createAxiosResponse({
9-
collection: [
10-
{
11-
date_created: '2020-09-01T14:49:00+02:00',
12-
date_updated: '2020-09-01T14:49:00+02:00',
13-
},
14-
],
15-
size: 1,
16-
});
6+
const response = {
7+
data: {
8+
collection: [
9+
{
10+
date_created: '2020-09-01T14:49:00+02:00',
11+
date_updated: '2020-09-01T14:49:00+02:00',
12+
},
13+
],
14+
size: 1,
15+
},
16+
};
1717

1818
// when
1919
const formattedResponse = formatResponseDates(response);
@@ -31,10 +31,12 @@ describe('formatResponseDates', () => {
3131

3232
it('should format dates for Group', () => {
3333
// given
34-
const response = createAxiosResponse({
35-
date_created: '2020-09-01T14:49:00+02:00',
36-
date_updated: '2020-09-01T14:49:00+02:00',
37-
});
34+
const response = {
35+
data: {
36+
date_created: '2020-09-01T14:49:00+02:00',
37+
date_updated: '2020-09-01T14:49:00+02:00',
38+
},
39+
};
3840

3941
// when
4042
const formattedResponse = formatResponseDates(response);
@@ -48,9 +50,11 @@ describe('formatResponseDates', () => {
4850

4951
it('should not format dates when date_created or date_updated are not present', () => {
5052
// given
51-
const response = createAxiosResponse({
52-
name: 'someName',
53-
});
53+
const response = {
54+
data: {
55+
name: 'someName',
56+
},
57+
};
5458

5559
// when
5660
const formattedResponse = formatResponseDates(response);

src/modules/contacts/httpClient/formatResponseDates/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { AxiosResponse } from 'axios';
2-
31
import { ApiCollection } from '../../../../types/ApiCollection';
42

53
interface ApiGroup {
@@ -30,7 +28,7 @@ const formatDates = (group: ApiGroup): Record<string, Date | string> => {
3028
};
3129
};
3230

33-
export const formatResponseDates = (response: AxiosResponse): AxiosResponse => {
31+
export const formatResponseDates = (response) => {
3432
const { data } = response;
3533

3634
if (isApiCollection(data)) {

src/modules/contacts/httpClient/prepareParamsForRequest/index.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import { stringify } from 'querystring';
2-
3-
import { InternalAxiosRequestConfig } from 'axios';
4-
51
import { formatDate } from '../../helpers/formatDate';
62
import { mapKeys } from '../../../../helpers/mapKeys';
73
import { mapValues } from '../../../../helpers/mapValues';
84
import { snakeCase } from '../../../../helpers/snakeCase';
5+
import { RequestInterceptor } from '../../../../smsapi/httpClient';
96

10-
const formatKeys = (data: Record<string, string | boolean | number>) => {
7+
const formatKeys = (data: Record<string, unknown>) => {
118
return mapKeys(data, snakeCase);
129
};
1310

14-
export const prepareParamsForRequest = (
15-
config: InternalAxiosRequestConfig,
16-
): InternalAxiosRequestConfig => {
17-
const { data, method, params } = config;
11+
export const prepareParamsForRequest: RequestInterceptor = (config) => {
12+
const { body, method, params } = config;
1813

19-
if (['get', 'delete'].includes((method as string).toLowerCase())) {
14+
if (params && ['get', 'delete'].includes((method as string).toLowerCase())) {
2015
let formattedParams = mapValues(params, (value, key) => {
2116
if (key === 'birthdayDate') {
2217
if (Array.isArray(value)) {
@@ -38,14 +33,13 @@ export const prepareParamsForRequest = (
3833
return {
3934
...config,
4035
params: formattedParams,
41-
paramsSerializer: (params) => stringify(params),
4236
};
4337
}
4438

45-
if (data) {
39+
if (body && typeof body === 'object' && !Buffer.isBuffer(body)) {
4640
return {
4741
...config,
48-
data: stringify(formatKeys(data)),
42+
body: formatKeys(body) as Record<string, string | number | boolean>,
4943
};
5044
}
5145

src/modules/contacts/index.ts

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import axios, { AxiosInstance } from 'axios';
2-
3-
import { extractDataFromResponse } from '../../smsapi/httpClient/extractDataFromResponse';
41
import { BaseModule } from '../baseModule';
52
import { ApiCollection } from '../../types/ApiCollection';
63
import { mapKeys } from '../../helpers/mapKeys';
74
import { snakeCase } from '../../helpers/snakeCase';
5+
import { HttpClient } from '../../smsapi/httpClient';
86

97
import { Contact } from './types/Contact';
108
import { NewContact } from './types/NewContact';
@@ -18,45 +16,31 @@ import { UpdateContact } from './types/UpdateContact';
1816
import { Fields } from './modules/fields';
1917

2018
export class Contacts extends BaseModule {
21-
private contactHttpClient: AxiosInstance;
22-
2319
public fields: Fields;
2420
public groups: Groups;
2521

26-
constructor(httpClient: AxiosInstance) {
22+
constructor(httpClient: HttpClient) {
2723
super(httpClient);
2824

29-
this.contactHttpClient = axios.create({
30-
adapter: httpClient.defaults.adapter,
31-
baseURL: httpClient.defaults.baseURL,
32-
headers: httpClient.defaults.headers,
33-
});
34-
35-
this.contactHttpClient.interceptors.request.use(prepareParamsForRequest);
36-
this.contactHttpClient.interceptors.response.use(formatResponseDates);
37-
this.contactHttpClient.interceptors.response.use(extractDataFromResponse);
25+
this.httpClient.addRequestInterceptor(prepareParamsForRequest);
26+
this.httpClient.addResponseInterceptor(formatResponseDates);
3827

39-
this.fields = new Fields(this.contactHttpClient);
40-
this.groups = new Groups(this.contactHttpClient);
28+
this.fields = new Fields(this.httpClient);
29+
this.groups = new Groups(this.httpClient);
4130
}
4231

4332
async get(params?: GetContactsQueryParams): Promise<ApiCollection<Contact>> {
44-
return await this.contactHttpClient.get<
45-
ApiCollection<Contact>,
46-
ApiCollection<Contact>
47-
>('/contacts', {
33+
return await this.httpClient.get<ApiCollection<Contact>>('/contacts', {
4834
params,
4935
});
5036
}
5137

5238
async getById(contactId: string): Promise<Contact> {
53-
return await this.contactHttpClient.get<Contact, Contact>(
54-
`/contacts/${contactId}`,
55-
);
39+
return await this.httpClient.get<Contact>(`/contacts/${contactId}`);
5640
}
5741

5842
async create(phoneNumber: string, details?: NewContact): Promise<Contact> {
59-
return await this.contactHttpClient.post<Contact, Contact>('/contacts', {
43+
return await this.httpClient.post<Contact>('/contacts', {
6044
phone_number: phoneNumber,
6145
...this.formatContactDetails(details || {}),
6246
});
@@ -66,27 +50,23 @@ export class Contacts extends BaseModule {
6650
contactId: string,
6751
updateContact: UpdateContact,
6852
): Promise<Contact> {
69-
return await this.contactHttpClient.put<Contact, Contact>(
70-
`/contacts/${contactId}`,
71-
{
72-
...this.formatContactDetails(updateContact || {}),
73-
},
74-
);
53+
return await this.httpClient.put<Contact>(`/contacts/${contactId}`, {
54+
...this.formatContactDetails(updateContact || {}),
55+
});
7556
}
7657

7758
async remove(contactId: string): Promise<void> {
78-
await this.contactHttpClient.delete(`/contacts/${contactId}`);
59+
await this.httpClient.delete(`/contacts/${contactId}`);
7960
}
8061

8162
async getGroups(contactId: string): Promise<ApiCollection<Group>> {
82-
return await this.contactHttpClient.get<
83-
ApiCollection<Group>,
84-
ApiCollection<Group>
85-
>(`/contacts/${contactId}/groups`);
63+
return await this.httpClient.get<ApiCollection<Group>>(
64+
`/contacts/${contactId}/groups`,
65+
);
8666
}
8767

8868
async getGroupById(contactId: string, groupId: string): Promise<Group> {
89-
return await this.contactHttpClient.get<Group, Group>(
69+
return await this.httpClient.get<Group>(
9070
`/contacts/${contactId}/groups/${groupId}`,
9171
);
9272
}
@@ -95,19 +75,16 @@ export class Contacts extends BaseModule {
9575
contactId: string,
9676
groupId: string,
9777
): Promise<ApiCollection<Group>> {
98-
return await this.contactHttpClient.put<
99-
ApiCollection<Group>,
100-
ApiCollection<Group>
101-
>(`/contacts/${contactId}/groups/${groupId}`);
78+
return await this.httpClient.put<ApiCollection<Group>>(
79+
`/contacts/${contactId}/groups/${groupId}`,
80+
);
10281
}
10382

10483
async unpinContactFromGroup(
10584
contactId: string,
10685
groupId: string,
10786
): Promise<void> {
108-
await this.contactHttpClient.delete(
109-
`/contacts/${contactId}/groups/${groupId}`,
110-
);
87+
await this.httpClient.delete(`/contacts/${contactId}/groups/${groupId}`);
11188
}
11289

11390
private formatContactDetails(details: NewContact): Record<string, unknown> {

src/modules/contacts/modules/fields/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,23 @@ import { FieldType } from './types/FieldType';
66

77
export class Fields extends BaseModule {
88
async get(): Promise<ApiCollection<Field>> {
9-
return await this.httpClient.get<
10-
ApiCollection<Field>,
11-
ApiCollection<Field>
12-
>('/contacts/fields');
9+
return await this.httpClient.get<ApiCollection<Field>>('/contacts/fields');
1310
}
1411

1512
async create(
1613
fieldName: string,
1714
fieldType: FieldType = 'text',
1815
): Promise<Field> {
19-
return await this.httpClient.post<Field, Field>('/contacts/fields', {
16+
return await this.httpClient.post<Field>('/contacts/fields', {
2017
name: fieldName,
2118
type: fieldType,
2219
});
2320
}
2421

2522
async update(fieldId: string, newName: string): Promise<Field> {
26-
return await this.httpClient.put<Field, Field>(
27-
`/contacts/fields/${fieldId}`,
28-
{
29-
name: newName,
30-
},
31-
);
23+
return await this.httpClient.put<Field>(`/contacts/fields/${fieldId}`, {
24+
name: newName,
25+
});
3226
}
3327

3428
async remove(fieldId: string): Promise<void> {

src/modules/contacts/modules/groups/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
import { BaseModule } from '../../../baseModule';
22
import { ApiCollection } from '../../../../types/ApiCollection';
3+
import { RequestConfig } from '../../../../smsapi/httpClient';
34

45
import { CreateGroupDetails } from './types/CreateGroupDetails';
56
import { Group } from './types/Group';
67
import { UpdateGroup } from './types/UpdateGroup';
78

89
export class Groups extends BaseModule {
910
async get(): Promise<ApiCollection<Group>> {
10-
return await this.httpClient.get<
11-
ApiCollection<Group>,
12-
ApiCollection<Group>
13-
>('/contacts/groups');
11+
return await this.httpClient.get<ApiCollection<Group>>('/contacts/groups');
1412
}
1513

1614
async getById(groupId: string): Promise<Group> {
17-
return await this.httpClient.get<Group, Group>(
18-
`/contacts/groups/${groupId}`,
19-
);
15+
return await this.httpClient.get<Group>(`/contacts/groups/${groupId}`);
2016
}
2117

2218
async create(name: string, details?: CreateGroupDetails): Promise<Group> {
23-
return await this.httpClient.post<Group, Group>('/contacts/groups', {
19+
return await this.httpClient.post<Group>('/contacts/groups', {
2420
name,
2521
...details,
2622
});
2723
}
2824

2925
async update(groupId: string, updateGroup: UpdateGroup): Promise<Group> {
30-
return await this.httpClient.put<Group, Group>(
26+
return await this.httpClient.put<Group>(
3127
`/contacts/groups/${groupId}`,
32-
updateGroup,
28+
updateGroup as RequestConfig['body'],
3329
);
3430
}
3531

0 commit comments

Comments
 (0)