-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
375 lines (334 loc) · 10.8 KB
/
index.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import QcloudAPI from 'qcloudapi-sdk'
/**
* Qcloud API Gateway API
*
* @example
* new QcloudAPIGateway({
* SecretId: 'xxx',
* SecretKey: 'xxx',
* Region: 'sh'
* })
* .describeServicesStatus()
* .then(console.log)
*
* { totalCount: 1, serviceStatusSet: [ { serviceId: 'service-0abc0def' ... } ] }
*/
export class QcloudAPIGateway {
qcloudAPI: QcloudAPIClass
constructor(
options: Options = {
SecretId: process.env.QCLOUD_SECRETID,
SecretKey: process.env.QCLOUD_SECRETKEY,
}
) {
// TODO: Do we just ignore secret check?
// if (!options.SecretId || !options.SecretKey) {
// console.warn('SecretId and SecretKey is required!')
// }
this.qcloudAPI = new QcloudAPI({
SecretId: options.SecretId,
SecretKey: options.SecretKey,
serviceType: 'apigateway',
Region: options.Region || 'gz',
})
}
setRegion(region: Region) {
this.qcloudAPI.defaults.Region = region
return this
}
request(data, opts = {}, extra = {}): Promise<any> {
return new Promise((resolve, reject) => {
this.qcloudAPI.request(
data,
opts,
(err, res) => {
if (err) {
reject(err)
} else if (res.code > 0) {
const error = new Error(res.message)
error.name = res.codeDesc
reject(error)
} else {
delete res.code
delete res.message
delete res.codeDesc
resolve(res)
}
},
extra
)
})
}
/**
* List or search services
* @desc limit range [0,100]
* @desc searchId is serviceId starts with `service-`
* @desc searchName is serviceName
*/
describeServicesStatus(
params?: Pager & { searchId?: string, searchName?: string }
): Promise<TotalCount & { serviceStatusSet: ServiceStatus[] }> {
return this.request({
Action: 'DescribeServicesStatus',
})
}
describeService(
params: Pick<Service, 'serviceId'>
): Promise<ServiceStatus> {
return this.request(Object.assign({}, params, {
Action: 'DescribeService',
}))
}
createService(
params: Partial<Pick<Service, 'protocol' | 'serviceName' | 'serviceDesc'>>
): Promise<Pick<Service, 'serviceId' | 'serviceName' | 'serviceDesc' | 'subDomain' | 'createdTime'>> {
return this.request(Object.assign({}, params, {
Action: 'CreateService',
}))
.then(res => res.data)
}
modifyService(
params: Pick<Service, 'serviceId'> & Partial<Pick<Service, 'serviceName' | 'serviceDesc' | 'protocol'>>
): Promise<Pick<Service, 'serviceId' | 'serviceName' | 'serviceDesc' | 'protocol' | 'modifiedTime'>> {
return this.request(Object.assign({}, params, {
Action: 'ModifyService',
}))
}
deleteService(
params: Pick<Service, 'serviceId'>
): Promise<{ requestId: null }> {
return this.request(Object.assign({}, params, {
Action: 'DeleteService',
}))
}
/**
* List or search apis
* @desc searchId starts with `service-`
* @desc limit range [0,100]
* @desc searchName is url path `/path`
*/
describeApisStatus(
params: Pick<Service, 'serviceId'> & Pager & { searchName?: string }
): Promise<TotalCount & { apiIdStatusSet: ApiStatus[] }> {
return this.request(Object.assign({}, params, {
Action: 'DescribeApisStatus',
}))
}
describeApi(
params: Pick<Api, 'apiId' | 'serviceId'>
): Promise<Api & Pick<Service, 'serviceName' | 'serviceDesc'>> {
// console.warn('Official working in progress.')
return this.request(Object.assign({}, params, {
Action: 'DescribeApi',
}))
}
createApi(
params: Omit<Api, 'apiId'>
): Promise<Pick<Api, 'apiId' | keyof ApiRequestConfig | keyof Timestamps>> {
return this.request(Object.assign({}, params, {
Action: 'CreateApi',
}))
}
modifyApi(
params: Api
): Promise<{}> {
return this.request(Object.assign({}, params, {
Action: 'ModifyApi',
}))
}
runApi(
params: Pick<Api, 'serviceId' | 'apiId'> & {
contentType: 'application/x-www-form-urlencoded' | 'application/json'
}
): Promise<{
returnCode: number
returnHeader: string
returnBody: string
delay: number
}> {
return this.request(Object.assign({}, params, {
Action: 'RunApi',
}))
}
deleteApi(
params: Pick<Api, 'apiId' | 'serviceId'>
): Promise<{}> {
return this.request(Object.assign({}, params, {
Action: 'DeleteApi',
}))
}
releaseService(
params: Omit<ServiceRelease, 'releaseTime'>
): Promise<Pick<ServiceRelease, 'releaseDesc' | 'releaseTime'>> {
return this.request(Object.assign({}, params, {
Action: 'ReleaseService',
}))
}
describeServiceEnvironmentList(
params: Pick<Service, 'serviceId'>
): Promise<TotalCount & { environmentList: ServiceEnvironment[] }> {
return this.request(Object.assign({}, params, {
Action: 'DescribeServiceEnvironmentList',
}))
}
unReleaseService(
params: Pick<ServiceRelease, 'serviceId' | 'environmentName'>
): Promise<{ unReleaseDesc: null }> {
return this.request(Object.assign({}, params, {
Action: 'UnReleaseService',
}))
}
describeServiceReleaseVersion(
params: Pick<ServiceRelease, 'serviceId'> & Pager
): Promise<TotalCount & { versionList: ServiceVersion[] }> {
return this.request(Object.assign({}, params, {
Action: 'DescribeServiceReleaseVersion',
}))
}
describeUsagePlansStatus(
params: Pager
): Promise<TotalCount & { usagePlanStatusSet: UsagePlanStatus[] }> {
return this.request(Object.assign({}, params, {
Action: 'DescribeUsagePlansStatus',
}))
}
describeUsagePlan(
params: Pick<UsagePlan, 'usagePlanId'>
): Promise<UsagePlanBind> {
return this.request(Object.assign({}, params, {
Action: 'DescribeUsagePlan',
}))
}
createUsagePlan(
params: Pick<UsagePlan, 'usagePlanName' | 'usagePlanDesc' | 'maxRequestNumPreSec' | 'requestControlUnit'>
): Promise<Pick<UsagePlan, 'usagePlanId' | 'usagePlanName' | 'usagePlanDesc' | 'createdTime'>> {
return this.request(Object.assign({}, params, {
Action: 'CreateUsagePlan',
}))
}
}
export type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]
export type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>
export type Region = 'bj' | 'sh' | 'gz'
export interface Options {
SecretId: string
SecretKey: string
Region?: Region
}
export interface QcloudAPIOptions extends Options {
serviceType: string
}
export interface QcloudAPIResponse {
code: number
message: string
codeDesc: string
data?: any
[key: string]: any
}
export type QcloudAPICallback = (err: Error, res: QcloudAPIResponse) => void
export declare class QcloudAPIClass {
constructor(options: QcloudAPIOptions)
defaults: {
protocol: 'https'
baseHost: 'api.qcloud.com'
path: '/v2/index.php'
method: 'POST'
serviceType: 'apigateway'
} & Options
request(
data: {},
opts: {},
callback: QcloudAPICallback,
extra?: {}
): void
}
export type StringBoolean = 'TRUE' | 'FALSE'
export interface Pager {
limit?: number
offset?: number
}
export type TotalCount = { totalCount: number }
export type CreatedTime = { createdTime?: string }
export type ModifiedTime = { modifiedTime?: string }
export type Timestamps = CreatedTime & ModifiedTime
export interface Service extends Timestamps {
serviceId: string
serviceName: string
serviceDesc: string
protocol: 'http' | 'https' | 'http&https'
subDomain: string
vailableEnvironments: any[]
}
export type ServiceStatus = Pick<Service, 'serviceId' | 'serviceName' | 'serviceDesc' | 'protocol' | 'subDomain' | 'vailableEnvironments'>
export interface ApiRequestConfig {
method: string
path: string
}
export interface ApiParameter {
name: string
desc: string
position: 'HEADER' | 'BODY' | 'QUERY' | 'PATH'
}
export interface ApiConstantParameter extends ApiParameter {
defaultValue: string
}
export interface ApiRequestParameter extends ApiParameter {
defaultValue: string
type: 'string' | 'int' | 'long' | 'float' | 'double' | 'boolean'
required: StringBoolean
}
export interface ApiResponseErrorCode {
code: string
msg: string
desc: string
}
export interface Api extends ApiRequestConfig, Timestamps, Pick<Service, 'serviceId'> {
apiId: string
apiName: string
apiDesc: string
serviceType: 'HTTP' | 'MOCK' | 'SCF'
serviceTimeout: number // 1-1800s
authRequired: StringBoolean
requestConfig: ApiRequestConfig
serviceScfFunctionName: string
constantParameters: ApiConstantParameter[]
requestParameters: ApiRequestParameter[]
responseType: 'HTML' | 'JSON' | 'TEST' | 'BINARY' | 'XML'
responseSuccessExample: string
responseFailExample: string
responseErrorCodes: ApiResponseErrorCode[]
serviceMockReturnMessage: string
}
export type ApiStatus = Pick<Api, 'apiId' | 'serviceId' | keyof ApiRequestConfig | keyof Timestamps>
export type ServiceEnvironmentName = 'test' | 'prepub' | 'release'
export interface ServiceRelease extends Pick<Service, 'serviceId'> {
releaseDesc: string
environmentName: ServiceEnvironmentName
releaseTime: string
}
export interface ServiceEnvironment {
url: string
environmentName: ServiceEnvironmentName
status: 0 | 1
versionName: string
}
export interface ServiceVersion {
versionName: string
versionDesc: string
createTime: string
environments: ServiceEnvironmentName[]
}
export interface UsagePlan extends Timestamps {
usagePlanId: string
usagePlanName: string
usagePlanDesc: string
maxRequestNumPreSec: number
requestControlUnit: 'SECOND'
}
export interface UsagePlanBind extends UsagePlan {
bindSecretIdTotalCount: number
bindSecretIds: any[]
bindEnvironmentTotalCount: number
bindEnvironments: any[]
}
export type UsagePlanStatus = Pick<UsagePlan, 'usagePlanId' | 'usagePlanName' | 'usagePlanDesc' | 'maxRequestNumPreSec' | keyof Timestamps> & { maxRequestNum: null }