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 | const STRICT = false
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const qs = require('querystring')
|
43 | const request = require('superagent')
|
44 | const Promise = require('bluebird')
|
45 | const _path = require('path')
|
46 | const JSONb = require('json-bigint')({ storeAsString: true })
|
47 | const version = require('./package.json').version
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function MailjetClient (api_key, api_secret, testMode) {
|
59 | this.config = require('./config')
|
60 | this.testMode = testMode || false
|
61 |
|
62 | this.version = version
|
63 | if (api_key && api_secret) {
|
64 | this.connect(api_key, api_secret)
|
65 | }
|
66 | }
|
67 |
|
68 | MailjetClient.prototype.typeJson = function (body) {
|
69 | var keys = Object.keys(body)
|
70 | for (var i in keys) {
|
71 | var key = keys[i]
|
72 | body[key] = parseInt(body[key]) || body[key]
|
73 | }
|
74 | return body
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | MailjetClient.connect = function (k, s) {
|
87 | return new MailjetClient().connect(k, s)
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | MailjetClient.prototype.connect = function (apiKey, apiSecret) {
|
100 | this.apiKey = apiKey
|
101 | this.apiSecret = apiSecret
|
102 | return this
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | MailjetClient.prototype.path = function (resource, sub, params) {
|
117 | if (DEBUG_MODE) {
|
118 | console.log('resource =', resource)
|
119 | console.log('subPath =', sub)
|
120 | console.log('filters =', params)
|
121 | }
|
122 | var base = _path.join(this.config.version, sub)
|
123 | if (Object.keys(params).length === 0) {
|
124 | return base + '/' + resource
|
125 | }
|
126 |
|
127 | var q = qs.stringify(params).replace(/%2B/g, '+')
|
128 | return base + '/' + resource + '/?' + q
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
|
144 |
|
145 | var req = request[method](url)
|
146 | .set('user-agent', 'mailjet-api-v3-nodejs/' + this.version)
|
147 |
|
148 | .set('Content-type', url.indexOf('text:plain') > -1
|
149 | ? 'text/plain'
|
150 | : 'application/json')
|
151 |
|
152 | .auth(this.apiKey, this.apiSecret)
|
153 |
|
154 | const payload = method === 'post' || method === 'put' ? data : {}
|
155 |
|
156 | if (DEBUG_MODE) {
|
157 | console.log('Final url: ' + url)
|
158 | console.log('body: ' + payload)
|
159 | }
|
160 |
|
161 | if (this.testMode) {
|
162 | return [url, payload]
|
163 | }
|
164 |
|
165 | if (method === 'delete') { method = 'del' }
|
166 | if (method === 'post' || method === 'put') { req = req.send(data) }
|
167 |
|
168 | return new Promise((resolve, reject) => {
|
169 |
|
170 | const ret = (err, result) => typeof callback === 'function'
|
171 | ? callback(err, result)
|
172 | : err
|
173 | ? reject(err)
|
174 | : resolve(result)
|
175 |
|
176 | req.end((err, result) => {
|
177 | var body
|
178 |
|
179 | try {
|
180 | body = JSONb.parse(result.text)
|
181 | } catch (e) {
|
182 | body = {}
|
183 | }
|
184 |
|
185 | if (result && result.status && result.status > 210) {
|
186 | const error = new Error('Unsuccessful')
|
187 | error.ErrorMessage = body.ErrorMessage || (result.res.statusMessage)
|
188 | error.statusCode = result.status
|
189 | error.response = result
|
190 | return ret(error)
|
191 | }
|
192 |
|
193 | return ret(null, {
|
194 | response: result,
|
195 | body
|
196 | })
|
197 | })
|
198 | })
|
199 | }
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | function MailjetResource (method, func, context) {
|
212 | this.base = func
|
213 | this.callUrl = func
|
214 |
|
215 | this.resource = func.toLowerCase()
|
216 |
|
217 | this.lastAdded = RESOURCE
|
218 | var self = context
|
219 |
|
220 | |
221 |
|
222 |
|
223 | this.subPath = (function () {
|
224 | if (func.toLowerCase() !== 'send') {
|
225 | return 'REST'
|
226 | }
|
227 | return ''
|
228 | })()
|
229 |
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | var that = this
|
238 | this.result = function (params, callback) {
|
239 | params = params || {}
|
240 | if (typeof params === 'function') {
|
241 | callback = params
|
242 | params = {}
|
243 | }
|
244 |
|
245 | |
246 |
|
247 |
|
248 |
|
249 | var path = self.path(that.callUrl, that.subPath, (function () {
|
250 | if (params['filters']) {
|
251 | var ret = params['filters']
|
252 | delete params['filters']
|
253 | return ret
|
254 | } else if (method === 'get') {
|
255 | return params
|
256 | } else {
|
257 | return {}
|
258 | }
|
259 | })())
|
260 |
|
261 | that.callUrl = that.base
|
262 | self.lastAdded = RESOURCE
|
263 | return self.httpRequest(method, 'https://' + _path.join(self.config.url, path), params, callback)
|
264 | }
|
265 | }
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | MailjetResource.prototype.id = function (value) {
|
278 | if (this.lastAdded === ID && DEBUG_MODE) {
|
279 | console.warn('[WARNING] your request may fail due to invalid id chaining')
|
280 | }
|
281 |
|
282 | this.callUrl = _path.join(this.callUrl, value.toString())
|
283 | this.lastAdded = ID
|
284 | return this
|
285 | }
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | MailjetResource.prototype.action = function (name) {
|
298 | if (this.lastAdded === ACTION && DEBUG_MODE) {
|
299 | console.warn('[WARNING] your request may fail due to invalid action chaining')
|
300 | }
|
301 |
|
302 | this.callUrl = _path.join(this.callUrl, name)
|
303 | this.action = name.toLowerCase()
|
304 |
|
305 | this.lastAdded = ACTION
|
306 |
|
307 | if (this.action.toLowerCase() === 'csvdata') {
|
308 | this.action = 'csvdata/text:plain'
|
309 | } else if (this.action.toLowerCase() === 'csverror') {
|
310 | this.action = 'csverror/text:csv'
|
311 | }
|
312 |
|
313 | var self = this
|
314 | this.subPath = (function () {
|
315 | if (self.resource === 'contactslist' && self.action === 'csvdata/text:plain' ||
|
316 | self.resource === 'batchjob' && self.action === 'csverror/text:csv') {
|
317 | return 'DATA'
|
318 | } else {
|
319 | return self.subPath
|
320 | }
|
321 | })()
|
322 | return self
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | MailjetResource.prototype.request = function (params, callback) {
|
336 | return this.result(params, callback)
|
337 | }
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | MailjetClient.prototype.post = function (func) {
|
347 | return new MailjetResource('post', func, this)
|
348 | }
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | MailjetClient.prototype.get = function (func) {
|
358 | return new MailjetResource('get', func, this)
|
359 | }
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | MailjetClient.prototype.delete = function (func) {
|
369 | return new MailjetResource('delete', func, this)
|
370 | }
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | MailjetClient.prototype.put = function (func) {
|
380 | return new MailjetResource('put', func, this)
|
381 | }
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | module.exports = MailjetClient
|