UNPKG

ws2801-client

Version:

A client for the webserver for the WS2801-Pi module.

79 lines (78 loc) 2.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); class HttpClient { constructor(baseUrl, apiKey) { this.baseUrl = baseUrl; this.apiKey = apiKey; } setApiKey(apiKey) { this.apiKey = apiKey; } get(route, options = {}) { const url = this.buildUrl(route); options.method = 'GET'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } head(route, options = {}) { const url = this.buildUrl(route); options.method = 'HEAD'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } post(route, options = {}) { const url = this.buildUrl(route); options.method = 'POST'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } put(route, options = {}) { const url = this.buildUrl(route); options.method = 'PUT'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } delete(route, options = {}) { const url = this.buildUrl(route); options.method = 'DELETE'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } connect(route, options = {}) { const url = this.buildUrl(route); options.method = 'CONNECT'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } options(route, options = {}) { const url = this.buildUrl(route); options.method = 'OPTIONS'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } trace(route, options = {}) { const url = this.buildUrl(route); options.method = 'TRACE'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } patch(route, options = {}) { const url = this.buildUrl(route); options.method = 'PATCH'; options.headers = { 'Content-Type': 'application/json' }; return node_fetch_1.default(url, options); } buildUrl(route) { const url = `${this.baseUrl}${route}`; if (this.apiKey) { return `${url}?apiKey=${this.apiKey}`; } return url; } } exports.HttpClient = HttpClient;