Merge responsemanager with respond middleware

This commit is contained in:
rijkvanzanten
2020-09-09 09:17:37 -04:00
parent 8fd31c7353
commit d781c2b0e5
9 changed files with 55 additions and 59 deletions

View File

@@ -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);
});

View File

@@ -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;

View File

@@ -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();