ksyun-sdk-node 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.browserslistrc +1 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.json +14 -0
  4. package/README.md +75 -0
  5. package/babel.config.json +11 -0
  6. package/dist/base/BaseClient.js +104 -0
  7. package/dist/index.js +41 -0
  8. package/dist/lib/fetch.js +35 -0
  9. package/dist/lib/sign.js +155 -0
  10. package/dist/service/Actiontrail/v20190401/index.js +58 -0
  11. package/dist/service/Bill/v20180601/index.js +117 -0
  12. package/dist/service/Bill_union/v20200101/index.js +118 -0
  13. package/dist/service/Bill_union/v20211209/index.js +45 -0
  14. package/dist/service/Ebs/v20160304/index.js +359 -0
  15. package/dist/service/Iam/v20151101/index.js +887 -0
  16. package/dist/service/Kad/v20161122/index.js +133 -0
  17. package/dist/service/Kead/v20200101/index.js +52 -0
  18. package/dist/service/Resourcemanager/v20210320/index.js +171 -0
  19. package/dist/service/Sts/v20151101/index.js +50 -0
  20. package/dist/service/Trade/v20200114/index.js +47 -0
  21. package/dist/service/Trade/v20200831/index.js +49 -0
  22. package/example/iam.js +40 -0
  23. package/example/res.js +43 -0
  24. package/package.json +33 -0
  25. package/src/base/BaseClient.js +92 -0
  26. package/src/index.js +34 -0
  27. package/src/lib/fetch.js +35 -0
  28. package/src/lib/sign.js +148 -0
  29. package/src/service/Actiontrail/v20190401/index.js +47 -0
  30. package/src/service/Bill/v20180601/index.js +111 -0
  31. package/src/service/Bill_union/v20200101/index.js +112 -0
  32. package/src/service/Bill_union/v20211209/index.js +35 -0
  33. package/src/service/Ebs/v20160304/index.js +348 -0
  34. package/src/service/Iam/v20151101/index.js +876 -0
  35. package/src/service/Kad/v20161122/index.js +122 -0
  36. package/src/service/Kead/v20200101/index.js +41 -0
  37. package/src/service/Resourcemanager/v20210320/index.js +161 -0
  38. package/src/service/Sts/v20151101/index.js +39 -0
  39. package/src/service/Trade/v20200114/index.js +36 -0
  40. package/src/service/Trade/v20200831/index.js +38 -0
