mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
nunjucks: Describe lib.TemplateError interface (#37714)
- nunjucks.lib.TemplateError is thrown when rendering.
- Note that unless you initialize the Environment with
opts = {dev: true}, lib._prettifyError will translate
the TemplateError into a regular Error; it still has the same
properties, but you can't use an `isinstance` check on it.
Hopefully this can be fixed in the future.
- Fix callback signatures to reflect null values, void returns, etc.
This commit is contained in:
parent
3be0a6398e
commit
713f17231e
36
types/nunjucks/index.d.ts
vendored
36
types/nunjucks/index.d.ts
vendored
@ -4,13 +4,19 @@
|
||||
// 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?: (err: any, res: string) => any): void;
|
||||
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?: (err: any, res: string) => any): void;
|
||||
export function renderString(src: string, context: object, callback?: TemplateCallback<string>): void;
|
||||
|
||||
export function compile(src: string, env?: Environment, callback?: (err: any, res: Template) => any): Template;
|
||||
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;
|
||||
@ -28,7 +34,7 @@ export interface PrecompileOptions {
|
||||
export class Template {
|
||||
constructor(src: string, env?: Environment, eagerCompile?: boolean);
|
||||
render(context?: object): string;
|
||||
render(context?: object, callback?: (err: any, res: string) => any): void;
|
||||
render(context?: object, callback?: TemplateCallback<string>): void;
|
||||
}
|
||||
|
||||
export function configure(options: ConfigureOptions): Environment;
|
||||
@ -63,10 +69,10 @@ export class Environment {
|
||||
|
||||
constructor(loader?: ILoader | ILoader[] | null, opts?: ConfigureOptions);
|
||||
render(name: string, context?: object): string;
|
||||
render(name: string, context?: object, callback?: (err: any, res: string) => any): void;
|
||||
render(name: string, context?: object, callback?: TemplateCallback<string>): void;
|
||||
|
||||
renderString(name: string, context: object): string;
|
||||
renderString(name: string, context: object, callback?: (err: any, res: string) => any): void;
|
||||
renderString(name: string, context: object, callback?: TemplateCallback<string>): void;
|
||||
|
||||
addFilter(name: string, func: (...args: any[]) => any, async?: boolean): void;
|
||||
getFilter(name: string): void;
|
||||
@ -79,7 +85,7 @@ export class Environment {
|
||||
addGlobal(name: string, value: any): void;
|
||||
|
||||
getTemplate(name: string, eagerCompile?: boolean): Template;
|
||||
getTemplate(name: string, eagerCompile?: boolean, callback?: (err: any, templ: Template) => Template): void;
|
||||
getTemplate(name: string, eagerCompile?: boolean, callback?: Callback<Error, Template>): void;
|
||||
|
||||
express(app: object): void;
|
||||
}
|
||||
@ -95,7 +101,7 @@ export function installJinjaCompat(): void;
|
||||
export interface ILoader {
|
||||
async?: boolean;
|
||||
getSource(name: string): LoaderSource;
|
||||
getSource(name: string, callback: (err?: any, result?: LoaderSource) => void): void;
|
||||
getSource(name: string, callback: Callback<Error, LoaderSource>): void;
|
||||
extend?(extender: ILoader): ILoader;
|
||||
}
|
||||
|
||||
@ -148,3 +154,17 @@ export namespace runtime {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,10 @@ nunjucks.configure({ autoescape: false });
|
||||
let rendered = nunjucks.render("./noexists.html");
|
||||
|
||||
nunjucks.render('foo.html', { username: 'James' });
|
||||
nunjucks.render('async.html', (err: any, res: string) => {});
|
||||
nunjucks.render(
|
||||
'async.html',
|
||||
(err: nunjucks.lib.TemplateError | null, res: string | null) => {},
|
||||
);
|
||||
|
||||
const ctx = { items: ["Hello", "this", "is", "for", "testing"] };
|
||||
const src = "{% for item in items %}{{item}}{% endfor %}";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user