Skip to content

Commit 408e89b

Browse files
committed
SourceBin Wrapper created
0 parents  commit 408e89b

8 files changed

Lines changed: 265 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
node_modules/
3+
lib/
4+
5+
sourcebin-wrapper.iml
6+
package-lock.json

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea/
2+
node_modules/
3+
src/
4+
5+
sourcebin-wrapper.iml
6+
package-lock.json
7+
tsconfig.json

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SourceBin Wrapper
2+
3+
With this wrapper you can create and get bins from https://sourceb.in/
4+
5+
### Install
6+
`npm i sourcebin-wrapper --save`
7+
8+
### Initialize
9+
```javascript
10+
// typescript
11+
import * as SourceBin from 'sourcebin-wrapper';
12+
13+
// Node/JavaScript
14+
const SourceBin = require('SourceBin');
15+
```
16+
17+
### Create
18+
```javascript
19+
SourceBin.create([
20+
new SourceBin.BinFile({
21+
content: 'This was created using the wrapper\n\nlanguageId: "js"',
22+
languageId: 'js'
23+
})
24+
]).then(console.log)
25+
.catch(console.error);
26+
```
27+
Language defaults to **Text**, if invalid or no language provided.
28+
29+
### Get
30+
```javascript
31+
SourceBin.get("d4ad855543").then(console.log);
32+
```
33+
34+
#### Sample Output
35+
Output for both **create** and **get**
36+
```json
37+
{
38+
"key": "d4ad855543",
39+
"url": "https://sourceb.in/d4ad855543",
40+
"created": "2020-03-17T21:12:30.549Z",
41+
"files": [
42+
{
43+
"languageId": 183,
44+
"language": {
45+
"name": "JavaScript",
46+
"extension": "js",
47+
"aliases": [
48+
"js",
49+
"node"
50+
],
51+
"aceMode": "javascript"
52+
},
53+
"content": "This was created using the wrapper\n\nlanguageId: \"js\""
54+
}
55+
]
56+
}
57+
```

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "sourcebin-wrapper",
3+
"version": "1.0.0",
4+
"description": "Create and get bins from https://sourceb.in/",
5+
"main": "index.js",
6+
"scripts": {
7+
"test-get": "ts-node src/__tests__/GetBin.test.ts",
8+
"test-create": "ts-node src/__tests__/CreateBin.test.ts",
9+
"build": "tsc"
10+
},
11+
"keywords": [
12+
"sourcebin",
13+
"sourcebin npm",
14+
"sourcebin-wrapper",
15+
"sourcebin-wrapper npm"
16+
],
17+
"author": "Jacxk",
18+
"license": "ISC",
19+
"devDependencies": {
20+
"typescript": "^3.8.3",
21+
"ts-node": "^8.6.2",
22+
"@types/node-fetch": "^2.5.5"
23+
},
24+
"dependencies": {
25+
"@sourcebin/linguist": "0.0.2",
26+
"node-fetch": "^2.6.0"
27+
},
28+
"directories": {
29+
"lib": "lib"
30+
},
31+
"repository": {
32+
"type": "git",
33+
"url": "git+https://github.com/Jacxk/Sourcebin-Wrapper.git"
34+
},
35+
"bugs": {
36+
"url": "https://github.com/Jacxk/Sourcebin-Wrapper/issues"
37+
},
38+
"homepage": "https://github.com/Jacxk/Sourcebin-Wrapper#readme"
39+
}

src/__tests__/CreateBin.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as SourceBin from "../index";
2+
3+
SourceBin.create([
4+
new SourceBin.BinFile({
5+
content: "This was created using the wrapper\n\nlanguageId: \"js\"",
6+
languageId: "js"
7+
})
8+
])
9+
.then(console.log)
10+
.catch(console.error);

src/__tests__/GetBin.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as SourceBin from "../index";
2+
3+
SourceBin.get("d4ad855543").then(console.log);

src/index.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"outDir": "./lib",
7+
"sourceMap": true
8+
},
9+
"exclude": [
10+
"node_modules",
11+
"**/__tests__/*"
12+
]
13+
}

0 commit comments

Comments
 (0)