-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdGenerator.js
More file actions
72 lines (66 loc) · 2.11 KB
/
oldGenerator.js
File metadata and controls
72 lines (66 loc) · 2.11 KB
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
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),
};
const urlParams = extract(request, 'urlParameters', 'params').params || {};
const reqBody = body(request).data || {};
const hasBody = ['PUT', 'POST', 'PATCH'].indexOf(request.method) >= 0;
const comment = `/**
${generateComments(urlParams, 'params')}
${generateComments(reqBody, 'data')}
*/
`;
return (
comment +
`export function fName (params${hasBody ? ' ,data' : ''}) {\r\n` +
` return request({\r\n` +
` method: "${config.method}",\r\n` +
` url: "${config.url}",\r\n` +
` params: params${hasBody ? ',' : ''}\r\n` +
`${hasBody ? ' data: data\r\n' : ''}` +
` })\r\n` +
`}`
);
};
};
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 {};
}
};
const generateComments = (obj, type) => {
console.log(obj, type);
let ret = [];
if (Object.keys(obj).length > 0) {
Object.keys(obj).forEach(key => {
const value = obj[key];
ret.push(` * @${type}.${key} ${typeof value}`);
});
return ret.join('\r\n');
} else {
return ` * @${type}`;
}
};
AxiosGenerator.identifier = 'dev.yzg.codeGenerator';
AxiosGenerator.title = 'freshes Code Generator';
registerCodeGenerator(AxiosGenerator);