Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
5a5515f2f4 |
6 changed files with 13 additions and 49 deletions
|
@ -1,3 +1,3 @@
|
|||
# bun frontend template
|
||||
|
||||
a simple bun frontend starting point i made and use
|
||||
a simle bun frontend starting point i made and use
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
"devDependencies": {
|
||||
"@eslint/js": "^9.23.0",
|
||||
"@types/bun": "^1.2.6",
|
||||
"@types/ejs": "^3.1.5",
|
||||
"@typescript-eslint/eslint-plugin": "^8.28.0",
|
||||
"@typescript-eslint/parser": "^8.28.0",
|
||||
"eslint": "^9.23.0",
|
||||
|
@ -26,8 +25,5 @@
|
|||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.8.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"ejs": "^3.1.10"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
import { renderFile } from "ejs";
|
||||
import { resolve } from "path";
|
||||
|
||||
export async function renderEjsTemplate(
|
||||
viewName: string | string[],
|
||||
data: EjsTemplateData,
|
||||
headers?: Record<string, string | number | boolean>,
|
||||
): Promise<Response> {
|
||||
let templatePath: string;
|
||||
|
||||
if (Array.isArray(viewName)) {
|
||||
templatePath = resolve("src", "views", ...viewName);
|
||||
} else {
|
||||
templatePath = resolve("src", "views", viewName);
|
||||
}
|
||||
|
||||
if (!templatePath.endsWith(".ejs")) {
|
||||
templatePath += ".ejs";
|
||||
}
|
||||
|
||||
const html: string = await renderFile(templatePath, data);
|
||||
|
||||
return new Response(html, {
|
||||
headers: { "Content-Type": "text/html", ...headers },
|
||||
});
|
||||
}
|
|
@ -1,17 +1,22 @@
|
|||
import { renderEjsTemplate } from "@helpers/ejs";
|
||||
|
||||
const routeDef: RouteDef = {
|
||||
method: "GET",
|
||||
accepts: "*/*",
|
||||
returns: "text/html",
|
||||
returns: "application/json",
|
||||
};
|
||||
|
||||
async function handler(): Promise<Response> {
|
||||
const ejsTemplateData: EjsTemplateData = {
|
||||
title: "Hello, World!",
|
||||
async function handler(request: ExtendedRequest): Promise<Response> {
|
||||
const endPerf: number = Date.now();
|
||||
const perf: number = endPerf - request.startPerf;
|
||||
|
||||
const { query, params } = request;
|
||||
|
||||
const response: Record<string, unknown> = {
|
||||
perf,
|
||||
query,
|
||||
params,
|
||||
};
|
||||
|
||||
return await renderEjsTemplate("index", ejsTemplateData);
|
||||
return Response.json(response);
|
||||
}
|
||||
|
||||
export { handler, routeDef };
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<h1> hello </h1>
|
||||
</body>
|
||||
</html>
|
3
types/ejs.d.ts
vendored
3
types/ejs.d.ts
vendored
|
@ -1,3 +0,0 @@
|
|||
interface EjsTemplateData {
|
||||
[key: string]: string | number | boolean | object | undefined | null;
|
||||
}
|
Loading…
Add table
Reference in a new issue