@@ -6,22 +6,12 @@ const {
6
6
arch,
7
7
} = require ( 'os' ) ;
8
8
const { extname } = require ( 'path' ) ;
9
+ const { getEnvConfig } = require ( '../../config' ) ;
9
10
const {
10
11
version : clientVersion ,
11
12
clientName,
12
13
} = require ( '../../package.json' ) ;
13
14
14
- /**
15
- * Common request setting object
16
- */
17
- const settings = {
18
- api : {
19
- host : 'api-eval.signnow.com' ,
20
- port : 443 ,
21
- credentials : '' ,
22
- } ,
23
- } ;
24
-
25
15
const acceptedDocumentExtensions = [
26
16
'doc' ,
27
17
'docx' ,
@@ -92,26 +82,77 @@ const errorHandler = callback => err => {
92
82
} ;
93
83
94
84
/**
95
- * user agent string format:
85
+ * Composes user agent string in format:
96
86
* <client name>/<version> (<OS type> <OS version>; <platform>; <arch>) <runtime>/<version>
87
+ * @return {string }
97
88
*/
98
89
const getUserAgent = ( ) => {
99
90
const { version : runtimeVersion } = process ;
100
91
return `${ clientName } /v${ clientVersion } (${ OSType } ${ OSVersion } ; ${ platform } ; ${ arch } ) node/${ runtimeVersion } ` ;
101
92
} ;
102
93
94
+ /**
95
+ * @type {string }
96
+ */
97
+ let basicCredentials ;
98
+
99
+ /**
100
+ * @return {string }
101
+ */
102
+ const getCredentials = ( ) => basicCredentials ;
103
+
104
+ /**
105
+ * @param {string }
106
+ * @return {string }
107
+ */
108
+ const setCredentials = credentials => {
109
+ return basicCredentials = credentials ;
110
+ } ;
111
+
112
+ /**
113
+ * Build Request authorization settings
114
+ * @typedef {Object } BuildRequestAuthorization
115
+ * @property {string } [type="Basic"]
116
+ * @property {string } [token=basicCredentials]
117
+ */
118
+
119
+ /**
120
+ * Build Request Options object params
121
+ * @typedef {Object } BuildRequestOptionsParams
122
+ * @property {string } [method="GET"]
123
+ * @property {string } [path="/"]
124
+ * @property {BuildRequestAuthorization } [authorization]
125
+ * @property {Object } [headers]
126
+ */
127
+
128
+ /**
129
+ * Request Options object
130
+ * @typedef {Object } RequestOptions
131
+ * @property {string } host
132
+ * @property {string } port
133
+ * @property {string } method
134
+ * @property {string } path
135
+ * @property {Object } headers
136
+ */
137
+
138
+ /**
139
+ * Builds Request Options object
140
+ * @param {BuildRequestOptionsParams }
141
+ * @return {RequestOptions }
142
+ */
103
143
const buildRequestOptions = ( {
104
144
method = 'GET' ,
105
145
path = '/' ,
106
146
authorization : {
107
147
type = 'Basic' ,
108
- token = settings . api . credentials ,
148
+ token = getCredentials ( ) ,
109
149
} = { } ,
110
150
headers = { } ,
111
151
} ) => {
152
+ const { api : { host, port } } = getEnvConfig ( ) ;
112
153
const options = {
113
- host : settings . api . host ,
114
- port : settings . api . port ,
154
+ host,
155
+ port,
115
156
method,
116
157
path,
117
158
headers,
@@ -121,14 +162,10 @@ const buildRequestOptions = ({
121
162
return options ;
122
163
} ;
123
164
124
- const setProductionApiHost = ( ) => {
125
- settings . api . host = 'api.signnow.com' ;
126
- } ;
127
-
128
- const setCredentials = credentials => {
129
- settings . api . credentials = credentials ;
130
- } ;
131
-
165
+ /**
166
+ * @param {Object } queryParams
167
+ * @return {string }
168
+ */
132
169
const stringifyQueryParams = queryParams => Object
133
170
. entries ( queryParams )
134
171
. reduce ( ( acc , cur ) => `${ acc } ${ cur . join ( '=' ) } &` , '' )
@@ -175,15 +212,25 @@ const isDocumentTypeAcceptable = fileExtension => acceptedDocumentExtensions.inc
175
212
*/
176
213
const isCSVFile = filePath => extname ( filePath ) . substr ( 1 ) . toLowerCase ( ) === 'csv' ;
177
214
178
- // get signers email by status
179
- const getByStatus = ( arr , key , status ) => {
180
- return arr
181
- . filter ( field => field . status === status )
182
- . map ( val => val [ key ] ) ;
183
- } ;
215
+ /**
216
+ * Return signer emails by status
217
+ * @param {Object[] } arr
218
+ * @param {string } key
219
+ * @param {string } status
220
+ * @return {string[] }
221
+ */
222
+ const getByStatus = ( arr , key , status ) => arr
223
+ . filter ( field => field . status === status )
224
+ . map ( val => val [ key ] ) ;
184
225
185
- // get signers email by key and status
186
- const getSignersEmails = ( arr , key , status ) => {
226
+ /**
227
+ * Returns signer emails by key and status
228
+ * @param {Object[] } arr
229
+ * @param {string } key
230
+ * @param {string|string[] } status
231
+ * @return {string[] }
232
+ */
233
+ const getSignerEmails = ( arr , key , status ) => {
187
234
if ( ! arr || arr . length <= 0 ) {
188
235
return ;
189
236
}
@@ -213,7 +260,11 @@ const getSignersEmails = (arr, key, status) => {
213
260
return arr ;
214
261
} ;
215
262
216
- // get signers email with declined status
263
+ /**
264
+ * Returns signer emails with declined to sign status
265
+ * @param {Object[] } arr
266
+ * @param {string[] } status
267
+ */
217
268
const getByDeclinedStatus = ( arr , status ) => {
218
269
if ( status . includes ( 'declined' ) || status === 'declined' ) {
219
270
return arr
@@ -228,13 +279,12 @@ module.exports = {
228
279
responseHandler,
229
280
errorHandler,
230
281
buildRequestOptions,
231
- setProductionApiHost,
232
282
setCredentials,
233
283
stringifyQueryParams,
234
284
getMimeTypeByFileExtension,
235
285
isDocumentTypeAcceptable,
236
286
isCSVFile,
237
287
getFormDataBoundary,
238
288
getByDeclinedStatus,
239
- getSignersEmails ,
289
+ getSignerEmails ,
240
290
} ;
0 commit comments