UNPKG

tabua-mare-sdk

Version:

SDK JavaScript/TypeScript para a API Tábua de Marés do Brasil

2 lines (1 loc) 1.75 kB
const BASE_URL = "https://tabuamare.devtu.qzz.io/api/v1"; class HttpClient { async get(e) { if ("undefined" != typeof window && window.fetch) { const t = await fetch(e); if (!t.ok) throw new Error(`HTTP Error: ${t.status}`); return t.json() } if ("undefined" != typeof require) { const t = require("https"); return new Promise(((r, s) => { t.get(e, (e => { let t = ""; e.on("data", (e => t += e)), e.on("end", (() => { e.statusCode >= 200 && e.statusCode < 300 ? r(JSON.parse(t)) : s(new Error(`HTTP Error: ${e.statusCode}`)) })) })).on("error", s) })) } throw new Error("Ambiente não suportado") } } class TabuaMareClient { constructor(e = {}) { this.baseUrl = e.baseUrl || BASE_URL, this.http = new HttpClient } async getStates() { return this.http.get(`${this.baseUrl}/states`) } async getHarborsByState(e) { if (!e || "string" != typeof e) throw new Error("Estado inválido"); return this.http.get(`${this.baseUrl}/harbor_names/${e.toLowerCase()}`) } async getHarbors(e) { const t = Array.isArray(e) ? e.join(",") : String(e); return this.http.get(`${this.baseUrl}/harbors/${t}`) } async getTabuaMare(e, t, r) { if (!e || !t) throw new Error("harborId e month são obrigatórios"); let s = r; Array.isArray(r) ? s = `[${r.join(",").replace(/\s/g, "")}]` : "string" == typeof r && !r.startsWith("[") && (s = `[${r}]`); const a = encodeURIComponent(s); return this.http.get(`${this.baseUrl}/tabua-mare/${e}/${t}/${a}`) } async getTabuaMareRange(e, t, r, s) { return this.getTabuaMare(e, t, `[${r}-${s}]`) } async getTabuaMareMonth(e, t) { return this.getTabuaMare(e, t, "[1-31]") } } "undefined" != typeof module && module.exports && (module.exports = { TabuaMareClient }), "undefined" != typeof window && (window.TabuaMareClient = TabuaMareClient);