✨ add `right` option to route()

This commit is contained in:
Flam3rboy committed 2021-10-03 01:55:57 +02:00
1 parent 62f727783d
commit 7268779dc3
6 files changed
+31 -5

No files matched your search

+20 -1
View File
@@ -1,4 +1,15 @@
import { DiscordApiErrors, EVENT, Event, EventData, getPermission, PermissionResolvable, Permissions } from "@fosscord/util";
import {
DiscordApiErrors,
EVENT,
Event,
EventData,
FosscordApiErrors,
getPermission,
PermissionResolvable,
Permissions,
RightResolvable,
Rights
} from "@fosscord/util";
import { NextFunction, Request, Response } from "express";
import fs from "fs";
import path from "path";
@@ -33,6 +44,7 @@ export type RouteResponse = { status?: number; body?: `${string}Response`; heade
export interface RouteOptions {
permission?: PermissionResolvable;
right?: RightResolvable;
body?: `${string}Schema`; // typescript interface name
test?: {
response?: RouteResponse;
@@ -89,6 +101,13 @@ export function route(opts: RouteOptions) {
}
}
if (opts.right) {
const required = new Rights(opts.right);
if (!req.rights || !req.rights.has(required)) {
throw FosscordApiErrors.MISSING_RIGHTS.withParams(opts.right as string);
}
}
if (validate) {
const valid = validate(normalizeBody(req.body));
if (!valid) {