This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.d.ts
262 lines (221 loc) · 6.3 KB
/
mod.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
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
/**
* Forked from
* https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/nunjucks
*/
declare namespace Nunjucks {
export type TemplateCallback<T> = (
err: lib.TemplateError | null,
res: T | null,
) => void;
export type Callback<E, T> = (err: E | null, res: T | null) => void;
export interface Extension {
tags: string[];
// Parser API is undocumented. It is suggested to check the source:
parse(parser: any, nodes: any, lexer: any): any;
// https://github.com/mozilla/nunjucks/blob/master/nunjucks/src/parser.js
}
export class Environment {
options: {
autoescape: boolean;
};
constructor(loader?: ILoader | ILoader[] | null, opts?: ConfigureOptions);
render(name: string, context?: Record<string, unknown>): string;
render(
name: string,
context?: Record<string, unknown>,
callback?: TemplateCallback<string>,
): void;
renderString(name: string, context: Record<string, unknown>): string;
renderString(
name: string,
context: Record<string, unknown>,
callback?: TemplateCallback<string>,
): void;
addFilter(
name: string,
func: (...args: any[]) => any,
async?: boolean,
): Environment;
getFilter(name: string): (...args: any[]) => any;
addExtension(name: string, ext: Extension): Environment;
removeExtension(name: string): void;
getExtension(name: string): Extension;
hasExtension(name: string): boolean;
addGlobal(name: string, value: any): Environment;
getGlobal(name: string): any;
getTemplate(name: string, eagerCompile?: boolean): Template;
getTemplate(
name: string,
eagerCompile?: boolean,
callback?: Callback<Error, Template>,
): void;
express(app: Record<string, unknown>): void;
on(
event: "load",
fn: (
name: string,
source: { src: string; path: string; noCache: boolean },
loader: Loader,
) => void,
): void;
}
export class Template {
constructor(
src: string,
env?: Environment,
path?: string,
eagerCompile?: boolean,
);
render(context?: Record<string, unknown>): string;
render(
context?: Record<string, unknown>,
callback?: TemplateCallback<string>,
): void;
}
export interface ILoader {
async?: boolean;
getSource(name: string): LoaderSource;
getSource(name: string, callback: Callback<Error, LoaderSource>): void;
}
export interface LoaderSource {
src: string;
path: string;
noCache: boolean;
}
export interface LoaderOptions {
/**
* If true, the system will avoid using a cache
* and templates will be recompiled every single time
*/
noCache?: boolean;
}
// Needs both Loader and ILoader since nunjucks uses a custom object system
// Object system is also responsible for the extend methods
export class Loader {
on(name: string, func: (...args: any[]) => any): void;
emit(name: string, ...args: any[]): void;
resolve(from: string, to: string): string;
isRelative(filename: string): boolean;
static extend<LoaderClass extends typeof Loader>(
this: LoaderClass,
toExtend: ILoader,
): LoaderClass;
}
export type FileSystemLoaderOptions = LoaderOptions;
export class FileSystemLoader extends Loader implements ILoader {
constructor(
searchPaths?: string | string[],
opts?: FileSystemLoaderOptions,
);
getSource(name: string): LoaderSource;
}
export type NodeResolveLoaderOptions = LoaderOptions;
export class NodeResolveLoader extends Loader implements ILoader {
constructor(
searchPaths?: string | string[],
opts?: NodeResolveLoaderOptions,
);
getSource(name: string): LoaderSource;
}
export class PrecompiledLoader extends Loader implements ILoader {
constructor(compiledTemplates?: any[]);
getSource(name: string): LoaderSource;
}
// export namespace compiler {
// // TODO
// }
// export namespace parser {
// // TODO
// }
// export namespace lexer {
// // TODO
// }
export namespace runtime {
class SafeString {
constructor(val: string);
val: string;
length: number;
valueOf(): string;
toString(): string;
}
}
export namespace lib {
class TemplateError extends Error {
constructor(message: string, lineno: number, colno: number);
name: string; // always 'Template render error'
message: string;
stack: string;
cause?: Error;
lineno: number;
colno: number;
}
}
// export namespace nodes {
// // TODO
// }
export function installJinjaCompat(): void;
export interface ConfigureOptions {
autoescape?: boolean;
throwOnUndefined?: boolean;
trimBlocks?: boolean;
lstripBlocks?: boolean;
noCache?: boolean;
express?: Record<string, unknown>;
tags?: {
blockStart?: string;
blockEnd?: string;
variableStart?: string;
variableEnd?: string;
commentStart?: string;
commentEnd?: string;
};
}
export function configure(options: ConfigureOptions): Environment;
export function configure(
path: string | string[],
options?: ConfigureOptions,
): Environment;
export function reset(): void;
export function compile(
message: string,
lineno: number,
colno: number,
eagerCompile?: boolean,
): Template;
export function render(
name: string,
context?: Record<string, unknown>,
): string;
export function render(
name: string,
context?: Record<string, unknown>,
callback?: TemplateCallback<string>,
): void;
export function renderString(
src: string,
context: Record<string, unknown>,
): string;
export function renderString(
src: string,
context: Record<string, unknown>,
callback?: TemplateCallback<string>,
): void;
export interface PrecompileOptions {
name?: string;
asFunction?: boolean;
force?: boolean;
env?: Environment;
include?: string[];
exclude?: string[];
wrapper?(
templates: { name: string; template: string },
opts: PrecompileOptions,
): string;
}
export function precompile(path: string, opts?: PrecompileOptions): string;
export function precompileString(
src: string,
opts?: PrecompileOptions,
): string;
}
export default Nunjucks;