-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAxiosGenerator.js
45 lines (39 loc) · 1.36 KB
/
AxiosGenerator.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
const AxiosGenerator = function () {
this.generate = function (context, requests, options) {
const request = requests[0];
const config = {
method: request.method,
url: request.urlBase,
...extract(request, 'urlParameters', 'params'),
...extract(request, 'headers'),
...extract(request, 'httpBasicAuth', 'auth'),
...extract(request, 'timeout'),
...body(request)
};
return `axios(${JSON.stringify(config, 2, '\t')})`;
}
}
const isEmpty = (value) => {
return value === undefined || value === null ||
(typeof value === "object" && Object.keys(value).length === 0) ||
(typeof value === "string" && value.trim().length === 0)
}
const extract = (request, pawKey, axiosKey = pawKey) => {
if (request[pawKey] && !isEmpty(request[pawKey])) {
return { [axiosKey]: request[pawKey] };
} else {
return {};
}
}
const body = (request) => {
if (['PUT', 'POST', 'PATCH'].indexOf(request.method) >= 0) {
return { data: request.jsonBody || request.body || {} };
} else {
return {};
}
}
AxiosGenerator.identifier = "dev.anuragsaini.AxiosGenerator";
AxiosGenerator.title = "axios";
AxiosGenerator.fileExtension = 'js';
AxiosGenerator.languageHighlighter = 'javascript';
registerCodeGenerator(AxiosGenerator);