forked from wednesday-solutions/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiUtils.js
83 lines (78 loc) · 2.66 KB
/
apiUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { create } from 'apisauce';
import snakeCase from 'lodash/snakeCase';
import camelCase from 'lodash/camelCase';
import { mapKeysDeep } from './index';
const API_TYPES = {
GITHUB: 'github',
DEFAULT: 'default'
};
const apiClients = {
[API_TYPES.GITHUB]: null,
[API_TYPES.DEFAULT]: null
};
/**
* Retrieves an API client for a specified type, defaulting to 'github'.
*
* @date 01/03/2024 - 14:47:28
*
* @param {string} [type='github'] - The type of API client to retrieve.
* @returns {Object} The requested API client, or undefined if it does not exist.
*/
export const getApiClient = (type = 'github') => apiClients[type];
/**
* Generates an API client for a specified type, defaulting to 'github'.
* If the type is 'github', it creates and stores an API client for GitHub.
* Otherwise, it creates and stores a default API client.
*
* @date 01/03/2024 - 14:48:09
*
* @param {string} [type='github'] - The type of API client to generate.
* @returns {Object} The generated API client.
*/
export const generateApiClient = (type = 'github') => {
if (type === API_TYPES.GITHUB) {
// store this value for time to come
// eslint-disable-next-line immutable/no-mutation
apiClients[type] = createApiClientWithTransForm(process.env.GITHUB_URL);
return apiClients[type];
}
// store this value for time to come
// eslint-disable-next-line immutable/no-mutation
apiClients.default = createApiClientWithTransForm(process.env.GITHUB_URL);
return apiClients[API_TYPES.DEFAULT];
};
/**
* Creates an API client with response and request transformations.
* The response transformation converts keys in the response data from snake_case to camelCase.
* The request transformation converts keys in the request data from camelCase to snake_case.
*
* @date 01/03/2024 - 14:47:28
*
* @param {string} baseURL - The base URL for the API client.
* @returns {Object} The API client with added transformations.
*/
export const createApiClientWithTransForm = (baseURL) => {
const api = create({
baseURL,
headers: { 'Content-Type': 'application/json' }
});
api.addResponseTransform((response) => {
const { ok, data } = response;
if (ok && data) {
// this needs to actually mutate the response
// eslint-disable-next-line immutable/no-mutation
response.data = mapKeysDeep(data, (keys) => camelCase(keys));
}
return response;
});
api.addRequestTransform((request) => {
const { data } = request;
if (data) {
// this needs to actually mutate the request
// eslint-disable-next-line immutable/no-mutation
request.data = mapKeysDeep(data, (keys) => snakeCase(keys));
}
return request;
});
return api;
};