From 3fb8be22b74204accabed8a8c8fb5161c5ad7504 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Feb 2021 11:34:01 +0100 Subject: [PATCH] Added typeing to the buffer and stream conversion ref #3 --- src/PdfCreator.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 8e547b2..2345c8c 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -1,6 +1,8 @@ import fs from "fs"; import pdf_converter from "html-pdf"; import path from 'path'; +import { Stream } from 'stream'; + /** * This class is responsible for all things pdf creation. * This uses the html templates from src/templates. @@ -17,6 +19,10 @@ export class PdfCreator { } +/** + * This class is a wrapper for the pdf objects created by html-pdf. + * It offers typed conversion to Buffer and Stream. + */ export class Pdf { content: any; @@ -27,9 +33,9 @@ export class Pdf { /** * Promise wrapper function that resolves the toBuffer promise for pdf generation. */ - public async toBuffer(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toBuffer(function (err, buffer) { + public async toBuffer(): Promise { + let promise = await new Promise((resolve, reject) => { + this.content.toBuffer(function (err, buffer: Buffer) { resolve(buffer); }); }); @@ -39,9 +45,9 @@ export class Pdf { /** * Promise wrapper function that resolves the toStream promise for pdf generation. */ - public async toStream(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toStream(function (err, stream) { + public async toStream(): Promise { + let promise = await new Promise((resolve, reject) => { + this.content.toStream(function (err, stream: Stream) { resolve(stream); }); });