UNPKG

tonicpow-js

Version:

TonicPow API Library in JS - https://docs.tonicpow.com

85 lines (67 loc) 1.58 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.pkgVersion=exports.getOptions=exports.createApiClient=exports.checkError=exports.apiVersion=void 0;const axios=require("axios"), pkgVersion="v0.2.6";// .default (@mrz what does this affect?) // Current version for requests from the API exports.pkgVersion=pkgVersion;const apiVersion="v1"; // getOptions is a factory for axios default options exports.apiVersion="v1";const getOptions=function(a){ return{ withCredentials:!0, headers:{ api_key:a.config.apiKey, "User-Agent":"tonicpow-js "+pkgVersion}}; };exports.getOptions=getOptions; const checkError=function(a){return( "undefined"==typeof a.response? a.message:a.response.data); }; // This wraps axios for cookie management for API vs User session token exports.checkError=checkError;const createApiClient=function(a){ const b=axios.create(); return{ async post(c,d){ try{ const e=await b.post( a.config.apiUrl+apiVersion+c, d, getOptions(a)); return e&&e.data?e.data:e; }catch(a){ throw checkError(a); } }, async put(c,d,e=!1){ try{ const f=await b.put( a.config.apiUrl+apiVersion+c, d, getOptions(a));return( e? f: f&&f.data?f.data:f); }catch(a){ throw checkError(a); } }, async get(c,d=!1){ try{ const e=await b.get( a.config.apiUrl+apiVersion+c, getOptions(a));return( d? e: e&&e.data?e.data:e); }catch(a){ throw checkError(a); } }, async delete(c){ try{ const d=await b.delete( a.config.apiUrl+apiVersion+c, getOptions(a)); return d&&d.data?d.data:d; }catch(a){ throw checkError(a); } }}; };exports.createApiClient=createApiClient;