Skip to content

Commit ad78979

Browse files
author
dfounderliu
committed
fix: 修复website/APIGW等功能
1 parent 210f489 commit ad78979

File tree

3 files changed

+64
-38
lines changed

3 files changed

+64
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tencent-component-toolkit",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "Tencent component toolkit",
55
"main": "src/index.js",
66
"scripts": {

src/baas/apigw/index.js

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { apigw } = require('tencent-cloud-sdk')
2-
const { uniqueArray } = require('../../utils/index')
1+
const {apigw} = require('tencent-cloud-sdk')
2+
const {uniqueArray} = require('../../utils/index')
33

44
class Apigw {
55
constructor(credentials = {}, region) {
@@ -55,7 +55,8 @@ class Apigw {
5555
protocol: protocols
5656
})
5757
}
58-
} catch (e) {}
58+
} catch (e) {
59+
}
5960
}
6061
if (!exist) {
6162
const createData = await this.request({
@@ -76,7 +77,7 @@ class Apigw {
7677
}
7778
}
7879

79-
async createOrUpdateApi({ serviceId, endpoint }) {
80+
async createOrUpdateApi({serviceId, endpoint}) {
8081
const output = {
8182
path: endpoint.path,
8283
method: endpoint.method,
@@ -106,8 +107,8 @@ class Apigw {
106107
const funcQualifier = endpoint.function.functionQualifier
107108
? endpoint.function.functionQualifier
108109
: '$LATEST'
109-
? endpoint.function.functionQualifier
110-
: '$LATEST'
110+
? endpoint.function.functionQualifier
111+
: '$LATEST'
111112

112113
if (endpoint.protocol === 'WEBSOCKET') {
113114
if (!endpoint.function.transportFunctionName) {
@@ -141,6 +142,23 @@ class Apigw {
141142
}
142143

143144
let exist = false
145+
146+
// 没有apiId,还需要根据path来确定
147+
if (!endpoint.apiId) {
148+
const pathAPIList = await this.request({
149+
Action: 'DescribeApisStatus',
150+
serviceId: serviceId,
151+
searchName: endpoint.path
152+
})
153+
if (pathAPIList.apiIdStatusSet) {
154+
for (let i = 0; i < pathAPIList.apiIdStatusSet.length; i++) {
155+
if (pathAPIList.apiIdStatusSet[i].method == endpoint.method && pathAPIList.apiIdStatusSet[i].path == endpoint.path) {
156+
endpoint.apiId = pathAPIList.apiIdStatusSet[i].apiId
157+
}
158+
}
159+
}
160+
}
161+
144162
if (endpoint.apiId) {
145163
try {
146164
const detail = await this.request({
@@ -159,10 +177,13 @@ class Apigw {
159177
output.apiId = endpoint.apiId
160178
console.log(`Service with id ${output.apiId} updated.`)
161179
}
162-
} catch (e) {}
180+
} catch (e) {
181+
}
163182
}
183+
184+
164185
if (!exist) {
165-
const { apiId } = await this.request({
186+
const {apiId} = await this.request({
166187
Action: 'CreateApi',
167188
...apiInputs
168189
})
@@ -171,7 +192,7 @@ class Apigw {
171192
console.log(`API with id ${output.apiId} created.`)
172193
}
173194

174-
const { internalDomain } = await this.request({
195+
const {internalDomain} = await this.request({
175196
Action: 'DescribeApi',
176197
serviceId: serviceId,
177198
apiId: output.apiId
@@ -181,15 +202,15 @@ class Apigw {
181202
return output
182203
}
183204

184-
async setupUsagePlanSecret({ secretName, secretIds }) {
205+
async setupUsagePlanSecret({secretName, secretIds}) {
185206
const secretIdsOutput = {
186207
created: false,
187208
secretIds
188209
}
189210

190211
if (secretIds.length === 0) {
191212
console.log(`Creating a new Secret key.`)
192-
const { secretId, secretKey } = await this.request({
213+
const {secretId, secretKey} = await this.request({
193214
Action: 'CreateApiKey',
194215
secretName: secretName,
195216
type: 'auto'
@@ -201,7 +222,7 @@ class Apigw {
201222
const uniqSecretIds = uniqueArray(secretIds)
202223

203224
// get all secretId, check local secretId exists
204-
const { apiKeyStatusSet } = await this.request({
225+
const {apiKeyStatusSet} = await this.request({
205226
Action: 'DescribeApiKeysStatus',
206227
secretIds: uniqSecretIds,
207228
limit: uniqSecretIds.length
@@ -238,7 +259,7 @@ class Apigw {
238259
return secretIdsOutput
239260
}
240261

241-
async setupApiUsagePlan({ usagePlan }) {
262+
async setupApiUsagePlan({usagePlan}) {
242263
const usageInputs = {
243264
usagePlanName: usagePlan.usagePlanName || '',
244265
usagePlanDesc: usagePlan.usagePlanDesc || '',
@@ -274,9 +295,9 @@ class Apigw {
274295
/**
275296
* get all unbound secretids
276297
*/
277-
async getUnboundSecretIds({ usagePlanId, secretIds }) {
278-
const getAllBoundSecrets = async (res = [], { limit, offset = 0 }) => {
279-
const { secretIdList } = await this.request({
298+
async getUnboundSecretIds({usagePlanId, secretIds}) {
299+
const getAllBoundSecrets = async (res = [], {limit, offset = 0}) => {
300+
const {secretIdList} = await this.request({
280301
Action: 'DescribeUsagePlanSecretIds',
281302
usagePlanId,
282303
limit,
@@ -285,9 +306,9 @@ class Apigw {
285306
if (secretIdList.length < limit) {
286307
return res
287308
}
288-
return getAllBoundSecrets(res, { limit, offset: offset + secretIdList.length })
309+
return getAllBoundSecrets(res, {limit, offset: offset + secretIdList.length})
289310
}
290-
const allBoundSecretObjs = await getAllBoundSecrets([], { limit: 100 })
311+
const allBoundSecretObjs = await getAllBoundSecrets([], {limit: 100})
291312
const allBoundSecretIds = allBoundSecretObjs.map((item) => item.secretId)
292313
const unboundSecretIds = secretIds.filter((item) => {
293314
if (allBoundSecretIds.indexOf(item) === -1) {
@@ -300,8 +321,8 @@ class Apigw {
300321
}
301322

302323
// bind custom domains
303-
async bindCustomDomain({ serviceId, subDomain, inputs }) {
304-
const { customDomains, oldState = {} } = inputs
324+
async bindCustomDomain({serviceId, subDomain, inputs}) {
325+
const {customDomains, oldState = {}} = inputs
305326
// 1. unbind all custom domain
306327
const customDomainDetail = await this.request({
307328
Action: 'DescribeServiceSubDomains',
@@ -312,7 +333,7 @@ class Apigw {
312333
customDomainDetail.domainSet &&
313334
customDomainDetail.domainSet.length > 0
314335
) {
315-
const { domainSet = [] } = customDomainDetail
336+
const {domainSet = []} = customDomainDetail
316337
// unbind all created domain
317338
const stateDomains = oldState.customDomains || []
318339
for (let i = 0; i < domainSet.length; i++) {
@@ -368,14 +389,14 @@ class Apigw {
368389

369390
// bind environment fo usage plan
370391
async bindUsagePlanEnvironment({
371-
environment,
372-
bindType = 'API',
373-
serviceId,
374-
apiId,
375-
endpoint,
376-
usagePlan
377-
}) {
378-
const { usagePlanList } = await this.request({
392+
environment,
393+
bindType = 'API',
394+
serviceId,
395+
apiId,
396+
endpoint,
397+
usagePlan
398+
}) {
399+
const {usagePlanList} = await this.request({
379400
Action: 'DescribeApiUsagePlan',
380401
serviceId,
381402
apiIds: [apiId]
@@ -403,17 +424,17 @@ class Apigw {
403424
}
404425

405426
async deploy(inputs) {
406-
const { environment, oldState = {} } = inputs
427+
const {environment, oldState = {}} = inputs
407428
inputs.protocols = this.getProtocolString(inputs.protocols)
408429

409-
const { serviceId, serviceName, subDomain, serviceCreated } = await this.createOrUpdateService(
430+
const {serviceId, serviceName, subDomain, serviceCreated} = await this.createOrUpdateService(
410431
inputs
411432
)
412433

413434
const apiList = []
414435
const stateApiList = oldState.apiList || []
415436

416-
const { endpoints } = inputs
437+
const {endpoints} = inputs
417438
for (let i = 0, len = endpoints.length; i < len; i++) {
418439
const endpoint = endpoints[i]
419440
// if exist in state list, set created to be true
@@ -445,7 +466,7 @@ class Apigw {
445466
// store in api list
446467
curApi.usagePlan = usagePlan
447468

448-
const { secretIds = [] } = endpoint.auth
469+
const {secretIds = []} = endpoint.auth
449470
const secrets = await this.setupUsagePlanSecret({
450471
secretName: endpoint.auth.secretName,
451472
secretIds
@@ -517,15 +538,15 @@ class Apigw {
517538
}
518539

519540
async remove(inputs) {
520-
const { created, environment, serviceId, apiList, customDomains } = inputs
541+
const {created, environment, serviceId, apiList, customDomains} = inputs
521542
// 1. remove all apis
522543
for (let i = 0; i < apiList.length; i++) {
523544
const curApi = apiList[i]
524545

525546
// 1. remove usage plan
526547
if (curApi.usagePlan) {
527548
// 1.1 unbind secrete ids
528-
const { secrets } = curApi.usagePlan
549+
const {secrets} = curApi.usagePlan
529550
if (secrets && secrets.secretIds) {
530551
await this.request({
531552
Action: 'UnBindSecretIds',

src/baas/cos/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@ class Cos {
388388
}
389389

390390
async website(inputs = {}) {
391-
await this.createBucket(inputs)
391+
await this.createBucket({
392+
bucket: inputs.bucket,
393+
force: true
394+
})
392395

393396
inputs.acl = {
394397
permissions: 'public-read',
@@ -400,6 +403,8 @@ class Cos {
400403

401404
await this.setWebsite(inputs)
402405

406+
await this.setCors(inputs)
407+
403408
// Build environment variables
404409
const envPath = inputs.code.envPath || inputs.code.root
405410
if (inputs.env && Object.keys(inputs.env).length && envPath) {
@@ -436,7 +441,7 @@ class Cos {
436441
}
437442
await this.upload(uploadDict)
438443

439-
return `${inputs.bucket}.cos-website.${this.region}.myqcloud.com`
444+
return `${inputs.bucket}-${inputs.appid}.cos-website.${this.region}.myqcloud.com`
440445
}
441446

442447
async deploy(inputs = {}) {

0 commit comments

Comments
 (0)