-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathapiUtils.js
97 lines (92 loc) · 3.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import wretch from 'wretch';
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) => {
// Middleware to transform request options
const transformRequestOptions = (next) => async (url, opts) => {
const { body } = opts;
if (body) {
// this needs to actually mutate the request
try {
const parsedBody = JSON.parse(body);
// eslint-disable-next-line immutable/no-mutation
opts.body = JSON.stringify(mapKeysDeep(parsedBody, (keys) => snakeCase(keys)));
} catch (error) {
console.error('Invalid JSON body:', error);
throw new Error('Invalid JSON body');
}
}
return next(url, opts);
};
// Create Wretch instance with middlewares and response resolver
return wretch(baseURL)
.headers({ 'Content-Type': 'application/json' })
.middlewares([transformRequestOptions])
.resolve(async (resolver) => {
try {
const response = await resolver.res((data) => data);
const data = await response.json();
return {
ok: response.ok,
status: response.status,
data: mapKeysDeep(data, (keys) => camelCase(keys))
};
} catch (error) {
return {
ok: false,
status: error.status,
data: error.json
};
}
});
};