mirror of
https://github.com/spacebarchat/server.git
synced 2026-10-22 13:12:19 +00:00
✨ add `right` option to route()
This commit is contained in:
1 parent
62f727783d
commit
7268779dc3
6 files changed
+31
-5
No files matched your search
+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