mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-01 11:39:28 +08:00
ejs: Add support for the async option
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import ejs = require("ejs");
|
||||
import { readFileSync as read } from 'fs';
|
||||
import LRU = require("lru-cache");
|
||||
import { TemplateFunction, Options } from "ejs";
|
||||
import { TemplateFunction, AsyncTemplateFunction, Options } from "ejs";
|
||||
|
||||
const fileName = 'test.ejs';
|
||||
const people = ['geddy', 'neil', 'alex'];
|
||||
@@ -12,6 +12,8 @@ const template = '<%= people.join(", "); %>';
|
||||
const options = { filename: fileName };
|
||||
let result: string;
|
||||
let ejsFunction: TemplateFunction;
|
||||
let asyncResult: Promise<string>;
|
||||
let ejsAsyncFunction: AsyncTemplateFunction;
|
||||
|
||||
const SimpleCallback = (err: any, html?: string) => {
|
||||
if (err) {
|
||||
@@ -44,11 +46,19 @@ ejsFunction = ejs.compile('<%= it.people.join(", "); %>', { _with: false, locals
|
||||
ejsFunction = ejs.compile(template, { rmWhitespace: true });
|
||||
const customEscape = (str: string) => !str ? '' : str.toUpperCase();
|
||||
ejsFunction = ejs.compile(template, { escape: customEscape });
|
||||
ejsFunction = ejs.compile(template, { async: false });
|
||||
|
||||
ejsAsyncFunction = ejs.compile(template, { async: true });
|
||||
ejsAsyncFunction = ejs.compile(template, { client: true, async: true });
|
||||
|
||||
result = ejsFunction();
|
||||
result = ejsFunction({});
|
||||
result = ejsFunction(data);
|
||||
|
||||
asyncResult = ejsAsyncFunction();
|
||||
asyncResult = ejsAsyncFunction({});
|
||||
asyncResult = ejsAsyncFunction(data);
|
||||
|
||||
/** @see https://github.com/mde/ejs/tree/v2.5.7#custom-fileloader */
|
||||
ejs.fileLoader = (path: string) => "";
|
||||
|
||||
|
||||
17
types/ejs/index.d.ts
vendored
17
types/ejs/index.d.ts
vendored
@@ -2,7 +2,7 @@
|
||||
// Project: http://ejs.co/
|
||||
// Definitions by: Ben Liddicott <https://github.com/benliddicott>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
export interface Data {
|
||||
[name: string]: any;
|
||||
@@ -28,14 +28,22 @@ export function resolveInclude(name: string, filename: string, isDir: boolean):
|
||||
/**
|
||||
* Compile the given `str` of ejs into a template function.
|
||||
*/
|
||||
export function compile(template: string, opts?: Options): (TemplateFunction);
|
||||
export function compile(template: string, opts?: Options & { async: false }): TemplateFunction;
|
||||
export function compile(template: string, opts: Options & { async: true }): AsyncTemplateFunction;
|
||||
export function compile(template: string, opts: Options & { async: boolean }): TemplateFunction | AsyncTemplateFunction;
|
||||
export function compile(template: string, opts: Exclude<Options, { async: any }>): TemplateFunction;
|
||||
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): string;
|
||||
export function render(template: string, data?: Data, opts?: Options & { async: false }): string;
|
||||
export function render(template: string, data: Data, opts: Options & { async: true }): Promise<string>;
|
||||
export function render(template: string, data: Data, opts: Options & { async: boolean }): string | Promise<string>;
|
||||
export function render(template: string, data: Data, opts: Exclude<Options, { async: any }>): string;
|
||||
export function render(template: string, data?: Data, opts?: Options): string | Promise<string>;
|
||||
|
||||
export type RenderFileCallback<T> = (err: Error, str?: string) => T;
|
||||
|
||||
@@ -55,6 +63,7 @@ export function renderFile<T>(path: string, data: Data, opts: Options, cb: Rende
|
||||
export function clearCache(): void;
|
||||
|
||||
export type TemplateFunction = (data?: Data) => string;
|
||||
export type AsyncTemplateFunction = (data?: Data) => Promise<string>;
|
||||
export interface Options {
|
||||
/** Compiled functions are cached, requires `filename` */
|
||||
cache?: boolean;
|
||||
@@ -95,6 +104,8 @@ export interface Options {
|
||||
* (By default escapes XML).
|
||||
*/
|
||||
escape?(str: string): string;
|
||||
/** Whether or not to use a Promise */
|
||||
async?: boolean;
|
||||
}
|
||||
|
||||
export function escapeRegexChars(s: string): string;
|
||||
|
||||
Reference in New Issue
Block a user