index.d.ts
13.2 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Type definitions for ejs 3.0
// Project: http://ejs.co/, https://github.com/mde/ejs
// Definitions by: Ben Liddicott <https://github.com/benliddicott>
// ExE Boss <https://github.com/ExE-Boss>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
export as namespace ejs;
/**
* Version of EJS.
*/
export const VERSION: string;
/**
* Name for detection of EJS.
*/
export const name: 'ejs';
/**
* Get the path to the included file from the parent file path and the
* specified path.
*
* @param name specified path
* @param filename parent file path
* @param isDir whether the parent file path is a directory
*/
export function resolveInclude(name: string, filename: string, isDir?: boolean): string;
/**
* Compile the given `str` of ejs into a template function.
*/
export function compile(template: string, opts: Options & { async: true; client?: false }): AsyncTemplateFunction;
export function compile(template: string, opts: Options & { async: true; client: true }): AsyncClientFunction;
export function compile(template: string, opts?: Options & { async?: false; client?: false }): TemplateFunction;
export function compile(template: string, opts?: Options & { async?: false; client: true }): ClientFunction;
export function compile(template: string, opts?: Options): TemplateFunction | AsyncTemplateFunction;
/**
* Render the given `template` of ejs.
*
* If you would like to include options but not data, you need to explicitly
* call this function with `data` being an empty object or `null`.
*/
export function render(template: string, data?: Data, opts?: Options & { async: false }): string;
export function render(template: string, data: Data | undefined, opts: Options & { async: true }): Promise<string>;
export function render(template: string, data: Data | undefined, opts: Options & { async?: never }): string;
export function render(template: string, data?: Data, opts?: Options): string | Promise<string>;
/**
* Callback for receiving data from `renderFile`.
*
* @param err error, if any resulted from the rendering process
* @param str output string, is `undefined` if there is an error
*/
export type RenderFileCallback<T> = (err: Error | null, str: string) => T;
/**
* Render an EJS file at the given `path` and callback `cb(err, str)`.
*
* If you would like to include options but not data, you need to explicitly
* call this function with `data` being an empty object or `null`.
*/
export function renderFile<T>(path: string, cb: RenderFileCallback<T>): T;
export function renderFile<T>(path: string, data: Data, cb: RenderFileCallback<T>): T;
export function renderFile<T>(path: string, data: Data, opts: Options, cb: RenderFileCallback<T>): T;
export function renderFile(path: string, data?: Data, opts?: Options): Promise<string>;
/**
* Clear intermediate JavaScript cache. Calls {@link Cache#reset}.
*/
export function clearCache(): void;
/**
* EJS template function cache. This can be a LRU object from lru-cache
* NPM module. By default, it is `utils.cache`, a simple in-process
* cache that grows continuously.
*/
export let cache: Cache;
/**
* Custom file loader. Useful for template preprocessing or restricting access
* to a certain part of the filesystem.
*
* @param path the path of the file to be read
* @return the contents of the file as a string or object that implements the toString() method
*
* @default fs.readFileSync
*/
export type fileLoader = (path: string) => string | { toString(): string };
export let fileLoader: fileLoader;
/**
* Name of the object containing the locals.
*
* This variable is overridden by {@link Options}`.localsName` if it is not
* `undefined`.
*
* @default 'locals'
*/
export let localsName: string;
/**
* The opening delimiter for all statements. This allows you to clearly delinate
* the difference between template code and existing delimiters. (It is recommended
* to synchronize this with the closeDelimiter property.)
*
* @default '<'
*/
export let openDelimiter: string;
/**
* The closing delimiter for all statements. This allows to to clearly delinate
* the difference between template code and existing delimiters. (It is recommended
* to synchronize this with the openDelimiter property.)
*
* @default '>'
*/
export let closeDelimiter: string;
/**
* The delimiter used in template compilation.
*
* @default '%'
*/
export let delimiter: string | undefined;
/**
* Promise implementation -- defaults to the native implementation if available
* This is mostly just for testability
*
* @default Promise
*/
export let promiseImpl: PromiseConstructorLike | undefined;
/**
* Escape characters reserved in XML.
*
* This is simply an export of `utils.escapeXML`.
*
* If `markup` is `undefined` or `null`, the empty string is returned.
*/
export function escapeXML(markup?: any): string;
export class Template {
/**
* The EJS template source text.
*/
readonly templateText: string;
/**
* The compiled JavaScript function source, or the empty string
* if the template hasn't been compiled yet.
*/
readonly source: string;
constructor(text: string, opts?: Options);
/**
* Compiles the EJS template.
*/
compile(): TemplateFunction | AsyncTemplateFunction | ClientFunction | AsyncClientFunction;
}
export namespace Template {
enum modes {
EVAL = 'eval',
ESCAPED = 'escaped',
RAW = 'raw',
COMMENT = 'comment',
LITERAL = 'literal',
}
}
export interface Data {
[name: string]: any;
}
/**
* This type of function is returned from `compile`, when
* `Options.client` is false.
*
* @param data an object of data to be passed into the template.
* @return Return type depends on `Options.async`.
*/
export type TemplateFunction = (data?: Data) => string;
/**
* This type of function is returned from `compile`, when
* `Options.client` is false.
*
* @param data an object of data to be passed into the template.
* @return Return type depends on `Options.async`.
*/
export type AsyncTemplateFunction = (data?: Data) => Promise<string>;
/**
* This type of function is returned from `compile`, when
* `Options.client` is true.
*
*
* This is also used internally to generate a `TemplateFunction`.
*
* @param locals an object of data to be passed into the template.
* The name of this variable is adjustable through `localsName`.
*
* @param escape callback used to escape variables
* @param include callback used to include files at runtime with `include()`
* @param rethrow callback used to handle and rethrow errors
*
* @return Return type depends on `Options.async`.
*/
export type ClientFunction = (
locals?: Data,
escape?: EscapeCallback,
include?: IncludeCallback,
rethrow?: RethrowCallback,
) => string;
/**
* This type of function is returned from `compile`, when
* `Options.client` is true.
*
*
* This is also used internally to generate a `TemplateFunction`.
*
* @param locals an object of data to be passed into the template.
* The name of this variable is adjustable through `localsName`.
*
* @param escape callback used to escape variables
* @param include callback used to include files at runtime with `include()`
* @param rethrow callback used to handle and rethrow errors
*
* @return Return type depends on `Options.async`.
*/
export type AsyncClientFunction = (
locals?: Data,
escape?: EscapeCallback,
include?: IncludeCallback,
rethrow?: RethrowCallback,
) => Promise<string>;
/**
* Escapes a string using HTML/XML escaping rules.
*
* Returns the empty string for `null` or `undefined`.
*
* @param markup Input string
* @return Escaped string
*/
export type EscapeCallback = (markup?: any) => string;
/**
* This type of callback is used when `Options.compileDebug`
* is `true`, and an error in the template is thrown.
*
* By default it is used to rethrow an error in a better-formatted way.
*
* @param err Error object
* @param str full EJS source
* @param filename file name of the EJS source
* @param lineno line number of the error
*/
export type RethrowCallback = (
err: Error,
str: string,
filename: string | null | undefined,
lineno: number,
esc: EscapeCallback,
) => never;
/**
* The callback called by `ClientFunction` to include files at runtime with `include()`
*
* @param path Path to be included
* @param data Data passed to the template
* @return Contents of the file requested
*/
export type IncludeCallback = (path: string, data?: Data) => string;
export interface Options {
/**
* Log the generated JavaScript source for the EJS template to the console.
*
* @default false
*/
debug?: boolean;
/**
* Include additional runtime debugging information in generated template
* functions.
*
* @default true
*/
compileDebug?: boolean;
/**
* Whether or not to use `with () {}` construct in the generated template
* functions. If set to `false`, data is still accessible through the object
* whose name is specified by `ejs.localsName` (defaults to `locals`).
*
* @default true
*/
_with?: boolean;
/**
* Whether to run in strict mode or not.
* Enforces `_with=false`.
*
* @default false
*/
strict?: boolean;
/**
* An array of local variables that are always destructured from `localsName`,
* available even in strict mode.
*
* @default []
*/
destructuredLocals?: string[];
/**
* Remove all safe-to-remove whitespace, including leading and trailing
* whitespace. It also enables a safer version of `-%>` line slurping for all
* scriptlet tags (it does not strip new lines of tags in the middle of a
* line).
*
* @default false
*/
rmWhitespace?: boolean;
/**
* Whether or not to compile a `ClientFunction` that can be rendered
* in the browser without depending on ejs.js. Otherwise, a `TemplateFunction`
* will be compiled.
*
* @default false
*/
client?: boolean;
/**
* The escaping function used with `<%=` construct. It is used in rendering
* and is `.toString()`ed in the generation of client functions.
*
* @default ejs.escapeXML
*/
escape?: EscapeCallback;
/**
* The filename of the template. Required for inclusion and caching unless
* you are using `renderFile`. Also used for error reporting.
*
* @default undefined
*/
filename?: string;
/**
* The path to the project root. When this is set, absolute paths for includes
* (/filename.ejs) will be relative to the project root.
*
* @default undefined
*/
root?: string;
/**
* The opening delimiter for all statements. This allows you to clearly delinate
* the difference between template code and existing delimiters. (It is recommended
* to synchronize this with the closeDelimiter property.)
*
* @default ejs.openDelimiter
*/
openDelimiter?: string;
/**
* The closing delimiter for all statements. This allows to to clearly delinate
* the difference between template code and existing delimiters. (It is recommended
* to synchronize this with the openDelimiter property.)
*
* @default ejs.closeDelimiter
*/
closeDelimiter?: string;
/**
* Character to use with angle brackets for open/close
* @default '%'
*/
delimiter?: string;
/**
* Whether or not to enable caching of template functions. Beware that
* the options of compilation are not checked as being the same, so
* special handling is required if, for example, you want to cache client
* and regular functions of the same file.
*
* Requires `filename` to be set. Only works with rendering function.
*
* @default false
*/
cache?: boolean;
/**
* The Object to which `this` is set during rendering.
*
* @default this
*/
context?: any;
/**
* Whether or not to create an async function instead of a regular function.
* This requires language support.
*
* @default false
*/
async?: boolean;
/**
* Make sure to set this to 'false' in order to skip UglifyJS parsing,
* when using ES6 features (`const`, etc) as UglifyJS doesn't understand them.
* @default true
*/
beautify?: boolean;
/**
* Name to use for the object storing local variables when not using `with` or destructuring.
*
* @default ejs.localsName
*/
localsName?: string;
/** Set to a string (e.g., 'echo' or 'print') for a function to print output inside scriptlet tags. */
outputFunctionName?: string;
/**
* An array of paths to use when resolving includes with relative paths
*/
views?: string[];
}
export interface Cache {
/**
* Cache the intermediate JavaScript function for a template.
*
* @param key key for caching
* @param val cached function
*/
set(key: string, val: TemplateFunction): void;
/**
* Get the cached intermediate JavaScript function for a template.
*
* @param key key for caching
*/
get(key: string): TemplateFunction | undefined;
/**
* Clear the entire cache.
*/
reset(): void;
}