mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Merge responsemanager with respond middleware
This commit is contained in:
@@ -3,12 +3,29 @@ import asyncHandler from "express-async-handler";
|
||||
import env from "../env";
|
||||
import { getCacheKey } from "../utils/get-cache-key";
|
||||
import cache from '../cache';
|
||||
import { Transform, transforms } from 'json2csv';
|
||||
import { PassThrough } from "stream";
|
||||
|
||||
export const respond: RequestHandler = asyncHandler(async (req, res) => {
|
||||
if (req.method.toLowerCase() === 'get' && env.CACHE_ENABLED === true && cache) {
|
||||
if (req.method.toLowerCase() === 'get' && env.CACHE_ENABLED === true && cache && !req.sanitizedQuery.export) {
|
||||
const key = getCacheKey(req);
|
||||
await cache.set(key, res.locals.payload);
|
||||
}
|
||||
|
||||
if (req.sanitizedQuery.export === 'json') {
|
||||
res.attachment('export.json');
|
||||
res.set('Content-Type', 'application/json');
|
||||
return res.status(200).send(JSON.stringify(res.locals.payload, null, '\t'));
|
||||
}
|
||||
|
||||
if (req.sanitizedQuery.export === 'csv') {
|
||||
res.attachment('export.csv');
|
||||
res.set('Content-Type', 'text/csv');
|
||||
const stream = new PassThrough();
|
||||
stream.end(Buffer.from(JSON.stringify(res.locals.payload.data), 'utf-8'));
|
||||
const json2csv = new Transform({ transforms: [transforms.flatten({ separator: '.' })] });
|
||||
return stream.pipe(json2csv).pipe(res);
|
||||
}
|
||||
|
||||
return res.json(res.locals.payload);
|
||||
});
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
/**
|
||||
* middleware to manage actions on responses such as
|
||||
* export / import and caching
|
||||
* @todo move set caching into here.
|
||||
* @todo error catching for
|
||||
*
|
||||
*/
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import { ExportFailedException } from '../exceptions';
|
||||
|
||||
const responseManager: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
if (!req.query.export) {
|
||||
res.json(res.locals.data);
|
||||
}
|
||||
// only want to export out on get
|
||||
if (req.method == 'GET') {
|
||||
const exportType = req.query.export;
|
||||
|
||||
if (exportType == 'json') {
|
||||
// have chosen to export json
|
||||
res.setHeader('Content-disposition', 'attachment; filename=export.json');
|
||||
res.set('Content-Type', 'text/json');
|
||||
res.status(200).send(res.locals.data);
|
||||
}
|
||||
|
||||
if (exportType == 'csv') {
|
||||
// have chosen to export csv
|
||||
const { Parser } = require('json2csv');
|
||||
|
||||
const exportData = res.locals.data.data;
|
||||
|
||||
const json2csvParser = new Parser();
|
||||
try {
|
||||
const csv = await json2csvParser.parse(exportData);
|
||||
// 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);
|
||||
} catch (err) {
|
||||
throw new ExportFailedException('CSV parse failed.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
export default responseManager;
|
||||
@@ -56,6 +56,10 @@ const sanitizeQuery: RequestHandler = (req, res, next) => {
|
||||
query.search = req.query.search;
|
||||
}
|
||||
|
||||
if (req.query.export && typeof req.query.export === 'string' && ['json', 'csv'].includes(req.query.export)) {
|
||||
query.export = req.query.export as 'json' | 'csv';
|
||||
}
|
||||
|
||||
req.sanitizedQuery = query;
|
||||
Object.freeze(req.sanitizedQuery);
|
||||
return next();
|
||||
|
||||
Reference in New Issue
Block a user