trm-api
Version:
Tasa Representativa del Mercado API wrapper to simplify GET requests and JSON response parsing
94 lines (92 loc) • 3.13 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/trm-api.ts
var trm_api_exports = {};
__export(trm_api_exports, {
default: () => trm_api_default
});
module.exports = __toCommonJS(trm_api_exports);
var TrmApi = class {
constructor(appToken = "") {
this.trmApiUrl = "https://www.datos.gov.co/resource/32sa-8pi3.json";
this.headers = {};
if (appToken !== "") {
this.headers["X-App-Token"] = appToken;
}
}
async latest() {
const searchParams = new URLSearchParams({
$limit: "1",
$order: "vigenciahasta DESC"
});
const response = await fetch(`${this.trmApiUrl}?${searchParams}`, {
headers: this.headers
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data[0];
}
async between({
startAt,
endAt,
order = "ASC"
}) {
const searchParams = new URLSearchParams({
$where: `(vigenciadesde <= '${startAt}' AND vigenciahasta >= '${startAt}') OR (vigenciadesde >= '${startAt}' AND vigenciahasta <= '${endAt}') OR (vigenciadesde <= '${endAt}' AND vigenciahasta >= '${endAt}')`,
$order: `vigenciadesde ${order}`
});
const response = await fetch(`${this.trmApiUrl}?${searchParams}`, {
headers: this.headers
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data;
}
async history({
limit = 1e3,
order = "ASC"
} = {}) {
const searchParams = new URLSearchParams({
$limit: String(limit),
$order: `vigenciadesde ${order}`
});
const response = await fetch(`${this.trmApiUrl}?${searchParams}`, {
headers: this.headers
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data;
}
async date(date) {
const data = await this.between({ startAt: date, endAt: date });
return data[0];
}
async query(query) {
const searchParams = new URLSearchParams({
$query: query
});
const response = await fetch(`${this.trmApiUrl}?${searchParams}`, {
headers: this.headers
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data;
}
};
var trm_api_default = TrmApi;