From 40c30c11adcc42f2eae7baf7346b6d63bb101d21 Mon Sep 17 00:00:00 2001 From: John Rassa Date: Thu, 8 Aug 2024 09:17:42 -0400 Subject: [PATCH] refactor: Improve reuseability of export-config.controller --- .../core/export/export-config.controller.ts | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/app/core/export/export-config.controller.ts b/src/app/core/export/export-config.controller.ts index 0db623d7..ae3fab8f 100644 --- a/src/app/core/export/export-config.controller.ts +++ b/src/app/core/export/export-config.controller.ts @@ -49,24 +49,7 @@ export const exportCSV = ( data: Array | Readable ) => { if (null !== data) { - exportStream( - req, - res, - filename, - 'text/csv', - buildExportStream( - data, - (stream) => () => { - if (Array.isArray(data)) { - data.forEach((row) => { - stream.push(row); - }); - } - stream.push(null); - }, - [csvStream.streamToCsv(columns)] - ) - ); + exportStream(req, res, filename, 'text/csv', buildCSVStream(data, columns)); } }; @@ -94,6 +77,24 @@ export const exportPlaintext = (req, res, filename: string, text: string) => { } }; +export const buildCSVStream = ( + data: Readable | unknown, + columns: ExportColumnDef[] +) => { + return buildExportStream( + data, + (stream) => () => { + if (Array.isArray(data)) { + data.forEach((row) => { + stream.push(row); + }); + } + stream.push(null); + }, + [csvStream.streamToCsv(columns)] + ); +}; + /** * Build a readable stream from data and pipe through a chain of transform streams * @@ -167,7 +168,7 @@ const buildExportStream = ( * @param contentType * @param stream */ -const exportStream = ( +export const exportStream = ( req, res, fileName: string,