forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
189 lines (154 loc) · 6.41 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
// Type definitions for nunjucks 3.2
// Project: http://mozilla.github.io/nunjucks/, https://github.com/mozilla/nunjucks
// Definitions by: Matthew Burstein <https://github.com/MatthewBurstein>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
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 function render(name: string, context?: object): string;
export function render(name: string, context?: object, callback?: TemplateCallback<string>): void;
export function renderString(src: string, context: object): string;
export function renderString(src: string, context: object, callback?: TemplateCallback<string>): void;
export function compile(src: string, env?: Environment, callback?: TemplateCallback<Template>): Template;
export function precompile(path: string, opts?: PrecompileOptions): string;
export function precompileString(src: string, opts?: PrecompileOptions): string;
export interface PrecompileOptions {
name?: string | undefined;
asFunction?: boolean | undefined;
force?: boolean | undefined;
env?: Environment | undefined;
include?: string[] | undefined;
exclude?: string[] | undefined;
wrapper?(templates: { name: string; template: string }, opts: PrecompileOptions): string;
}
export class Template {
constructor(src: string, env?: Environment, path?: string, eagerCompile?: boolean);
render(context?: object): string;
render(context?: object, callback?: TemplateCallback<string>): void;
}
export function configure(options: ConfigureOptions): Environment;
export function configure(path: string | string[], options?: ConfigureOptions): Environment;
export interface ConfigureOptions {
autoescape?: boolean | undefined;
throwOnUndefined?: boolean | undefined;
trimBlocks?: boolean | undefined;
lstripBlocks?: boolean | undefined;
watch?: boolean | undefined;
noCache?: boolean | undefined;
dev?: boolean | undefined;
web?:
| {
useCache?: boolean | undefined;
async?: boolean | undefined;
}
| undefined;
express?: object | undefined;
tags?:
| {
blockStart?: string | undefined;
blockEnd?: string | undefined;
variableStart?: string | undefined;
variableEnd?: string | undefined;
commentStart?: string | undefined;
commentEnd?: string | undefined;
}
| undefined;
}
export class Environment {
options: {
autoescape: boolean;
};
constructor(loader?: ILoader | ILoader[] | null, opts?: ConfigureOptions);
render(name: string, context?: object): string;
render(name: string, context?: object, callback?: TemplateCallback<string>): void;
renderString(name: string, context: object): string;
renderString(name: string, context: object, 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: object): void;
on(
event: 'load',
fn: (name: string, source: { src: string; path: string; noCache: boolean }, loader: Loader) => void,
): void;
}
export interface Extension {
tags: string[];
// Parser API is undocumented it is suggested to check the source: https://github.com/mozilla/nunjucks/blob/master/src/parser.js
parse(parser: any, nodes: any, lexer: any): any;
}
export function installJinjaCompat(): void;
export interface ILoader {
async?: boolean | undefined;
getSource(name: string): LoaderSource;
getSource(name: string, callback: Callback<Error, LoaderSource>): void;
}
// 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 interface LoaderSource {
src: string;
path: string;
noCache: boolean;
}
export interface LoaderOptions {
/** if true, the system will automatically update templates when they are changed on the filesystem */
watch?: boolean;
/** if true, the system will avoid using a cache and templates will be recompiled every single time */
noCache?: boolean;
}
export type FileSystemLoaderOptions = LoaderOptions;
export type NodeResolveLoaderOptions = LoaderOptions;
export class FileSystemLoader extends Loader implements ILoader {
constructor(searchPaths?: string | string[], opts?: FileSystemLoaderOptions);
getSource(name: string): LoaderSource;
}
export class NodeResolveLoader extends Loader implements ILoader {
constructor(searchPaths?: string | string[], opts?: NodeResolveLoaderOptions);
getSource(name: string): LoaderSource;
}
export interface WebLoaderOptions {
useCache?: boolean;
async?: boolean;
}
export class WebLoader extends Loader implements ILoader {
constructor(baseUrl?: string, opts?: WebLoaderOptions);
getSource(name: string): LoaderSource;
}
export class PrecompiledLoader extends Loader implements ILoader {
constructor(compiledTemplates?: any[]);
getSource(name: string): LoaderSource;
}
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 | undefined;
lineno: number;
colno: number;
}
}