mirror of
https://github.com/directus/directus.git
synced 2026-02-01 00:18:09 -05:00
* Rework hook registration * Remove event and action fields from hook payloads * Move "error" action to "request.error" filter * Emit meta and context objects in filters and actions * Run filters sequentially * Update hook templates * Fix CLI hook test * Also emit `<collection>.items.crud` when emitting `items.crud`. * Update hook docs Co-authored-by: Oreilles <oreilles.github@nitoref.io> Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
39 lines
912 B
TypeScript
39 lines
912 B
TypeScript
import { RequestHandler } from 'express';
|
|
import getDatabase from '../database';
|
|
import emitter from '../emitter';
|
|
import { RouteNotFoundException } from '../exceptions';
|
|
|
|
/**
|
|
* Handles not found routes.
|
|
*
|
|
* - If a hook throws an error, the error gets forwarded to the error handler.
|
|
* - If a hook returns true, the handler assumes the response has been
|
|
* processed and won't generate a response.
|
|
*
|
|
* @param req
|
|
* @param res
|
|
* @param next
|
|
*/
|
|
const notFound: RequestHandler = async (req, res, next) => {
|
|
try {
|
|
const hooksResult = await emitter.emitFilter(
|
|
'request.not_found',
|
|
false,
|
|
{ request: req, response: res },
|
|
{
|
|
database: getDatabase(),
|
|
schema: req.schema,
|
|
accountability: req.accountability ?? null,
|
|
}
|
|
);
|
|
if (hooksResult) {
|
|
return next();
|
|
}
|
|
next(new RouteNotFoundException(req.path));
|
|
} catch (err: any) {
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export default notFound;
|