Skip to content

Commit e69ff1e

Browse files
committed
fix: support error code config
1 parent 3ee8f71 commit e69ff1e

File tree

8 files changed

+93
-62
lines changed

8 files changed

+93
-62
lines changed

__tests__/index.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const path = require('path')
2+
const axios = require('axios')
3+
const { generateId, getServerlessSdk } = require('./lib/utils')
4+
5+
const instanceYaml = {
6+
org: 'orgDemo',
7+
app: 'appDemo',
8+
component: 'website@dev',
9+
name: `website-integration-tests-${generateId()}`,
10+
stage: 'dev',
11+
inputs: {
12+
src: {
13+
src: path.join(__dirname, '..', 'example/src'),
14+
index: 'index.html',
15+
error: 'index.html'
16+
},
17+
bucketName: 'my-bucket',
18+
region: 'ap-guangzhou',
19+
}
20+
}
21+
22+
const credentials = {
23+
tencent: {
24+
SecretId: process.env.TENCENT_SECRET_ID,
25+
SecretKey: process.env.TENCENT_SECRET_KEY,
26+
}
27+
}
28+
29+
const sdk = getServerlessSdk(instanceYaml.org)
30+
31+
it('should deploy success', async () => {
32+
const instance = await sdk.deploy(instanceYaml, credentials)
33+
34+
expect(instance).toBeDefined()
35+
expect(instance.instanceName).toEqual(instanceYaml.name)
36+
expect(instance.outputs.website).toBeDefined()
37+
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
38+
// exist page
39+
const { data } = await axios.get(instance.outputs.website)
40+
expect(data).toContain('Serverless Framework');
41+
42+
// not exist page
43+
try {
44+
await axios.get(`${instance.outputs.website}/error.html`)
45+
} catch (e) {
46+
const { response } = e
47+
expect(response.data).toContain('Serverless Framework');
48+
expect(response.status).toBe(404)
49+
}
50+
})
51+
52+
it('should update success', async () => {
53+
instanceYaml.inputs.disableErrorStatus = true
54+
const instance = await sdk.deploy(instanceYaml, credentials)
55+
56+
expect(instance).toBeDefined()
57+
expect(instance.instanceName).toEqual(instanceYaml.name)
58+
expect(instance.outputs.website).toBeDefined()
59+
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
60+
// exist page
61+
const { data } = await axios.get(instance.outputs.website)
62+
expect(data).toContain('Serverless Framework');
63+
// not exist page
64+
const res = await axios.get(`${instance.outputs.website}/error.html`)
65+
expect(res.data).toContain('Serverless Framework');
66+
expect(res.status).toBe(200)
67+
})
68+
69+
it('should remove success', async () => {
70+
await sdk.remove(instanceYaml, credentials)
71+
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)
72+
73+
expect(result.instance.instanceStatus).toEqual('inactive')
74+
})
File renamed without changes.

