Skip to content

Commit 006e96e

Browse files
committed
fix: project build configure
1 parent 748fb1b commit 006e96e

File tree

14 files changed

+139
-34
lines changed

14 files changed

+139
-34
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"name": "tencent-sdk",
33
"private": true,
44
"scripts": {
5-
"bootstrap": "lerna exec npm install && lerna link",
6-
"build": "yarn build:proto && yarn build:ts",
5+
"bootstrap": "lerna bootstrap && yarn build:ts",
76
"build:ts": "tsc -b tsconfig.json",
87
"bundle": "ts-node -P scripts/tsconfig.json scripts/bundle.ts umd,esm",
9-
"clean": "lerna clean --yes && lerna run clean && rimraf includes",
10-
"release": "yarn bundle && lerna publish --exact",
8+
"clean": "lerna clean --yes && lerna run clean",
9+
"release": "yarn bootstrap && yarn bundle && lerna publish --exact",
1110
"format": "prettier --write '**/*.{ts,tsx}' --config .prettierrc"
1211
},
1312
"workspaces": [

packages/api-gw/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"access": "public"
1414
},
1515
"scripts": {
16-
"test": "echo \"Error: no test specified\" && exit 1"
16+
"test": "ts-node test/index.spec.ts",
17+
"clean": "rimraf ./dist tsconfig.tsbuildinfo"
1718
},
1819
"keywords": [
1920
"tencent-clound",

packages/api-gw/src/apis/index.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ const CheckExistsFromError = (err: Error) => {
2727
return true;
2828
};
2929

30+
type ApiGwRequestData = Omit<RequestData, 'Action'>;
31+
type ApiGwOptions = Omit<CapiOptions, 'ServiceType'>;
32+
3033
interface ApiObject {
3134
[propName: string]: (
32-
data: RequestData,
33-
options: CapiOptions,
35+
data: ApiGwRequestData,
36+
options?: ApiGwOptions,
3437
isV3?: boolean,
3538
) => Promise<any>;
3639
}
@@ -75,7 +78,7 @@ export class ApiGwRequest {
7578
apiRequest: CapiInstance;
7679
apis: ApiObject = {};
7780

78-
constructor(options: CapiOptions) {
81+
constructor(options: ApiGwOptions) {
7982
this.apiRequest = new Capi({
8083
...options,
8184
...{
@@ -88,18 +91,17 @@ export class ApiGwRequest {
8891

8992
async request(
9093
Action: string,
91-
data: RequestData,
92-
options: CapiOptions,
93-
isV3: boolean = false,
94+
data: ApiGwRequestData,
95+
options: ApiGwOptions | undefined,
96+
isV3: boolean | undefined = false,
9497
needCheck: boolean = false,
9598
) {
9699
try {
97100
const res = await this.apiRequest.request(
98101
{
99-
RequestClient: 'tss-api-gw',
100102
Action,
101103
...data,
102-
},
104+
} as RequestData,
103105
{
104106
...(options || {}),
105107
...{
@@ -128,9 +130,9 @@ export class ApiGwRequest {
128130
this.apis = {};
129131
this.actionList.forEach((item: string) => {
130132
this.apis[item] = (
131-
data: RequestData,
132-
options: CapiOptions,
133-
isV3: boolean = false,
133+
data: ApiGwRequestData,
134+
options?: ApiGwOptions,
135+
isV3?: boolean,
134136
) => {
135137
return this.request(
136138
item,

packages/api-gw/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './apis/';
1+
export * from './apis';

packages/api-gw/test/index.test.js renamed to packages/api-gw/test/index.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const { ApiGwRequest } = require('../dist/index');
1+
import { ApiGwRequest } from '../src';
22

33
async function main() {
44
const client = new ApiGwRequest({
55
Region: 'ap-guangzhou',
6-
SecretId: 'Please input your SecretId',
7-
SecretKey: 'Please input your SecretKey',
6+
SecretId: 'Please input SecretId',
7+
SecretKey: 'Please input SecretKey',
88
debug: true,
99
SignatureMethod: 'sha256',
1010
});
1111
try {
1212
const res = await client.apis.DescribeService({
1313
Version: '2017-03-12',
14+
RequestClient: 'TENCENT_SDK',
1415
serviceId: 'service-7kqwzu92',
1516
});
1617
console.log('res', res);

packages/capi/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"access": "public"
1414
},
1515
"scripts": {
16-
"test": "echo \"Error: no test specified\" && exit 1"
16+
"test": "ts-node test/index.spec.ts",
17+
"clean": "rimraf ./dist tsconfig.tsbuildinfo"
1718
},
1819
"keywords": [
1920
"tencent-clound",
@@ -23,7 +24,6 @@
2324
"license": "MIT",
2425
"dependencies": {
2526
"chalk": "^3.0.0",
26-
"dot-qs": "^0.2.0",
2727
"moment": "^2.24.0",
2828
"object-assign": "^4.1.1",
2929
"querystring": "^0.2.0",

packages/capi/src/utils.ts

+100-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import crypto from 'crypto';
22
import moment from 'moment';
33
import chalk from 'chalk';
4-
import dotQs from 'dot-qs';
54
import qs from 'querystring';
65
import { CapiOptions } from './index';
76

@@ -71,6 +70,102 @@ export function sign(
7170
return hmac.update(Buffer.from(str, 'utf8')).digest();
7271
}
7372

73+
/**
74+
* is array
75+
* @param obj object
76+
*/
77+
export function isArray(obj: any) {
78+
return Object.prototype.toString.call(obj) == '[object Array]';
79+
}
80+
81+
/**
82+
* is object
83+
* @param obj object
84+
*/
85+
export function isObject(obj: any) {
86+
return obj === Object(obj);
87+
}
88+
89+
/**
90+
* iterate object or array
91+
* @param obj object or array
92+
* @param iterator iterator function
93+
*/
94+
export function _forEach(
95+
obj: object | any[],
96+
iterator: (value: any, index: number | string, array: any) => void,
97+
) {
98+
if (isArray(obj)) {
99+
let arr = obj as Array<any>;
100+
if (arr.forEach) {
101+
arr.forEach(iterator);
102+
return;
103+
}
104+
for (let i = 0; i < arr.length; i += 1) {
105+
iterator(arr[i], i, arr);
106+
}
107+
} else {
108+
const oo = obj as { [propName: string]: any };
109+
for (let key in oo) {
110+
if (obj.hasOwnProperty(key)) {
111+
iterator(oo[key], key, obj);
112+
}
113+
}
114+
}
115+
}
116+
117+
/**
118+
* flatter request parameter
119+
* @param obj target object or array
120+
*/
121+
export function flatten(obj: {
122+
[propName: string]: any;
123+
}): { [propName: string]: any } {
124+
if (!isArray(obj) && !isObject(obj)) {
125+
return {};
126+
}
127+
const ret: { [propName: string]: any } = {};
128+
const _dump = function(
129+
obj: object | Array<any>,
130+
prefix: string | null,
131+
parents?: any[],
132+
) {
133+
const checkedParents: any[] = [];
134+
if (parents) {
135+
let i;
136+
for (i = 0; i < parents.length; i++) {
137+
if (parents[i] === obj) {
138+
throw new Error('object has circular references');
139+
}
140+
checkedParents.push(obj);
141+
}
142+
}
143+
checkedParents.push(obj);
144+
if (!isArray(obj) && !isObject(obj)) {
145+
if (!prefix) {
146+
throw obj + 'is not object or array';
147+
}
148+
ret[prefix] = obj;
149+
return {};
150+
}
151+
152+
if (isArray(obj)) {
153+
// it's an array
154+
_forEach(obj, function(obj, i) {
155+
_dump(obj, prefix ? prefix + '.' + i : '' + i, checkedParents);
156+
});
157+
} else {
158+
// it's an object
159+
_forEach(obj, function(obj, key) {
160+
_dump(obj, prefix ? prefix + '.' + key : '' + key, checkedParents);
161+
});
162+
}
163+
};
164+
165+
_dump(obj, null);
166+
return ret;
167+
}
168+
74169
/**
75170
* generate tencent cloud sign result
76171
*
@@ -97,8 +192,9 @@ export function tencentSign(
97192
// const Nonce = Math.round(Math.random() * 65535)
98193
const date = nowTime.toISOString().slice(0, 10);
99194
const Algorithm = 'TC3-HMAC-SHA256';
195+
payload.RequestClient = payload.RequestClient || 'TENCENT_SDK_CAPI';
100196

101-
payload = dotQs.flatten(payload);
197+
payload = flatten(payload);
102198

103199
// 1. create Canonical request string
104200
const HTTPRequestMethod = (options.method || 'POST').toUpperCase();
@@ -178,13 +274,13 @@ export function tencentSignV1(
178274
payload.Nonce = Nonce;
179275
payload.Timestamp = Timestamp;
180276
payload.SecretId = options.SecretId;
181-
payload.RequestClient = 'SDK_NODEJS_v0.0.1';
277+
payload.RequestClient = payload.RequestClient || 'SDK_NODEJS_v0.0.1';
182278

183279
if (options.SignatureMethod === 'sha256') {
184280
payload.SignatureMethod = 'HmacSHA256';
185281
}
186282

187-
payload = dotQs.flatten(payload);
283+
payload = flatten(payload);
188284

189285
const keys = Object.keys(payload).sort();
190286
const method = (options.method || 'POST').toUpperCase();

packages/capi/test/index.test.js renamed to packages/capi/test/index.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const { Capi } = require('../dist/index');
1+
import { Capi } from '../src';
22

33
async function main() {
44
const client = new Capi({
55
Region: 'ap-guangzhou',
6-
SecretId: 'AKIDERQREz5KfomYBj8SUWO4zP4qSqcYAn6E',
7-
SecretKey: '62rUkofMtSp7AlCPZ4LOwHMFwOcbLsBR',
6+
SecretId: 'Please input SecretId',
7+
SecretKey: 'Please input SecretKey',
88
ServiceType: 'tmt',
99
});
1010
try {
@@ -16,6 +16,7 @@ async function main() {
1616
Source: 'auto',
1717
Target: 'zh',
1818
ProjectId: 0,
19+
RequestClient: 'TENCENT_SDK_CAPI',
1920
},
2021
{
2122
debug: true,

packages/capi/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"rootDir": "src",
55
"outDir": "dist"
66
},
7-
"include": ["src", "../../typings/**/*.d.ts"]
7+
"include": ["src", "../../typings/**/*.d.ts", "typings/**/*.d.ts"]
88
}

packages/cos/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"access": "public"
1414
},
1515
"scripts": {
16-
"test": "echo \"Error: no test specified\" && exit 1"
16+
"test": "echo \"Error: no test specified\" && exit 1",
17+
"clean": "rimraf ./dist tsconfig.tsbuildinfo"
1718
},
1819
"keywords": [
1920
"tencent-cloud",

packages/function/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"access": "public"
1414
},
1515
"scripts": {
16-
"test": "echo \"Error: no test specified\" && exit 1"
16+
"test": "echo \"Error: no test specified\" && exit 1",
17+
"clean": "rimraf ./dist tsconfig.tsbuildinfo"
1718
},
1819
"keywords": [
1920
"tencent-cloud",

packages/login/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"access": "public"
1414
},
1515
"scripts": {
16-
"test": "ts-node ./test/index.spec.ts"
16+
"test": "ts-node ./test/index.spec.ts",
17+
"clean": "rimraf ./dist tsconfig.tsbuildinfo"
1718
},
1819
"keywords": [
1920
"tencent-cloud",

typings/index.d.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)