UNPKG

express-ts-skeleton

Version:

This is a skeleton(boiler plate) for nodejs, express and typescript.

77 lines (76 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.responseHelper = void 0; const fs_1 = require("fs"); const response_1 = require("../core/response"); class responseHelper { constructor() { } /** * Handles file download by streaming a file to the response and optionally * deletes the file after the download completes. * @param {{ filePath: PathLike; fileName: string; willDelete: boolean }} result - An object containing file information: * - `filePath` (PathLike): The path to the file to be downloaded. * - `fileName` (string): The name that will be set for the downloaded file. * - `willDelete` (boolean): A flag indicating if the file should be deleted after download. * @param {Response} res - The Express response object used to send the file to the client. * @returns {void} Sends the file in the response, sets headers, and handles any errors that occur during access, streaming, or deletion. * @fires Response#finish - Triggered when the response is finished, indicating download completion. * @fires Response#close - Triggered when the connection is closed, which optionally deletes the file if `willDelete` is true. */ download(result, res) { (0, fs_1.access)(result.filePath, fs_1.constants.F_OK, (err) => { if (err) { console.error(`File not found: ${result.filePath}`); const errorHandling = response_1.FailureResponse.failure({ error: "Unable to find file", message: "file found failed", }); return res.status(404).send(errorHandling); } res.setHeader("Content-Disposition", `attachment; filename="${result.fileName}"`); res.setHeader("Content-Type", "application/octet-stream"); const stream = (0, fs_1.createReadStream)(result.filePath); stream.on("error", (err) => { console.error(`Stream error: ${err.message}`); res.status(500).send({ message: "Error while streaming file" }); }); stream.pipe(res); res.on("finish", () => { console.info(`File download complete: ${result.filePath}`); }); res.on("close", () => { if (result.willDelete) (0, fs_1.unlink)(result.filePath, (err) => { if (err) { console.error(`Error deleting file: ${err.message}`); } else { console.info(`${result.filePath} deleted!`); } }); }); }); } /** * Renders a view with the specified content. * @param {{ view: string; content: object }} result - An object containing: * - `view` (string): The name of the view template to render. * - `content` (object): The data to pass to the view for rendering. * @param {Response} res - The Express response object used to render the view. * @returns {void} Sends the rendered view in the response. */ render(result, res) { res.render(result.view, result.content); } /** * Sends a JSON response with a specified status code. * @param {{ statusCode: number }} result - An object containing: * - `statusCode` (number): The HTTP status code to set for the response. * @param {Response} res - The Express response object used to send the JSON response. * @returns {void} Sends the JSON response with the specified status code. */ json(result, res) { res.status(result.statusCode).json(result); } } exports.responseHelper = responseHelper;