1
1
import crypto from 'crypto' ;
2
2
import moment from 'moment' ;
3
3
import chalk from 'chalk' ;
4
- import dotQs from 'dot-qs' ;
5
4
import qs from 'querystring' ;
6
5
import { CapiOptions } from './index' ;
7
6
@@ -71,6 +70,102 @@ export function sign(
71
70
return hmac . update ( Buffer . from ( str , 'utf8' ) ) . digest ( ) ;
72
71
}
73
72
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
+
74
169
/**
75
170
* generate tencent cloud sign result
76
171
*
@@ -97,8 +192,9 @@ export function tencentSign(
97
192
// const Nonce = Math.round(Math.random() * 65535)
98
193
const date = nowTime . toISOString ( ) . slice ( 0 , 10 ) ;
99
194
const Algorithm = 'TC3-HMAC-SHA256' ;
195
+ payload . RequestClient = payload . RequestClient || 'TENCENT_SDK_CAPI' ;
100
196
101
- payload = dotQs . flatten ( payload ) ;
197
+ payload = flatten ( payload ) ;
102
198
103
199
// 1. create Canonical request string
104
200
const HTTPRequestMethod = ( options . method || 'POST' ) . toUpperCase ( ) ;
@@ -178,13 +274,13 @@ export function tencentSignV1(
178
274
payload . Nonce = Nonce ;
179
275
payload . Timestamp = Timestamp ;
180
276
payload . SecretId = options . SecretId ;
181
- payload . RequestClient = 'SDK_NODEJS_v0.0.1' ;
277
+ payload . RequestClient = payload . RequestClient || 'SDK_NODEJS_v0.0.1' ;
182
278
183
279
if ( options . SignatureMethod === 'sha256' ) {
184
280
payload . SignatureMethod = 'HmacSHA256' ;
185
281
}
186
282
187
- payload = dotQs . flatten ( payload ) ;
283
+ payload = flatten ( payload ) ;
188
284
189
285
const keys = Object . keys ( payload ) . sort ( ) ;
190
286
const method = ( options . method || 'POST' ) . toUpperCase ( ) ;
0 commit comments