docs/configure.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ inputs:
2222
bucketName: my-bucket
2323
protocol: http
2424
replace: false # 是否覆盖式部署
25+
disableErrorStatus: false # 是否禁用错误码,默认 false
2526
autoSetupAcl: true # 自动配置 bucket 访问权限为 ”公有读私有写“
2627
autoSetupPolicy: false # 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“
2728
env: # 配置前端环境变量
@@ -91,18 +92,19 @@ inputs:
9192
9293
主要参数描述
9394
94-
| 参数名称 | 必选 | 类型 | 默认值 | 描述 |
95-
| --------------- | :--: | :-------------- | :------------: | :---------------------------------------------------------------------------------------- |
96-
| src | 是 | [Src](#Src)[] | | 该项目的代码信息,参数参考执行目录 |
97-
| bucketName | 是 | string | | Bucket 名称。 不允许大写字母。如果你不加 AppId 后缀,则默认自动会为你加上。 |
98-
| region | 否 | string | `ap-guangzhou` | 代码上传所在的 cos 区域。区。 |
99-
| replace | 否 | boolean | `false` | 是否是替换式部署,如果为 `true`,部署时将 `先删除对应 bucket 的所有旧文件`。 |
100-
| protocol | 否 | string | `https` | 请求协议。`https` 或 `http` |
101-
| env | 否 | [Env](#Env) | | 环境变量参数文件。会将 env 下配置的参数写入 env.js 文件中,将该文件打包上传到你的代码里。 |
102-
| cors | 否 | [Cors](#Cors)[] | | 跨域访问配置 |
103-
| hosts | 否 | [Cdn](#Cdn)[] | | CND 加速域名配置 |
104-
| autoSetupAcl | 否 | boolean | `true` | 自动配置 bucket 访问权限为 ”公有读私有写“ |
105-
| autoSetupPolicy | 否 | boolean | `false` | 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“ |
95+
| 参数名称 | 必选 | 类型 | 默认值 | 描述 |
96+
| ------------------ | :--: | :-------------- | :------------: | :---------------------------------------------------------------------------------------- |
97+
| src | 是 | [Src](#Src)[] | | 该项目的代码信息,参数参考执行目录 |
98+
| bucketName | 是 | string | | Bucket 名称。 不允许大写字母。如果你不加 AppId 后缀,则默认自动会为你加上。 |
99+
| region | 否 | string | `ap-guangzhou` | 代码上传所在的 cos 区域。区。 |
100+
| replace | 否 | boolean | `false` | 是否是替换式部署,如果为 `true`,部署时将 `先删除对应 bucket 的所有旧文件`。 |
101+
| protocol | 否 | string | `https` | 请求协议。`https` 或 `http` |
102+
| env | 否 | [Env](#Env) | | 环境变量参数文件。会将 env 下配置的参数写入 env.js 文件中,将该文件打包上传到你的代码里。 |
103+
| cors | 否 | [Cors](#Cors)[] | | 跨域访问配置 |
104+
| hosts | 否 | [Cdn](#Cdn)[] | | CND 加速域名配置 |
105+
| autoSetupAcl | 否 | boolean | `true` | 自动配置 bucket 访问权限为 ”公有读私有写“ |
106+
| autoSetupPolicy | 否 | boolean | `false` | 自动配置 bucket 的 Policy 权限为 ”所有用户资源可读“ |
107+
| disableErrorStatus | 否 | boolean | `false` | 是否禁用错误码,默认 false,不存在文件会返回 404;如果禁用,就会返回 200 |
106108

107109
> 针对 COS 静态资源托管,通常需要配置所有用户公有读私有写,website 组件因此默认通过配置 `autoSetupAcl` 为 `true`,来帮助用户自动配置访问权限为 `公有读私有写`,由于 COS 针对账号的 ACL 配置条数有 1000 限制,当子账号很多的情况下,通过 `autoSetupAcl` 来配置 ACL 可能超过上限。此时用户可以配置 `autoSetupAcl` 为 `false`,同时配置 `autoSetupPolicy` 为 `true`,来解决此问题。
108110

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@semantic-release/git": "^9.0.0",
4545
"@semantic-release/npm": "^7.0.4",
4646
"@semantic-release/release-notes-generator": "^9.0.1",
47-
"@serverless/platform-client-china": "^1.0.19",
47+
"@serverless/platform-client-china": "^2.0.9",
4848
"@ygkit/secure": "0.0.3",
4949
"axios": "^0.21.0",
5050
"babel-eslint": "^10.1.0",

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: website
2-
version: 0.1.1
2+
version: 0.1.2
33
author: 'Tencent Cloud, Inc.'
44
org: 'Tencent Cloud, Inc.'
55
description: Deploy a static website on Tencent Cloud.

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"download": "^8.0.0",
4-
"tencent-component-toolkit": "^1.19.7",
4+
"tencent-component-toolkit": "^1.20.5",
55
"type": "^2.1.0"
66
}
77
}

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const prepareInputs = async (instance, inputs) => {
6464
const bucketName =
6565
removeAppid(inputs.bucketName, appId) || `sls-website-${region}-${generateId()}`
6666

67-
const websiteInputs = {
67+
const websiteInputs = Object.assign(inputs, {
6868
replace: inputs.replace,
6969
useDefault: !code.src,
7070
code: {
@@ -78,7 +78,7 @@ const prepareInputs = async (instance, inputs) => {
7878
region: region,
7979
protocol: inputs.protocol || CONFIGS.protocol,
8080
cors: inputs.cors
81-
}
81+
})
8282

8383
// auto setup acl for public-read
8484
if (inputs.autoSetupAcl !== false) {

tests/index.test.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)