Skip to content

Commit 5f1ad52

Browse files
author
Ruslan Tereshchenko
committed
refactor configs and common helpers annotations
1 parent 616e139 commit 5f1ad52

File tree

9 files changed

+187
-60
lines changed

9 files changed

+187
-60
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ test-settings.js
1313
*.DS_Store
1414
launch.json
1515
.vscode
16+
.history

config/eval.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
/**
4+
* @type {EnvConfig}
5+
*/
6+
const config = {
7+
api: {
8+
host: 'api-eval.signnow.com',
9+
port: 443,
10+
},
11+
};
12+
13+
module.exports = config;

config/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable global-require */
2+
'use strict';
3+
4+
let currentEnvironment = 'prod';
5+
6+
/**
7+
* @type {EnvConfig}
8+
*/
9+
const currentConfig = {};
10+
11+
/**
12+
* Returns environment config data
13+
* @return {EnvConfig}
14+
*/
15+
const getEnvConfig = () => currentConfig;
16+
17+
/**
18+
* Sets and returns environment config data
19+
* @param {string} [env]
20+
* @return {EnvConfig}
21+
*/
22+
const setEnvConfig = env => {
23+
if (env) {
24+
currentEnvironment = env;
25+
return Object.assign(currentConfig, require(`./${env}`));
26+
}
27+
28+
return Object.assign(currentConfig, require(`./${currentEnvironment}`));
29+
};
30+
31+
module.exports = {
32+
getEnvConfig,
33+
setEnvConfig,
34+
};

config/prod.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/**
4+
* API settings
5+
* @typedef {Object} APISettings
6+
* @property {string} host
7+
* @property {string} port
8+
*/
9+
10+
/**
11+
* Environment configuration
12+
* @typedef {Object} EnvConfig
13+
* @property {APISettings} api
14+
*/
15+
16+
/**
17+
* @type {EnvConfig}
18+
*/
19+
const config = {
20+
api: {
21+
host: 'api.signnow.com',
22+
port: 443,
23+
},
24+
};
25+
26+
module.exports = config;

lib/common/index.js

