file-streamer
Version:
A TypeScript package for streaming data as files (Excel, CSV, JSON, Base64) and streaming files from file system through HTTP responses
33 lines • 1.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.pipeJsonAsJson = void 0;
const pipeJsonAsJson = async (res, data, fileName) => {
try {
let responseFileName = !fileName
? 'data.json'
: fileName.endsWith('.json')
? fileName
: `${fileName}.json`;
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Disposition', `attachment; filename=${responseFileName}`);
res.write('[\n');
data.forEach((item, index) => {
const isLast = index === data.length - 1;
res.write(JSON.stringify(item, null, 2));
if (!isLast) {
res.write(',\n');
}
else {
res.write('\n');
}
});
res.write(']\n');
res.end();
}
catch (err) {
console.error(err);
throw err;
}
};
exports.pipeJsonAsJson = pipeJsonAsJson;
//# sourceMappingURL=json.js.map
;