Skip to content

Commit cf875fa

Browse files
committed
Added support for srcb.in & status check
1 parent c7c391d commit cf875fa

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sourcebin-wrapper",
3-
"version": "1.1.3",
3+
"version": "1.4.0",
44
"description": "Create and get bins from https://sourceb.in/",
55
"main": "lib/index.js",
66
"scripts": {

src/index.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import fetch from 'node-fetch';
33
const { version } = require('../package.json');
44
const linguist = require('@sourcebin/linguist/dist/linguist.json');
55

6-
const url_long = 'https://sourceb.in';
7-
const url_short = 'sourceb.in';
6+
const [
7+
url_long, ...urls
8+
] = [
9+
'https://sourceb.in', 'sourceb.in', 'srcb.in'
10+
];
811

912
export class Bin {
1013
public key: string;
@@ -55,18 +58,20 @@ interface Language {
5558
}
5659

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

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

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

67-
k = match[1].replace(/\//g, '');
72+
k = key.replace(/\//g, '');
6873
} else {
69-
return Promise.reject(`Url must be a valid '${ url_short }' url!`);
74+
return Promise.reject(`Url must be a valid '${ urls.join(' or ') }' url!`);
7075
}
7176
}
7277

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

8187
const binFiles: Array<BinFile> = [];
8288

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

102108
const body = {
103-
files: binFiles.map(file => file.object()).map(file => {
104-
return { content: file.content, languageId: file.languageId };
105-
})
109+
files: binFiles
110+
.map(file => file.object())
111+
.map(file => {
112+
return { content: file.content, languageId: file.languageId };
113+
})
106114
};
107115

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

117126
if (message) {
118127
return message;
@@ -143,4 +152,12 @@ export function getLanguageId(lang: string | number): number {
143152
if (name) return Number(name);
144153

145154
return null;
155+
}
156+
157+
function checkStatus(res) {
158+
if (res.ok) {
159+
return res;
160+
} else {
161+
throw Error(res.statusText);
162+
}
146163
}

0 commit comments

Comments
 (0)