Skip to content

Commit

Permalink
Added support for srcb.in & status check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacxk committed Apr 25, 2020
1 parent c7c391d commit cf875fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sourcebin-wrapper",
"version": "1.1.3",
"version": "1.4.0",
"description": "Create and get bins from https://sourceb.in/",
"main": "lib/index.js",
"scripts": {
Expand Down
41 changes: 29 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import fetch from 'node-fetch';
const { version } = require('../package.json');
const linguist = require('@sourcebin/linguist/dist/linguist.json');

const url_long = 'https://sourceb.in';
const url_short = 'sourceb.in';
const [
url_long, ...urls
] = [
'https://sourceb.in', 'sourceb.in', 'srcb.in'
];

export class Bin {
public key: string;
Expand Down Expand Up @@ -55,18 +58,20 @@ interface Language {
}

export async function get(k: string): Promise<Bin> {
if (/((https?)(:\/\/))?(.+)\.(.*)\/?/.test(k)) {
if (urls.filter(url => k.includes(url))) {

if (/(https?)(:\/\/)?(.+)\.(.*)\/?/.test(k)) {
if (k.includes(url_short)) {
const match = k.match(/sourceb.in\/(.+)/);
const [
match, , , , key
] = k.match(/s((ource)|(rc))b\.in\/(\S+)/) || [];

if (!match) {
return Promise.reject('Url must have a valid path!');
}

k = match[1].replace(/\//g, '');
k = key.replace(/\//g, '');
} else {
return Promise.reject(`Url must be a valid '${ url_short }' url!`);
return Promise.reject(`Url must be a valid '${ urls.join(' or ') }' url!`);
}
}

Expand All @@ -76,7 +81,8 @@ export async function get(k: string): Promise<Bin> {
'User-Agent': 'SourceBin Wrapper/' + version
},
method: 'get'
}).then(res => res.json());
}).then(checkStatus)
.then(res => res.json());

const binFiles: Array<BinFile> = [];

Expand All @@ -100,9 +106,11 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
}

const body = {
files: binFiles.map(file => file.object()).map(file => {
return { content: file.content, languageId: file.languageId };
})
files: binFiles
.map(file => file.object())
.map(file => {
return { content: file.content, languageId: file.languageId };
})
};

const { key, message } = await fetch(`${ url_long }/api/bins`, {
Expand All @@ -112,7 +120,8 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
'User-Agent': 'SourceBin Wrapper/' + version
},
body: JSON.stringify(body)
}).then(res => res.json());
}).then(checkStatus)
.then(res => res.json());

if (message) {
return message;
Expand Down Expand Up @@ -143,4 +152,12 @@ export function getLanguageId(lang: string | number): number {
if (name) return Number(name);

return null;
}

function checkStatus(res) {
if (res.ok) {
return res;
} else {
throw Error(res.statusText);
}
}

0 comments on commit cf875fa

Please sign in to comment.