UNPKG

philips-hue-light-api

Version:

An API client for the Philips Hue API.

166 lines 5.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fetch_1 = __importDefault(require("node-fetch")); class HueFetchClient { constructor(ip) { this._baseUrl = `http://${ip}/api`; } async get(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'GET'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = Array.isArray(responseValue) && responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async head(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'HEAD'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async post(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'POST'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async put(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'PUT'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async delete(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'DELETE'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async connect(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'CONNECT'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async options(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'OPTIONS'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async trace(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'TRACE'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } async patch(path, options = {}) { const url = `${this._baseUrl}${path}`; options.method = 'PATCH'; const response = await node_fetch_1.default(url, options); let responseValue = await response.json(); let responseError; const requestFailed = responseValue[0].error !== undefined; if (requestFailed) { responseError = responseValue[0].error; responseValue = undefined; } return { statusCode: response.status, error: responseError, value: responseValue, }; } } exports.HueFetchClient = HueFetchClient; //# sourceMappingURL=hue-fetch-client.js.map