Bind the dependencies
import { UtilitiesDependencies } from '@digitowl/backend-utilities';
const newContainer = new Container();
const registrationList = [
// ...Other dependencies
UtilitiesDependencies,
];
for (const reg of registrationList) {
new reg().register(newContainer);
}
export const container = newContainer;
Bind the dependency types
import { UtilitiesDependencyTypes } from '@digitowl/backend-utilities';
export const types = {
// ...Other dependency types
...UtilitiesDependencyTypes
}
Map errors
export class ExceptionToFailure {
public failureMap = new Map<string, Map<string, boolean>>([
[CoreFailureEnums.UPSTREAM_FAILURES, new Map<string, boolean>([
...CoreExceptionToFailure.failureMap.get(CoreFailureEnums.UPSTREAM_FAILURES),
...UtilityExceptionToFailure.failureMap.get(UtilityFailureEnums.UPSTREAM_FAILURES)
])],
// ...Others failure mapping
]);
// ...Converter function
}
generateFile
The generateFile method is responsible for converting a given JSON data structure into a CSV formatted string, and then returning the resulting CSV content as a Buffer. The method uses a json2csv parser service that handles the parsing operation asynchronously
Example
const jsonData = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 }
];
// Read more: https://github.com/juanjoDiaz/json2csv/blob/main/packages/node/README.md#options-1
const parseOptions = {
delimiter: ",",
includeHeader: true,
quoteChar: '"'
};
const csvBuffer = await jsonToCSVParserOperation.generateFile(jsonData, parseOptions);
// Do something with the buffer
The JSZipFileCompressOperation class provides methods to create and manage ZIP files by adding files from external sources or in-memory buffers and generating the final compressed file. It leverages JSZip for ZIP file operations and uses fetch for downloading external resources.
addFileFromExternalSourceToZip
Adds a file from an external source to the ZIP file.
addFileFromBufferToZip
Adds a file from an in-memory buffer to the ZIP file.
generateFile
Generates the ZIP file and returns its content as a Node.js Buffer.
Example
...
@inject(types.iJSZipFileCompressOperation)
private zipFileCompressOperation: iJSZipFileCompressOperation,
await zipFileCompressOperation.addFileFromExternalSourceToZip({
sourceExternalLink: "https://example.com/file.txt",
destPathInZipFile: "folder/file.txt",
})
const fileBuffer = Buffer.from("Hello, world!");
await zipFileCompressOperation. .addFileFromBufferToZip({
sourceFile: fileBuffer,
destPathInZipFile: "hello.txt",
})
const zipBuffer = await this.zipFileCompressOperation.generateFile();
// Do something with the zip buffer
The PuppeteerHeadlessBrowserOperation class provides functionality to generate a PDF file from an EJS template using Puppeteer, a headless browser automation library. This class is designed to handle the entire flow from rendering an EJS template to creating a PDF document.
generatePDFFromEJSTemplate
Generates a PDF from an EJS template with the provided data and PDF options.
Example
@inject(types.iPuppeteerHeadlessBrowserOperation)
private puppeteerHeadlessBrowserOperation: iPuppeteerHeadlessBrowserOperation,
const templatePath = "path/to/template.ejs";
const data = { title: "Sample Title", content: "Sample Content" };
const pdfOptions = { format: "A4", printBackground: true };
await this.puppeteerHeadlessBrowserOperation.generatePDFFromEJSTemplate(
templatePath,
data,
pdfOptions
)