Files
directus/api/src/middleware/collection-exists.ts
rijkvanzanten 801e868554 Fix remaining eslint errors
h/t @paescuj
2021-04-29 15:55:12 -04:00

33 lines
924 B
TypeScript

/**
* Check if requested collection exists, and save it to req.collection
*/
import { RequestHandler } from 'express';
import { systemCollectionRows } from '../database/system-data/collections';
import { ForbiddenException } from '../exceptions';
import asyncHandler from '../utils/async-handler';
const collectionExists: RequestHandler = asyncHandler(async (req, res, next) => {
if (!req.params.collection) return next();
if (req.params.collection in req.schema.collections === false) {
throw new ForbiddenException();
}
req.collection = req.params.collection;
if (req.collection.startsWith('directus_')) {
const systemRow = systemCollectionRows.find((collection) => {
return collection?.collection === req.collection;
});
req.singleton = !!systemRow?.singleton;
} else {
req.singleton = req.schema.collections[req.collection].singleton;
}
return next();
});
export default collectionExists;