Skip to content

Commit

Permalink
Fix the typings
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Jan 29, 2025
1 parent 6314b4b commit b0c0aa8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 117 deletions.
6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import gulp from "gulp";
import {spawn} from "node:child_process";
import {readdir, rm} from "node:fs/promises";
import {cp, readdir, readFile, rm, writeFile} from "node:fs/promises";
import {EOL} from "node:os";
import {join} from "node:path";
import {env} from "node:process";
import pkg from "./package.json" with {type: "json"};

/** Builds the project. */
export async function build() {
await npx("tsc", "--build", "src/tsconfig.json");
await cp("src/types.d.ts", "lib/types.d.ts");
const types = await readFile("lib/index.d.ts", "utf8");
await writeFile("lib/index.d.ts", types.replace("//# sourceMappingURL", `import "./types.js";${EOL}//# sourceMappingURL`));
}

/** Deletes all generated files. */
Expand Down
35 changes: 31 additions & 4 deletions src/eta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Eta} from "eta";
import type {default as Koa, Context} from "koa";
import type {PdfOptions, RendererOptions, RenderingOptions} from "./options.js";
import {htmlToPdf} from "./playwright.js";
import {Eta, type EtaConfig} from "eta";
import type {Context, default as Koa} from "koa";
import type {LaunchOptions} from "playwright-core";
import {htmlToPdf, type PdfOptions} from "./playwright.js";

/**
* Attaches a view renderer to the context of the specified application.
Expand Down Expand Up @@ -58,3 +58,30 @@ export function eta(application: Koa, rendererOptions: RendererOptions = {}): Et

return renderer;
}

/**
* Defines the renderer options.
*/
export type RendererOptions = Partial<EtaConfig & {

/**
* The launch options for the browser used to render PDF documents.
*/
browser: LaunchOptions;
}>;

/**
* Defines the rendering options.
*/
export type RenderingOptions = Partial<{

/**
* Value indicating whether the template is asynchronous.
*/
async: boolean;

/**
* Value indicating whether to write the rendering result to the response.
*/
writeResponse: boolean;
}>;
110 changes: 0 additions & 110 deletions src/options.ts

This file was deleted.

82 changes: 81 additions & 1 deletion src/playwright.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Buffer} from "node:buffer";
import {chromium} from "playwright";
import type {LaunchOptions} from "playwright-core";
import type {PdfOptions} from "./options.js";

/**
* Converts the specified HTML code into a PDF document.
Expand All @@ -17,3 +16,84 @@ export async function htmlToPdf(html: string, options: {browser?: LaunchOptions,
await browser.close();
return Buffer.from(pdf);
}

/**
* Defines the PDF rendering options.
*/
export type PdfOptions = Partial<{

/**
* Value indicating whether to display the header and footer.
*/
displayHeaderFooter: boolean;

/**
* The HTML template for the print footer.
*/
footerTemplate: string;

/**
* The paper format.
*/
format: string;

/**
* The HTML template for the print header.
*/
headerTemplate: string;

/**
* The paper height.
*/
height: number|string;

/**
* Value indicating the landscape orientation.
*/
landscape: boolean;

/**
* The paper margins.
*/
margin: {bottom?: number|string, left?: number|string, right?: number|string, top?: number|string};

/**
* Value indicating wether to embed the document outline into the PDF.
*/
outline: boolean;

/**
* The paper ranges to print.
*/
pageRanges: string;

/**
* The file path to save the PDF to.
*/
path: string;

/**
* Value indicating whether to give prority to any CSS `@page` size declared in the page.
*/
preferCSSPageSize: boolean;

/**
* Value indicating whether to print the background graphics.
*/
printBackground: boolean;

/**
* The scale of the webpage rendering.
*/
scale: number;

/**
* Value indicating whether to generate tagged (accessible) PDF.
*/
tagged: boolean;

/**
* The paper width.
*/
width: number|string;
}>;
3 changes: 2 additions & 1 deletion src/type.d.ts → src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {PdfOptions, RenderingOptions} from "./options.ts";
import type {RenderingOptions} from "./eta.ts";
import type {PdfOptions} from "./playwright.ts";

/**
* Declaration merging.
Expand Down

0 comments on commit b0c0aa8

Please sign in to comment.