Skip to content

Commit 4cd5bad

Browse files
committed
Added raw bin file url
1 parent 26dc690 commit 4cd5bad

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ SourceBin.create([
2626
new SourceBin.BinFile({
2727
content: 'This was created using the wrapper\n\nlanguageId: "js"',
2828
languageId: 'js'
29-
})
29+
}),
30+
...
3031
]).then(console.log)
3132
.catch(console.error);
3233
```
@@ -47,9 +48,12 @@ Output for both **create** and **get**
4748
{
4849
"key": "d4ad855543",
4950
"url": "https://sourceb.in/d4ad855543",
51+
"shortened": "https://srcb.in/d4ad855543",
5052
"created": "2020-03-17T21:12:30.549Z",
5153
"files": [
5254
{
55+
"raw": "https://sourceb.in/d4ad855543/0",
56+
"content": "This was created using the wrapper\n\nlanguageId: \"js\"",
5357
"languageId": 183,
5458
"language": {
5559
"name": "JavaScript",
@@ -59,8 +63,7 @@ Output for both **create** and **get**
5963
"node"
6064
],
6165
"aceMode": "javascript"
62-
},
63-
"content": "This was created using the wrapper\n\nlanguageId: \"js\""
66+
}
6467
}
6568
]
6669
}

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.4.0",
3+
"version": "1.5.0",
44
"description": "Create and get bins from https://sourceb.in/",
55
"main": "lib/index.js",
66
"scripts": {

src/index.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ export class Bin {
3131
}
3232

3333
export class BinFile {
34-
public languageId: number;
35-
public language: Language;
34+
public raw: string;
3635
public content: string;
36+
public language: Language;
37+
public languageId: number;
3738

3839
constructor(options: BinFileOptions) {
40+
this.raw = `${ url_long }/${ options.raw }`;
41+
this.content = options.content;
3942
this.languageId = getLanguageId(options.languageId) || 372;
4043
this.language = linguist[this.languageId];
41-
this.content = options.content;
4244
}
4345

4446
public object(): any {
@@ -53,23 +55,24 @@ interface BinOptions {
5355
}
5456

5557
interface BinFileOptions {
58+
raw?: string;
5659
content: string;
5760
languageId?: number | string;
5861
}
5962

6063
interface Language {
6164
name: string;
62-
extension: string;
63-
aliases?: Array<string>;
6465
aceMode: string;
66+
aliases?: Array<string>;
67+
extension: string;
6568
}
6669

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

7174
const [
72-
match, , , , key
75+
match, , , , key
7376
] = k.match(/s((ource)|(rc))b\.in\/(\S+)/) || [];
7477

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

85-
const { files, key, created } = await fetch(`${ url_long }/api/bins/${ k }`, {
88+
const { files, key, created }: Bin = await fetch(`${ url_long }/api/bins/${ k }`, {
8689
headers: {
8790
'Content-Type': 'application/json',
8891
'User-Agent': 'SourceBin Wrapper/' + version
@@ -94,10 +97,11 @@ export async function get(k: string): Promise<Bin> {
9497

9598
const binFiles: Array<BinFile> = [];
9699

97-
files.forEach(file => {
100+
files.forEach((file, index) => {
98101
binFiles.push(new BinFile({
99102
content: file.content,
100-
languageId: file.languageId
103+
languageId: file.languageId,
104+
raw: `${ key }/${ index }`
101105
}));
102106
});
103107

@@ -117,7 +121,10 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
117121
files: binFiles
118122
.map(file => file.object())
119123
.map(file => {
120-
return { content: file.content, languageId: file.languageId };
124+
return {
125+
content: file.content,
126+
languageId: file.languageId
127+
};
121128
})
122129
};
123130

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

134142
if (message) {
@@ -138,7 +146,12 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
138146
return new Bin({
139147
key: key,
140148
created: new Date(),
141-
files: binFiles
149+
files: binFiles.map((file, i) => {
150+
return {
151+
...file.object(),
152+
raw: `${ url_long }/${ key }/${ i }`
153+
};
154+
})
142155
});
143156
}
144157

0 commit comments

Comments
 (0)