feat: express responses

This commit is contained in:
TresAbhi committed 2023-06-27 01:43:30 +00:00
1 parent 0f46f333b5
commit f2ae698438
45 files changed
+153 -93

No files matched your search

+32 -10
View File
@@ -1,18 +1,40 @@
import express from 'express';
import jsxToSvg from './core/ui/jsxToSvg.js';
import today from './renderers/today.js';
import express, { Request, Response } from 'express';
import normalizeResponderReturnable from './core/express/normalizeResponderReturnable.js';
import isDev from './core/node/isDev.js';
import { Registry } from './events/interactionCreate/index.js';
import stats from './responders/stats.js';
export type ResponderReturnable = JSX.Element | Promise<JSX.Element>;
export interface ResponderRegistry<HandlesInteraction extends boolean = false>
extends Registry {
route: string;
handlesInteraction?: HandlesInteraction;
handler: (
req: Request,
res: Response,
) => HandlesInteraction extends true ? void : ResponderReturnable;
}
const app = express();
app.get('/', async (req, res) => {
const jsx = await today({ server: 'com', id: 1028700193 });
const svg = await jsxToSvg(jsx);
([stats] as ResponderRegistry[]).forEach((registry) => {
if (!(isDev() ? registry.inDevelopment : registry.inProduction)) return;
res.writeHead(200, {
'Content-Type': 'image/svg+xml',
'Content-Length': svg.length,
app.get(registry.route, async (req, res) => {
const returnable = registry.handler(req, res);
if (registry.handlesInteraction) return;
const response = await normalizeResponderReturnable(returnable);
res.writeHead(200, {
'Content-Type': 'image/svg+xml',
'Content-Length': response.length,
});
res.end(response);
});
res.end(svg);
});
app.listen(process.env.PORT ?? 3000);