This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
145 lines (132 loc) · 3.58 KB
/
util.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
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
/**
* 从请求中提取 url 的 path 部分
* @param {*} event 腾讯 SCF 提供事件触发对象
*/
function str_path(event) {
const pre_path = event.path
? event.path.split(event.requestContext.path)[1]
: "";
return (path = pre_path ? pre_path : "/");
}
/**
* 从请求中提取 query string 的 password 部分
* @param {*} event 腾讯 SCF 提供事件触发对象
*/
function str_pass(event) {
const query = event.queryString;
return query.password ? query.password : "";
}
/**
* 从列表中提取 `.password` 文件的文件名部分
* @param {*} files Teambition 获取 Works API 返回数组
*/
function pick_pass(files) {
const passFile = files.find((file) => /^.+\.password$/.test(file.fileName));
if(!passFile) return ""
return passFile.fileName.split('.password')[0]
}
/**
* 返回一个腾讯云 SCF 的响应对象( 状态 `200` | 格式 `JSON` )
* @param {*} body JSON 格式字符串或者 JS 对象
*/
function res_200(body) {
if (typeof body !== "string") {
body = JSON.stringify(body);
}
return {
isBase64Encoded: false,
statusCode: 200,
headers: { "Content-Type": "application/json; charset=utf-8" },
body,
};
}
/**
* 返回一个腾讯云 SCF 的响应对象( 状态 `302` | **重定向** )
* @param {*} body JSON 格式字符串或者 JS 对象
*/
function res_302(url) {
return {
isBase64Encoded: false,
statusCode: 302,
headers: { Location: url },
};
}
/**
* 返回一个腾讯云 SCF 的响应对象( 状态 `403` | 格式 `JSON` )
* @param {*} body JSON 格式字符串或者 JS 对象
*/
function res_403() {
return {
isBase64Encoded: false,
statusCode: 403,
headers: { "Content-Type": "application/json; charset=utf-8" },
body: '{"name":"Forbidden","message":"访问被拒绝"}',
};
}
/**
* 返回一个腾讯云 SCF 的响应对象( 状态 `404` | 格式 `JSON` )
* @param {*} body JSON 格式字符串或者 JS 对象
*/
function res_404() {
return {
isBase64Encoded: false,
statusCode: 404,
headers: { "Content-Type": "application/json; charset=utf-8" },
body: '{"name":"NotFound","message":"项目不存在"}',
};
}
/**
* 返回一个腾讯云 SCF 的响应对象( 状态 `500` | 格式 `JSON` )
* @param {*} body JSON 格式字符串或者 JS 对象
*/
function res_500() {
return {
isBase64Encoded: false,
statusCode: 500,
headers: { "Content-Type": "application/json; charset=utf-8" },
body: '{"name":"InternalServerError","message":"服务器内部错误"}',
};
}
function ali_200(resp, body) {
if (typeof body !== "string") {
body = JSON.stringify(body);
}
resp.setStatusCode(200);
resp.setHeader("Content-Type", "application/json; charset=utf-8");
resp.send(body);
}
function ali_302(resp, url) {
resp.setStatusCode(302);
resp.setHeader("Location", url);
resp.send(url);
}
function ali_403(resp) {
resp.setStatusCode(403);
resp.setHeader("Content-Type", "application/json; charset=utf-8");
resp.send('{"name":"Forbidden","message":"访问被拒绝"}');
}
function ali_404(resp) {
resp.setStatusCode(404);
resp.setHeader("Content-Type", "application/json; charset=utf-8");
resp.send('{"name":"NotFound","message":"项目不存在"}');
}
function ali_500(resp) {
resp.setStatusCode(500);
resp.setHeader("Content-Type", "application/json; charset=utf-8");
resp.send('{"name":"InternalServerError","message":"服务器内部错误"}');
}
module.exports = {
str_path,
str_pass,
pick_pass,
res_200,
res_302,
res_403,
res_404,
res_500,
ali_200,
ali_302,
ali_403,
ali_404,
ali_500,
};