Skip to content

Commit 7a09f48

Browse files
committed
initial commit
0 parents  commit 7a09f48

File tree

135 files changed

+70386
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+70386
-0
lines changed

.eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
parser: 'typescript-eslint-parser',
3+
plugins: ['typescript'],
4+
rules: {
5+
'typescript/no-unused-vars': 'error',
6+
},
7+
};

.github/PULL_REQUEST_TEMPLATE.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--- Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests --->
2+
3+
FEATURES:
4+
5+
6+
ENHANCEMENTS:
7+
8+
9+
BUG FIXES:
10+

.github/workflows/workflow.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Running Code Coverage
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [10.x, 12.x, 14.x]
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v2
17+
with:
18+
fetch-depth: 2
19+
20+
- name: Set up Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: Run the tests
29+
run: npm run coverage
30+
31+
- name: Upload coverage to Codecov
32+
uses: codecov/codecov-action@v2

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.swp
2+
.idea/
3+
dist/
4+
node_modules/
5+
yarn-error.log
6+
coverage/

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!lib/index.js
3+
README.md

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@types

.prettierrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true,
6+
"trailingComma": "es5",
7+
"useTabs": false,
8+
"parser": "babel-ts",
9+
"overrides": [
10+
{
11+
"files": "*.json",
12+
"options": { "parser": "json", "printWidth": 200 }
13+
}
14+
]
15+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
help:
2+
@echo "help"
3+
4+
.PHONY: test
5+
test:
6+
npm run test
7+
8+
test-cov:
9+
npm run coverage
10+
11+
lint:
12+
npm run tslint
13+
npm run prettier:check
14+
15+
fmt:
16+
npm run prettier:write
17+
18+
build:
19+
npm run publish
20+
21+
gen:
22+
ucloud-spec create opensdk \
23+
--only "UFS" \
24+
--only "UDisk" \
25+
--only "UHost" \
26+
--only "PathX" \
27+
--only "UDDB" \
28+
--only "UCDN" \
29+
--only "UNet" \
30+
--only "VPC2.0" \
31+
--only "UDB" \
32+
--only "UMem" \
33+
--only "ULB" \
34+
--only "Cube" \
35+
--only "UK8S" \
36+
--only "IPSecVPN" \
37+
--only "UAccount" \
38+
--only "UDPN" \
39+
--only "UBill" \
40+
--only "UPHost" \
41+
--only "UFile" \
42+
--only "USMS" \
43+
--only "UEC" \
44+
--public \
45+
-s https://git.ucloudadmin.com/apispec/apispec.git \
46+
/Users/user/code/oas/plugins/template-opensdk-js .

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ucloud-sdk-js

