@sodiumlabs/plume-url
Version:
The official Plume URL wrapper
116 lines (112 loc) • 2.93 kB
JavaScript
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");
}
};
// src/PlumeURLREST.ts
var PlumeURLREST = class _PlumeURLREST {
constructor(options = {}) {
this.options = options;
}
static {
__name(this, "PlumeURLREST");
}
static baseURL = "https://url.sodiumlabs.xyz/api";
static defaultUserAgent = "plume-url.js";
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);
}
/**
* Create a new shortened URL.
*/
async createURL(options) {
return await this.rest.post("/create", options);
}
/**
* Search URLs.
*/
async search({ customId, limit, page, expired }) {
const params = queryfy({ customId, limit, page, expired });
return await this.rest.get(`/search${params}`);
}
/**
* Get an URL.
*/
async getURL(id) {
return await this.rest.get(`/urls/${id}`);
}
/**
* Edit an URL.
*/
async editURL(id, options) {
await this.rest.request("PATCH", `/urls/${id}`, options);
}
/**
* Delete an URL.
*/
async deleteURL(id) {
await this.rest.request("DELETE", `/urls/${id}`);
}
};
export {
PlumeURL,
PlumeURLError,
PlumeURLREST
};
//# sourceMappingURL=index.mjs.map