we're actually calling openai

This commit is contained in:
Kyle Corbitt
2023-06-22 17:57:21 -07:00
parent 0fa3af4e9f
commit a31c112745
14 changed files with 271 additions and 30 deletions

View File

@@ -0,0 +1,27 @@
export type JSONSerializable =
| string
| number
| boolean
| null
| JSONSerializable[]
| { [key: string]: JSONSerializable };
export type VariableMap = Record<string, string>;
export default function fillTemplate<T extends JSONSerializable>(
template: T,
variables: VariableMap
): T {
if (typeof template === "string") {
return template.replace(/{{\s*(\w+)\s*}}/g, (_, key: string) => variables[key] || "") as T;
} else if (Array.isArray(template)) {
return template.map((item) => fillTemplate(item, variables)) as T;
} else if (typeof template === "object" && template !== null) {
return Object.keys(template).reduce((acc, key) => {
acc[key] = fillTemplate(template[key] as JSONSerializable, variables);
return acc;
}, {} as { [key: string]: JSONSerializable } & T);
} else {
return template;
}
}