✨ add `right` option to route()
This commit is contained in:
+20
-1
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user