import express from 'express'; import asyncHandler from 'express-async-handler'; import ItemsService from '../services/items'; import sanitizeQuery from '../middleware/sanitize-query'; import collectionExists from '../middleware/collection-exists'; import * as MetaService from '../services/meta'; import { RouteNotFoundException } from '../exceptions'; import { Accountability, PrimaryKey } from '../types'; const router = express.Router(); router.post( '/:collection', collectionExists, sanitizeQuery, asyncHandler(async (req, res) => { if (req.singleton) { throw new RouteNotFoundException(req.path); } const service = new ItemsService(req.collection, { accountability: req.accountability }); const primaryKey = await service.create(req.body); const result = await service.readByKey(primaryKey, req.sanitizedQuery); res.json({ data: result || null }); }) ); router.get( '/:collection', collectionExists, sanitizeQuery, asyncHandler(async (req, res) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); const [records, meta] = await Promise.all([ req.singleton ? service.readSingleton(req.sanitizedQuery) : service.readByQuery(req.sanitizedQuery), MetaService.getMetaForQuery(req.collection, req.sanitizedQuery), ]); return res.json({ meta: meta, data: records || null, }); }) ); router.get( '/:collection/:pk', collectionExists, sanitizeQuery, asyncHandler(async (req, res) => { if (req.singleton) { throw new RouteNotFoundException(req.path); } const service = new ItemsService(req.collection, { accountability: req.accountability }); const primaryKey = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk; const result = await service.readByKey(primaryKey as any, req.sanitizedQuery); return res.json({ data: result || null, }); }) ); router.patch( '/:collection', collectionExists, sanitizeQuery, asyncHandler(async (req, res) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); if (req.singleton === true) { await service.upsertSingleton(req.body); const item = await service.readSingleton(req.sanitizedQuery); return res.json({ data: item || null }); } throw new RouteNotFoundException(req.path); }) ); router.patch( '/:collection/:pk', collectionExists, sanitizeQuery, asyncHandler(async (req, res) => { if (req.singleton) { throw new RouteNotFoundException(req.path); } const service = new ItemsService(req.collection, { accountability: req.accountability }); const primaryKey = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk; const updatedPrimaryKey = await service.update(req.body, primaryKey as any); const result = await service.readByKey(updatedPrimaryKey, req.sanitizedQuery); res.json({ data: result || null }); }) ); router.delete( '/:collection/:pk', collectionExists, asyncHandler(async (req, res) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk; await service.delete(pk as any); return res.status(200).end(); }) ); export default router;