UNPKG

waterbase

Version:
85 lines (84 loc) 2.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); class Client { constructor(config) { this.endpoint = ''; this.headers = {}; this.headers = config.defaultHeaders || { 'content-type': '', }; this.setEndpoint(config.endpoint); this.setKey(config.secretKey); } /** * Set secret key * * @param string value * * @return self */ setKey(value) { this.addHeader('x-waterbase-key', value); return this; } /** * Set the endpoint * * @param endpoint * * @return this */ setEndpoint(endpoint) { this.endpoint = endpoint; return this; } /** * Adds a header for the http request * * @param key string * * @param value string */ addHeader(key, value) { this.headers[key] = value; return this; } /** * Makes the call to the server a lot easier * * @param method string * * @param path string * * @param headers object * * @param params object */ call(method, path = '', headers = {}, params = {}) { return __awaiter(this, void 0, void 0, function* () { this.headers = Object.assign(Object.assign({}, this.headers), headers); const options = { method: method.toUpperCase(), url: this.endpoint + path, headers: this.headers, data: method.toUpperCase() === 'GET' ? null : params, }; const response = yield axios_1.default(options); return response.data; }); } } exports.default = Client;