node-hue-api
Version:
Philips Hue API Library for Node.js
146 lines (145 loc) • 4.2 kB
JavaScript
import { UsernamePlaceholder } from '../../placeholders/UsernamePlaceholder';
const DEBUG = /node-hue-api/.test(process.env['NODE_DEBUG'] || '');
export class ApiEndpoint {
// private _version?: string;
constructor() {
this._data = {
placeholders: [new UsernamePlaceholder()],
headers: {}
};
}
placeholder(placeholder) {
if (placeholder) {
this._getData().placeholders.push(placeholder);
}
return this;
}
// //TODO what is this being used for now?
// version(version: string) {
// 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) {
this._getData().postProcessing = fn;
return this;
}
errorHandler(fn) {
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) {
const data = this._getData();
const config = {
url: replacePlaceholders(data.url || '', data.placeholders, parameters),
method: data.method || 'GET',
headers: Object.assign({}, data.headers),
json: data.json,
};
//TODO a number of optional parameters are not necessarily being passed in here - Requires a review
//data - done via payloadFn
//timeout
//params
//responseType
//baseURL
if (data._payloadFn) {
const payload = data._payloadFn(parameters);
config.data = payload.body;
if (payload.type) {
if (config.headers) {
config.headers['Content-Type'] = payload.type;
}
else {
config.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 (config.headers) {
console.log(`Headers: ${JSON.stringify(config.headers)}`);
}
}
if (data.statusCode) {
config.validateStatus = function (status) {
return status === data.statusCode;
};
}
return config;
}
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;
}
}
function replacePlaceholders(url, placeholders, parameters) {
let result = url;
if (placeholders) {
placeholders.forEach(function (placeholder) {
result = placeholder.inject(result, parameters);
});
}
return result;
}