This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
forked from speedymonster/node-grpc-interceptors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
53 lines (48 loc) · 1.56 KB
/
utils.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
const getType = method => {
if (method.requestStream === false && method.responseStream === false) {
return 'unary';
}
return 'unknown';
};
const toLowerCamelCase = str => {
return str.charAt(0).toLowerCase() + str.slice(1);
};
const getAllPropertyNames = (obj) => {
var props = [];
do {
props= props.concat(Object.getOwnPropertyNames(obj ));
} while (obj = Object.getPrototypeOf(obj));
return props;
};
const lookupServiceMetadata = (service, implementation) => {
const serviceKeys = Object.keys(service);
const implementationKeys = getAllPropertyNames(implementation);
const intersectingMethods = serviceKeys
.filter(k => {
return implementationKeys.map(k => toLowerCamelCase(k)).indexOf(k) !== -1;
})
.reduce((acc, k) => {
const method = service[k];
if (!method) {
throw new Error(`cannot find method ${k} on service`);
}
const components = method.path.split('/');
acc[k] = {
name: components[1],
method: components[2],
type: getType(method),
path: method.path,
responseType: method.responseType,
requestType: method.requestType,
};
return acc;
}, {});
return key => {
return Object.keys(intersectingMethods)
.filter(k => toLowerCamelCase(key) === k)
.map(k => intersectingMethods[k]).pop();
};
};
module.exports = {
lookupServiceMetadata,
};