forked from mailjet/mailjet-apiv3-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailjet-client.js
executable file
·389 lines (351 loc) · 9.8 KB
/
mailjet-client.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Mailjet
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
const DEBUG_MODE = false
const RESOURCE = 0
const ID = 1
const ACTION = 2
const STRICT = false
/*
* Imports.
*
* qs is used to format the url from the provided parameters and method
* _path will join a path according to the OS specifications
* https will be used to make a secure http request to the API
* fs will simply be used to read files
*/
const qs = require('querystring')
const request = require('request')
const Promise = require('bluebird')
const _path = require('path')
/*
* MailjetClient constructor.
*
* @qpi_key (optional) {String} mailjet account api key
* @api_secret (optional) {String} mailjet account api secret
*
* If you don't know what this is about, sign up to Mailjet at:
* https://www.mailjet.com/
*/
function MailjetClient (api_key, api_secret, testMode) {
this.config = require('./config')
this.testMode = testMode || false
// To be updated according to the npm repo version
this.version = '1.0.3'
if (api_key && api_secret) {
this.connect(api_key, api_secret)
}
}
MailjetClient.prototype.typeJson = function (body) {
var keys = Object.keys(body)
for (var i in keys) {
var key = keys[i]
body[key] = parseInt(body[key]) || body[key]
}
return body
}
/*
* [Static] connect.
*
* Return a nez connected instance of the MailjetClient class
*
* @k {String} mailjet qpi key
* @s {String} mailjet api secret
*
*/
MailjetClient.connect = function (k, s) {
return new MailjetClient().connect(k, s)
}
/*
* connect.
*
* create a auth property from the api key and secret
*
* @api_key {String}
* @api_secret {String}
*
*/
MailjetClient.prototype.connect = function (apiKey, apiSecret) {
this.apiKey = apiKey
this.apiSecret = apiSecret
return this
}
/*
* path.
*
* Returns a formatted url from a http method and
* a parameters object literal
*
* @resource {String}
* @sub {String} REST/''/DATA
* @params {Object literal} {name: value}
*
*/
MailjetClient.prototype.path = function (resource, sub, params) {
if (DEBUG_MODE) {
console.log('resource =', resource)
console.log('subPath =', sub)
console.log('filters =', params)
}
var base = _path.join(this.config.version, sub)
if (Object.keys(params).length === 0) {
return base + '/' + resource
}
var q = qs.stringify(params)
return base + '/' + resource + '/?' + q
}
/*
* httpRequest.
*
* @method {String} http method (GET/POST...)
* @url {String} url path to be used for the request
* @data {Object literal} additional data espacially for POST/PUT operations
* @callback -optional {Function} called on response from the server, or on error
*
* @return a promise triggering 'success' on response
* and error on error
*/
MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
/**
* json: true converts the output from string to JSON
*/
var options = {
json: url.indexOf('text:plain') === -1,
url: url,
useragent: 'mailjet-api-v3-nodejs/' + this.version,
auth: {user: this.apiKey, pass: this.apiSecret}
}
if (method === 'post' || method === 'put') {
options.body = STRICT ? this.typeJson(data) : data
}
options['Content-Type'] = (url.toLowerCase().indexOf('text:plain') > -1)
? 'text/plain'
: 'application/json'
if (DEBUG_MODE) {
console.log('Final url: ' + url)
console.log('body: ' + options.body)
}
if (this.testMode) {
return [options.url, options.body || {}]
}
if (method === 'delete') {
method = 'del'
}
return new Promise((resolve, reject) => {
/*
* request[method] returns either request.post, request.get etc
*
* If a callback is provided, it triggers it, else it trigger an event
* on the promise object
*/
request[method](options, function (err, response, body) {
if (err || (response.statusCode > 210)) {
if (typeof callback === 'function') {
callback(err || new Error(body), response)
}
reject(err || new Error(body))
} else {
if (typeof callback === 'function') {
callback(null, response, body)
}
resolve({
response: response,
body: body
})
}
})
})
}
/*
*
* MailjetResource constructor
*
* This class creates a function that can be build through method chaining
*
* @method {String} http method
* @func {String} resource/path to be sent
* @context {MailjetClient[instance]} parent client
*/
function MailjetResource (method, func, context) {
this.base = func
this.callUrl = func
this.resource = func.toLowerCase()
this.lastAdded = RESOURCE
var self = context
/*
It can be REST or nothing if we only know the resource
*/
this.subPath = (function () {
if (func.toLowerCase() !== 'send') {
return 'REST'
}
return ''
})()
/**
*
* result.
*
* @params (optional) {Object Littteral} parameters to be sent to the server
* @callback (optional) {Function} called on response or error
*/
var that = this
this.result = function (params, callback) {
params = params || {}
if (typeof params === 'function') {
callback = params
params = {}
}
/*
We build the querystring depending on the parameters. if the user explicitly mentionned
a filters property, we pass it to the url
*/
var path = self.path(that.callUrl, that.subPath, (function () {
if (params['filters']) {
var ret = params['filters']
delete params['filters']
return ret
} else if (method === 'get') {
return params
} else {
return {}
}
})())
that.callUrl = that.base
self.lastAdded = RESOURCE
return self.httpRequest(method, 'https://' + _path.join(self.config.url, path), params, callback)
}
}
/**
*
* id.
*
* Add an ID and prevent invalid id chaining
*
* @value {String/Number} append an id to the path
* @return the MailjetResource instance to allow method chaining
*
*/
MailjetResource.prototype.id = function (value) {
if (this.lastAdded === ID && DEBUG_MODE) {
console.warn('[WARNING] your request may fail due to invalid id chaining')
}
this.callUrl = _path.join(this.callUrl, value.toString())
this.lastAdded = ID
return this
}
/**
*
* action.
*
* Add an Action and prevent invalid action chaining
*
* @value {String} append an action to the path
* @return the MailjetResource instance to allow method chaining
*
*/
MailjetResource.prototype.action = function (name) {
if (this.lastAdded === ACTION && DEBUG_MODE) {
console.warn('[WARNING] your request may fail due to invalid action chaining')
}
this.callUrl = _path.join(this.callUrl, name)
this.action = name.toLowerCase()
this.lastAdded = ACTION
if (this.action.toLowerCase() === 'csvdata') {
this.action = 'csvdata/text:plain'
} else if (this.action.toLowerCase() === 'csverror') {
this.action = 'csverror/text:csv'
}
var self = this
this.subPath = (function () {
if (self.resource === 'contactslist' && self.action === 'csvdata/text:plain' ||
self.resource === 'batchjob' && self.action === 'csverror/text:csv') {
return 'DATA'
} else {
return self.subPath
}
})()
return self
}
/**
*
* request.
*
* @parmas {Object literal} method parameters
* @callback (optional) {Function} triggered when done
*
* @return {String} the server response
*/
MailjetResource.prototype.request = function (params, callback) {
return this.result(params, callback)
}
/*
* post.
*
* @func {String} required Mailjet API function to be used (can contain a whole action path)
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.post = function (func) {
return new MailjetResource('post', func, this)
}
/*
* get.
*
* @func {String} required Mailjet API function to be used (can contain a whole action path)
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.get = function (func) {
return new MailjetResource('get', func, this)
}
/*
* delete.
*
* @func {String} required Mailjet API function to be used (can contain a whole action path)
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.delete = function (func) {
return new MailjetResource('delete', func, this)
}
/*
* put.
*
* @func {String} required Mailjet API function to be used (can contain a whole action path)
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.put = function (func) {
return new MailjetResource('put', func, this)
}
/*
* Exports the Mailjet client.
*
* you can require it like so:
* var mj = require ('./mailjet-client')
*
* or for the bleeding edge developpers out there:
* import mj from './mailjet-client'
*/
module.exports = MailjetClient