Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve reuseability of export-config.controller #302

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading