endpoint.js
3.42 KB
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
'use strict';
const UsernamePlaceholder = require('../../../placeholders/UsernamePlaceholder');
const DEBUG = /node-hue-api/.test(process.env['NODE_DEBUG']);
class ApiEndpoint {
constructor() {
this._data = {
placeholders: [new UsernamePlaceholder()],
headers: {}
};
}
placeholder(placeholder) {
if (placeholder) {
this._getData().placeholders.push(placeholder);
}
return this;
}
version(version) {
this.version = version;
return this;
}
uri(uri) {
this._getData().url = uri;
return this;
}
get() {
return this.method('GET');
}
post() {
return this.method('POST');
}
put() {
return this.method('PUT');
}
delete() {
return this.method('DELETE');
}
method(method) {
this._getData().method = method;
return this;
}
acceptJson() {
return this.setHeader('Accept', 'application/json');
}
acceptXml() {
return this.setHeader('Accept', 'application/xml');
}
pureJson() {
this._getData().json = true;
return this;
}
postProcess(fn) {
if (typeof(fn) === 'function') {
this._getData().postProcessing = fn;
}
return this;
}
errorHandler(fn) {
if (typeof(fn) === 'function') {
this._getData().errorHandler = fn;
}
return this;
}
statusCode(expectedStatusCode) {
this._getData().statusCode = expectedStatusCode;
return this;
}
setHeader(name, value) {
this._getData().headers[name] = value;
return this;
}
getRequest(parameters) {
let self = this
, data = Object.assign({}, self._getData())
;
data.url = replacePlaceholders(data.url, data.placeholders, parameters);
if (data._payloadFn) {
let payload = data._payloadFn(parameters)
, headers = data.headers
;
data.data = payload.body;
if (payload.type) {
headers['Content-Type'] = payload.type;
}
}
if (DEBUG) {
if (data.placeholders) {
//TODO redact the username from logs, although it would still appear in the URL...
console.log('URL Placeholders:');
data.placeholders.forEach(placeholder => {
console.log(` ${placeholder.toString()}`);
});
}
if (data.headers) {
console.log(`Headers: ${JSON.stringify(data.headers)}`);
}
}
if (data.statusCode) {
data.validateStatus = function(status) {
return status === data.statusCode;
};
}
// Stop axios parsing text data as JSON
if (!self.requiresJsonConversion()) {
data.transformResponse = function(data) {
return data;
};
}
return data;
}
getPostProcessing() {
return this._getData().postProcessing;
}
getErrorHandler() {
return this._getData().errorHandler;
}
payload(fn) {
this._getData()._payloadFn = fn;
return this;
}
requiresJsonConversion() {
const data = this._getData();
return data.json || (data.headers && data.headers['Accept'] === 'application/json');
}
get successCode() {
return this._getData().statusCode || 200;
}
get headers() {
return this._getData().headers;
}
_getData() {
return this._data;
}
}
module.exports = ApiEndpoint;
function replacePlaceholders(url, placeholders, parameters) {
let result = url;
if (placeholders) {
placeholders.forEach(function(placeholder) {
result = placeholder.inject(result, parameters);
});
}
return result;
}