Testversion im Browser und Sicherung einspielen
Die Testversion bündelt Oberfläche und Server-Logik mit PGlite (Postgres als WebAssembly). POST /api/import/stand spielt einen kompletten Stand mit allen IDs in ein leeres Programm ein; damit ziehen die Daten später auf den Server um. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
79
demo/fastify.ts
Normal file
79
demo/fastify.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
// Ersatz für Fastify in der Browser-Testversion: dieselben Routen, aber ohne Netzwerk.
|
||||
// Unterstützt nur das, was src/server/app.ts benutzt.
|
||||
|
||||
type Handler = (req: Anfrage, reply: Antwort) => unknown;
|
||||
|
||||
export interface Anfrage {
|
||||
method: string;
|
||||
url: string;
|
||||
params: Record<string, string>;
|
||||
query: Record<string, string>;
|
||||
body: unknown;
|
||||
headers: Record<string, string>;
|
||||
}
|
||||
|
||||
export class Antwort {
|
||||
statusCode = 200;
|
||||
kopf: Record<string, string> = {};
|
||||
inhalt: unknown = undefined;
|
||||
gesendet = false;
|
||||
status(code: number) { this.statusCode = code; return this; }
|
||||
code(code: number) { return this.status(code); }
|
||||
header(name: string, wert: string) { this.kopf[name.toLowerCase()] = wert; return this; }
|
||||
send(inhalt?: unknown) { this.inhalt = inhalt; this.gesendet = true; return this; }
|
||||
sendFile() { return this.status(404).send({ fehler: "Nicht gefunden" }); }
|
||||
}
|
||||
|
||||
interface Route { methode: string; teile: string[]; handler: Handler }
|
||||
|
||||
export default function Fastify(_o?: unknown) {
|
||||
const routen: Route[] = [];
|
||||
let fehlerHandler: (err: unknown, req: Anfrage, reply: Antwort) => unknown = (err, _r, reply) =>
|
||||
reply.status(500).send({ fehler: (err as Error).message });
|
||||
const neu = (methode: string) => (pfad: string, handler: Handler) => { routen.push({ methode, teile: pfad.split("/"), handler }); };
|
||||
|
||||
const app = {
|
||||
log: { error: console.error, warn: console.warn, info: () => {} },
|
||||
get: neu("GET"), post: neu("POST"), put: neu("PUT"), patch: neu("PATCH"), delete: neu("DELETE"),
|
||||
setErrorHandler(fn: typeof fehlerHandler) { fehlerHandler = fn; },
|
||||
setNotFoundHandler() {},
|
||||
async register() {},
|
||||
async close() {},
|
||||
|
||||
/** Bearbeitet eine Anfrage wie der Server und liefert eine fetch-Antwort. */
|
||||
async verarbeite(methode: string, adresse: string, body: unknown, headers: Record<string, string> = {}): Promise<Response> {
|
||||
const u = new URL(adresse, "http://demo");
|
||||
const teile = u.pathname.split("/").map(decodeURIComponent);
|
||||
const reply = new Antwort();
|
||||
let route: Route | undefined;
|
||||
const params: Record<string, string> = {};
|
||||
for (const r of routen) {
|
||||
if (r.methode !== methode || r.teile.length !== teile.length) continue;
|
||||
if (r.teile.every((t, i) => t.startsWith(":") || t === teile[i])) {
|
||||
route = r;
|
||||
r.teile.forEach((t, i) => { if (t.startsWith(":")) params[t.slice(1)] = teile[i]!; });
|
||||
break;
|
||||
}
|
||||
}
|
||||
const req: Anfrage = { method: methode, url: u.pathname + u.search, params, query: Object.fromEntries(u.searchParams), body, headers };
|
||||
if (!route) reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
else {
|
||||
try {
|
||||
const erg = await route.handler(req, reply);
|
||||
if (!reply.gesendet && erg !== reply) reply.send(erg);
|
||||
} catch (err) {
|
||||
await fehlerHandler(err, req, reply);
|
||||
}
|
||||
}
|
||||
const leer = reply.inhalt === undefined || reply.statusCode === 204;
|
||||
const text = typeof reply.inhalt === "string";
|
||||
return new Response(leer ? null : text ? (reply.inhalt as string) : JSON.stringify(reply.inhalt), {
|
||||
status: reply.statusCode,
|
||||
headers: { "content-type": text ? "text/plain; charset=utf-8" : "application/json", ...reply.kopf },
|
||||
});
|
||||
},
|
||||
};
|
||||
return app;
|
||||
}
|
||||
export type FastifyInstance = ReturnType<typeof Fastify>;
|
||||
export type FastifyRequest = Anfrage;
|
||||
Reference in New Issue
Block a user