1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

add explicit types to req and res

This commit is contained in:
Flam3rboy 2021-05-31 21:03:09 +02:00
parent 536900d255
commit 491565aef8
26 changed files with 68 additions and 68 deletions

14
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@fosscord/server-util": "^1.3.15",
"@fosscord/server-util": "^1.3.16",
"@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7",
"ajv": "^8.4.0",
@ -520,9 +520,9 @@
}
},
"node_modules/@fosscord/server-util": {
"version": "1.3.15",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.15.tgz",
"integrity": "sha512-PCSOYOUiEzpdcUoyBSCHqOB72b+xdNuYStEuvvxLJt6MV6id4GA18DvD79Hw7F38F4gm/PIK6di+GnHWMd3Prw==",
"version": "1.3.16",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.16.tgz",
"integrity": "sha512-/oDDGyWllVNMb/84wzzYQ6vpwkcW+mg57ECWS5bUDQ0OhT3apdpYgQFfWxwGYgyYo6fB4gmyJh/U2bzWo6Jmeg==",
"dependencies": {
"@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1",
@ -12864,9 +12864,9 @@
}
},
"@fosscord/server-util": {
"version": "1.3.15",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.15.tgz",
"integrity": "sha512-PCSOYOUiEzpdcUoyBSCHqOB72b+xdNuYStEuvvxLJt6MV6id4GA18DvD79Hw7F38F4gm/PIK6di+GnHWMd3Prw==",
"version": "1.3.16",
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.16.tgz",
"integrity": "sha512-/oDDGyWllVNMb/84wzzYQ6vpwkcW+mg57ECWS5bUDQ0OhT3apdpYgQFfWxwGYgyYo6fB4gmyJh/U2bzWo6Jmeg==",
"requires": {
"@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1",

View File

@ -29,7 +29,7 @@
},
"homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": {
"@fosscord/server-util": "^1.3.15",
"@fosscord/server-util": "^1.3.16",
"@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7",
"ajv": "^8.4.0",

View File

@ -9,8 +9,8 @@ import i18nextMiddleware, { I18next } from "i18next-http-middleware";
import i18nextBackend from "i18next-node-fs-backend";
import { ErrorHandler } from "./middlewares/ErrorHandler";
import { BodyParser } from "./middlewares/BodyParser";
import express, { Router } from "express";
import fetch, { Response } from "node-fetch";
import express, { Router, Request, Response } from "express";
import fetch, { Response as FetchResponse } from "node-fetch";
import mongoose from "mongoose";
import path from "path";
@ -31,13 +31,13 @@ declare global {
const assetCache = new Map<
string,
{
response: Response;
response: FetchResponse;
buffer: Buffer;
}
>();
export class FosscordServer extends Server {
public options: FosscordServerOptions;
public declare options: FosscordServerOptions;
constructor(opts?: Partial<FosscordServerOptions>) {
// @ts-ignore
@ -101,9 +101,9 @@ export class FosscordServer extends Server {
this.app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
this.app.get("/assets/:file", async (req, res) => {
this.app.get("/assets/:file", async (req: Request, res: Response) => {
delete req.headers.host;
var response: Response;
var response: FetchResponse;
var buffer: Buffer;
const cache = assetCache.get(req.params.file);
if (!cache) {
@ -140,7 +140,7 @@ export class FosscordServer extends Server {
return res.send(buffer);
});
this.app.get("*", (req, res) => {
this.app.get("*", (req: Request, res: Response) => {
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
res.set("content-type", "text/html");
res.send(

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router: Router = Router();
// TODO:

View File

@ -1,5 +1,5 @@
import { ChannelDeleteEvent, ChannelModel, ChannelUpdateEvent, getPermission, GuildUpdateEvent, toObject } from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel";
import { emitEvent } from "../../../util/Event";
@ -8,7 +8,7 @@ const router: Router = Router();
// TODO: delete channel
// TODO: Get channel
router.get("/", async (req, res) => {
router.get("/", async (req: Request, res: Response) => {
const { channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
@ -20,7 +20,7 @@ router.get("/", async (req, res) => {
return res.send(toObject(channel));
});
router.delete("/", async (req, res) => {
router.delete("/", async (req: Request, res: Response) => {
const { channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
@ -39,7 +39,7 @@ router.delete("/", async (req, res) => {
res.send(data);
});
router.patch("/", check(ChannelModifySchema), async (req, res) => {
router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
var payload = req.body as ChannelModifySchema;
const { channel_id } = req.params;

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
// TODO:
// router.post("/", (req, res) => {});
// router.post("/", (req: Request, res: Response) => {});
export default router;

View File

@ -1,5 +1,5 @@
import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
import { MessageCreateSchema } from "../../../../../schema/Message";
import { emitEvent } from "../../../../../util/Event";
@ -8,7 +8,7 @@ import { handleMessage, postHandleMessage } from "../../../../../util/Message";
const router = Router();
router.patch("/", check(MessageCreateSchema), async (req, res) => {
router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
var body = req.body as MessageCreateSchema;
@ -47,7 +47,7 @@ router.patch("/", check(MessageCreateSchema), async (req, res) => {
// TODO: delete attachments in message
router.delete("/", async (req, res) => {
router.delete("/", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true });

View File

@ -13,7 +13,7 @@ import {
toObject,
UserModel
} from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../../../util/Event";
@ -35,7 +35,7 @@ function getEmoji(emoji: string): PartialEmoji {
};
}
router.delete("/", async (req, res) => {
router.delete("/", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
@ -61,7 +61,7 @@ router.delete("/", async (req, res) => {
res.sendStatus(204);
});
router.delete("/:emoji", async (req, res) => {
router.delete("/:emoji", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);
@ -95,7 +95,7 @@ router.delete("/:emoji", async (req, res) => {
res.sendStatus(204);
});
router.get("/:emoji", async (req, res) => {
router.get("/:emoji", async (req: Request, res: Response) => {
const { message_id, channel_id } = req.params;
const emoji = getEmoji(req.params.emoji);
@ -112,7 +112,7 @@ router.get("/:emoji", async (req, res) => {
res.json(toObject(users));
});
router.put("/:emoji/:user_id", async (req, res) => {
router.put("/:emoji/:user_id", async (req: Request, res: Response) => {
const { message_id, channel_id, user_id } = req.params;
if (user_id !== "@me") throw new HTTPError("Invalid user");
const emoji = getEmoji(req.params.emoji);
@ -162,7 +162,7 @@ router.put("/:emoji/:user_id", async (req, res) => {
res.sendStatus(204);
});
router.delete("/:emoji/:user_id", async (req, res) => {
router.delete("/:emoji/:user_id", async (req: Request, res: Response) => {
var { message_id, channel_id, user_id } = req.params;
const emoji = getEmoji(req.params.emoji);

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
import { ChannelModel, Config, getPermission, MessageDeleteBulkEvent, MessageModel } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../../util/Event";
@ -11,7 +11,7 @@ export default router;
// TODO: should users be able to bulk delete messages or only bots?
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
router.post("/", check({ messages: [String] }), async (req, res) => {
router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => {
const { channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
import { Attachment, ChannelModel, ChannelType, getPermission, MessageDocument, MessageModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { MessageCreateSchema } from "../../../../schema/Message";
@ -28,7 +28,7 @@ export function isTextChannel(type: ChannelType): boolean {
// https://discord.com/developers/docs/resources/channel#create-message
// get messages
router.get("/", async (req, res) => {
router.get("/", async (req: Request, res: Response) => {
const channel_id = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
if (!channel) throw new HTTPError("Channel not found", 404);
@ -101,7 +101,7 @@ const messageUpload = multer({
// TODO: trim and replace message content and every embed field
// Send message
router.post("/", check(MessageCreateSchema), messageUpload.single("file"), async (req, res) => {
router.post("/", check(MessageCreateSchema), messageUpload.single("file"), async (req: Request, res: Response) => {
const { channel_id } = req.params;
var body = req.body as MessageCreateSchema;
const attachments: Attachment[] = [];

View File

@ -1,5 +1,5 @@
import { ChannelModel, ChannelPermissionOverwrite, ChannelUpdateEvent, getPermission, MemberModel, RoleModel } from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf";
@ -7,7 +7,7 @@ const router: Router = Router();
// TODO: Only permissions your bot has in the guild or channel can be allowed/denied (unless your bot has a MANAGE_ROLES overwrite in the channel)
router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, id: String }), async (req, res) => {
router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, id: String }), async (req: Request, res: Response) => {
const { channel_id, overwrite_id } = req.params;
const body = req.body as { allow: bigint; deny: bigint; type: number; id: string };
@ -52,7 +52,7 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number,
});
// TODO: check permission hierarchy
router.delete("/:overwrite_id", async (req, res) => {
router.delete("/:overwrite_id", async (req: Request, res: Response) => {
const { channel_id, overwrite_id } = req.params;
const permissions = await getPermission(req.user_id, undefined, channel_id);

View File

@ -53,7 +53,7 @@ router.put("/:message_id", async (req: Request, res: Response) => {
res.sendStatus(204);
});
router.delete("/:message_id", async (req, res) => {
router.delete("/:message_id", async (req: Request, res: Response) => {
const { channel_id, message_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }).exec();

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router: Router = Router();
// TODO:

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
import { check, Length } from "../../../util/instanceOf";
import { ChannelModel, getPermission, trimSpecial } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
@ -8,7 +8,7 @@ const router: Router = Router();
// TODO:
// TODO: use Image Data Type for avatar instead of String
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req, res) => {
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => {
const channel_id = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec();
if (!channel) throw new HTTPError("Channel not found", 404);

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.get("/", (req, res) => {
router.get("/", (req: Request, res: Response) => {
// TODO:
res.send({ fingerprint: "", assignments: [] });
});

View File

@ -1,9 +1,9 @@
import { Config } from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.get("/", (req, res) => {
router.get("/", (req: Request, res: Response) => {
const { endpoint } = Config.get().gateway;
res.send({ url: endpoint || process.env.GATEWAY || "ws://localhost:3002" });
});

View File

@ -1,4 +1,4 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
import {
ChannelCreateEvent,
ChannelModel,
@ -16,14 +16,14 @@ import { check } from "../../../util/instanceOf";
import { createChannel } from "../../../util/Channel";
const router = Router();
router.get("/", async (req, res) => {
router.get("/", async (req: Request, res: Response) => {
const { guild_id } = req.params;
const channels = await ChannelModel.find({ guild_id }).exec();
res.json(toObject(channels));
});
router.post("/", check(ChannelModifySchema), async (req, res) => {
router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
const { guild_id } = req.params;
const body = req.body as ChannelModifySchema;
@ -32,7 +32,7 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
res.json(channel);
});
router.patch("/", check(ChannelModifySchema), async (req, res) => {
router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
const { guild_id } = req.params;
const body = req.body as ChannelModifySchema;

View File

@ -28,7 +28,7 @@ router.get("/", async (req: Request, res: Response) => {
return res.json(toObject(member));
});
router.patch("/", check(MemberChangeSchema), async (req, res) => {
router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response) => {
const { guild_id, member_id } = req.params;
const body = req.body as MemberChangeSchema;
if (body.roles) {

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.post("/", (req, res) => {
router.post("/", (req: Request, res: Response) => {
// TODO:
res.sendStatus(204);
});

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.get("/", (req, res) => {
router.get("/", (req: Request, res: Response) => {
// TODO:
res.status(200).send({ guild_affinities: [] });
});

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.get("/", (req, res) => {
router.get("/", (req: Request, res: Response) => {
// TODO:
res.status(200).send({ user_affinities: [], inverse_user_affinities: [] });
});

View File

@ -23,7 +23,7 @@ router.get("/", async (req: Request, res: Response) => {
res.json(toObject(channels));
});
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => {
const body = req.body as DmChannelCreateSchema;
body.recipients = body.recipients.filter((x) => x !== req.user_id).unique();

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.post("/", (req, res) => {
router.post("/", (req: Request, res: Response) => {
// TODO:
res.sendStatus(204);
});

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.get("/", (req, res) => {
router.get("/", (req: Request, res: Response) => {
// TODO:
res.status(200).send([]);
});

View File

@ -6,7 +6,7 @@ import {
RelationshipType,
RelationshipRemoveEvent
} from "@fosscord/server-util";
import { Router } from "express";
import { Router, Response, Request } from "express";
import { check, HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
@ -14,7 +14,7 @@ const router = Router();
const userProjection = { "user_data.relationships": true, ...PublicUserProjection };
router.put("/:id", check({ $type: Number }), async (req, res) => {
router.put("/:id", check({ $type: Number }), async (req: Request, res: Response) => {
const { id } = req.params;
if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend");
const body = req.body as { type?: number };
@ -107,7 +107,7 @@ router.put("/:id", check({ $type: Number }), async (req, res) => {
return res.sendStatus(204);
});
router.delete("/:id", async (req, res) => {
router.delete("/:id", async (req: Request, res: Response) => {
const { id } = req.params;
if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend");

View File

@ -1,8 +1,8 @@
import { Router } from "express";
import { Router, Response, Request } from "express";
const router = Router();
router.patch("/", (req, res) => {
router.patch("/", (req: Request, res: Response) => {
// TODO:
res.sendStatus(204);
});