/
index.ts
319 lines (286 loc) · 9.62 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
import COS from 'cos-nodejs-sdk-v5'
import Util from 'util'
import { Environment } from '../environment'
import { IResponseInfo, AuthDomain, EnvInfo, LoginConfigItem } from '../interfaces'
import { CloudBaseError } from '../error'
import { guid6, rsaEncrypt, CloudService, preLazy } from '../utils'
interface ICreateEnvRes {
// 环境当前状态:NORMAL:正常可用 NOINITIALIZE:尚未初始化 INITIALIZING:初始化过程中
Status: 'NORMAL' | 'NOINITIALIZE' | 'INITIALIZING'
// 唯一请求 ID,每次请求都会返回。定位问题时需要提供该次请求的 RequestId。
RequestId: string
}
interface IDeleteDomainRes {
RequestId: string
Deleted: number
}
interface IAuthDomainsRes {
RequestId: string
Domains: AuthDomain[]
}
interface IListEnvRes {
RequestId: string
EnvList: EnvInfo[]
}
interface IEnvLoginConfigRes {
RequestId: string
ConfigList: LoginConfigItem[]
}
export class EnvService {
private environment: Environment
private envId: string
private cloudService: CloudService
constructor(environment: Environment) {
this.environment = environment
this.envId = environment.getEnvId()
this.cloudService = new CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08')
}
/**
* 列出所有环境
* @returns {Promise<IListEnvRes>}
*/
async listEnvs(): Promise<IListEnvRes> {
return this.cloudService.request('DescribeEnvs')
}
/**
* 创建新环境
* @param {string} name 环境名称
* @returns {Promise<ICreateEnvRes>}
*/
async createEnv(name: string): Promise<ICreateEnvRes> {
const params = {
Alias: name,
EnvId: `${name}-${guid6()}`,
Source: 'qcloud'
}
throw new CloudBaseError(`创建环境失败:当前接口暂不可使用`)
// try {
// return this.cloudService.request('CreateEnvAndResource', params)
// } catch (e) {
// throw new CloudBaseError(`创建环境失败:${e.message}`)
// }
}
/**
* 拉取安全域名列表
* @returns {Promise<IAuthDomainsRes>}
*/
public async getEnvAuthDomains(): Promise<IAuthDomainsRes> {
return this.cloudService.request('DescribeAuthDomains', {
EnvId: this.envId
})
}
/**
* 添加环境安全域名
* @param {string[]} domains 域名字符串数组
* @returns {Promise<IResponseInfo>}
*/
@preLazy()
public async createEnvDomain(domains: string[]): Promise<IResponseInfo> {
const res = await this.cloudService.request('CreateAuthDomain', {
EnvId: this.envId,
Domains: domains
})
// 添加 COS CORS 域名
const promises = domains.map(async domain => {
this.modifyCosCorsDomain(domain)
})
await Promise.all(promises)
return res
}
/**
* 删除环境安全域名
* @param {string[]} domainIds 域名字符串数组
* @returns {Promise<IDeleteDomainRes>}
*/
@preLazy()
public async deleteEnvDomain(domains: string[]): Promise<IDeleteDomainRes> {
// 根据域名获取域名 Id
const { Domains } = await this.getEnvAuthDomains()
const domainIds = Domains.filter(item => domains.includes(item.Domain)).map(item => item.Id)
const res = await this.cloudService.request('DeleteAuthDomain', {
EnvId: this.envId,
DomainIds: domainIds
})
// 删除 COS CORS 域名
const promises = domains.map(async domain => {
this.modifyCosCorsDomain(domain, true)
})
await Promise.all(promises)
return res
}
/**
* 获取环境信息
* @returns {Promise<IEnvInfoRes>}
*/
public async getEnvInfo(): Promise<{
EnvInfo: EnvInfo
RequestId: string
}> {
// NOTE: DescribeEnv 接口废弃,需要使用 DescribeEnvs 接口
const { EnvList, RequestId } = await this.cloudService.request('DescribeEnvs', {
EnvId: this.envId
})
return {
EnvInfo: EnvList && EnvList.length ? EnvList[0] : {},
RequestId
}
}
/**
* 修改环境名称
* @param {string} alias 环境名称
* @returns {Promise<IResponseInfo>}
*/
public async updateEnvInfo(alias: string): Promise<IResponseInfo> {
return this.cloudService.request('ModifyEnv', {
EnvId: this.envId,
Alias: alias
})
}
/**
* 拉取登录配置列表
* @returns {Promise<IEnvLoginConfigRes>}
*/
async getLoginConfigList(): Promise<IEnvLoginConfigRes> {
return this.cloudService.request('DescribeLoginConfigs', {
EnvId: this.envId
})
}
/**
* 创建登录方式
* 'WECHAT-OPEN':微信开放平台
* 'WECHAT-PUBLIC':微信公众平台
* @param {('WECHAT-OPEN' | 'WECHAT-PUBLIC')} platform 'WECHAT-OPEN' | 'WECHAT-PUBLIC'
* @param {string} appId 微信 appId
* @param {string} appSecret 微信 appSecret
* @returns {Promise<IResponseInfo>}
*/
async createLoginConfig(
platform: 'WECHAT-OPEN' | 'WECHAT-PUBLIC',
appId: string,
appSecret: string
): Promise<IResponseInfo> {
const validPlatform = ['WECHAT-OPEN', 'WECHAT-PUBLIC']
if (!validPlatform.includes(platform)) {
throw new CloudBaseError(
`Invalid platform value: ${platform}. Now only support 'WECHAT-OPEN', 'WECHAT-PUBLIC'`
)
}
return this.cloudService.request('CreateLoginConfig', {
EnvId: this.envId,
// 平台, “QQ" "WECHAT-OPEN" "WECHAT-PUBLIC"
Platform: platform,
PlatformId: appId,
PlatformSecret: rsaEncrypt(appSecret),
Status: 'ENABLE'
})
}
/**
* 更新登录方式配置
* @param {string} configId 配置 Id,从配置列表中获取
* @param {string} [status='ENABLE'] 是否启用 'ENABLE', 'DISABLE' ,可选
* @param {string} [appId=''] 微信 appId,可选
* @param {string} [appSecret=''] 微信 appSecret,可选
* @returns {Promise<IResponseInfo>}
*/
async updateLoginConfig(
configId: string,
status = 'ENABLE',
appId = '',
appSecret = ''
): Promise<IResponseInfo> {
const validStatus = ['ENABLE', 'DISABLE']
if (!validStatus.includes(status)) {
throw new CloudBaseError(
`Invalid status value: ${status}. Only support 'ENABLE', 'DISABLE'`
)
}
const params: any = {
EnvId: this.envId,
ConfigId: configId,
Status: status
}
appId && (params.PlatformId = appId)
appSecret && (params.PlatformSecret = rsaEncrypt(appSecret))
return this.cloudService.request('UpdateLoginConfig', params)
}
// 获取 COS CORS 域名
private async getCOSDomains() {
const cos = this.getCos()
const getBucketCors = Util.promisify(cos.getBucketCors).bind(cos)
const { bucket, region } = this.getStorageConfig()
const res = await getBucketCors({
Bucket: bucket,
Region: region
})
return res.CORSRules
}
// 添加 COS CORS 域名,和 Web 端行为保持一致
private async modifyCosCorsDomain(domain: string, deleted = false) {
const cos = this.getCos()
const putBucketCors = Util.promisify(cos.putBucketCors).bind(cos)
const { bucket, region } = this.getStorageConfig()
// 去掉原有此域名CORS配置
let corsRules = await this.getCOSDomains()
corsRules = corsRules.filter(item => {
return !(
item.AllowedOrigins &&
item.AllowedOrigins.length === 2 &&
item.AllowedOrigins[0] === `http://${domain}` &&
item.AllowedOrigins[1] === `https://${domain}`
)
})
if (!deleted) {
corsRules.push({
AllowedOrigin: [`http://${domain}`, `https://${domain}`],
AllowedMethod: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD'],
AllowedHeader: ['*'],
ExposeHeader: ['Etag', 'Date'],
MaxAgeSeconds: '5'
})
}
await putBucketCors({
Bucket: bucket,
Region: region,
CORSRules: corsRules
})
}
private getCos() {
const { secretId, secretKey, token } = this.getAuthConfig()
if (!token) {
return new COS({
SecretId: secretId,
SecretKey: secretKey
})
}
return new COS({
getAuthorization: function(_, callback) {
callback({
TmpSecretId: secretId,
TmpSecretKey: secretKey,
XCosSecurityToken: token,
ExpiredTime: 3600 * 1000
})
}
})
}
private getAuthConfig() {
const { secretId, secretKey, token } = this.environment.cloudBaseContext
const envId = this.environment.getEnvId()
return {
envId,
secretId,
secretKey,
token
}
}
private getStorageConfig() {
const envConfig = this.environment.lazyEnvironmentConfig
const storageConfig = envConfig.Storages && envConfig.Storages[0]
const { Region, Bucket } = storageConfig
return {
env: envConfig.EnvId,
region: Region,
bucket: Bucket
}
}
}