mirror of
https://github.com/directus/directus.git
synced 2026-04-03 03:00:39 -04:00
Rename collection-exists
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
/**
|
||||
* Check if requested collection exists, and save it to req.collection
|
||||
*/
|
||||
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import database from '../database';
|
||||
import APIError, { ErrorCode } from '../error';
|
||||
|
||||
const collectionExists: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
const validateCollection: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
if (!req.params.collection) return next();
|
||||
|
||||
const exists = await database.schema.hasTable(req.params.collection);
|
||||
|
||||
if (exists) {
|
||||
res.locals.collection = req.params.collection;
|
||||
req.collection = req.params.collection;
|
||||
return next();
|
||||
}
|
||||
|
||||
throw new APIError(ErrorCode.NOT_FOUND, `Collection "${req.params.collection}" doesn't exist.`);
|
||||
});
|
||||
|
||||
export default collectionExists;
|
||||
export default validateCollection;
|
||||
Reference in New Issue
Block a user