docs/configure.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 通用配置
2+
3+
了解如何配置 SDK,如日志、重试、服务访问端点(公有云、专有云)等。
4+
5+
## 配置项清单
6+
7+
| 配置 | 类型 | 描述 |
8+
| --------------- | ---- | ------------------------------------------------------------ |
9+
| **region** | string | (必填)服务所在地域,可参考 [地域和可用区列表](https://docs.ucloud.cn/api/summary/regionlist) |
10+
| **projectId** | string | (选填)项目的唯一标识,用于组织资源,大多数资源都需要 ProjectId,如果是主账号或财务账号,默认值为默认 ProjectId,如果是子账号非财务账号,则该字段必须填写。 |
11+
| **baseUrl** | string | (选填) API 服务的访问端点,默认是 https://api.ucloud.cn |
12+
| **userAgent** | string | (选填)UserAgent 是 SDK 客户端特有的属性,用于区分使用 SDK 的版本。User-Agent 的定义请参考 [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent)。用户自定义的 UserAgent 将追加到 SDK 版本号的末尾。例如, “MyAPP/0.10.1” -> “PHP/8.0.6 PHP-SDK/0.1.0 MyAPP/0.10.1” |
13+
| **timeout** | number | (选填)请求超时时间,默认 30s |
14+
| **maxRetries** | number | (选填)最大重试次数. 默认重试 3 次。设置该值大于 0 将对网络和服务可用性问题进行自动重试,使用指数退避的重试间隔,并自动跳过资源创建类的接口。 |
15+
| **logger** | logger | (选填)自定义 Logger |
16+
17+
## 进阶配置选项
18+
19+
### 修改默认日志
20+
21+
关闭日志:
22+
23+
```javascript
24+
const client = new Client({
25+
config: {
26+
// ...
27+
logger: null,
28+
},
29+
// ...
30+
});
31+
```
32+
33+
### 访问专有云/渠道云或其它网关
34+
35+
```javascript
36+
const client = new Client({
37+
config: {
38+
// ...
39+
region: "专有云地域",
40+
baseUrl: "foo.api.ucloud.cn", // 替换成专有云网关
41+
},
42+
// ...
43+
});
44+
```

docs/error.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 错误处理
2+
3+
了解如何处理不同类型的 SDK 异常,包括参数错误,RetCode 不为 0 的业务异常等。
4+
5+
```javascript
6+
try {
7+
resp = await client.uhost().describeImage();
8+
} catch (e) {
9+
console.log(e.typ);
10+
console.log(e.retCode);
11+
console.log(e.requestId);
12+
throw e;
13+
}
14+
```

docs/generic.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 泛化调用
2+
3+
如何调用 SDK 尚未支持的 API ?可以使用泛化调用方式。
4+
5+
**NOTE** 如果没有必须使用的理由,不建议使用泛化方式调用 API,因为无法享受 OpenAPI 提供的兼容性保证。
6+
7+
## 调用方式
8+
9+
```javascript
10+
const {Client} = require("../../lib/services");
11+
const Request = require("../../lib/core/request").default;
12+
13+
async function main() {
14+
// Build client
15+
const client = new Client({
16+
config: {
17+
region: 'cn-bj2',
18+
projectId: process.env.UCLOUD_PROJECT_ID || '',
19+
},
20+
credential: {
21+
publicKey: process.env.UCLOUD_PUBLIC_KEY || '',
22+
privateKey: process.env.UCLOUD_PRIVATE_KEY || '',
23+
}
24+
});
25+
let resp = null;
26+
27+
try {
28+
resp = await client.invoke(new Request({
29+
Action: "DescribeImage",
30+
}));
31+
} catch (e) {
32+
throw e;
33+
}
34+
const image = resp.toObject()["ImageSet"][0];
35+
}
36+
37+
main().catch(e => { console.error(e) })
38+
```

docs/middleware.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 请求中间件
2+
3+
了解如何拦截 SDK 发起的请求,并统一添加额外的逻辑。
4+
5+
UCloud SDK 为请求提供了请求中间件的特性。
6+
7+
该特性允许在 请求/响应 的生命周期中添加自定义的逻辑。
8+
9+
例如,Client 级别的中间件,可以拦截参数/响应字典:
10+
11+
```php
12+
client.useMiddleware({
13+
request: function (ctx: Context) {
14+
ctx.config.logger.info(ctx.request.toObject());
15+
return ctx.request;
16+
},
17+
response: function (ctx: Context) {
18+
ctx.config.logger.info(ctx.response.toObject());
19+
return ctx.response;
20+
},
21+
error: function (ctx: Context) {
22+
ctx.config.logger.error(ctx.exception?.message);
23+
},
24+
})
25+
```

docs/quickstart.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 快速开始
2+
3+
## 安装
4+
5+
使用 `npm` 安装(推荐):
6+
7+
```bash
8+
$ npm install @ucloud/ucloud-sdk-js
9+
```
10+
11+
> 注意:内测阶段,未注册到官方仓库,只能采用源码安装,npm registry 方式暂时不可用。
12+
13+
```bash
14+
git clone https://git.ucloudadmin.com/ucloud-sdk/ucloud-sdk-nodejs.git
15+
cd ucloud-sdk-nodejs && npm install
16+
cd examples/uhost && node index.js
17+
```
18+
19+
## 初次使用
20+
21+
目前,SDK 使用 PublicKey/PrivateKey 作为唯一的鉴权方式,该公私钥可以从以下途径获取:
22+
23+
- [UAPI 密钥管理](https://console.ucloud.cn/uapi/apikey)
24+
25+
下面提供一个简单的示例:
26+
27+
```javascript
28+
const {Client} = require("ucloud-sdk-js/lib/services");
29+
30+
function sleep(ms) {
31+
return new Promise(resolve => setTimeout(resolve, ms))
32+
}
33+
34+
async function main() {
35+
// Build client
36+
const client = new Client({
37+
config: {
38+
region: 'cn-bj2',
39+
projectId: process.env.UCLOUD_PROJECT_ID || '',
40+
},
41+
credential: {
42+
publicKey: process.env.UCLOUD_PUBLIC_KEY || '',
43+
privateKey: process.env.UCLOUD_PRIVATE_KEY || '',
44+
}
45+
});
46+
const zone = "cn-bj2-05";
47+
48+
let resp = null;
49+
50+
// Describe Image
51+
try {
52+
resp = await client.uhost().describeImage();
53+
} catch (e) {
54+
throw e;
55+
}
56+
const image = resp["ImageSet"][0];
57+
58+
// Create Instance
59+
try {
60+
resp = await client.uhost().createUHostInstance({
61+
Name: "sdk-js-example",
62+
Zone: zone,
63+
ImageId: image["ImageId"],
64+
LoginMode: "Password",
65+
Password: new Buffer("UCloud1234!").toString('base64'),
66+
CPU: 1,
67+
Memory: 1024,
68+
Disks: [{
69+
Size: image["ImageSize"],
70+
Type: "CLOUD_SSD",
71+
IsBoot: "true",
72+
}],
73+
});
74+
} catch (e) {
75+
throw e;
76+
}
77+
}
78+
79+
main().catch(e => { console.error(e) })
80+
```
81+
82+
将上述代码中 client 相关配置,以及主机的 image id 等,替换成自己的配置,即可创建一台云主机。
83+
84+
在该示例中,使用 SDK 完成了一个创建云主机的请求。至此,已经涵盖了 SDK 的基本核心用法,可以构建自己的脚本啦!
85+
86+
SDK 中的每一个 api 调用都有详细的注释文档,
87+
可以通过 Editor/IDE 跳转到具体的方法中查看(也可以 [查看接口文档](https://docs.ucloud.cn/api/summary/README) ),
88+
并根据 IDE 自动补全和报错信息继续探索 SDK 的用法。
89+
90+
如果需要了解这段代码提及但未完全覆盖的使用技巧,请参考:
91+
92+
- [通用配置](configure.md),了解如何配置 SDK,如日志、重试、服务访问端点(公有云、专有云)等
93+
- [错误处理](error.md),了解如何处理不同类型的 SDK 异常,包括参数错误,RetCode 不为 0 的业务异常等
94+
- [类型系统](typesystem.md),了解 SDK 如何校验参数,并规范化 API 的返回值。
95+
- [请求中间件](middleware.md),了解如何拦截 SDK 发起的请求,并统一添加额外的逻辑。
96+
- [泛化调用](generic.md),如何调用 SDK 尚未支持的 API(不建议使用此类 API,因为没有兼容性保证)
97+
98+
## 获取更多示例
99+
100+
### 基于场景的示例
101+
102+
SDK 提供了部分基于场景的示例,并提供了对应的资源销毁逻辑,可以点击以下链接查看源码:
103+
104+
- [批量创建云主机](../examples/uhost)
105+
- [创建基于负载均衡器的两层架构](../examples/two-tier)

docs/typesystem.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 类型系统
2+
3+
待定。

0 commit comments

Comments
 (0)