-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
71 lines (65 loc) · 1.6 KB
/
index.js
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
const EventEmitter = require("events");
const fetch = require("phin");
const { api } = require("./package.json");
class AnimeFact extends EventEmitter {
/**
* The main class
* @param {token} [auth] - the authorization token
*/
constructor(token) {
super();
this._auth = token;
}
/**
* Gets a fact from the api
* @returns {Promise<Fact>}
*/
async getFact() {
const auth = this._auth;
if (!auth) throw new TypeError("Missing authorization token");
return fetch({
url: `${api}/fact`,
headers: {
Auth: auth,
"Content-Type": "application/json",
},
parse: "json",
})
.then((res) => {
if (res.statusCode !== 200)
switch (res.statusCode) {
case 404:
return {
statusCode: res.statusCode,
body: res.body,
error: "Could not find any fact",
};
case 502:
return {
statusCode: res.statusCode,
body: res.body,
error: "Server down",
};
default:
return {
statusCode: res.statusCode,
body: res.body,
error: "Unknown error",
};
}
return {
id: res.body._id,
fact: res.body.fact,
};
})
.catch((err) => {
throw err;
});
}
}
module.exports = AnimeFact;
/**
* @typedef {object} Fact
* @prop {number} id
* @prop {string} fact
*/