@@ -0,0 +1,92 @@
1
+ const getSignedParams = require('../lib/sign.js').getSignedParams
2
+ const fetch = require('../lib/fetch.js')
3
+ const dayjs = require('dayjs')
4
+ const utc = require('dayjs/plugin/utc.js')
5
+ const qs = require('qs')
6
+
7
+ dayjs.extend(utc)
8
+
9
+ module.exports = class BaseClient {
10
+ _baseConfig = {}
11
+ _apiList = {}
12
+ constructor (clientConfig) {
13
+ this.ak = clientConfig.credential.secretId,
14
+ this.sk = clientConfig.credential.secretKey
15
+ this.region = clientConfig.region
16
+ this.httpProfile = clientConfig.httpProfile || {}
17
+
18
+ if (!this.ak || !this.sk) {
19
+ throw new Error('secretId and secretKey are required')
20
+ }
21
+ }
22
+ /**
23
+ * 发起请求
24
+ * @param {string} apiAction 接口名
25
+ * @param {object} data 参数 { body, query }
26
+ */
27
+ request (apiAction, params = {}) {
28
+
29
+ let apiConfig = this._apiList[apiAction]
30
+ let publicParams = {
31
+ Accesskey: this.ak,
32
+ Service: this._baseConfig.config.credentials.service,
33
+ Action: apiAction,
34
+ Version: apiConfig.config.query.Version,
35
+ Timestamp: dayjs().utc().format('YYYY-MM-DDThh:mm:ss')+'Z',
36
+ SignatureVersion: '1.0',
37
+ SignatureMethod: 'HMAC-SHA256',
38
+ Region: this.region || this._baseConfig.config.credentials.region,
39
+ }
40
+
41
+ let combainParams = {
42
+ ...publicParams,
43
+ ...params
44
+ }
45
+
46
+ let signedParams = getSignedParams(combainParams, this.sk)
47
+
48
+ let protocol = this.httpProfile.protocol || this._baseConfig.protocol
49
+ let endpoint = this.httpProfile.endpoint || this._baseConfig.endpoint
50
+ let method = this.httpProfile.method || apiConfig.method
51
+
52
+ let url = `${protocol}${endpoint}${apiConfig.url}`
53
+ if (['GET', 'DELETE', 'OPTION', 'HEAD'].includes(method.toUpperCase())) {
54
+ url += `?${qs.stringify(signedParams)}`
55
+ }
56
+ let headers = {
57
+ ...(this._baseConfig.config.headers || {}),
58
+ ...(apiConfig.config.headers || {}),
59
+ // 目前kop只支持application/x-www-form-urlencoded类型的签名解析
60
+ 'Content-Type': 'application/x-www-form-urlencoded',
61
+ }
62
+ let body = this.getBody(method, headers['Content-Type'], signedParams)
63
+
64
+ let timeoutSecond = this.httpProfile.timeout || this._baseConfig.config.timeout
65
+ return fetch(url, {
66
+ method: method,
67
+ timeout: timeoutSecond * 1000,
68
+ headers,
69
+ body
70
+ })
71
+ }
72
+ /**
73
+ * 获取body
74
+ * @param {string} method 请求类型
75
+ * @param {string} contentType
76
+ * @param {object} signedParams 参数对象
77
+ * @returns {string}
78
+ */
79
+ getBody (method, contentType, signedParams) {
80
+ if (!['POST', 'PUT'].includes(method.toUpperCase())) {
81
+ return undefined
82
+ }
83
+ // 目前只有下面这两种
84
+ if (contentType == 'application/x-www-form-urlencoded') {
85
+ return qs.stringify(signedParams)
86
+ }
87
+ if (contentType == 'application/json') {
88
+ return JSON.stringify(signedParams)
89
+ }
90
+ return JSON.stringify(signedParams)
91
+ }
92
+ }
package/src/index.js ADDED
@@ -0,0 +1,34 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+
4
+ // /xxx/node-sdk/src/service
5
+ let servicePath = path.resolve(__dirname, './service')
6
+ // [Iam, Kec]
7
+ let serviceDirs = fs.readdirSync(servicePath)
8
+
9
+ /**
10
+ * {
11
+ * Iam: {
12
+ * v20150101: Client
13
+ * }
14
+ * }
15
+ */
16
+ module.exports = serviceDirs.reduce((acc, serviceDir) => {
17
+ // /xxx/node-sdk/src/service/iam
18
+ let versionPath = path.resolve(servicePath, serviceDir)
19
+ if (fs.statSync(versionPath).isDirectory()) {
20
+ // [v20151101, v20160606]
21
+ let versionDirs = fs.readdirSync(versionPath)
22
+ // acc.Iam
23
+ acc[serviceDir] = versionDirs.reduce((accVersion, versionDir) => {
24
+ // /xxx/node-sdk/src/service/iam/v20151101/index.js
25
+ let filePath = path.resolve(versionPath, versionDir, 'index.js')
26
+ if (fs.existsSync(filePath)) {
27
+ // accVersion.v20151101
28
+ accVersion[versionDir] = require(filePath)
29
+ }
30
+ return accVersion
31
+ }, {})
32
+ }
33
+ return acc
34
+ }, {})
@@ -0,0 +1,35 @@
1
+ const nodeFetch = require('node-fetch')
2
+ const AbortController = require('abort-controller')
3
+ // const HttpsProxyAgent = require('https-proxy-agent')
4
+
5
+
6
+ let proxyConfig = {}
7
+
8
+ // if (process.env.isKscSdkTest) {
9
+ // 本地代理
10
+ // proxyConfig = {
11
+ // agent: new HttpsProxyAgent('http://localhost:9090')
12
+ // }
13
+ // }
14
+
15
+ module.exports = function (url, options) {
16
+ const controller = new AbortController();
17
+ if (options.timeout) {
18
+ setTimeout(() => {
19
+ controller.abort();
20
+ }, options.timeout);
21
+ }
22
+
23
+ return nodeFetch(url, {
24
+ signal: controller.signal,
25
+ ...proxyConfig,
26
+ ...options
27
+ })
28
+ .catch(err => {
29
+ if (err.type == 'aborted') {
30
+ return new Error('timeout')
31
+ } else {
32
+ throw err
33
+ }
34
+ })
35
+ }
@@ -0,0 +1,148 @@
1
+ const crypto = require("crypto-js")
2
+ // const qs = require("qs")
3
+
4
+ module.exports = {
5
+ getSignedParams,
6
+ fixedEncodeURIComponent,
7
+ getFlatParams,
8
+ flatObj,
9
+ getFormatParams,
10
+ isArrayOrObjectNotNull
11
+ }
12
+
13
+ /**
14
+ * 简化版签名算法
15
+ * @param {*} params 参数
16
+ * @param {*} sk
17
+ */
18
+ function getSignedParams(params, sk) {
19
+ if (!sk) {
20
+ throw new Error('need sk')
21
+ }
22
+ // 第零点一步:参数扁平化处理
23
+ let flatParams = getFlatParams(params)
24
+ // 第零点二步:去除值为null的参数, boolean转字符串
25
+ flatParams = getFormatParams(flatParams)
26
+ // 第一步:请求参数排序 请求参数包括公共参数和业务参数,不包含公共参数Signature。 排序规则以参数名按照字典排序.
27
+ let sortParamKeys = Object.keys(flatParams).sort()
28
+ // 第二步:请求参数编码
29
+ // 使用UTF-8字符集按照RFC3986规则编码请求参数和参数取值
30
+ // 第三步:请求参数拼接成CanonicalizedQueryString
31
+ // 每对URLEncode后的参数名称和参数值,用=进行连接。每对之间使用&进行连接。得到规范化请求字符串CanonicalizedQueryString
32
+ let canonicalizedQueryString = sortParamKeys.map(key => {
33
+ let value = flatParams[key]
34
+ return `${fixedEncodeURIComponent(key)}=${fixedEncodeURIComponent(value)}`
35
+ }).join('&')
36
+
37
+ // 参见源码得qs的处理顺序-符合流程:1. 去除null 2. 排序、3. 编码 4. 按照url参数拼接
38
+ // let canonicalizedQueryString = qs.stringify(flatParams, {
39
+ // sort: (a, b) => a.localeCompare(b),
40
+ // format : 'RFC3986',
41
+ // skipNulls: true,
42
+ // arrayFormat: 'indices', // 数组转成a[0]
43
+ // allowDots: true, // 对象转成a.b.c
44
+ // } )
45
+
46
+ // 计算签名。
47
+ let sign = crypto.HmacSHA256(canonicalizedQueryString, sk).toString()
48
+
49
+ // 添加到请求参数
50
+ return {
51
+ ...flatParams,
52
+ Signature: sign
53
+ }
54
+ }
55
+
56
+ /**
57
+ * RFC 3986
58
+ * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
59
+ * @param {*} str
60
+ * @returns
61
+ */
62
+ function fixedEncodeURIComponent(str) {
63
+ // encodeURIComponent比RFC 3986少了!'()*这个几个字符的转义
64
+ return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
65
+ return '%' + c.charCodeAt(0).toString(16).toUpperCase();
66
+ });
67
+ }
68
+
69
+ /**
70
+ * 格式化参数,对Array,Filter(Object),Object类型做扁平处理
71
+ * @example
72
+ * 转换前:{ arr: [1,2], filter: {1: {a: 1}} }
73
+ * 转换后:{ 'arr[0]':1, 'arr[1]':2, 'filter.1.a': 1 }
74
+ * @param {string} apiAction 接口名
75
+ * @param {object} params 参数值
76
+ * @returns 格式化后的参数
77
+ */
78
+ function getFlatParams (params) {
79
+ let res = {}
80
+ Object.keys(params).forEach(key => {
81
+ let value = params[key]
82
+ if (isArrayOrObjectNotNull(value)) {
83
+ res = {
84
+ ...res,
85
+ ...flatObj(key, params[key])
86
+ }
87
+ } else {
88
+ res[key] = value
89
+ }
90
+ })
91
+ return res
92
+ }
93
+
94
+ /**
95
+ * 扁平化对象
96
+ * @param {string} pKey key
97
+ * @param {*} value 对象或者数组,非null
98
+ * @returns
99
+ */
100
+ function flatObj (pKey, value) {
101
+ let res = {}
102
+ let prefixName = ''
103
+
104
+ Object.keys(value).forEach((key) => {
105
+ if (Array.isArray(value)) {
106
+ prefixName = `${pKey}[${key}]`
107
+ } else {
108
+ prefixName = `${pKey}.${key}`
109
+ }
110
+ let childValue = value[key]
111
+ if (isArrayOrObjectNotNull(childValue)) {
112
+ res = {
113
+ ...res,
114
+ ...flatObj(prefixName, childValue)
115
+ }
116
+ } else {
117
+ res[prefixName] = childValue
118
+ }
119
+ })
120
+ return res
121
+ }
122
+
123
+ /**
124
+ * 1. 剔除null、undefined,
125
+ * 2. boolean转字符串
126
+ * @param {object} params 参数对象
127
+ * @returns {object}
128
+ */
129
+ function getFormatParams (params) {
130
+ Object.keys(params).forEach(key => {
131
+ if (params[key] == null) {
132
+ delete params[key]
133
+ }
134
+ if (Object.prototype.toString.call(params[key]) == '[object Boolean]') {
135
+ params[key] = params[key].toString()
136
+ }
137
+ })
138
+ return params
139
+ }
140
+
141
+ /**
142
+ * 是否为数组或者对象,非null
143
+ * @param {any} value
144
+ * @returns {boolean}
145
+ */
146
+ function isArrayOrObjectNotNull (value) {
147
+ return Array.isArray(value) || Object.prototype.toString.call(value) == '[object Object]'
148
+ }
@@ -0,0 +1,47 @@
1
+ const BaseClient = require("../../../base/BaseClient.js");
2
+
3
+ module.exports = class Client extends BaseClient {
4
+ _baseConfig = {
5
+ 'protocol': 'http://',
6
+ 'endpoint': 'actiontrail.api.ksyun.com',
7
+ 'config': {
8
+ 'timeout': 60, //设置timeout
9
+ 'headers': {
10
+ 'Accept': 'application/json'
11
+ },
12
+ 'credentials': {
13
+ 'region': 'cn-shanghai-3',
14
+ 'service': 'actiontrail',
15
+ },
16
+ },
17
+ }
18
+ _apiList = {
19
+ 'ListOperateLogs': {
20
+ 'url': '/',
21
+ 'method': 'GET',
22
+ 'config': {
23
+ 'query': {
24
+ 'Version': '2019-04-01',
25
+ 'Action': 'ListOperateLogs',
26
+ },
27
+ 'headers': {
28
+ 'Content-Type': 'application/x-www-form-urlencoded'
29
+ },
30
+ },
31
+ 'paramsType': {
32
+ 'EventName': 'String',
33
+ 'EventRw': 'String',
34
+ 'ServiceName': 'String',
35
+ 'UserName': 'String',
36
+ 'AccessKey': 'String',
37
+ 'EventBeginDate': 'String',
38
+ 'EventEndDate': 'String',
39
+ 'ResourceType': 'String',
40
+ 'ResourceName': 'String',
41
+ 'Page': 'String',
42
+ 'PageSize': 'String',
43
+ 'SearchAfter': 'String',
44
+ }
45
+ },
46
+ }
47
+ }
@@ -0,0 +1,111 @@
1
+ const BaseClient = require("../../../base/BaseClient.js");
2
+
3
+ module.exports = class Client extends BaseClient {
4
+ _baseConfig = {
5
+ 'protocol': 'http://',
6
+ 'endpoint': 'bill.api.ksyun.com',
7
+ 'config': {
8
+ 'timeout': 60, //设置timeout
9
+ 'headers': {
10
+ 'Accept': 'application/json'
11
+ },
12
+ 'credentials': {
13
+ 'region': 'cn-shanghai-3',
14
+ 'service': 'bill',
15
+ },
16
+ },
17
+ }
18
+ _apiList = {
19
+ 'GetMonthBill': {
20
+ 'url': '/',
21
+ 'method': 'GET',
22
+ 'config': {
23
+ 'query': {
24
+ 'Version': '2018-06-01',
25
+ 'Action': 'GetMonthBill',
26
+ },
27
+ 'headers': {
28
+ 'Content-Type': 'application/json'
29
+ },
30
+ },
31
+ 'paramsType': {
32
+ 'Action': 'String',
33
+ }
34
+ },
35
+ 'GetPostpayDetailBill': {
36
+ 'url': '/',
37
+ 'method': 'GET',
38
+ 'config': {
39
+ 'query': {
40
+ 'Version': '2018-06-01',
41
+ 'Action': 'GetPostpayDetailBill',
42
+ },
43
+ 'headers': {
44
+ 'Content-Type': 'application/json'
45
+ },
46
+ },
47
+ 'paramsType': {
48
+ }
49
+ },
50
+ 'GetPostpayDetailBillCSV': {
51
+ 'url': '/',
52
+ 'method': 'GET',
53
+ 'config': {
54
+ 'query': {
55
+ 'Version': '2018-06-01',
56
+ 'Action': 'GetPostpayDetailBillCSV',
57
+ },
58
+ 'headers': {
59
+ 'Content-Type': 'application/json'
60
+ },
61
+ },
62
+ 'paramsType': {
63
+ }
64
+ },
65
+ 'GetProductCode': {
66
+ 'url': '/',
67
+ 'method': 'GET',
68
+ 'config': {
69
+ 'query': {
70
+ 'Version': '2018-06-01',
71
+ 'Action': 'GetProductCode',
72
+ },
73
+ 'headers': {
74
+ 'Content-Type': 'application/json'
75
+ },
76
+ },
77
+ 'paramsType': {
78
+ }
79
+ },
80
+ 'getMonthConsume': {
81
+ 'url': '/',
82
+ 'method': 'GET',
83
+ 'config': {
84
+ 'query': {
85
+ 'Version': '2018-06-01',
86
+ 'Action': 'getMonthConsume',
87
+ },
88
+ 'headers': {
89
+ 'Content-Type': 'application/json'
90
+ },
91
+ },
92
+ 'paramsType': {
93
+ }
94
+ },
95
+ 'getPostpayDetailConsume': {
96
+ 'url': '/',
97
+ 'method': 'GET',
98
+ 'config': {
99
+ 'query': {
100
+ 'Version': '2018-06-01',
101
+ 'Action': 'getPostpayDetailConsume',
102
+ },
103
+ 'headers': {
104
+ 'Content-Type': 'application/json'
105
+ },
106
+ },
107
+ 'paramsType': {
108
+ }
109
+ },
110
+ }
111
+ }
@@ -0,0 +1,112 @@
1
+ const BaseClient = require("../../../base/BaseClient.js");
2
+
3
+ module.exports = class Client extends BaseClient {
4
+ _baseConfig = {
5
+ 'protocol': 'http://',
6
+ 'endpoint': 'bill-union.api.ksyun.com',
7
+ 'config': {
8
+ 'timeout': 60, //设置timeout
9
+ 'headers': {
10
+ 'Accept': 'application/json'
11
+ },
12
+ 'credentials': {
13
+ 'region': 'cn-shanghai-3',
14
+ 'service': 'bill-union',
15
+ },
16
+ },
17
+ }
18
+ _apiList = {
19
+ 'DescribeBillSummaryByPayMode': {
20
+ 'url': '/',
21
+ 'method': 'GET',
22
+ 'config': {
23
+ 'query': {
24
+ 'Version': '2020-01-01',
25
+ 'Action': 'DescribeBillSummaryByPayMode',
26
+ },
27
+ 'headers': {
28
+ 'Content-Type': 'application/json'
29
+ },
30
+ },
31
+ 'paramsType': {
32
+ }
33
+ },
34
+ 'DescribeBillSummaryByProduct': {
35
+ 'url': '/',
36
+ 'method': 'GET',
37
+ 'config': {
38
+ 'query': {
39
+ 'Version': '2020-01-01',
40
+ 'Action': 'DescribeBillSummaryByProduct',
41
+ },
42
+ 'headers': {
43
+ 'Content-Type': 'application/json'
44
+ },
45
+ },
46
+ 'paramsType': {
47
+ }
48
+ },
49
+ 'DescribeBillSummaryByProject': {
50
+ 'url': '/',
51
+ 'method': 'GET',
52
+ 'config': {
53
+ 'query': {
54
+ 'Version': '2020-01-01',
55
+ 'Action': 'DescribeBillSummaryByProject',
56
+ },
57
+ 'headers': {
58
+ 'Content-Type': 'application/json'
59
+ },
60
+ },
61
+ 'paramsType': {
62
+ 'BillBeginMonth': 'String',
63
+ 'BillEndMonth': 'String',
64
+ }
65
+ },
66
+ 'DescribeInstanceSummaryBills': {
67
+ 'url': '/',
68
+ 'method': 'GET',
69
+ 'config': {
70
+ 'query': {
71
+ 'Version': '2020-01-01',
72
+ 'Action': 'DescribeInstanceSummaryBills',
73
+ },
74
+ 'headers': {
75
+ 'Content-Type': 'application/json'
76
+ },
77
+ },
78
+ 'paramsType': {
79
+ }
80
+ },
81
+ 'DescribeProductCode': {
82
+ 'url': '/',
83
+ 'method': 'GET',
84
+ 'config': {
85
+ 'query': {
86
+ 'Version': '2020-01-01',
87
+ 'Action': 'DescribeProductCode',
88
+ },
89
+ 'headers': {
90
+ 'Content-Type': 'application/json'
91
+ },
92
+ },
93
+ 'paramsType': {
94
+ }
95
+ },
96
+ 'DescribeSplitItemBillDetails': {
97
+ 'url': '/',
98
+ 'method': 'GET',
99
+ 'config': {
100
+ 'query': {
101
+ 'Version': '2020-01-01',
102
+ 'Action': 'DescribeSplitItemBillDetails',
103
+ },
104
+ 'headers': {
105
+ 'Content-Type': 'application/json'
106
+ },
107
+ },
108
+ 'paramsType': {
109
+ }
110
+ },
111
+ }
112
+ }
@@ -0,0 +1,35 @@
1
+ const BaseClient = require("../../../base/BaseClient.js");
2
+
3
+ module.exports = class Client extends BaseClient {
4
+ _baseConfig = {
5
+ 'protocol': 'http://',
6
+ 'endpoint': 'bill-union.api.ksyun.com',
7
+ 'config': {
8
+ 'timeout': 60, //设置timeout
9
+ 'headers': {
10
+ 'Accept': 'application/json'
11
+ },
12
+ 'credentials': {
13
+ 'region': 'cn-shanghai-3',
14
+ 'service': 'bill-union',
15
+ },
16
+ },
17
+ }
18
+ _apiList = {
19
+ 'DescribeCostBill': {
20
+ 'url': '/',
21
+ 'method': 'GET',
22
+ 'config': {
23
+ 'query': {
24
+ 'Version': '2021-12-09',
25
+ 'Action': 'DescribeCostBill',
26
+ },
27
+ 'headers': {
28
+ 'Content-Type': 'application/json'
29
+ },
30
+ },
31
+ 'paramsType': {
32
+ }
33
+ },
34
+ }
35
+ }