-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
290 lines (274 loc) · 8.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const req = require('requestretry');
const _ = require('lodash');
const Promise = require('bluebird');
/**
* Get all the necessary headers for the request.
*
* @param {Object} context the request context
* @param {String} method the HTTP method for the request
* @param {String} resource the resource being requested
* @param {Object} payload the body of the request
*
* @return {Object} the headers objects
*/
function getHeaders(context, method, resource, payload) {
return {
'Authorization': context.hmacAuthorize.sign(
method,
resource,
'application/json',
payload ? JSON.stringify(payload) : null
),
'User-Agent': 'ClassyPay Node.JS',
'Content-Type': payload ? 'application/json' : undefined,
};
}
/**
* Build the request query.
*
* @param {Number} appId the pay application id
* @param {Object} params params passed by client user
*
* @return {Object} params for request
*/
function getQs(appId, params) {
return _.extend({
appId,
meta: true,
}, params);
}
/**
* Get the request options.
*
* @param {Object} context the request context
* @param {Number} appId the pay application id
* @param {String} method the http method
* @param {String} resource the resource for the request
* @param {Object} payload the body of the request
* @param {Object} params the params provided by the caller
*
* @return {Object} the options for the request
*/
function getOptions(context, appId, method, resource, payload, params) {
return {
method,
url: `${context.config.apiUrl}${resource}`,
qs: getQs(appId, params),
body: payload ? JSON.stringify(payload) : null,
timeout: context.config.timeout,
headers: getHeaders(context, method, resource, payload),
};
}
/**
* Get the response for http request.
*
* @param {Object} error the error
* @param {Object} response the http response
* @param {Object} body the body of the response
*
* @return {Object} a well formed response object
*/
function getResult(error, response, body) {
return {
status: _.get(response, 'statusCode'),
error,
response,
body,
object: (body &&
response.headers['content-type'].includes('application/json')) ?
JSON.parse(body) : body,
};
}
/**
* A general Pay request.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} method the http method
* @param {String} resource the pay resource
* @param {Object} payload the payload for the request
* @param {Object} params the parameters for the request
* @param {Method} callback a callback
*/
function request(context, appId, method, resource, payload, params, callback) {
let options = getOptions(context, appId, method, resource, payload, params);
req(options, function(error, response, body) {
callback(error, getResult(error, response, body));
});
}
let prequest = Promise.promisify(request);
/**
* Retrieve the total number of objects for the resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
*
* @return {Number} the total number of objects for the resource
*/
function getMax(context, appId, resource) {
return prequest(context, appId, 'GET', `${resource}/count`, null, null)
.then(function(result) {
if (_.get(result, 'status') !== 200) {
throw new Error(`${result.status}: ${resource}/count ${result.body}`);
} else {
return result.object.count;
}
});
}
/**
* Get all the objects for a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Number} max total objects to retrieve
* @param {Array} collection the collection to add objects to
*
* @return {Promise} a promise when resolved populates the collection
*/
function getAll(context, appId, resource, max, collection) {
return Promise.map(_.range(0, max, 25),
function(page) {
return prequest(context, appId, 'GET', resource, null, {
limit: 25,
offset: page,
}).then(function(result) {
if (_.get(result, 'status') !== 200) {
throw new Error(`${result.status}: ${resource} ${result.body}`);
} else {
result.object.forEach((obj) => obj ? collection.push(obj) : null);
}
});
}, {
concurrency: 10,
});
}
/**
* Get all objects.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Function} callback the callback
*/
function forList(context, appId, resource, callback) {
let collection = [];
getMax(context, appId, resource).then(function(max) {
return getAll(context, appId, resource, max, collection);
}).then(function() {
callback(null, collection);
}).catch(callback);
}
/**
* Get an object.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} method the http method
* @param {String} resource the pay resource
* @param {Object} body the body of the request
* @param {Function} callback the callback
*/
function forObject(context, appId, method, resource, body, callback) {
request(context, appId, method, resource, body, null,
function(error, result) {
if (_.get(result, 'status') !== 200) {
callback(error || `${result.status}: ${resource} ${result.body}`);
} else {
callback(null, result.object);
}
});
}
/**
* Get a list of objects for a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Function} callback the callback
*
* @return {Array} an array of objects
*/
function list(context, appId, resource, callback) {
return forList(context, appId, resource, callback);
}
/**
* Get an object given a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Function} callback the callback
*
* @return {Object} an object
*/
function get(context, appId, resource, callback) {
return forObject(context, appId, 'GET', resource, null, callback);
}
/**
* Create an object at a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Object} object the object to create
* @param {Function} callback the callback
*
* @return {Object} the created object
*/
function post(context, appId, resource, object, callback) {
return forObject(context, appId, 'POST', resource, object, callback);
}
/**
* Update an object at a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Object} object the updated object
* @param {Function} callback the callback
*
* @return {Object} the updated object
*/
function put(context, appId, resource, object, callback) {
return forObject(context, appId, 'PUT', resource, object, callback);
}
/**
* Remove an object at a resource.
*
* @param {Object} context the context for the request
* @param {Number} appId the pay application id
* @param {String} resource the pay resource
* @param {Function} callback the callback
*
* @return {Object} the removed object
*/
function del(context, appId, resource, callback) {
return forObject(context, appId, 'DELETE', resource, null, callback);
}
module.exports = (callerConfig) => {
const config = callerConfig;
if (!config || !config.apiUrl || !config.token || !config.secret) {
throw new Error('You must provide apiUrl, token, and secret.');
}
const hmacAuthorize = require('authorization-hmac256')({
service: 'CWS',
token: config.token,
secret: config.secret,
});
const context = {config, hmacAuthorize};
const methods = {
request: (appId, method, resource, payload, params, callback) =>
request(context, appId, method, resource, payload, params, callback),
list: (appId, resource, callback) =>
list(context, appId, resource, callback),
get: (appId, resource, callback) => get(context, appId, resource, callback),
post: (appId, resource, object, callback) =>
post(context, appId, resource, object, callback),
put: (appId, resource, object, callback) =>
put(context, appId, resource, object, callback),
del: (appId, resource, callback) => del(context, appId, resource, callback),
};
return Promise.promisifyAll(methods);
};