Skip to content

Commit d6d915e

Browse files
committed
Get bins using a sourceb.in url
1 parent d8c61bc commit d6d915e

3 files changed

Lines changed: 24 additions & 6 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.0.1",
3+
"version": "1.1.0",
44
"description": "Create and get bins from https://sourceb.in/",
55
"main": "index.js",
66
"scripts": {

src/__tests__/GetBin.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import * as SourceBin from "../index";
22

3-
SourceBin.get("d4ad855543").then(console.log);
3+
SourceBin.get("https://sourceb.in/d4ad855543")
4+
.then(console.log)
5+
.catch(console.error);

src/index.ts

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

6-
const url = "https://sourceb.in";
6+
const url_long = "https://sourceb.in";
7+
const url_short = "sourceb.in";
78

89
export class Bin {
910
public key: string;
@@ -13,7 +14,7 @@ export class Bin {
1314

1415
constructor(options: BinOptions) {
1516
this.key = options.key;
16-
this.url = `${ url }/${ this.key }`;
17+
this.url = `${ url_long }/${ this.key }`;
1718
this.created = options.created;
1819
this.files = options.files;
1920
}
@@ -54,7 +55,22 @@ interface Language {
5455
}
5556

5657
export async function get(k: string): Promise<Bin> {
57-
const { files, key, created } = await fetch(`${ url }/api/bins/${ k }`, {
58+
59+
if (/(https?)(:\/\/)?(.+)\.(.*)\/?/.test(k)) {
60+
if (k.includes(url_short)) {
61+
const match = k.match(/sourceb.in\/(.+)/);
62+
63+
if (!match) {
64+
return Promise.reject("Url must have a valid path!");
65+
}
66+
67+
k = match[1].replace(/\//g, "");
68+
} else {
69+
return Promise.reject(`Url must be a valid '${ url_short }' url!`);
70+
}
71+
}
72+
73+
const { files, key, created } = await fetch(`${ url_long }/api/bins/${ k }`, {
5874
headers: {
5975
"Content-Type": "application/json",
6076
"User-Agent": "SourceBin Wrapper/" + version
@@ -89,7 +105,7 @@ export async function create(binFiles: Array<BinFile>): Promise<Bin | string> {
89105
})
90106
};
91107

92-
const { key, message } = await fetch(`${ url }/api/bins`, {
108+
const { key, message } = await fetch(`${ url_long }/api/bins`, {
93109
method: "post",
94110
headers: {
95111
"Content-Type": "application/json",

0 commit comments

Comments
 (0)