@@ -3,8 +3,11 @@ import fetch from 'node-fetch';
33const { version } = require ( '../package.json' ) ;
44const 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
912export class Bin {
1013 public key : string ;
@@ -55,18 +58,20 @@ interface Language {
5558}
5659
5760export async function get ( k : string ) : Promise < Bin > {
61+ if ( / ( ( h t t p s ? ) ( : \/ \/ ) ) ? ( .+ ) \. ( .* ) \/ ? / . test ( k ) ) {
62+ if ( urls . filter ( url => k . includes ( url ) ) ) {
5863
59- if ( / ( h t t p s ? ) ( : \/ \/ ) ? ( . + ) \. ( . * ) \/ ? / . test ( k ) ) {
60- if ( k . includes ( url_short ) ) {
61- const match = k . match ( / s o u r c e b .i n \/ ( . + ) / ) ;
64+ const [
65+ match , , , , key
66+ ] = k . match ( / s ( ( o u r c e ) | ( r c ) ) b \ .i n \/ ( \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