UNPKG

@kwikpik/kwikpik.js

Version:

Javascript/Typescript convenience library that interfaces with the Kwik Pik business API

74 lines (73 loc) 3.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KwikPikHTTPsAgent = exports.KwikPikCallableHTTPsService = exports.KwikPikSendableHTTPsService = void 0; var axios_1 = __importDefault(require("axios")); var lodash_1 = require("lodash"); var config_json_1 = __importDefault(require("../config.json")); var KwikPikSendableHTTPsService = /** @class */ (function () { function KwikPikSendableHTTPsService(path, agent, body, sendableType) { if (sendableType === void 0) { sendableType = "post"; } this.path = path; this.agent = agent; this.body = body; this.sendableType = sendableType; } KwikPikSendableHTTPsService.prototype.send = function () { return this.sendableType === "post" ? this.agent .post(this.path, this.body) .then(function (resp) { return resp.data.result; }) : this.sendableType === "patch" ? this.agent .patch(this.path, this.body) .then(function (resp) { return resp.data.result; }) : this.agent .delete(this.path, { data: this.body }) .then(function (resp) { return resp.data.result; }); }; return KwikPikSendableHTTPsService; }()); exports.KwikPikSendableHTTPsService = KwikPikSendableHTTPsService; var KwikPikCallableHTTPsService = /** @class */ (function () { function KwikPikCallableHTTPsService(path, agent, callableType) { if (callableType === void 0) { callableType = "get"; } this.path = path; this.agent = agent; this.callableType = callableType; } KwikPikCallableHTTPsService.prototype.call = function () { return this.callableType === "get" ? this.agent .get(this.path) .then(function (resp) { return resp.data.result; }) : this.agent .delete(this.path) .then(function (resp) { return resp.data.result; }); }; return KwikPikCallableHTTPsService; }()); exports.KwikPikCallableHTTPsService = KwikPikCallableHTTPsService; var KwikPikHTTPsAgent = /** @class */ (function () { function KwikPikHTTPsAgent(apiKey, environment) { if (environment === void 0) { environment = "prod"; } var baseURL = environment === "prod" ? config_json_1.default.baseProdUrl : config_json_1.default.baseDevUrl; var headers = {}; var axiosConfig = {}; headers = (0, lodash_1.assign)(headers, { Authorization: "X-API-Key ".concat(apiKey) }); axiosConfig = (0, lodash_1.assign)(axiosConfig, { baseURL: baseURL, headers: headers }); this.axiosInstance = axios_1.default.create(axiosConfig); } KwikPikHTTPsAgent.prototype.createKwikPikSendableInstance = function (path, sendableType, body) { return new KwikPikSendableHTTPsService(path, this.axiosInstance, body, sendableType); }; KwikPikHTTPsAgent.prototype.createKwikPikCallableInstance = function (path, callableType) { return new KwikPikCallableHTTPsService(path, this.axiosInstance, callableType); }; return KwikPikHTTPsAgent; }()); exports.KwikPikHTTPsAgent = KwikPikHTTPsAgent;