mirror of
https://github.com/directus/directus.git
synced 2026-01-30 10:37:56 -05:00
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import express from 'express';
|
|
import asyncHandler from 'express-async-handler';
|
|
import WebhooksService from '../services/webhooks';
|
|
import MetaService from '../services/meta';
|
|
|
|
const router = express.Router();
|
|
|
|
router.post(
|
|
'/',
|
|
asyncHandler(async (req, res, next) => {
|
|
const service = new WebhooksService({ accountability: req.accountability });
|
|
const primaryKey = await service.create(req.body);
|
|
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
|
|
|
|
res.locals.payload = { data: item || null };
|
|
}),
|
|
);
|
|
|
|
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 };
|
|
}),
|
|
);
|
|
|
|
router.get(
|
|
'/:pk',
|
|
asyncHandler(async (req, res, next) => {
|
|
const service = new WebhooksService({ accountability: req.accountability });
|
|
const record = await service.readByKey(req.params.pk, req.sanitizedQuery);
|
|
|
|
res.locals.payload = { data: record || null };
|
|
}),
|
|
);
|
|
|
|
router.patch(
|
|
'/:pk',
|
|
asyncHandler(async (req, res, next) => {
|
|
const service = new WebhooksService({ accountability: req.accountability });
|
|
const primaryKey = await service.update(req.body, req.params.pk);
|
|
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
|
|
|
|
res.locals.payload = { data: item || null };
|
|
}),
|
|
);
|
|
|
|
router.delete(
|
|
'/:pk',
|
|
asyncHandler(async (req, res, next) => {
|
|
const service = new WebhooksService({ accountability: req.accountability });
|
|
await service.delete(req.params.pk);
|
|
|
|
return next();
|
|
}),
|
|
);
|
|
|
|
export default router;
|