Skip to content

Commit

Permalink
Improve return types for render, renderAsync, renderFile and `r…
Browse files Browse the repository at this point in the history
…enderFileAsync` (#199)

resolves #189
  • Loading branch information
nhaef authored Jan 10, 2023
1 parent 7190909 commit 242e9fc
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface EtaConfigWithFilename extends EtaConfig {
}

export type PartialConfig = Partial<EtaConfig>
export type PartialAsyncConfig = PartialConfig & { async: true }

/* END TYPES */

Expand Down
30 changes: 28 additions & 2 deletions src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,27 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
* ```
*/

function renderFile(
filename: string,
data: DataObj,
config?: PartialConfig,
): Promise<string>

function renderFile(
filename: string,
data: DataObj,
config: PartialConfig,
cb: CallbackFn
): void

function renderFile(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn
): Promise<string> | void

function renderFile(filename: string, data: DataObj, cb?: CallbackFn): Promise<string> | void
function renderFile(filename: string, data: DataObj, cb: CallbackFn): void

function renderFile(
filename: string,
Expand Down Expand Up @@ -268,14 +281,27 @@ function renderFile(
* ```
*/

function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig
): Promise<string>

function renderFileAsync(
filename: string,
data: DataObj,
config: PartialConfig,
cb: CallbackFn
): void

function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn
): Promise<string> | void

function renderFileAsync(filename: string, data: DataObj, cb?: CallbackFn): Promise<string> | void
function renderFileAsync(filename: string, data: DataObj, cb: CallbackFn): void

function renderFileAsync(
filename: string,
Expand Down
125 changes: 124 additions & 1 deletion src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import EtaErr from './err'

/* TYPES */

import type { EtaConfig, PartialConfig } from './config'
import type { EtaConfig, PartialConfig, PartialAsyncConfig } from './config'
import type { TemplateFunction } from './compile'
import type { CallbackFn } from './file-handlers'

Expand Down Expand Up @@ -45,6 +45,83 @@ function handleCache(template: string | TemplateFunction, options: EtaConfig): T
* @param config Optional config options
* @param cb Callback function
*/
export default function render(
template: string | TemplateFunction,
data: object,
config: PartialAsyncConfig,
cb: CallbackFn
): void

/**
* Render a template
*
* If `template` is a string, Eta will compile it to a function and then call it with the provided data.
* If `template` is a template function, Eta will call it with the provided data.
*
* If `config.async` is `false`, Eta will return the rendered template.
*
* If `config.async` is `true` and there's a callback function, Eta will call the callback with `(err, renderedTemplate)`.
* If `config.async` is `true` and there's not a callback function, Eta will return a Promise that resolves to the rendered template.
*
* If `config.cache` is `true` and `config` has a `name` or `filename` property, Eta will cache the template on the first render and use the cached template for all subsequent renders.
*
* @param template Template string or template function
* @param data Data to render the template with
* @param config Optional config options
*/
export default function render(
template: string | TemplateFunction,
data: object,
config: PartialAsyncConfig
): Promise<string>

/**
* Render a template
*
* If `template` is a string, Eta will compile it to a function and then call it with the provided data.
* If `template` is a template function, Eta will call it with the provided data.
*
* If `config.async` is `false`, Eta will return the rendered template.
*
* If `config.async` is `true` and there's a callback function, Eta will call the callback with `(err, renderedTemplate)`.
* If `config.async` is `true` and there's not a callback function, Eta will return a Promise that resolves to the rendered template.
*
* If `config.cache` is `true` and `config` has a `name` or `filename` property, Eta will cache the template on the first render and use the cached template for all subsequent renders.
*
* @param template Template string or template function
* @param data Data to render the template with
* @param config Optional config options
*/
export default function render(
template: string | TemplateFunction,
data: object,
config?: PartialConfig
): string

/**
* Render a template
*
* If `template` is a string, Eta will compile it to a function and then call it with the provided data.
* If `template` is a template function, Eta will call it with the provided data.
*
* If `config.async` is `false`, Eta will return the rendered template.
*
* If `config.async` is `true` and there's a callback function, Eta will call the callback with `(err, renderedTemplate)`.
* If `config.async` is `true` and there's not a callback function, Eta will return a Promise that resolves to the rendered template.
*
* If `config.cache` is `true` and `config` has a `name` or `filename` property, Eta will cache the template on the first render and use the cached template for all subsequent renders.
*
* @param template Template string or template function
* @param data Data to render the template with
* @param config Optional config options
* @param cb Callback function
*/
export default function render(
template: string | TemplateFunction,
data: object,
config?: PartialConfig,
cb?: CallbackFn
): string | Promise<string> | void

export default function render(
template: string | TemplateFunction,
Expand Down Expand Up @@ -84,6 +161,46 @@ export default function render(
}
}

/**
* Render a template asynchronously
*
* If `template` is a string, Eta will compile it to a function and call it with the provided data.
* If `template` is a function, Eta will call it with the provided data.
*
* If there is a callback function, Eta will call it with `(err, renderedTemplate)`.
* If there is not a callback function, Eta will return a Promise that resolves to the rendered template
*
* @param template Template string or template function
* @param data Data to render the template with
* @param config Optional config options
*/
export function renderAsync(
template: string | TemplateFunction,
data: object,
config?: PartialConfig
): Promise<string>

/**
* Render a template asynchronously
*
* If `template` is a string, Eta will compile it to a function and call it with the provided data.
* If `template` is a function, Eta will call it with the provided data.
*
* If there is a callback function, Eta will call it with `(err, renderedTemplate)`.
* If there is not a callback function, Eta will return a Promise that resolves to the rendered template
*
* @param template Template string or template function
* @param data Data to render the template with
* @param config Optional config options
* @param cb Callback function
*/
export function renderAsync(
template: string | TemplateFunction,
data: object,
config: PartialConfig,
cb: CallbackFn
): void

/**
* Render a template asynchronously
*
Expand All @@ -98,6 +215,12 @@ export default function render(
* @param config Optional config options
* @param cb Callback function
*/
export function renderAsync(
template: string | TemplateFunction,
data: object,
config?: PartialConfig,
cb?: CallbackFn
): string | Promise<string> | void

export function renderAsync(
template: string | TemplateFunction,
Expand Down

0 comments on commit 242e9fc

Please sign in to comment.