This repository was archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (98 loc) · 3.69 KB
/
index.js
File metadata and controls
117 lines (98 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file Index file for the weeb.sh Wrapper.
* @author WizardLink
*/
const { get, post } = require('snekfetch');
const { readFile } = require('fs');
const { promisify } = require('util');
const readFileAsync = promisify(readFile);
class WeebWrapper {
constructor(token) {
if (!token) throw new Error('You must put a token to use non-static methods.');
if (typeof token !== 'string') throw new TypeError('The token you provided is not a string.');
if (/^(Bearer|Wolke) /.test(token)) this.token = token;
else if (token.length > 100) this.token = `Bearer ${token}`;
else this.token = `Wolke ${token}`;
}
/**
* @description Base URL for reaching weeb.sh's endpoints.
* @static
* @return {string}
*/
static get baseURL() {
return 'https://api.weeb.sh/images';
}
/**
* @description A function to receive the parsed object of the request.
* @param {string} endpoint Which endpoint of the api that will be requested to.
* @param {Object} query The query of the request.
* @param {boolean} toPost If it is a post request.
* @return {Promise<Object>}
*/
async requestToAPI(endpoint = '', query = {}, toPost = false) {
let request = toPost
? post(`${WeebWrapper.baseURL}/${endpoint}`).send(query)
: get(`${WeebWrapper.baseURL}/${endpoint}`).query(query);
request.set({ Authorization: this.token });
request = await request;
return JSON.parse(request.text);
}
/**
* @description Basic information about the API (And version control).
* @static
* @return {Promise<Object>}
*/
static info() {
return this.requestToAPI();
}
/**
* @description Fetch a random (type/tag) image from the API.
* @param {string} type The type or tag you want to retrieve an image from.
* @return {Object}
* @param {Object} [options = {}] The query options.
* @param {boolean} [options.hidden] Includes hidden images that can be received.
* @param {boolean} [options.nsfw] Includes nsfw images that can be received.
* @param {string} [options.filetype] If you want to receive a specific file format only.
* @return {Promise<Object>}
*/
random(type, { hidden, nsfw, filetype } = {}) {
if (!type) throw new TypeError('You must provide a type or a tag!');
return this.requestToAPI('random', { type, hidden, nsfw, filetype });
}
/**
* @description Fetch the current image tags for the weeb.sh API.
* @param {boolean} [hidden] Include hidden tags when fetching.
* @return {Promise<Object>}
*/
tags(hidden = false) {
return this.requestToAPI('tags', { hidden });
}
/**
* @description Fetch the current image types for the weeb.sh API.
* @param {boolean} [hidden] Include hidden tags when fetching.
* @return {Promise<Object>}
*/
types(hidden = false) {
return this.requestToAPI('types', { hidden });
}
/**
* @description Upload an image to the weeb.sh API.
* @param {Buffer} file The file to be uploaded.
* @param {string} type The type of the image being uploaded.
* @param {Object} [options] The additional data of the image.
* @param {boolean} [options.nsfw] If the image is NSFW or not.
* @param {string} [options.source] The image's source (Link or Anime).
* @param {string} [options.tags] The image's tags (Seperated by commas).
* @return {Promise<Object>}
*/
async upload(file, type, { nsfw, source, tags } = {}) {
if (!file) throw new Error('You must provide a file to be uploaded!');
if (!type) throw new Error('You must provide a type to be set to the file!');
if (typeof file === 'string') {
file = await readFileAsync(file);
}
if (file instanceof Buffer) return this.requestToAPI('upload', { file, type, nsfw, source, tags }, true);
throw new Error('The file parameter is not a Buffer or a String');
}
}
module.exports = WeebWrapper;