-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.ts
36 lines (32 loc) · 986 Bytes
/
function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient();
export interface TemplateData {
[key: string]: string;
}
export function getQueryParams(url: string): TemplateData {
const paramArr = url.slice(url.indexOf('?') + 1).split('&');
const params: TemplateData = {};
for (const param of paramArr) {
const [key, val] = param.split('=');
params[key] = decodeURIComponent(val);
}
return params;
}
export async function renderTemplate(template: string): Promise<string> {
try {
const KVDB = await prisma.keyValue.findMany();
return template.replace(/\{\{([^\}]+)\}\}/g, (match, key) => {
if(key.startsWith('SECRET')) {
return '***';
}
const KVMatch = KVDB.find(item => item.key === key);
if (KVMatch) {
return KVMatch.value || match;
}
return match;
});
} catch (error) {
console.error('Error occurred while rendering template:', error);
throw error;
}
}