Files
directus/api/src/controllers/webhooks.ts
rijkvanzanten 85ca7b5d17 Run prettier
2020-09-22 16:11:28 -04:00

92 lines
2.4 KiB
TypeScript

import express from 'express';
import asyncHandler from 'express-async-handler';
import { WebhooksService, MetaService } from '../services';
import { ForbiddenException } from '../exceptions';
import useCollection from '../middleware/use-collection';
const router = express.Router();
router.use(useCollection('directus_webhooks'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
const service = new WebhooksService({ accountability: req.accountability });
const primaryKey = await service.create(req.body);
try {
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
res.locals.payload = { data: item || null };
} catch (error) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
})
);
router.get(
'/',
asyncHandler(async (req, res, next) => {
const service = new WebhooksService({ accountability: req.accountability });
const metaService = new MetaService({ accountability: req.accountability });
const records = await service.readByQuery(req.sanitizedQuery);
const meta = await metaService.getMetaForQuery(req.collection, req.sanitizedQuery);
res.locals.payload = { data: records || null, meta };
return next();
})
);
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new WebhooksService({ accountability: req.accountability });
const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;
const record = await service.readByKey(pk as any, req.sanitizedQuery);
res.locals.payload = { data: record || null };
return next();
})
);
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new WebhooksService({ accountability: req.accountability });
const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;
const primaryKey = await service.update(req.body, pk as any);
try {
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
res.locals.payload = { data: item || null };
} catch (error) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
})
);
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new WebhooksService({ accountability: req.accountability });
const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;
await service.delete(pk as any);
return next();
})
);
export default router;