From 9d2122435c3e4c41ed343e42e7c2efe1162edf2f Mon Sep 17 00:00:00 2001 From: Nina Blanson Date: Sat, 16 Mar 2024 11:14:10 -0500 Subject: [PATCH] feat: adjust render types to be generic (#278) --- src/render.ts | 12 ++++++------ test/render.spec.ts | 6 +++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/render.ts b/src/render.ts index 64a66f1..1e08856 100644 --- a/src/render.ts +++ b/src/render.ts @@ -36,10 +36,10 @@ function handleCache(this: Eta, template: string, options: Partial): Te } } -export function render( +export function render( this: Eta, template: string | TemplateFunction, // template name or template function - data: object, + data: T, meta?: { filepath: string } ): string { let templateFn: TemplateFunction; @@ -60,10 +60,10 @@ export function render( return res; } -export function renderAsync( +export function renderAsync( this: Eta, template: string | TemplateFunction, // template name or template function - data: object, + data: T, meta?: { filepath: string } ): Promise { let templateFn: TemplateFunction; @@ -85,13 +85,13 @@ export function renderAsync( return Promise.resolve(res); } -export function renderString(this: Eta, template: string, data: object): string { +export function renderString(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 { +export function renderStringAsync(this: Eta, template: string, data: T): Promise { const templateFn = this.compile(template, { async: true }); return renderAsync.call(this, templateFn, data); diff --git a/test/render.spec.ts b/test/render.spec.ts index 8484340..0d3b14b 100644 --- a/test/render.spec.ts +++ b/test/render.spec.ts @@ -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(); @@ -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("simple.eta", { name: "friend" }); expect(res).toEqual("Hi friend"); });