Skip to content

Commit

Permalink
Added raw bin file url
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacxk committed Apr 26, 2020
1 parent 26dc690 commit 4cd5bad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand All @@ -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",
Expand All @@ -59,8 +63,7 @@ Output for both **create** and **get**
"node"
],
"aceMode": "javascript"
},
"content": "This was created using the wrapper\n\nlanguageId: \"js\""
}
}
]
}
Expand Down
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.4.0",
"version": "1.5.0",
"description": "Create and get bins from https://sourceb.in/",
"main": "lib/index.js",
"scripts": {
Expand Down
37 changes: 25 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,23 +55,24 @@ interface BinOptions {
}

interface BinFileOptions {
raw?: string;
content: string;
languageId?: number | string;
}

interface Language {
name: string;
extension: string;
aliases?: Array<string>;
aceMode: string;
aliases?: Array<string>;
extension: string;
}

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

const [
match, , , , key
match, , , , key
] = k.match(/s((ource)|(rc))b\.in\/(\S+)/) || [];

if (!match) {
Expand All @@ -82,7 +85,7 @@ export async function get(k: string): Promise<Bin> {
}
}

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
Expand All @@ -94,10 +97,11 @@ export async function get(k: string): Promise<Bin> {

const binFiles: Array<BinFile> = [];

files.forEach(file => {
files.forEach((file, index) => {
binFiles.push(new BinFile({
content: file.content,
languageId: file.languageId
languageId: file.languageId,
raw: `${ key }/${ index }`
}));
});

Expand All @@ -117,7 +121,10 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
files: binFiles
.map(file => file.object())
.map(file => {
return { content: file.content, languageId: file.languageId };
return {
content: file.content,
languageId: file.languageId
};
})
};

Expand All @@ -128,7 +135,8 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
'User-Agent': 'SourceBin Wrapper/' + version
},
body: JSON.stringify(body)
}).then(checkStatus)
})
.then(checkStatus)
.then(res => res.json());

if (message) {
Expand All @@ -138,7 +146,12 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
return new Bin({
key: key,
created: new Date(),
files: binFiles
files: binFiles.map((file, i) => {
return {
...file.object(),
raw: `${ url_long }/${ key }/${ i }`
};
})
});
}

Expand Down

0 comments on commit 4cd5bad

Please sign in to comment.