Skip to content

Commit

Permalink
feat: adjust render types to be generic (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninanator committed Mar 16, 2024
1 parent a282536 commit 9d21224
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/render.ts
Expand Up @@ -36,10 +36,10 @@ function handleCache(this: Eta, template: string, options: Partial<Options>): Te
}
}

export function render(
export function render<T extends object>(
this: Eta,
template: string | TemplateFunction, // template name or template function
data: object,
data: T,
meta?: { filepath: string }
): string {
let templateFn: TemplateFunction;
Expand All @@ -60,10 +60,10 @@ export function render(
return res;
}

export function renderAsync(
export function renderAsync<T extends object>(
this: Eta,
template: string | TemplateFunction, // template name or template function
data: object,
data: T,
meta?: { filepath: string }
): Promise<string> {
let templateFn: TemplateFunction;
Expand All @@ -85,13 +85,13 @@ export function renderAsync(
return Promise.resolve(res);
}

export function renderString(this: Eta, template: string, data: object): string {
export function renderString<T extends object>(this: Eta, template: string, data: T): string {
const templateFn = this.compile(template, { async: false });

return render.call(this, templateFn, data);
}

export function renderStringAsync(this: Eta, template: string, data: object): Promise<string> {
export function renderStringAsync<T extends object>(this: Eta, template: string, data: T): Promise<string> {
const templateFn = this.compile(template, { async: true });

return renderAsync.call(this, templateFn, data);
Expand Down
6 changes: 5 additions & 1 deletion test/render.spec.ts
Expand Up @@ -4,6 +4,10 @@ import path from "node:path";

import { Eta } from "../src/index";

interface SimpleEtaTemplate {
name: string;
}

describe("basic functionality", () => {
const eta = new Eta();

Expand Down Expand Up @@ -139,7 +143,7 @@ describe("file rendering", () => {
const eta = new Eta({ views: path.join(__dirname, "templates") });

it("renders template file properly", () => {
const res = eta.render("simple.eta", { name: "friend" });
const res = eta.render<SimpleEtaTemplate>("simple.eta", { name: "friend" });

expect(res).toEqual("Hi friend");
});
Expand Down

0 comments on commit 9d21224

Please sign in to comment.