UNPKG

huddle-record

Version:

Record is a Object-Relational Mapping (ORM) TypeScript plugin that makes it easy to work with data.

103 lines (102 loc) 3.89 kB
import { record } from './main.js'; export default class Http { static async get(url, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.get(url, config), requestMethod: "GET" /* HttpRequestMethod.GET */, }); } static async post(url, data, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.post(url, data, config), requestMethod: "POST" /* HttpRequestMethod.POST */, }); } static async put(url, data, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.put(url, data, config), requestMethod: "PUT" /* HttpRequestMethod.PUT */, }); } static async patch(url, data, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.patch(url, data, config), requestMethod: "PATCH" /* HttpRequestMethod.PATCH */, }); } static async head(url, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.head(url, config), requestMethod: "HEAD" /* HttpRequestMethod.HEAD */, }); } static async delete(url, config) { return await this.makeRequest({ url, config, callback: () => record.api.axiosInstance.delete(url, config), requestMethod: "DELETE" /* HttpRequestMethod.DELETE */, }); } static async makeRequest(apiRequest) { let baseUrl = record.api.axiosInstance.defaults.baseURL; if (baseUrl && apiRequest.config?.baseURL) { // remove everything after the main domain baseUrl = baseUrl.replace(/(https?:\/\/[^\/]+\/).*/, '$1'); // combine the axios base url with the request base url baseUrl = baseUrl + (baseUrl.endsWith('/') && apiRequest.config.baseURL.startsWith('/') ? apiRequest.config.baseURL.slice(1) : apiRequest.config.baseURL) + '/'; } this.logRequest(apiRequest, baseUrl); return new Promise(async (resolve, reject) => { await apiRequest .callback() .then((response) => { this.logResponse(response, apiRequest, baseUrl); resolve(response); }) .catch((error) => { reject(error); }); }); } static logRequest(apiRequest, baseUrl) { if (record.api.debug?.logRequest) { console.log(`Record | Request: ${apiRequest.requestMethod} ${baseUrl}${apiRequest.url}`); } if (record.api.debug?.logRequestHeaders) { console.log(`Record | Headers - ${apiRequest.config?.headers}`); } if (record.api.debug?.logRequestBody && apiRequest.config) { console.log(`Record | Body - ${apiRequest.config.data}`); } } static logResponse(response, apiRequest, baseUrl) { if (record.api.debug?.logRequest) { console.log(`Record | Response: ${apiRequest.requestMethod} ${baseUrl}${apiRequest.url}`); } if (record.api.debug?.logResponseStatus) { console.log(`Record | Status - ${response.status} ${response.statusText}`); } if (record.api.debug?.logResponseHeaders) { console.log(`Record | Headers - ${response.headers}`); } if (record.api.debug?.logResponseBody) { console.log(`Record | Response body: ${response.data}`); } } }