1+ import fetch from "node-fetch" ;
2+
3+ const { version } = require ( "../package.json" ) ;
4+ const linguist = require ( "@sourcebin/linguist/dist/linguist.json" ) ;
5+
6+ const url = "https://sourceb.in" ;
7+
8+ export class Bin {
9+ public key : string ;
10+ public files : Array < BinFile > ;
11+ public created : Date ;
12+ public url : string ;
13+
14+ constructor ( options : BinOptions ) {
15+ this . key = options . key ;
16+ this . url = `${ url } /${ this . key } ` ;
17+ this . created = options . created ;
18+ this . files = options . files ;
19+ }
20+ }
21+
22+ export class BinFile {
23+ public languageId : number ;
24+ public language ?: Language ;
25+ public content : string ;
26+
27+ constructor ( options : BinFileOptions ) {
28+ this . languageId = getLanguageId ( options . languageId ) || 372 ;
29+ this . language = linguist [ this . languageId ] ;
30+ this . content = options . content ;
31+ }
32+
33+ public object ( ) : any {
34+ return Object . assign ( { } , this )
35+ }
36+ }
37+
38+ interface BinOptions {
39+ key : string ;
40+ files : Array < BinFile > ;
41+ created : Date ;
42+ }
43+
44+ interface BinFileOptions {
45+ content : string ;
46+ languageId ?: number | string ;
47+ }
48+
49+ interface Language {
50+ name : string ;
51+ extension : string ;
52+ aliases ?: Array < string > ;
53+ aceMode : string ;
54+ }
55+
56+ export async function get ( k : string ) : Promise < Bin > {
57+ const { files, key, created } = await fetch ( `${ url } /api/bins/${ k } ` , {
58+ headers : {
59+ "Content-Type" : "application/json" ,
60+ "User-Agent" : "SourceBin Wrapper/" + version
61+ } ,
62+ method : "get"
63+ } ) . then ( res => res . json ( ) ) ;
64+
65+ const binFiles : Array < BinFile > = [ ] ;
66+
67+ files . forEach ( file => {
68+ binFiles . push ( new BinFile ( {
69+ content : file . content ,
70+ languageId : file . languageId
71+ } ) )
72+ } ) ;
73+
74+ return new Bin ( {
75+ files : binFiles ,
76+ key : key ,
77+ created : created
78+ } ) ;
79+ }
80+
81+ export async function create ( binFiles : Array < BinFile > ) : Promise < Bin | string > {
82+ if ( ! binFiles || binFiles . length < 1 ) {
83+ throw "Cannot create from empty bin array" ;
84+ }
85+
86+ const body = {
87+ files : binFiles . map ( file => file . object ( ) ) . map ( file => {
88+ return { content : file . content , languageId : file . languageId }
89+ } )
90+ } ;
91+
92+ const { key, message } = await fetch ( `${ url } /api/bins` , {
93+ method : "post" ,
94+ headers : {
95+ "Content-Type" : "application/json" ,
96+ "User-Agent" : "SourceBin Wrapper/" + version
97+ } ,
98+ body : JSON . stringify ( body )
99+ } ) . then ( res => res . json ( ) ) ;
100+
101+ if ( message ) {
102+ return message ;
103+ }
104+
105+ return new Bin ( {
106+ key : key ,
107+ created : new Date ( ) ,
108+ files : binFiles
109+ } ) ;
110+ }
111+
112+ export function getLanguageId ( lang : string | number ) : number {
113+ if ( ! lang ) return null ;
114+
115+ if ( typeof lang === "number" ) return lang ;
116+
117+ lang = lang . toLowerCase ( ) ;
118+
119+ const extension = Object
120+ . keys ( linguist )
121+ . find ( key => linguist [ key ] . extension === lang ) ;
122+ if ( extension ) return Number ( extension ) ;
123+
124+ const name = Object
125+ . keys ( linguist )
126+ . find ( key => linguist [ key ] . name . toLowerCase ( ) === lang ) ;
127+ if ( name ) return Number ( name ) ;
128+
129+ return null ;
130+ }
0 commit comments