+84-34
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ const {
66
arch,
77
} = require('os');
88
const { extname } = require('path');
9+
const { getEnvConfig } = require('../../config');
910
const {
1011
version: clientVersion,
1112
clientName,
1213
} = require('../../package.json');
1314

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-
2515
const acceptedDocumentExtensions = [
2616
'doc',
2717
'docx',
@@ -92,26 +82,77 @@ const errorHandler = callback => err => {
9282
};
9383

9484
/**
95-
* user agent string format:
85+
* Composes user agent string in format:
9686
* <client name>/<version> (<OS type> <OS version>; <platform>; <arch>) <runtime>/<version>
87+
* @return {string}
9788
*/
9889
const getUserAgent = () => {
9990
const { version: runtimeVersion } = process;
10091
return `${clientName}/v${clientVersion} (${OSType} ${OSVersion}; ${platform}; ${arch}) node/${runtimeVersion}`;
10192
};
10293

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+
*/
103143
const buildRequestOptions = ({
104144
method = 'GET',
105145
path = '/',
106146
authorization: {
107147
type = 'Basic',
108-
token = settings.api.credentials,
148+
token = getCredentials(),
109149
} = {},
110150
headers = {},
111151
}) => {
152+
const { api: { host, port } } = getEnvConfig();
112153
const options = {
113-
host: settings.api.host,
114-
port: settings.api.port,
154+
host,
155+
port,
115156
method,
116157
path,
117158
headers,
@@ -121,14 +162,10 @@ const buildRequestOptions = ({
121162
return options;
122163
};
123164

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+
*/
132169
const stringifyQueryParams = queryParams => Object
133170
.entries(queryParams)
134171
.reduce((acc, cur) => `${acc}${cur.join('=')}&`, '')
@@ -175,15 +212,25 @@ const isDocumentTypeAcceptable = fileExtension => acceptedDocumentExtensions.inc
175212
*/
176213
const isCSVFile = filePath => extname(filePath).substr(1).toLowerCase() === 'csv';
177214

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]);
184225

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) => {
187234
if (!arr || arr.length <= 0) {
188235
return;
189236
}
@@ -213,7 +260,11 @@ const getSignersEmails = (arr, key, status) => {
213260
return arr;
214261
};
215262

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+
*/
217268
const getByDeclinedStatus = (arr, status) => {
218269
if (status.includes('declined') || status === 'declined') {
219270
return arr
@@ -228,13 +279,12 @@ module.exports = {
228279
responseHandler,
229280
errorHandler,
230281
buildRequestOptions,
231-
setProductionApiHost,
232282
setCredentials,
233283
stringifyQueryParams,
234284
getMimeTypeByFileExtension,
235285
isDocumentTypeAcceptable,
236286
isCSVFile,
237287
getFormDataBoundary,
238288
getByDeclinedStatus,
239-
getSignersEmails,
289+
getSignerEmails,
240290
};

lib/document/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
isDocumentTypeAcceptable,
1212
getFormDataBoundary,
1313
getByDeclinedStatus,
14-
getSignersEmails,
14+
getSignerEmails,
1515
} = require('../common');
1616
const { promisify } = require('../../utils');
1717

@@ -1044,11 +1044,11 @@ class Document {
10441044
* @property {boolean} [allSignatures=true] - if true receive all document signers email
10451045
* @property {boolean} [freeFormInvites=false] - if `true` return free from invite signer emails
10461046
* @property {?string[]} [fieldInvitesStatus=null] - return signers according to specified field invite status(es)
1047-
* Accepts list of statuses or a single status.
1048-
* Acceptable statuses: all, pending, declined, fulfilled, created, skipped.
1047+
* Accepts list of statuses or a single status.
1048+
* Acceptable statuses: all, pending, declined, fulfilled, created, skipped.
10491049
* @property {?string[]} [paymentRequestsStatus=null] - return signers according to specified to payment request status(es)
1050-
* Accepts list of statuses or a single status.
1051-
* Available statuses: all, pending, fulfilled, created, skipped.
1050+
* Accepts list of statuses or a single status.
1051+
* Available statuses: all, pending, fulfilled, created, skipped.
10521052
*/
10531053

10541054
/**
@@ -1094,21 +1094,21 @@ class Document {
10941094
const emails = [];
10951095

10961096
if (Object.keys(options).length === 0) {
1097-
const fieldInvitesEmails = getSignersEmails(field_invites, 'email') || [];
1098-
const freeFormInvitesEmails = getSignersEmails(requests, 'signer_email') || [];
1097+
const fieldInvitesEmails = getSignerEmails(field_invites, 'email') || [];
1098+
const freeFormInvitesEmails = getSignerEmails(requests, 'signer_email') || [];
10991099

11001100
emails.push(...fieldInvitesEmails);
11011101
emails.push(...freeFormInvitesEmails);
11021102
}
11031103

11041104
if (freeFormInvites) {
1105-
const freeFomInvites = getSignersEmails(requests, 'signer_email') || [];
1105+
const freeFomInvites = getSignerEmails(requests, 'signer_email') || [];
11061106

11071107
emails.push(...freeFomInvites);
11081108
}
11091109

11101110
if (fieldInviteStatus) {
1111-
const fieldInvites = getSignersEmails(field_invites, 'email', fieldInviteStatus) || [];
1111+
const fieldInvites = getSignerEmails(field_invites, 'email', fieldInviteStatus) || [];
11121112
const declined = getByDeclinedStatus(field_invites, fieldInviteStatus) || [];
11131113

11141114
if (fieldInvites.length > 0) {
@@ -1120,14 +1120,14 @@ class Document {
11201120
}
11211121

11221122
if (paymentRequestStatus) {
1123-
const paymentRequests = getSignersEmails(field_invites, 'email', paymentRequestStatus) || [];
1123+
const paymentRequests = getSignerEmails(field_invites, 'email', paymentRequestStatus) || [];
11241124

11251125
emails.push(...paymentRequests);
11261126
}
11271127

11281128
callback(null, [...new Set(emails)]);
11291129
})
1130-
.catch(err => callback(null, err));
1130+
.catch(err => callback(err));
11311131
}
11321132
}
11331133

lib/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const template = require('./template');
99
const link = require('./link');
1010
const documentGroup = require('./document-group');
1111
const documentGroupTemplate = require('./document-group-template');
12-
const { setProductionApiHost, setCredentials } = require('./common');
12+
const { setCredentials } = require('./common');
13+
const { setEnvConfig } = require('../config');
1314

1415
/**
1516
* Api client initialization params
@@ -36,17 +37,17 @@ const { setProductionApiHost, setCredentials } = require('./common');
3637
* @return {Features}
3738
*/
3839
module.exports = function init ({
39-
credentials,
4040
production = true,
41+
credentials,
4142
}) {
42-
4343
if (credentials) {
4444
setCredentials(credentials);
4545
}
4646

47-
// toggle production
4847
if (production) {
49-
setProductionApiHost();
48+
setEnvConfig('prod');
49+
} else {
50+
setEnvConfig('eval');
5051
}
5152

5253
return {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"files": [
4646
"lib",
47+
"config",
4748
"utils",
4849
"bin"
4950
],

0 commit comments

Comments
 (0)