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 | const DEBUG_MODE = false;
|
27 | const RESOURCE = 0;
|
28 | const ID = 1;
|
29 | const ACTION = 2;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const qs = require('querystring');
|
41 | const request = require('superagent');
|
42 | const _path = require('path');
|
43 | const JSONb = require('json-bigint')({ storeAsString: true });
|
44 | const version = require('./package.json').version;
|
45 | const setValueIfExist = require('./lib/utils/setValueIfExist');
|
46 |
|
47 |
|
48 | require('superagent-proxy')(request);
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function MailjetClient (api_key, api_secret, options, perform_api_call) {
|
61 | return this.authStrategy(api_key, api_secret, options, perform_api_call);
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | MailjetClient.prototype.authStrategy = function(api_key, api_secret, options, perform_api_call) {
|
71 |
|
72 | var isTokenRequired = this.isTokenRequired(api_key, api_secret, options, perform_api_call);
|
73 | var self = this;
|
74 |
|
75 |
|
76 | if (isTokenRequired) {
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | return tokenAuthentication(api_key, api_secret, options);
|
82 | } else {
|
83 |
|
84 | return basicAuthentication(api_key, api_secret, options, perform_api_call);
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function basicAuthentication(api_key, api_secret, options, perform_api_call) {
|
95 |
|
96 | self.config = self.setConfig(options);
|
97 | self.perform_api_call = perform_api_call || false;
|
98 |
|
99 | self.version = version;
|
100 | if (api_key && api_secret) {
|
101 | self.connect(api_key, api_secret, options);
|
102 | }
|
103 |
|
104 | return self;
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function tokenAuthentication(api_token, options, perform_api_call) {
|
114 | self.perform_api_call = perform_api_call || false;
|
115 |
|
116 | self.version = version;
|
117 | if (api_token) {
|
118 | self.connect(api_token, options);
|
119 | }
|
120 |
|
121 | return self;
|
122 | }
|
123 | };
|
124 |
|
125 | MailjetClient.prototype.isTokenRequired = function () {
|
126 | var args = [].slice.call(arguments);
|
127 | var vals = args.filter(a => a !== undefined);
|
128 |
|
129 | if (DEBUG_MODE) {
|
130 | console.log('Defined arguments: ' + JSON.stringify(vals));
|
131 | }
|
132 |
|
133 | return (vals.length === 1 || (vals.length >= 2 && typeof vals[1] === 'object'));
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | MailjetClient.connect = function (k, s, o) {
|
147 | return new MailjetClient().connect(k, s, o);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | MailjetClient.prototype.connect = function (apiKey, apiSecret, options) {
|
161 | return this.connectStrategy(apiKey, apiSecret, options);
|
162 | };
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | MailjetClient.prototype.connectStrategy = function (apiKey, apiSecret, options) {
|
170 |
|
171 | var self = this;
|
172 | var isTokenRequired = this.isTokenRequired(apiKey, apiSecret, options);
|
173 |
|
174 | if (isTokenRequired) {
|
175 | return tokenConnectStrategy(apiKey, apiSecret);
|
176 | } else {
|
177 | return basicConnectStrategy(apiKey, apiSecret, options);
|
178 | }
|
179 |
|
180 | function basicConnectStrategy(apiKey, apiSecret, options) {
|
181 | if (!apiKey) {
|
182 | throw new Error('Mailjet API_KEY is required');
|
183 | }
|
184 | if (!apiSecret) {
|
185 | throw new Error('Mailjet API_SECRET is required');
|
186 | }
|
187 |
|
188 | setOptions(options);
|
189 | self.apiKey = apiKey;
|
190 | self.apiSecret = apiSecret;
|
191 |
|
192 | return self;
|
193 | }
|
194 |
|
195 | function tokenConnectStrategy(apiToken, options) {
|
196 | if (!apiToken) {
|
197 | throw new Error('Mailjet API_TOKEN is required');
|
198 | }
|
199 |
|
200 | setOptions(options);
|
201 | self.apiToken = apiToken;
|
202 |
|
203 | return self;
|
204 | }
|
205 |
|
206 | function setOptions(options) {
|
207 | self.options = options || {};
|
208 | if (self.options) {
|
209 | self.config = self.setConfig(options);
|
210 | }
|
211 | }
|
212 | };
|
213 |
|
214 | MailjetClient.prototype.setConfig = function (options) {
|
215 | const config = { ...require('./config.json') };
|
216 | if (typeof options === 'object' && options !== null) {
|
217 | if (options.url) config.url = options.url;
|
218 | if (options.version) config.version = options.version;
|
219 | if ('perform_api_call' in options) config.perform_api_call = options.perform_api_call;
|
220 | } else if (options != null) {
|
221 | throw new Error('warning, your options variable is not a valid object.');
|
222 | }
|
223 |
|
224 | return config;
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | MailjetClient.prototype.path = function (resource, sub, params, options) {
|
239 | if (DEBUG_MODE) {
|
240 | console.log('resource =', resource);
|
241 | console.log('subPath =', sub);
|
242 | console.log('filters =', params);
|
243 | }
|
244 |
|
245 | const url = (options && 'url' in options) ? options.url : this.config.url;
|
246 | const api_version = (options && 'version' in options) ? options.version : this.config.version;
|
247 |
|
248 | const base = _path.join(api_version, sub);
|
249 | const path = _path.join(url, base + '/' + resource);
|
250 |
|
251 | if (Object.keys(params).length === 0) {
|
252 | return path;
|
253 | }
|
254 |
|
255 | const querystring = qs.stringify(params);
|
256 | return `${path}?${querystring}`;
|
257 | };
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | MailjetClient.prototype.httpRequest = function (method, url, data, callback, perform_api_call){
|
272 | var req = request[method](url)
|
273 | .set('user-agent', 'mailjet-api-v3-nodejs/' + this.version)
|
274 |
|
275 | .set('Content-type', url.indexOf('text:plain') > -1
|
276 | ? 'text/plain'
|
277 | : 'application/json');
|
278 |
|
279 | if (this.apiToken) {
|
280 | req.set('Authorization', 'Bearer ' + this.apiToken);
|
281 | } else {
|
282 | req.auth(this.apiKey, this.apiSecret);
|
283 | }
|
284 |
|
285 | if (this.options.proxyUrl) {
|
286 | req = req.proxy(this.options.proxyUrl);
|
287 | }
|
288 | if (this.options.timeout) {
|
289 | req = req.timeout(this.options.timeout);
|
290 | }
|
291 |
|
292 | const payload = method === 'post' || method === 'put' ? data : {};
|
293 |
|
294 | if (DEBUG_MODE) {
|
295 | console.log('Final url: ' + url);
|
296 | console.log('body: ' + JSON.stringify(payload));
|
297 | }
|
298 |
|
299 | if (perform_api_call === false || this.perform_api_call) {
|
300 | return Promise.resolve({body: payload, url: url});
|
301 | }
|
302 |
|
303 | if (method === 'delete') { method = 'del'; }
|
304 | if (method === 'post' || method === 'put') { req = req.send(data); }
|
305 |
|
306 | return new Promise(function (resolve, reject) {
|
307 |
|
308 | const ret = function (err, result) {
|
309 | return typeof callback === 'function'
|
310 | ? callback(err, result)
|
311 | : err
|
312 | ? reject(err)
|
313 | : resolve(result);
|
314 | };
|
315 |
|
316 | req.end(function (err, result) {
|
317 | var body;
|
318 |
|
319 | try {
|
320 | body = JSONb.parse(result.text);
|
321 | } catch (e) {
|
322 | body = {};
|
323 | }
|
324 |
|
325 | if (err) {
|
326 | const error = new Error();
|
327 | error.ErrorMessage = body.ErrorMessage || err.message;
|
328 |
|
329 | error.statusCode = err.status || null;
|
330 | error.response = result || null;
|
331 | error.message = `Unsuccessful: Status Code: "${error.statusCode}" Message: "${error.ErrorMessage}"`;
|
332 |
|
333 | setValueIfExist(error, 'ErrorIdentifier', body.ErrorIdentifier);
|
334 | setValueIfExist(error, 'ErrorCode', body.ErrorCode);
|
335 | setValueIfExist(error, 'ErrorRelatedTo', body.ErrorRelatedTo);
|
336 |
|
337 | return ret(error);
|
338 | }
|
339 |
|
340 | return ret(null, {
|
341 | response: result,
|
342 | body: body
|
343 | });
|
344 | });
|
345 | });
|
346 | };
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | function MailjetResource (method, func, options, context) {
|
359 | this.base = func;
|
360 | this.callUrl = func;
|
361 | this.options = options || context.options;
|
362 |
|
363 | this.resource = func.toLowerCase();
|
364 |
|
365 | this.lastAdded = RESOURCE;
|
366 | var self = context;
|
367 |
|
368 | |
369 |
|
370 |
|
371 | this.subPath = (function () {
|
372 | if (func.toLowerCase() !== 'send' && func.indexOf('sms') === -1) {
|
373 | return 'REST';
|
374 | }
|
375 | return '';
|
376 | })();
|
377 |
|
378 | |
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | var that = this;
|
386 | this.result = function (params, callback) {
|
387 | params = params || {};
|
388 | if (typeof params === 'function') {
|
389 | callback = params;
|
390 | params = {};
|
391 | }
|
392 |
|
393 | |
394 |
|
395 |
|
396 |
|
397 | var path = self.path(that.callUrl, that.subPath, (function () {
|
398 | if (params.filters) {
|
399 | var ret = params.filters;
|
400 | delete params.filters;
|
401 | return ret;
|
402 | } else if (method === 'get') {
|
403 | return params;
|
404 | } else {
|
405 | return {};
|
406 | }
|
407 | })(), that.options);
|
408 |
|
409 | var perform_api_call = null;
|
410 | if (that.options && 'perform_api_call' in that.options) {
|
411 | perform_api_call = that.options.perform_api_call;
|
412 | } else {
|
413 | perform_api_call = self.config.perform_api_call;
|
414 | }
|
415 |
|
416 | that.callUrl = that.base;
|
417 | self.lastAdded = RESOURCE;
|
418 | return self.httpRequest(method, 'https://' + path, params, callback, perform_api_call);
|
419 | };
|
420 | }
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 | MailjetResource.prototype.id = function (value) {
|
433 | if (this.lastAdded === ID && DEBUG_MODE) {
|
434 | console.warn('[WARNING] your request may fail due to invalid id chaining');
|
435 | }
|
436 |
|
437 | this.callUrl = _path.join(this.callUrl, value.toString());
|
438 | this.lastAdded = ID;
|
439 | return this;
|
440 | };
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 | MailjetResource.prototype.action = function (name) {
|
453 | if (this.lastAdded === ACTION && DEBUG_MODE) {
|
454 | console.warn('[WARNING] your request may fail due to invalid action chaining');
|
455 | }
|
456 |
|
457 | this.action = name.toLowerCase();
|
458 | this.lastAdded = ACTION;
|
459 |
|
460 | if (this.action.toLowerCase() === 'csvdata') {
|
461 | this.action = 'csvdata/text:plain';
|
462 | } else if (this.action.toLowerCase() === 'csverror') {
|
463 | this.action = 'csverror/text:csv';
|
464 | }
|
465 |
|
466 | this.callUrl = _path.join(this.callUrl, this.action);
|
467 |
|
468 | var self = this;
|
469 | this.subPath = (function () {
|
470 | const isContactListWithCSV = self.resource === 'contactslist' && self.action === 'csvdata/text:plain';
|
471 | const isBatchJobWithCSV = self.resource === 'batchjob' && self.action === 'csverror/text:csv';
|
472 |
|
473 | return (isContactListWithCSV || isBatchJobWithCSV) ? 'DATA' : self.subPath;
|
474 | })();
|
475 | return self;
|
476 | };
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | MailjetResource.prototype.request = function (params, callback) {
|
489 | return this.result(params, callback).catch(function (err) {
|
490 | try {
|
491 | const ErrorDetails = JSON.parse(err.response.text);
|
492 | const fullMessage = ErrorDetails.Messages[0].Errors[0].ErrorMessage;
|
493 |
|
494 | if (typeof fullMessage === 'string') {
|
495 | err.message = err.message + ';\n' + fullMessage;
|
496 | }
|
497 |
|
498 | throw err;
|
499 | } catch {
|
500 | throw err;
|
501 | }
|
502 | });
|
503 | };
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 | MailjetClient.prototype.post = function (func, options) {
|
513 | return new MailjetResource('post', func, options, this);
|
514 | };
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 | MailjetClient.prototype.get = function (func, options) {
|
524 | return new MailjetResource('get', func, options, this);
|
525 | };
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | MailjetClient.prototype.delete = function (func, options) {
|
535 | return new MailjetResource('delete', func, options, this);
|
536 | };
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 | MailjetClient.prototype.put = function (func, options) {
|
546 | return new MailjetResource('put', func, options, this);
|
547 | };
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 | module.exports.MailjetClient = MailjetClient;
|
559 | module.exports.MailjetResource = MailjetResource; |
\ | No newline at end of file |