UNPKG

@sodiumlabs/plume-url

Version:
105 lines (100 loc) 2.84 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/PlumeURLError.ts var PlumeURLError = class extends Error { constructor(message, options, res) { super(message, options); this.res = res; this.res = res || null; } static { __name(this, "PlumeURLError"); } }; // package.json var version = "1.0.4"; // src/PlumeURLREST.ts var PlumeURLREST = class _PlumeURLREST { constructor(options = {}) { this.options = options; this.options = options; } static { __name(this, "PlumeURLREST"); } static baseURL = "https://url.sodiumlabs.xyz/api"; static defaultUserAgent = `plume-url.js/${version}`; async request(method, path, body) { if (!path.startsWith("/")) { throw new Error(`Invalid path: ${path}`); } const headers = new Headers({ "User-Agent": `${_PlumeURLREST.defaultUserAgent} ${this.options.userAgent || ""}`.trim() }); if (this.options.apiKey) { headers.set("Authorization", this.options.apiKey); } if (body) { headers.append("Content-Type", "application/json"); } const url = `${_PlumeURLREST.baseURL}${path}`; let res; try { res = await fetch(url, { method, headers, body: body ? JSON.stringify(body) : void 0 }); } catch (err) { throw new PlumeURLError(`Failed to fetch ${url}`, { cause: err }); } if (!res.ok) { throw new PlumeURLError(`Invalid response ${url}: ${res.statusText}`, void 0, res); } return res; } async get(path) { const res = await this.request("GET", path); return await res.json(); } async post(path, body) { const res = await this.request("POST", path, body); return await res.json(); } }; // src/utils.ts var queryfy = /* @__PURE__ */ __name((options) => { const params = new URLSearchParams( Object.entries(options).filter(([, v]) => v !== void 0).map(([k, v]) => [k, `${v}`]) ); const encoded = params.toString(); return encoded ? `?${encoded}` : ""; }, "queryfy"); // src/PlumeURL.ts var PlumeURL = class { static { __name(this, "PlumeURL"); } rest; constructor(options) { this.rest = new PlumeURLREST(options); } async createURL(options) { return await this.rest.post("/create", options); } async search({ customId, limit, page, expired }) { const params = queryfy({ customId, limit, page, expired }); return await this.rest.get(`/search${params}`); } async getURL(id) { return await this.rest.get(`/urls/${id}`); } async editURL(id, options) { await this.rest.request("PATCH", `/urls/${id}`, options); } async deleteURL(id) { await this.rest.request("DELETE", `/urls/${id}`); } }; export { PlumeURL, PlumeURLError, PlumeURLREST }; //# sourceMappingURL=index.mjs.map