-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathsvrkit-utils-template.js
113 lines (92 loc) · 2.82 KB
/
svrkit-utils-template.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function generate(options) {
if (!options) {
throw new Error('options must be provided')
}
const {serviceName, funcName, data} = options
const serviceConfig = config.find(c => c.serviceName === serviceName)
if (!serviceConfig) {
throw new Error('service not found')
}
if (!serviceConfig.functions[funcName]) {
throw new Error('function not found')
}
const reqProtoName = serviceConfig.functions[funcName].req
const reqProto = proto[reqProtoName]
if (!reqProto) {
throw new Error('request proto not found')
}
const resProtoName = serviceConfig.functions[funcName].res
const resProto = resProtoName && proto[resProtoName]
const reqProtoVerifyErr = reqProto.verify(data)
if (reqProtoVerifyErr) {
throw new Error(`verify proto data error: ${reqProtoVerifyErr}`)
}
const reqProtoJSON = protoJSON.nested[reqProtoName]
if (reqProtoJSON && reqProtoJSON.fields) {
if (Object.prototype.toString.call(data).slice(8, -1) === 'Object') {
for (const key in data) {
if (!reqProtoJSON.fields[key]) {
throw new Error(`'${key}' doesn't exist in '${reqProtoName}' proto, valid keys are ${Object.keys(reqProtoJSON.fields)}`)
}
}
} else {
throw new Error('data must be object')
}
}
return {
data: {
serviceName,
funcName,
magic: serviceConfig.magic,
cmdid: serviceConfig.functions[funcName].cmdid,
existResp: Boolean(resProto),
reqBodyBuffer: reqProto.encode(data).finish(),
},
reqProto,
resProto,
decode: buf => resProto && resProto.decode(buf)
}
}
function generateV2(options) {
if (!options) {
throw new Error('options must be provided')
}
const {apiName, data} = options
const apiConfig = config.find(c => c.apiName === apiName)
const reqProtoName = apiConfig.req
const reqProto = proto[reqProtoName]
if (!reqProto) {
throw new Error('request proto not found')
}
const resProtoName = apiConfig.res
const resProto = proto[resProtoName]
const reqProtoVerifyErr = reqProto.verify(data)
if (reqProtoVerifyErr) {
throw new Error(`verify proto data error: ${reqProtoVerifyErr}`)
}
const reqProtoJSON = protoJSON.nested[reqProtoName]
if (reqProtoJSON && reqProtoJSON.fields) {
if (Object.prototype.toString.call(data).slice(8, -1) === 'Object') {
for (const key in data) {
if (!reqProtoJSON.fields[key]) {
throw new Error(`'${key}' doesn't exist in '${reqProtoName}' proto, valid keys are ${Object.keys(reqProtoJSON.fields)}`)
}
}
} else {
throw new Error('data must be object')
}
}
return {
data: {
apiName,
reqBodyBuffer: reqProto.encode(data).finish(),
},
reqProto,
resProto,
decode: buf => resProto && resProto.decode(buf)
}
}
module.exports = {
generate,
generateV2,
}