UNPKG

@obelisk/client

Version:

Typescript client to interact with Obelisk on a higher level than the regular ReST API calls.

98 lines (97 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Endpoint = void 0; const util_1 = require("../util"); /** * Endpoint class represents an Obelisk API Endpoint. */ class Endpoint { constructor(client, uri, apiVersion = 'v1') { this.client = client; this._url = util_1.InternalUtils.norm(client, uri, apiVersion); } /** * Absolute url to use in requests */ get url() { return this._url; } /** * Creates an Endpoint instance, the client is used to add tokens and uri is relative path starting after /api/<version>. * Examples are: <code>/things/my_thing/metrics/my_metric/events?from=1530089953000</code> or <code>/locations/my_loc/metrics/my_metric/stats/unit</code> */ static create(client, uri, apiVersion = 'v1') { return new Endpoint(client, uri, apiVersion); } /** * Perform an ajax get request and handle any auth errors. */ get() { util_1.Logger.debug(this._url, 'AJAX'); const request = { responseType: 'json', url: this._url }; return util_1.InternalUtils.authRequest(this.client, request); } /** * Perform an ajax head request and handle any auth errors. */ head() { util_1.Logger.debug(this._url, 'AJAX'); const request = { responseType: 'json', url: this._url, method: 'head' }; return util_1.InternalUtils.authRequest(this.client, request); } /** * Perform an ajax post request and handle any auth errors. * @param body The body */ post(body) { util_1.Logger.debug(this._url, 'AJAX'); const request = { responseType: 'json', url: this._url, method: 'post', headers: { 'Content-Type': 'application/json' }, body }; return util_1.InternalUtils.authRequest(this.client, request); } /** * Perform an ajax put request and handle any auth errors. * @param body The body */ put(body) { util_1.Logger.debug(this._url, 'AJAX'); const request = { responseType: 'json', url: this._url, method: 'put', headers: { 'Content-Type': 'application/json' }, body }; return util_1.InternalUtils.authRequest(this.client, request); } /** * Perform an ajax delete request and handle any auth errors. * @param body The body */ delete() { util_1.Logger.debug(this._url, 'AJAX'); const request = { responseType: 'json', url: this._url, method: 'delete' }; return util_1.InternalUtils.authRequest(this.client, request); } } exports.Endpoint = Endpoint;