-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
194 lines (172 loc) · 5.03 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { LiquidOptions } from "liquidjs/dist/liquid-options";
import { PDFOptions } from "puppeteer";
export interface BaseArgs {
/**
* The path to the YAML or JSON data file.
*/
data: string;
/**
* The path to the template file.
*/
template: string;
/**
* The path to the PDF output file.
*/
output: string;
}
/**
* The arguments passed to the CLI.
*/
export interface PMEArgs extends BaseArgs {
/**
* The path to the MJS config file with a default export that conforms to
* {@link PMEConfig}.
*/
config: string;
}
/**
* The CLI config file settings which are passed down to {@link Builder.build},
* {@link build}, and {@link develop} through {@link CommandArgs.options}.
*/
export interface PMEConfig<T = LiquidOptions> {
/**
* The options passed to {@link CommandArgs.getTemplateRenderer}.
*/
templateOptions?: T;
/**
* The options passed to {@link PDFRenderer.render}.
*/
pdfOptions?: PDFOptions;
/**
* Sets up and returns a function that renders HTML from a template string and
* data object.
*/
getTemplateRenderer?(options?: T): RenderTemplate;
}
/**
* The arguments passed to {@link Builder.build}, {@link build}, and
* {@link develop}.
*/
export interface CommandArgs<T = LiquidOptions> extends BaseArgs {
/**
* The options passed down to the template and PDF renderers.
*/
options?: PMEConfig<T>;
}
/**
* Instantiates resources needed for PDF generation, then watches data and
* template files for changes and outputs a PDF file on change.
*
* @param rootDir Defaults to `process.cwd()`. The directory watched for changes
* to trigger re-emmiting the PDF file on change. Prepended to the relative
* paths passed to `args` to find their absolute paths.
* @returns A cleanup function that disposes all resources instantiated.
*/
export function develop<T = LiquidOptions>(
args: CommandArgs<T>,
rootDir?: string
): Promise<() => Promise<void>>;
/**
* Instantiates resources needed for PDF generation, outputs a PDF file using
* data and template files, then disposes all resources instantiated.
*
* @param rootDir Defaults to `process.cwd()`. Prepended to the relative paths
* passed to `args` to find their absolute paths.
*/
export function build<T = LiquidOptions>(
args: CommandArgs<T>,
rootDir?: string
): Promise<void>;
/**
* Contains methods for rendering PDF files from data and template files.
*/
export interface Builder {
/**
* Outputs a PDF file using data and template files.
*
* @param rootDir Defaults to `process.cwd()`. Prepended to the relative paths
* passed to `args` to find their absolute paths.
*/
build<T = LiquidOptions>(
args: CommandArgs<T>,
rootDir?: string
): Promise<void>;
/**
* Disposes all resources instantiated by {@link getBuilder} and turns
* {@link Builder.build} and {@link Builder.close} into dummy methods.
*/
close(): Promise<void>;
}
/**
* Instantiates resources needed for PDF generation then returns a
* {@link Builder} object.
*/
export function getBuilder(): Promise<Builder>;
/**
* Reads the contents of the YAML, JSON, JSONC, or JSON5 file in `path` then
* returns its JavaScript representation.
*/
export function getData(path: string): Promise<any>;
/**
* Parses `data` then returns its JavaScript representation.
*
* @param type The type of the data to be parsed.
*/
export function parseData(data: string, type?: "yaml" | "json"): any;
/**
* @param template The template to render.
* @param data The data to populate the template with.
* @returns The rendered template.
*/
export interface RenderTemplate {
(template: string, data?: object): string | Promise<string>;
}
/**
* Renders HTML from the template found in `path` using the `render` function
* and `data` object passed.
*
* Due to this function's signature, it can theoretically render any format.
* However, it is intended to only render HTML in this context.
*
* @returns The rendered HTML.
*/
export function renderHTML(
path: string,
render: RenderTemplate,
data?: object
): Promise<string>;
/**
* Encodes `html` into a Data URL.
*
* @returns The HTML Data URL.
*/
export function encodeHTML(html: string): string;
/**
* Contains methods for rendering PDF files from URLs.
*/
export interface PDFRenderer {
/**
* Renders a PDF from the HTML found in `url`.
*
* @returns The rendered PDF.
*/
render(url: string, options?: PDFOptions): Promise<Buffer>;
/**
* Disposes all resources instantiated by {@link getPDFRenderer} and turns
* {@link PDFRenderer.render} and {@link PDFRenderer.close} into dummy
* methods.
*/
close(): Promise<void>;
}
/**
* Instantiates resources needed for PDF generation then returns a
* {@link PDFRenderer} object.
*/
export function getPDFRenderer(): Promise<PDFRenderer>;
/**
* Absolutizes `path` by joining it to `rootDir` then normalizes the output.
* Returns a normalized `path` if it is already an absolute path.
*
* @param rootDir Defaults to `process.cwd()`.
*/
export function absolutizePath(path: string, rootDir?: string): string;