-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (96 loc) · 3.11 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
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
const { isURL } = require('validator');
const { OK, CREATED } = require('http-status-codes');
const Bluebird = require('bluebird');
const request = Bluebird.promisify(require('request'));
const { UnexpectedStatusCodeError, TrembitaError } = require('./error');
const Trembita = class Trembita {
constructor (options) {
// eslint-disable-line space-before-function-paren
this.raw = clientRequestOptions =>
this.client(clientRequestOptions).then(
Trembita._validateExpectedCodes.bind({
...clientRequestOptions,
endpoint: this.endpoint
})
);
this.request = clientRequestOptions => this.raw(clientRequestOptions);
Trembita._validateOptions(options);
Trembita._validateEndpoint(options.endpoint);
this.endpoint = options.endpoint;
this.log = options.log || console;
this.client = request.defaults({
baseUrl: this.endpoint,
json: true
});
}
/**
* Options validator.
* @method _validateOptions.
* @param {Object} options object comes from plugin,
includes required endpoint.
* @returns {TrembitaError} errors: missing options,
options is not an object.
*/
static _validateOptions (options) {
// eslint-disable-line space-before-function-paren
function isObject (value) {
// eslint-disable-line space-before-function-paren
const type = typeof value;
return value !== null && (type === 'object' || type === 'function');
}
if (!options) {
throw new TrembitaError('missing options');
}
if (!isObject(options)) {
throw new TrembitaError('options is not an object');
}
}
/**
* Endpoint validator.
* @method _validateEndpoint.
* @param {String} endpoint API.
* @returns {TrembitaError} errors: missing endpoint, endpoint is not string,
endpoint is not valid url.
*/
static _validateEndpoint (endpoint) {
// eslint-disable-line space-before-function-paren
if (!endpoint) {
throw new TrembitaError('missing endpoint');
}
if (typeof endpoint !== 'string') {
throw new TrembitaError('endpoint is not string');
}
if (
!isURL(endpoint, {
protocols: ['http', 'https'],
require_protocol: true,
require_host: true
})
) {
throw new TrembitaError('endpoint is not valid url');
}
}
/**
* Status code validator.
* @method _validateExpectedCodes.
* @param {Number} statusCode res.statusCode
* @param {Object} body res.body
* @returns {Object} error or body
*/
static _validateExpectedCodes ({ statusCode, body }) {
// eslint-disable-line space-before-function-paren
const options = this;
const defaultStatusCodes = [OK, CREATED];
const expectedCodes = options.expectedCodes || defaultStatusCodes;
if (!expectedCodes.includes(statusCode)) {
const error = new UnexpectedStatusCodeError({
options,
httpStatusCode: statusCode,
httpBody: body
});
return Bluebird.reject(error);
}
return Bluebird.resolve(body);
}
};
module.exports = Trembita;