forked from confuser/node-dotmailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (21 loc) · 842 Bytes
/
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
const request = require('request')
const endPoints = require('./lib/end-points')
const handleRequest = require('./lib/handle-request')
module.exports = function (options) {
const defaultOptions = { json: true, timeout: 20000, baseUrl: 'https://api.dotmailer.com/v2/' }
options = Object.assign({}, defaultOptions, options)
return send
function send (endpoint) {
const args = Array.prototype.slice.call(arguments, 1)
const cb = args.pop()
if (typeof cb !== 'function') throw new Error('Invalid callback, must be a function')
if (!endPoints[endpoint]) throw new Error('Unknown API endpoint')
let preparedRequest
try {
preparedRequest = Object.assign({}, options, endPoints[endpoint].apply(null, args))
} catch (e) {
return cb(e)
}
request(preparedRequest, handleRequest(cb))
}
}