Skip to content

Commit

Permalink
refactor: Improve reuseability of export-config.controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jrassa committed Aug 8, 2024
1 parent b241a12 commit 40c30c1
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/app/core/export/export-config.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,7 @@ export const exportCSV = (
data: Array<unknown> | 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));
}
};

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -167,7 +168,7 @@ const buildExportStream = (
* @param contentType
* @param stream
*/
const exportStream = (
export const exportStream = (
req,
res,
fileName: string,
Expand Down

0 comments on commit 40c30c1

Please sign in to comment.