Enable LLMs to handle webhooks with plaintext files
1import { Liquid } from "liquidjs";
2import type { ParsedLure, LureRequest } from "./types.js";
3
4const engine = new Liquid({ strictVariables: false, strictFilters: false });
5
6export async function renderTemplate(lure: ParsedLure, req: LureRequest): Promise<string> {
7 let payload: unknown = null;
8
9 if (lure.frontmatter.payload?.contentType === "json") {
10 const text = new TextDecoder().decode(req.rawBody);
11 payload = JSON.parse(text) as unknown;
12 }
13
14 const scope: Record<string, unknown> = {
15 payload,
16 headers: Object.fromEntries(req.headers),
17 query: Object.fromEntries(req.query),
18 };
19
20 return engine.parseAndRender(lure.template, scope);
21}