@apica-io/url-xi
Version:
URL Check for integrations and API monitoring
243 lines • 9.37 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = void 0;
const axios_1 = __importDefault(require("axios"));
const axios_timing_1 = require("./axios-timing");
const btoa_1 = __importDefault(require("btoa"));
class Api {
/**
* Creates an instance of Api.
*
* @param {import("axios").AxiosRequestConfig} [config] - axios configuration.
* @memberof Api
*/
constructor(config, timings) {
this.baseURL = '';
this.requestConfig = {};
this.api = axios_1.default.create(config);
this.timings = timings || false;
if (timings)
(0, axios_timing_1.addAxiosTiming)(this.api);
if (config && config.baseURL)
this.baseURL = config.baseURL;
this.getUri = this.getUri.bind(this);
this.request = this.request.bind(this);
this.get = this.get.bind(this);
this.delete = this.delete.bind(this);
this.head = this.head.bind(this);
this.post = this.post.bind(this);
this.put = this.put.bind(this);
this.patch = this.patch.bind(this);
}
getApi() {
return this.api;
}
/**
* Get Uri
*
* @param {import("axios").AxiosRequestConfig} [config]
* @returns {string}
* @memberof Api
*/
getUri(config) {
return this.api.getUri(config);
}
/**
* Generic request.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP axios response payload.
* @memberof Api
*
* @example
* api.request({
* method: "GET|POST|DELETE|PUT|PATCH"
* baseUrl: "http://www.domain.com",
* url: "/api/v1/users",
* headers: {
* "Content-Type": "application/json"
* }
* }).then((response: AxiosResponse<User>) => response.data)
*
*/
request(config) {
Api.fixBasicAuth(config);
this.fixRequestUrl(config);
const theConfig = Object.assign({}, config, this.requestConfig);
return this.api.request(theConfig);
}
/**
* HTTP GET method, used to fetch data `statusCode`: 200.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} HTTP `axios` response payload.
* @memberof Api
*/
get(url, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.get(url, config);
}
/**
* HTTP DELETE method, `statusCode`: 204 No Content.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
delete(url, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.delete(url, config);
}
/**
* HTTP HEAD method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
head(url, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.head(url, config);
}
/**
* HTTP POST method `statusCode`: 201 Created.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
post(url, data, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.post(url, data, config);
}
/**
* HTTP PUT method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
put(url, data, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.put(url, data, config);
}
/**
* HTTP PATCH method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
patch(url, data, config) {
if (config)
Api.fixBasicAuth(config);
return this.api.patch(url, data, config);
}
/**
*
* @template T - type.
* @param {import("axios").AxiosResponse<T>} response - axios response.
* @returns {T} - expected object.
* @memberof Api
*/
success(response) {
return response.data;
}
error(error) {
throw error;
}
static fixBasicAuth(config) {
if (config.auth) {
if (!config.headers) {
config.headers = {};
}
config.headers.Authorization = 'Basic ' + (0, btoa_1.default)((config.auth.username || '') + ':' + config.auth.password);
delete config.auth;
}
return config;
}
//* Convert to full url
fixRequestUrl(config) {
if (this.baseURL && config.url) {
const url = config.url;
if (!url.toLocaleLowerCase().match('^((http|https)://)')) {
//let headers: string = JSON.stringify(config.headers).toLocaleLowerCase()
//if (headers.includes('application/x-www-form-urlencoded')) {
config.url = this.baseURL + url;
}
}
}
setRequestHeader(header, value) {
if (!this.requestConfig.headers)
this.requestConfig.headers = {};
if (value)
this.requestConfig.headers[header] = value;
else if (this.requestConfig.headers[header])
delete this.requestConfig.headers[header];
}
static getTimings(response) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
const timingData = {
timingStart: response.timingStart || 0,
timings: response.timings || {},
};
const performanceTimings = {
wait: timingData.timings.socket || 0,
dns: (((_a = timingData.timings) === null || _a === void 0 ? void 0 : _a.lookup) || 0) - (((_b = timingData.timings) === null || _b === void 0 ? void 0 : _b.socket) || 0),
secureHandshake: 0,
tcp: (((_c = timingData.timings) === null || _c === void 0 ? void 0 : _c.connect) || 0) - (((_d = timingData.timings) === null || _d === void 0 ? void 0 : _d.socket) || 0),
firstByte: (((_e = timingData.timings) === null || _e === void 0 ? void 0 : _e.response) || 0) - (((_f = timingData.timings) === null || _f === void 0 ? void 0 : _f.connect) || 0),
download: (((_g = timingData.timings) === null || _g === void 0 ? void 0 : _g.end) || 0) - (((_h = timingData.timings) === null || _h === void 0 ? void 0 : _h.response) || 0),
total: ((_j = timingData.timings) === null || _j === void 0 ? void 0 : _j.end) || 0,
};
if (timingData.timings.secureConnect) {
performanceTimings.secureHandshake = timingData.timings.secureConnect - (timingData.timings.connect || 0);
performanceTimings.firstByte =
(((_k = timingData.timings) === null || _k === void 0 ? void 0 : _k.response) || 0) - (((_l = timingData.timings) === null || _l === void 0 ? void 0 : _l.secureConnect) || 0);
}
return performanceTimings;
}
}
exports.Api = Api;
//# sourceMappingURL=api.js.map