forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetServices.js
More file actions
62 lines (56 loc) · 1.75 KB
/
getServices.js
File metadata and controls
62 lines (56 loc) · 1.75 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
const camelCase = require('camelcase');
const parseParams = require('./parseParams');
const getServiceName = path => {
return path.match(/^\/(\w+)/)[1];
};
const handleMethodsGroup = (path, methodsGroup) => {
const servicePath = [];
for (const method in methodsGroup) {
if (methodsGroup.hasOwnProperty(method)) {
const request = methodsGroup[method];
const functionName = camelCase(path.split('/'), {
pascalCase: true,
});
const eachRequest = {
method: method.toLocaleUpperCase(),
path,
name: `${method}${functionName}`, // unsupport dynamic route now.
requestParams: parseParams(method, request),
description: request.description,
};
servicePath.push(eachRequest);
}
}
return servicePath;
};
const expandEachPath = services => {
const servicesObj = {};
services.forEach((serviceValue, serviceKey) => {
const pathArray = [];
serviceValue.forEach(pathValue => {
pathArray.push(...pathValue);
});
servicesObj[serviceKey] = pathArray;
});
return servicesObj;
};
const splitServices = paths => {
const services = new Map(); // key: service name, value: service
for (const path in paths) {
if (paths.hasOwnProperty(path)) {
const methodsGroup = paths[path];
const serviceName = getServiceName(path);
const eachPath = [path, handleMethodsGroup(path, methodsGroup)];
services.get(serviceName)
? services.get(serviceName).set(...eachPath)
: services.set(serviceName, new Map([eachPath]));
}
}
return services;
};
const getServices = openApi => {
const splitedServices = splitServices(openApi.paths);
const services = expandEachPath(splitedServices);
return services;
};
module.exports = getServices;