@playbooks/adapters
Version:
A collection of adapters for various projects.
79 lines (78 loc) • 3.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dates = require("@playbooks/utils/dates");
const helpers = require("@playbooks/utils/helpers");
const logger = require("@playbooks/utils/logger");
const fetch = require("cross-fetch");
const Https = require("https");
class BaseAdapter {
domain;
constructor({ domain }) {
this.domain = domain;
}
// Private
async client(url, options) {
const response = await fetch(url, options);
if (!response.ok) {
const data = await response.json();
throw data.errors;
}
return await response.json();
}
formatUrl(url, params) {
const formattedUrl = new URL(this.domain + url);
Object.keys(params).map((key) => {
const value = params[key];
if (helpers.isEmpty(value)) return;
if (helpers.isArray(value)) return formattedUrl.searchParams.append(key, value.join(","));
if (helpers.isObject(value)) return Object.keys(value).map((key2) => formattedUrl.searchParams.append(key2, value[key2]));
return formattedUrl.searchParams.append(key, value);
});
return formattedUrl;
}
formatOptions(method, headers, data) {
const formattedOptions = {};
formattedOptions["method"] = method;
formattedOptions["headers"] = { ["Content-Type"]: "application/json", ...headers };
formattedOptions["body"] = JSON.stringify(data);
if (helpers.env === "development") formattedOptions["agent"] = new Https.Agent({ rejectUnauthorized: false });
return formattedOptions;
}
formatRequest({ method = "GET", url, headers, params, data }) {
const date = /* @__PURE__ */ new Date();
const formattedUrl = this.formatUrl(url, params);
const formattedOptions = this.formatOptions(method, headers, data);
return { date, formattedUrl, formattedOptions };
}
async request({ method = "GET", url, headers, params, data }) {
const { date, formattedUrl, formattedOptions } = this.formatRequest({ method, url, headers, params, data });
const response = await this.client(formattedUrl, formattedOptions);
return [dates.timeElapsed(date), response];
}
async apiRequest({ method = "GET", url, headers, params, data }) {
logger.logger.info(`apiRequest: `, { method, url, params, data });
const [date, response] = await this.request({ method, url, headers, params, data });
logger.logger.info(`apiResponse (${date}): `, { method, url, params, response });
return response;
}
async storeRequest({ method = "GET", url, headers, params, data }) {
if (helpers.env === "development") await helpers.sleep(300);
logger.logger.info(`storeRequest: `, { method, url, params, data });
const [date, response] = await this.request({ method, url, headers, params, data });
logger.logger.info(`storeResponse (${date}): `, { method, url, params, response });
return response;
}
async downloadRequest({ method = "GET", url, headers, params, data }) {
if (helpers.env === "development") await helpers.sleep(300);
logger.logger.info(`downloadRequest: `, { method, url, params, data });
const { date, formattedUrl, formattedOptions } = await this.formatRequest({ method, url, headers, params, data });
const response = await fetch(formattedUrl, formattedOptions);
logger.logger.info(`downloadResponse (${date}): `, { method, url, params, response });
if (!response.ok) {
const data2 = await response.json();
throw data2.errors;
}
return await response;
}
}
exports.BaseAdapter = BaseAdapter;