-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
31 lines (23 loc) · 1.03 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
const request = require('request')
const endPoints = require('./lib/end-points')
const handleRequest = require('./lib/handle-request')
module.exports = function (options) {
const defaultOptions = { timeout: 20000, baseUrl: 'https://api.dotmailer.com/v2/' }
let _options = Object.assign({}, defaultOptions, options)
function configFactory ({type, options}) {
if (type === 'formData') return Object.setPrototypeOf(options, { isFormData: true })
else return Object.setPrototypeOf(options, { isJson: true })
}
return function send ({endpoint, tokens = [], config, callback}) {
const _config = configFactory(config)
if (typeof callback !== '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, [tokens, _config]))
} catch (e) {
return callback(e)
}
request(preparedRequest, handleRequest(callback))
}
}