Skip to content

Commit 33d2103

Browse files
authored
Merge pull request #1 from serverless-tencent/sls-sdk
Sls sdk
2 parents 37b490c + c41d4fe commit 33d2103

File tree

3 files changed

+225
-1
lines changed

3 files changed

+225
-1
lines changed

README.md

100644100755
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [一键登录功能](#一键登录功能)
1111
- [检测用户实名](#检测用户实名)
1212
- [判断中国用户](#判断中国用户)
13+
- [ServerlessApi](#ServerlessApi)
1314

1415

1516
## 基本功能
@@ -373,4 +374,24 @@ new OthersAction().getIsInChina()
373374
| IsInChina | 输出参数true或false,如果是true,表示是中国用户,否则表示非中国用户 |
374375

375376

377+
### ServerlessApi
378+
379+
serverless api
380+
381+
```javascript
382+
const {Serverless} = require('serverless-tencent-tools');
383+
384+
const sls = new Serverless({
385+
appid: app_id,
386+
secret_id: secret_id,
387+
secret_key: secret_key,
388+
options: {
389+
region: 'ap-guangzhou'
390+
}
391+
});
392+
393+
const ret = await sls.getComponentAndVersions('Component name');
394+
console.log(ret)
395+
```
396+
376397
* 该接口目前为1.0版本,后期会增加其复杂度,但是接口规范不会变。)

sdk/index.js

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ module.exports = {
33
Login: require('./login/index'),
44
Logs: require('./logs/index'),
55
Account: require('./account/index'),
6-
Others: require('./others/index')
6+
Others: require('./others/index'),
7+
Serverless: require('./serverless')
78
}

sdk/serverless/index.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
'use strict';
2+
const util = require('util');
3+
const assert = require('assert');
4+
const Tencentcloud = require('tencentcloud-sdk-nodejs');
5+
const ClientProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/client_profile.js');
6+
const HttpProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/http_profile.js');
7+
const SlsClient = Tencentcloud.sls.v20200205.Client;
8+
const SlsModels = Tencentcloud.sls.v20200205.Models;
9+
const { Credential } = Tencentcloud.common;
10+
const { BindRole } = require('../cam');
11+
12+
class Serverless {
13+
constructor ({appid, secret_id, secret_key, options}) {
14+
this.appid = appid;
15+
this.secretKey = secret_key;
16+
this.secretId = secret_id;
17+
this.options = options;
18+
assert(options, 'Options should not is empty');
19+
this._slsClient = Serverless.createClient(secret_id, secret_key, options);
20+
}
21+
22+
23+
static getCredential(secret_id, secret_key, options) {
24+
const cred = options.token
25+
? new Credential(secret_id, secret_key, options.token)
26+
: new Credential(secret_id, secret_key)
27+
const httpProfile = new HttpProfile();
28+
httpProfile.reqTimeout = 30;
29+
const clientProfile = new ClientProfile('HmacSHA256', httpProfile);
30+
assert(options.region, 'Region should not is empty');
31+
return {
32+
cred: cred,
33+
region: options.region,
34+
clientProfile: clientProfile
35+
}
36+
}
37+
38+
39+
static createClient(secret_id, secret_key, options) {
40+
const info = Serverless.getCredential(secret_id, secret_key, options);
41+
const scfCli = new SlsClient(info.cred, info.region, info.clientProfile);
42+
scfCli.sdkVersion = 'ServerlessFramework';
43+
return scfCli;
44+
}
45+
46+
47+
async getComponentAndVersions(name) {
48+
const compVersion = {
49+
ComponentName: name
50+
}
51+
const req = new SlsModels.GetComponentAndVersionsRequest();
52+
req.from_json_string(JSON.stringify(compVersion));
53+
return await this._call('GetComponentAndVersions', req);
54+
}
55+
56+
async _call(api, params) {
57+
const handler = util.promisify(this._slsClient[api].bind(this._slsClient));
58+
return await handler(params);
59+
}
60+
61+
62+
async getComponentVersion(name, version) {
63+
const componentVersion = {
64+
ComponentName: name,
65+
ComponentVersion: version
66+
}
67+
const req = new SlsModels.GetComponentVersionRequest();
68+
req.from_json_string(JSON.stringify(componentVersion));
69+
return await this._call('GetComponentVersion', req);
70+
}
71+
72+
73+
async prePublishComponent(body) {
74+
const pubComponent = {
75+
Body: body
76+
}
77+
78+
const req = new SlsModels.PrePublishComponentRequest();
79+
req.from_json_string(JSON.stringify(pubComponent));
80+
return await this._call('PrePublishComponent', req);
81+
}
82+
83+
84+
async postPublishComponent(body) {
85+
const pubComponent = {
86+
Body: body
87+
}
88+
const req = new SlsModels.PostPublishComponentRequest();
89+
req.from_json_string(JSON.stringify(pubComponent));
90+
return await this._call('PostPublishComponent', req);
91+
}
92+
93+
94+
async getInstance(body) {
95+
const ins = {
96+
Body: body
97+
}
98+
const req = new SlsModels.GetInstanceRequest();
99+
req.from_json_string(JSON.stringify(ins));
100+
return await this._call('GetInstance', req);
101+
}
102+
103+
104+
async saveInstance(body) {
105+
const ins = {
106+
Body: body
107+
}
108+
const req = new SlsModels.SaveInstanceRequest();
109+
req.from_json_string(JSON.stringify(ins));
110+
return await this._call('SaveInstance', req);
111+
}
112+
113+
114+
async listInstances(body) {
115+
const ins = {
116+
Body: body
117+
}
118+
const req = new SlsModels.ListInstancesRequest();
119+
req.from_json_string(JSON.stringify(ins));
120+
return await this._call('ListInstances', req);
121+
}
122+
123+
124+
async getUploadUrls(body) {
125+
const uploadUrl = {
126+
Body: body
127+
}
128+
const req = new SlsModels.GetUploadUrlsRequest();
129+
req.from_json_string(JSON.stringify(uploadUrl));
130+
return await this._call('GetUploadUrls', req);
131+
}
132+
133+
134+
async runComponent(body) {
135+
const comp = {
136+
Body: body
137+
}
138+
const req = new SlsModels.RunComponentRequest();
139+
req.from_json_string(JSON.stringify(comp));
140+
return await this._call('RunComponent', req);
141+
}
142+
143+
144+
async runFinishComponent(body) {
145+
const comp = {
146+
Body: body
147+
}
148+
const req = new SlsModels.RunFinishComponentRequest();
149+
req.from_json_string(JSON.stringify(comp));
150+
return await this._call('RunFinishComponent', req);
151+
}
152+
153+
154+
async unpublishComponentVersion(name, version) {
155+
const componentVersion = {
156+
Name: name,
157+
ComponentVersion: version
158+
}
159+
const req = new SlsModels.UnpublishComponentVersionRequest();
160+
req.from_json_string(JSON.stringify(componentVersion));
161+
return await this._call('UnpublishComponentVersion', req);
162+
}
163+
164+
165+
async publishComponentVersion({name, componentVersion, org, author, description, keywords, license}) {
166+
167+
const camRole = new BindRole.BindRole({
168+
SecretId: this.secret_id,
169+
SecretKey: this.secret_key,
170+
token: this.options.token
171+
});
172+
173+
camRole.bindSLSQcsRole();
174+
175+
const pubComVersionRequest = {
176+
Name: name,
177+
ComponentVersion: componentVersion,
178+
Org: org,
179+
Author: author,
180+
Description: description,
181+
Keywords: keywords,
182+
License: license
183+
}
184+
185+
const req = new SlsModels.PublishComponentVersionRequest()
186+
req.from_json_string(JSON.stringify(pubComVersionRequest));
187+
return await this._call('PublishComponentVersion', req);
188+
}
189+
190+
191+
async fetchComponentMetadata(name, version) {
192+
const componentVersion = {
193+
Name: name,
194+
ComponentVersion: version
195+
}
196+
const req = new SlsModels.FetchComponentMetadataRequest();
197+
req.from_json_string(JSON.stringify(componentVersion));
198+
return await this._call('FetchComponentMetadata', req);
199+
}
200+
}
201+
202+
module.exports = Serverless;

0 commit comments

Comments
 (0)