refactor: make PRECHECKs an array again
This commit is contained in:
parent
5ee654f280
commit
62eba8612b
4 changed files with 30 additions and 21 deletions
|
@ -15,8 +15,8 @@ export type PRECHECK = (
|
|||
meta: Record<string, any>
|
||||
) => undefined | Response | Promise<undefined | Response>;
|
||||
|
||||
/** A `PRECHECK_TABLE` maps from HTTP methods to your `PRECHECK`s. */
|
||||
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK>;
|
||||
/** A `PRECHECK_TABLE` maps from HTTP methods to an array of `PRECHECK`s to be run. */
|
||||
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK[]>;
|
||||
|
||||
/** A `ROUTE_HANDLER_METHOD` must take a `Request` and `meta` data and return a `Response`. */
|
||||
export type ROUTE_HANDLER_METHOD = (request: Request, meta: Record<string, any>) => Promise<Response> | Response;
|
||||
|
@ -108,11 +108,13 @@ export default async function handle_typescript(request: Request): Promise<Respo
|
|||
query
|
||||
};
|
||||
|
||||
const precheck: PRECHECK | undefined = handler_module.PRECHECKS?.[request.method as keyof PRECHECKS_TABLE];
|
||||
if (precheck) {
|
||||
const result = await precheck(request, metadata);
|
||||
if (result) {
|
||||
return result;
|
||||
const prechecks: PRECHECK[] | undefined = handler_module.PRECHECKS?.[request.method as keyof PRECHECKS_TABLE];
|
||||
if (Array.isArray(prechecks)) {
|
||||
for (const precheck of prechecks) {
|
||||
const result = await precheck(request, metadata);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue