diff --git a/api/src/app.ts b/api/src/app.ts index a533b69dff..e1cdf201d2 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -77,7 +77,7 @@ app.use('/auth', authRouter) .use('/fields', fieldsRouter) .use('/files', filesRouter) .use('/folders', foldersRouter) - .use('/items', itemsRouter) + .use('/items', itemsRouter, responseManager) .use('/permissions', permissionsRouter) .use('/presets', presetsRouter) .use('/relations', relationsRouter) diff --git a/api/src/controllers/items.ts b/api/src/controllers/items.ts index 9dc11f8596..b42bac9c7c 100644 --- a/api/src/controllers/items.ts +++ b/api/src/controllers/items.ts @@ -29,7 +29,7 @@ router.get( '/:collection', collectionExists, sanitizeQuery, - asyncHandler(async (req, res) => { + asyncHandler(async (req, res, next) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); const metaService = new MetaService({ accountability: req.accountability }); @@ -39,10 +39,12 @@ router.get( const meta = await metaService.getMetaForQuery(req.collection, req.sanitizedQuery); - return res.json({ + res.locals.data = { meta: meta, data: records || null, - }); + }; + + return next(); }) ); @@ -50,7 +52,7 @@ router.get( '/:collection/:pk', collectionExists, sanitizeQuery, - asyncHandler(async (req, res) => { + asyncHandler(async (req, res, next) => { if (req.singleton) { throw new RouteNotFoundException(req.path); } @@ -58,10 +60,11 @@ router.get( 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({ + res.locals.data = { data: result || null, - }); + }; + + return next(); }) ); diff --git a/api/src/middleware/response-manager.ts b/api/src/middleware/response-manager.ts index a95a4f037e..0e6d8df2cb 100644 --- a/api/src/middleware/response-manager.ts +++ b/api/src/middleware/response-manager.ts @@ -33,7 +33,7 @@ const responseManager: RequestHandler = asyncHandler(async (req, res, next) => { const fieldsOut = Object.keys(exportData); const csv = await json2csv.parse(exportData, fieldsOut); - + // will this be ok for larger files? res.setHeader('Content-disposition', 'attachment; filename=export.csv'); res.set('Content-Type', 'text/csv'); res.status(200).send(csv);