UNPKG

7.01 kBTypeScriptView Raw
1// Type definitions for nunjucks 3.2
2// Project: http://mozilla.github.io/nunjucks/, https://github.com/mozilla/nunjucks
3// Definitions by: Matthew Burstein <https://github.com/MatthewBurstein>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.2
6
7export type TemplateCallback<T> = (err: lib.TemplateError | null, res: T | null) => void;
8export type Callback<E, T> = (err: E | null, res: T | null) => void;
9
10export function render(name: string, context?: object): string;
11export function render(name: string, context?: object, callback?: TemplateCallback<string>): void;
12
13export function renderString(src: string, context: object): string;
14export function renderString(src: string, context: object, callback?: TemplateCallback<string>): void;
15
16export function compile(src: string, env?: Environment, callback?: TemplateCallback<Template>): Template;
17
18export function precompile(path: string, opts?: PrecompileOptions): string;
19export function precompileString(src: string, opts?: PrecompileOptions): string;
20
21export interface PrecompileOptions {
22 name?: string | undefined;
23 asFunction?: boolean | undefined;
24 force?: boolean | undefined;
25 env?: Environment | undefined;
26 include?: string[] | undefined;
27 exclude?: string[] | undefined;
28 wrapper?(templates: { name: string; template: string }, opts: PrecompileOptions): string;
29}
30
31export class Template {
32 constructor(src: string, env?: Environment, path?: string, eagerCompile?: boolean);
33 render(context?: object): string;
34 render(context?: object, callback?: TemplateCallback<string>): void;
35}
36
37export function configure(options: ConfigureOptions): Environment;
38export function configure(path: string | string[], options?: ConfigureOptions): Environment;
39
40export interface ConfigureOptions {
41 autoescape?: boolean | undefined;
42 throwOnUndefined?: boolean | undefined;
43 trimBlocks?: boolean | undefined;
44 lstripBlocks?: boolean | undefined;
45 watch?: boolean | undefined;
46 noCache?: boolean | undefined;
47 dev?: boolean | undefined;
48 web?:
49 | {
50 useCache?: boolean | undefined;
51 async?: boolean | undefined;
52 }
53 | undefined;
54 express?: object | undefined;
55 tags?:
56 | {
57 blockStart?: string | undefined;
58 blockEnd?: string | undefined;
59 variableStart?: string | undefined;
60 variableEnd?: string | undefined;
61 commentStart?: string | undefined;
62 commentEnd?: string | undefined;
63 }
64 | undefined;
65}
66
67export class Environment {
68 options: {
69 autoescape: boolean;
70 };
71
72 constructor(loader?: ILoaderAny | ILoaderAny[] | null, opts?: ConfigureOptions);
73 render(name: string, context?: object): string;
74 render(name: string, context?: object, callback?: TemplateCallback<string>): void;
75
76 renderString(name: string, context: object): string;
77 renderString(name: string, context: object, callback?: TemplateCallback<string>): void;
78
79 addFilter(name: string, func: (...args: any[]) => any, async?: boolean): Environment;
80 getFilter(name: string): (...args: any[]) => any;
81
82 addExtension(name: string, ext: Extension): Environment;
83 removeExtension(name: string): void;
84 getExtension(name: string): Extension;
85 hasExtension(name: string): boolean;
86
87 addGlobal(name: string, value: any): Environment;
88 getGlobal(name: string): any;
89
90 getTemplate(name: string, eagerCompile?: boolean): Template;
91 getTemplate(name: string, eagerCompile?: boolean, callback?: Callback<Error, Template>): void;
92
93 express(app: object): void;
94
95 on(
96 event: 'load',
97 fn: (name: string, source: { src: string; path: string; noCache: boolean }, loader: Loader) => void,
98 ): void;
99}
100
101export interface Extension {
102 tags: string[];
103 // Parser API is undocumented it is suggested to check the source: https://github.com/mozilla/nunjucks/blob/master/src/parser.js
104 parse(parser: any, nodes: any, lexer: any): any;
105}
106
107export function installJinjaCompat(): void;
108
109/** A synchronous or an asynchronous loader. */
110export type ILoaderAny = ILoader | ILoaderAsync | WebLoader;
111// WebLoader is part of the union because it can be both sync or async depending
112// on its constructor arguments, which possibly could only be known on runtime.
113
114/** A synchronous loader. */
115export interface ILoader {
116 async?: false | undefined;
117
118 getSource: (name: string) => LoaderSource;
119}
120
121/** An asynchronous loader. */
122export interface ILoaderAsync {
123 async: true;
124
125 getSource: (name: string, callback: Callback<Error, LoaderSource>) => void;
126}
127
128// Needs both Loader and ILoader since nunjucks uses a custom object system
129// Object system is also responsible for the extend methods
130export class Loader {
131 on(name: string, func: (...args: any[]) => any): void;
132 emit(name: string, ...args: any[]): void;
133 resolve(from: string, to: string): string;
134 isRelative(filename: string): boolean;
135 static extend<LoaderClass extends typeof Loader>(this: LoaderClass, toExtend: ILoaderAny): LoaderClass;
136}
137
138export interface LoaderSource {
139 src: string;
140 path: string;
141 noCache: boolean;
142}
143
144export interface LoaderOptions {
145 /** if true, the system will automatically update templates when they are changed on the filesystem */
146 watch?: boolean;
147
148 /** if true, the system will avoid using a cache and templates will be recompiled every single time */
149 noCache?: boolean;
150}
151
152export type FileSystemLoaderOptions = LoaderOptions;
153export type NodeResolveLoaderOptions = LoaderOptions;
154
155export class FileSystemLoader extends Loader implements ILoader {
156 constructor(searchPaths?: string | string[], opts?: FileSystemLoaderOptions);
157 getSource(name: string): LoaderSource;
158}
159
160export class NodeResolveLoader extends Loader implements ILoader {
161 constructor(searchPaths?: string | string[], opts?: NodeResolveLoaderOptions);
162 getSource(name: string): LoaderSource;
163}
164
165export interface WebLoaderOptions {
166 useCache?: boolean;
167 async?: boolean;
168}
169
170export class WebLoader extends Loader {
171 constructor(baseUrl?: string, opts?: WebLoaderOptions);
172 async: boolean;
173
174 getSource: (name: string, callback: Callback<Error, LoaderSource>) => LoaderSource;
175}
176
177export class PrecompiledLoader extends Loader implements ILoader {
178 constructor(compiledTemplates?: any[]);
179 getSource(name: string): LoaderSource;
180}
181
182export namespace runtime {
183 class SafeString {
184 constructor(val: string);
185 val: string;
186 length: number;
187 valueOf(): string;
188 toString(): string;
189 }
190}
191
192export namespace lib {
193 class TemplateError extends Error {
194 constructor(message: string, lineno: number, colno: number);
195
196 name: string; // always 'Template render error'
197 message: string;
198 stack: string;
199
200 cause?: Error | undefined;
201 lineno: number;
202 colno: number;
203 }
204}