Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeiminds committed Aug 31, 2021
0 parents commit 7a09f48
Show file tree
Hide file tree
Showing 135 changed files with 70,386 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
parser: 'typescript-eslint-parser',
plugins: ['typescript'],
rules: {
'typescript/no-unused-vars': 'error',
},
};
10 changes: 10 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--- Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests --->

FEATURES:


ENHANCEMENTS:


BUG FIXES:

32 changes: 32 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Running Code Coverage

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Run the tests
run: npm run coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.swp
.idea/
dist/
node_modules/
yarn-error.log
coverage/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!lib/index.js
README.md
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@types
15 changes: 15 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"printWidth": 80,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"useTabs": false,
"parser": "babel-ts",
"overrides": [
{
"files": "*.json",
"options": { "parser": "json", "printWidth": 200 }
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
help:
@echo "help"

.PHONY: test
test:
npm run test

test-cov:
npm run coverage

lint:
npm run tslint
npm run prettier:check

fmt:
npm run prettier:write

build:
npm run publish

gen:
ucloud-spec create opensdk \
--only "UFS" \
--only "UDisk" \
--only "UHost" \
--only "PathX" \
--only "UDDB" \
--only "UCDN" \
--only "UNet" \
--only "VPC2.0" \
--only "UDB" \
--only "UMem" \
--only "ULB" \
--only "Cube" \
--only "UK8S" \
--only "IPSecVPN" \
--only "UAccount" \
--only "UDPN" \
--only "UBill" \
--only "UPHost" \
--only "UFile" \
--only "USMS" \
--only "UEC" \
--public \
-s https://git.ucloudadmin.com/apispec/apispec.git \
/Users/user/code/oas/plugins/template-opensdk-js .
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ucloud-sdk-js
44 changes: 44 additions & 0 deletions docs/configure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 通用配置

了解如何配置 SDK,如日志、重试、服务访问端点(公有云、专有云)等。

## 配置项清单

| 配置 | 类型 | 描述 |
| --------------- | ---- | ------------------------------------------------------------ |
| **region** | string | (必填)服务所在地域,可参考 [地域和可用区列表](https://docs.ucloud.cn/api/summary/regionlist) |
| **projectId** | string | (选填)项目的唯一标识,用于组织资源,大多数资源都需要 ProjectId,如果是主账号或财务账号,默认值为默认 ProjectId,如果是子账号非财务账号,则该字段必须填写。 |
| **baseUrl** | string | (选填) API 服务的访问端点,默认是 https://api.ucloud.cn |
| **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” |
| **timeout** | number | (选填)请求超时时间,默认 30s |
| **maxRetries** | number | (选填)最大重试次数. 默认重试 3 次。设置该值大于 0 将对网络和服务可用性问题进行自动重试,使用指数退避的重试间隔,并自动跳过资源创建类的接口。 |
| **logger** | logger | (选填)自定义 Logger |

## 进阶配置选项

### 修改默认日志

关闭日志:

```javascript
const client = new Client({
config: {
// ...
logger: null,
},
// ...
});
```

### 访问专有云/渠道云或其它网关

```javascript
const client = new Client({
config: {
// ...
region: "专有云地域",
baseUrl: "foo.api.ucloud.cn", // 替换成专有云网关
},
// ...
});
```
14 changes: 14 additions & 0 deletions docs/error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 错误处理

了解如何处理不同类型的 SDK 异常,包括参数错误,RetCode 不为 0 的业务异常等。

```javascript
try {
resp = await client.uhost().describeImage();
} catch (e) {
console.log(e.typ);
console.log(e.retCode);
console.log(e.requestId);
throw e;
}
```
38 changes: 38 additions & 0 deletions docs/generic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 泛化调用

如何调用 SDK 尚未支持的 API ?可以使用泛化调用方式。

**NOTE** 如果没有必须使用的理由,不建议使用泛化方式调用 API,因为无法享受 OpenAPI 提供的兼容性保证。

## 调用方式

```javascript
const {Client} = require("../../lib/services");
const Request = require("../../lib/core/request").default;

async function main() {
// Build client
const client = new Client({
config: {
region: 'cn-bj2',
projectId: process.env.UCLOUD_PROJECT_ID || '',
},
credential: {
publicKey: process.env.UCLOUD_PUBLIC_KEY || '',
privateKey: process.env.UCLOUD_PRIVATE_KEY || '',
}
});
let resp = null;

try {
resp = await client.invoke(new Request({
Action: "DescribeImage",
}));
} catch (e) {
throw e;
}
const image = resp.toObject()["ImageSet"][0];
}

main().catch(e => { console.error(e) })
```
25 changes: 25 additions & 0 deletions docs/middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 请求中间件

了解如何拦截 SDK 发起的请求,并统一添加额外的逻辑。

UCloud SDK 为请求提供了请求中间件的特性。

该特性允许在 请求/响应 的生命周期中添加自定义的逻辑。

例如,Client 级别的中间件,可以拦截参数/响应字典:

```php
client.useMiddleware({
request: function (ctx: Context) {
ctx.config.logger.info(ctx.request.toObject());
return ctx.request;
},
response: function (ctx: Context) {
ctx.config.logger.info(ctx.response.toObject());
return ctx.response;
},
error: function (ctx: Context) {
ctx.config.logger.error(ctx.exception?.message);
},
})
```
105 changes: 105 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 快速开始

## 安装

使用 `npm` 安装(推荐):

```bash
$ npm install @ucloud/ucloud-sdk-js
```

> 注意:内测阶段,未注册到官方仓库,只能采用源码安装,npm registry 方式暂时不可用。
```bash
git clone https://git.ucloudadmin.com/ucloud-sdk/ucloud-sdk-nodejs.git
cd ucloud-sdk-nodejs && npm install
cd examples/uhost && node index.js
```

## 初次使用

目前,SDK 使用 PublicKey/PrivateKey 作为唯一的鉴权方式,该公私钥可以从以下途径获取:

- [UAPI 密钥管理](https://console.ucloud.cn/uapi/apikey)

下面提供一个简单的示例:

```javascript
const {Client} = require("ucloud-sdk-js/lib/services");

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function main() {
// Build client
const client = new Client({
config: {
region: 'cn-bj2',
projectId: process.env.UCLOUD_PROJECT_ID || '',
},
credential: {
publicKey: process.env.UCLOUD_PUBLIC_KEY || '',
privateKey: process.env.UCLOUD_PRIVATE_KEY || '',
}
});
const zone = "cn-bj2-05";

let resp = null;

// Describe Image
try {
resp = await client.uhost().describeImage();
} catch (e) {
throw e;
}
const image = resp["ImageSet"][0];

// Create Instance
try {
resp = await client.uhost().createUHostInstance({
Name: "sdk-js-example",
Zone: zone,
ImageId: image["ImageId"],
LoginMode: "Password",
Password: new Buffer("UCloud1234!").toString('base64'),
CPU: 1,
Memory: 1024,
Disks: [{
Size: image["ImageSize"],
Type: "CLOUD_SSD",
IsBoot: "true",
}],
});
} catch (e) {
throw e;
}
}

main().catch(e => { console.error(e) })
```

将上述代码中 client 相关配置,以及主机的 image id 等,替换成自己的配置,即可创建一台云主机。

在该示例中,使用 SDK 完成了一个创建云主机的请求。至此,已经涵盖了 SDK 的基本核心用法,可以构建自己的脚本啦!

SDK 中的每一个 api 调用都有详细的注释文档,
可以通过 Editor/IDE 跳转到具体的方法中查看(也可以 [查看接口文档](https://docs.ucloud.cn/api/summary/README) ),
并根据 IDE 自动补全和报错信息继续探索 SDK 的用法。

如果需要了解这段代码提及但未完全覆盖的使用技巧,请参考:

- [通用配置](configure.md),了解如何配置 SDK,如日志、重试、服务访问端点(公有云、专有云)等
- [错误处理](error.md),了解如何处理不同类型的 SDK 异常,包括参数错误,RetCode 不为 0 的业务异常等
- [类型系统](typesystem.md),了解 SDK 如何校验参数,并规范化 API 的返回值。
- [请求中间件](middleware.md),了解如何拦截 SDK 发起的请求,并统一添加额外的逻辑。
- [泛化调用](generic.md),如何调用 SDK 尚未支持的 API(不建议使用此类 API,因为没有兼容性保证)

## 获取更多示例

### 基于场景的示例

SDK 提供了部分基于场景的示例,并提供了对应的资源销毁逻辑,可以点击以下链接查看源码:

- [批量创建云主机](../examples/uhost)
- [创建基于负载均衡器的两层架构](../examples/two-tier)
3 changes: 3 additions & 0 deletions docs/typesystem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 类型系统

待定。
Loading

0 comments on commit 7a09f48

Please sign in to comment.