Files
directus/api/src/controllers/collections.ts
Nicola Krumschmidt d64ca14348 Explicitly set catch parameters to any type (#7654)
This fixes not being able to build the repo due to type issues
introduced by the Typescript 4.4 option "useUnknownInCatchVariables",
which is enabled by default in strict mode.
2021-08-27 10:33:30 -04:00

120 lines
2.9 KiB
TypeScript

import { Router } from 'express';
import { ForbiddenException } from '../exceptions';
import { respond } from '../middleware/respond';
import { validateBatch } from '../middleware/validate-batch';
import { CollectionsService, MetaService } from '../services';
import { Item } from '../types';
import asyncHandler from '../utils/async-handler';
const router = Router();
router.post(
'/',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
if (Array.isArray(req.body)) {
const collectionKey = await collectionsService.createMany(req.body);
const records = await collectionsService.readMany(collectionKey);
res.locals.payload = { data: records || null };
} else {
const collectionKey = await collectionsService.createOne(req.body);
const record = await collectionsService.readOne(collectionKey);
res.locals.payload = { data: record || null };
}
return next();
}),
respond
);
const readHandler = asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
const metaService = new MetaService({
accountability: req.accountability,
schema: req.schema,
});
let result: Item[] = [];
if (req.body.keys) {
result = await collectionsService.readMany(req.body.keys);
} else {
result = await collectionsService.readByQuery();
}
const meta = await metaService.getMetaForQuery('directus_collections', {});
res.locals.payload = { data: result, meta };
return next();
});
router.get('/', validateBatch('read'), readHandler, respond);
router.search('/', validateBatch('read'), readHandler, respond);
router.get(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
const collection = await collectionsService.readOne(req.params.collection);
res.locals.payload = { data: collection || null };
return next();
}),
respond
);
router.patch(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
await collectionsService.updateOne(req.params.collection, req.body);
try {
const collection = await collectionsService.readOne(req.params.collection);
res.locals.payload = { data: collection || null };
} catch (error: any) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
}),
respond
);
router.delete(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
await collectionsService.deleteOne(req.params.collection);
return next();
}),
respond
);
export default router;