sourcebin
Version:
Fast and simple package to get and create bins from https://sourceb.in/
161 lines (152 loc) • 4.23 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/utils/languages.ts
import { languages, linguist } from "@sourcebin/linguist";
function resolveLanguageId(language) {
if (typeof language == "number") {
if (!Object.values(languages).includes(language))
throw new Error(`Unable to find language with id "${language}"`);
return language;
}
language = language.toLowerCase();
for (const [id, data] of Object.entries(linguist)) {
const hasLanguage = data.name.toLowerCase() == language || data.aliases?.map((a) => a.toLowerCase()).includes(language);
if (hasLanguage) {
return Number(id);
}
}
throw new Error(`Unable to find language "${language}"`);
}
__name(resolveLanguageId, "resolveLanguageId");
// src/utils/fetch.ts
import axios from "axios";
var fetch = axios.create({
baseURL: "https://sourceb.in/api",
headers: {
"User-Agent": `SourcebinJS https://www.npmjs.com/package/sourcebin`
}
});
// src/utils/url.ts
var resolveKey = /* @__PURE__ */ __name((keyOrUrl) => {
const sanitised = keyOrUrl.replace(
/http(s)?:\/\/(sourceb.in|srcb.in)\//gi,
""
);
const key = (sanitised.match(/[a-zA-Z0-9]{10}/g) || [])[0];
return sanitised.length == 10 && key ? key : void 0;
}, "resolveKey");
// src/structures/File.ts
import { linguist as linguist2 } from "@sourcebin/linguist";
var File = class {
rawUrl;
name;
content;
languageId;
language;
constructor(key, index, data) {
this.rawUrl = `https://cdn.sourceb.in/bins/${key}/${index}`;
this.name = data.name;
this.content = data.content;
this.languageId = data.languageId;
this.language = linguist2[data.languageId];
}
};
__name(File, "File");
// src/structures/Bin.ts
var Bin = class {
key;
url;
shortUrl;
title;
description;
views;
created;
files;
constructor(data) {
this.key = data.key;
this.url = `https://sourceb.in/${data.key}`;
this.shortUrl = `https://srcb.in/${data.key}`;
this.title = data.title;
this.description = data.description;
this.views = data.hits;
this.created = new Date(data.created);
this.files = data.files.map((f, index) => new File(data.key, index, f));
}
};
__name(Bin, "Bin");
// src/methods/get.ts
var get = /* @__PURE__ */ __name(async (options) => {
const { fetchContent = true } = options;
const key = resolveKey(options.key);
const { data } = await fetch(`/bins/${key}`);
const parsedFiles = [];
if (fetchContent) {
for (let i = 0; i < data.files.length; i++) {
const index = i;
const { data: content } = await fetch({
baseURL: "https://cdn.sourceb.in/",
url: `/bins/${key}/${index}`,
responseType: "text"
});
parsedFiles.push({
...data.files[index],
content
});
}
}
return new Bin({
...data,
files: parsedFiles
});
}, "get");
// src/methods/create.ts
var create = /* @__PURE__ */ __name(async (options) => {
if (!Array.isArray(options.files) || !options.files.length)
throw new TypeError("Expected an array of one or more files");
if (options.files.length > 1) {
throw new Error(
"You must have Sourcebin pro in order to have multiple files in one bin. This is currently not supported with this library"
);
}
const data = {
title: options.title,
description: options.description,
files: []
};
for (const file of options.files) {
const languageId = resolveLanguageId(file.language || "text");
data.files.push({
languageId,
content: file.content,
name: file.name
});
}
const res = await fetch("/bins", {
method: "POST",
data
});
return await get({
fetchContent: options.fetchContent || true,
key: res.data.key
});
}, "create");
// src/methods/url.ts
var url = /* @__PURE__ */ __name((keyOrUrl) => {
const key = resolveKey(keyOrUrl);
if (!key)
throw new Error(
"Invalid item given to url, must be a valid sourcebin url or bin key"
);
return {
key,
url: `https://sourceb.in/${key}`,
short: `http://srcb.in/${key}`
};
}, "url");
export {
Bin,
File,
create,
get,
url
};