From 4cd5bad94630774016017b8c81144b05049f2e5c Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 25 Apr 2020 22:45:22 -0400 Subject: [PATCH] Added raw bin file url --- README.md | 9 ++++++--- package.json | 2 +- src/index.ts | 37 +++++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 490c1c0..f1677db 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,8 @@ SourceBin.create([ new SourceBin.BinFile({ content: 'This was created using the wrapper\n\nlanguageId: "js"', languageId: 'js' - }) + }), + ... ]).then(console.log) .catch(console.error); ``` @@ -47,9 +48,12 @@ Output for both **create** and **get** { "key": "d4ad855543", "url": "https://sourceb.in/d4ad855543", + "shortened": "https://srcb.in/d4ad855543", "created": "2020-03-17T21:12:30.549Z", "files": [ { + "raw": "https://sourceb.in/d4ad855543/0", + "content": "This was created using the wrapper\n\nlanguageId: \"js\"", "languageId": 183, "language": { "name": "JavaScript", @@ -59,8 +63,7 @@ Output for both **create** and **get** "node" ], "aceMode": "javascript" - }, - "content": "This was created using the wrapper\n\nlanguageId: \"js\"" + } } ] } diff --git a/package.json b/package.json index 267823d..4022a16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sourcebin-wrapper", - "version": "1.4.0", + "version": "1.5.0", "description": "Create and get bins from https://sourceb.in/", "main": "lib/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index cfb93f7..cd8510f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,14 +31,16 @@ export class Bin { } export class BinFile { - public languageId: number; - public language: Language; + public raw: string; public content: string; + public language: Language; + public languageId: number; constructor(options: BinFileOptions) { + this.raw = `${ url_long }/${ options.raw }`; + this.content = options.content; this.languageId = getLanguageId(options.languageId) || 372; this.language = linguist[this.languageId]; - this.content = options.content; } public object(): any { @@ -53,15 +55,16 @@ interface BinOptions { } interface BinFileOptions { + raw?: string; content: string; languageId?: number | string; } interface Language { name: string; - extension: string; - aliases?: Array; aceMode: string; + aliases?: Array; + extension: string; } export async function get(k: string): Promise { @@ -69,7 +72,7 @@ export async function get(k: string): Promise { if (urls.filter(url => k.includes(url))) { const [ - match, , , , key + match, , , , key ] = k.match(/s((ource)|(rc))b\.in\/(\S+)/) || []; if (!match) { @@ -82,7 +85,7 @@ export async function get(k: string): Promise { } } - const { files, key, created } = await fetch(`${ url_long }/api/bins/${ k }`, { + const { files, key, created }: Bin = await fetch(`${ url_long }/api/bins/${ k }`, { headers: { 'Content-Type': 'application/json', 'User-Agent': 'SourceBin Wrapper/' + version @@ -94,10 +97,11 @@ export async function get(k: string): Promise { const binFiles: Array = []; - files.forEach(file => { + files.forEach((file, index) => { binFiles.push(new BinFile({ content: file.content, - languageId: file.languageId + languageId: file.languageId, + raw: `${ key }/${ index }` })); }); @@ -117,7 +121,10 @@ export async function create(binFiles: Array): Promise { files: binFiles .map(file => file.object()) .map(file => { - return { content: file.content, languageId: file.languageId }; + return { + content: file.content, + languageId: file.languageId + }; }) }; @@ -128,7 +135,8 @@ export async function create(binFiles: Array): Promise { 'User-Agent': 'SourceBin Wrapper/' + version }, body: JSON.stringify(body) - }).then(checkStatus) + }) + .then(checkStatus) .then(res => res.json()); if (message) { @@ -138,7 +146,12 @@ export async function create(binFiles: Array): Promise { return new Bin({ key: key, created: new Date(), - files: binFiles + files: binFiles.map((file, i) => { + return { + ...file.object(), + raw: `${ url_long }/${ key }/${ i }` + }; + }) }); }