forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
193 lines (162 loc) · 4.88 KB
/
Copy pathapi.js
File metadata and controls
193 lines (162 loc) · 4.88 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import cors from "cors";
import express from "express";
import sanitize from "mongo-sanitize";
import * as helper from "../helper.js";
import { db } from "../mongo.js";
import * as accessories from "./api/accessories.js";
import * as armor from "./api/armor.js";
import * as bazaar from "./api/bazaar.js";
import * as cakebag from "./api/cakebag.js";
import * as collections from "./api/collections.js";
import * as items from "./api/items.js";
import * as minions from "./api/minions.js";
import * as pets from "./api/pets.js";
import * as profiles from "./api/profiles.js";
import * as skills from "./api/skills.js";
import * as wardrobe from "./api/wardrobe.js";
import * as weapons from "./api/weapons.js";
const router = express.Router();
router.use(cors());
// Checks if the request is HTML and also sets options for cacheOnly and other stuff.
router.use((req, res, next) => {
req.isHtml = "html" in req.query;
req.options = {
cacheOnly: true,
};
next();
});
// /api/bazaar is the only endpoint that still supports JSON..
// So I had to make an exception for it. Great.
router.get("/bazaar", bazaar.router);
router.use((req, res, next) => {
if (!req.isHtml) {
helper.sendMetric("endpoint_api_fail_jsondisabled");
handleError(req, res, new Error("Old JSON API has been disabled."), 403, false);
return;
}
next();
});
// Endpoints with player parameter
router.get("/:player/profiles", handleParams, profiles.router);
// Endpoints with player and profile parameters
router.get("/:player/:profile/accessories", handleParams, accessories.router);
router.get("/:player/:profile/armor", handleParams, armor.router);
router.get("/:player/:profile/cakebag", handleParams, cakebag.router);
router.get("/:player/:profile/collections", handleParams, collections.router);
router.get("/:player/:profile/items", handleParams, items.router);
router.get("/:player/:profile/minions", handleParams, minions.router);
router.get("/:player/:profile/pets", handleParams, pets.router);
router.get("/:player/:profile/skills", handleParams, skills.router);
router.get("/:player/:profile/wardrobe", handleParams, wardrobe.router);
router.get("/:player/:profile/weapons", handleParams, weapons.router);
// Handler of non-existing endpoints
router.get("/*", async (req, res) => {
helper.sendMetric("endpoint_apiv2_fail_notfound");
handleError(req, res, new Error("Endpoint was not found."), 404, false);
});
// Handler of unsupported methods
router.all("/*", async (req, res) => {
helper.sendMetric("endpoint_apiv2_fail_onlyget");
handleError(req, res, new Error("API only supports GET requests."), 405, false);
});
// Error handler for all /api endpoints
// Meant to be a safenet if some endpoint returns an error.
router.use((err, req, res, next) => {
helper.sendMetric("endpoint_apiv2_fail");
handleError(req, res, err);
});
/**
* converts an array of objects to a table
*
* based on github.com/tillhub/tableify
*
* @param items {Iterable}
*/
export function tableify(items) {
const keys = new Set();
// get unique keys of all objects
for (const item of items) {
for (const key in item) {
keys.add(key);
}
}
let html = "<table><tbody>";
for (const item of items) {
html += "<tr>";
for (const key of keys) {
html += "<td>";
html += item[key] || "";
html += "</td>";
}
html += "</tr>";
}
html += "</tbody></table>";
return html;
}
export const productInfo = {};
/**
* Prepares productInfo for /api/bazaar
* @returns void
*/
async function prepareProductInfo() {
try {
const bazaarProducts = await db.collection("bazaar").find().toArray();
const itemInfo = await db
.collection("items")
.find({ id: { $in: bazaarProducts.map((a) => a.productId) } })
.toArray();
for (const product of bazaarProducts) {
const info = itemInfo.filter((a) => a.id == product.productId);
if (info.length > 0) {
productInfo[product.productId] = info[0];
}
}
return;
} catch (e) {
console.error(e);
}
}
/**
* Initializes prepare functions.
* @returns void
*/
export async function init() {
await prepareProductInfo();
return;
}
/**
* Does things with parameters, because I can't work with req.params across routers.
*
* @param Request req
* @param Response res
* @param Next next
*/
export function handleParams(req, res, next) {
for (const key in req.params) {
req[key] = sanitize(req.params[key]);
}
next();
}
/**
* Sends error messages for handled errors.
*
* @param Request req
* @param Response res
* @param Error err
* @param number status
* @param boolean logged
*/
export function handleError(req, res, err, status = 500, logged = true) {
if (logged) {
console.error(err);
}
if (req.isHtml) {
res.set("Content-Type", "text/plain");
res.status(status).send(err.message);
} else {
res.status(status).json({
error: err.message,
});
}
}
export { router };