diff --git a/.vscode/settings.json b/.vscode/settings.json index 7becf87..97a4932 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,7 @@ { "cSpell.words": [ - "elysia" + "autoroutes", + "elysia", + "elysiajs" ] } \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 504e720..cced51b 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index bd0095a..ccaaac8 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "@elysiajs/cors": "^1.0.2", "elysia": "^1.0.21", + "elysia-autoroutes": "^0.5.0", "tslib": "^2.6.2" }, "devDependencies": { diff --git a/src/classes/Server.ts b/src/classes/Server.ts index 3e96543..c9a0485 100644 --- a/src/classes/Server.ts +++ b/src/classes/Server.ts @@ -1,5 +1,6 @@ import cors from '@elysiajs/cors'; import { Elysia } from 'elysia'; +import { autoroutes } from 'elysia-autoroutes'; export class Server { public static readonly port = process.env['PORT'] ?? 4000; @@ -9,7 +10,7 @@ export class Server { public constructor() { this.initCORS(); this.initErrorListener(); - this.initEndpoints(); + this.initRoutes(); } private initCORS(): void { @@ -39,7 +40,14 @@ export class Server { }); } - private initEndpoints(): void {} + private initRoutes(): void { + this.elysia.use( + autoroutes({ + routesDir: './routes', // -> optional, defaults to './routes' + generateTags: false // -> optional, defaults to true + }) + ); + } public listen() { this.elysia.listen(Server.port, ({ port }) => console.info(`Listening on: http://localhost:${port}`)); diff --git a/src/index.ts b/src/index.ts index 69ab611..0f4a969 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,5 @@ import { Server } from './classes/Server.ts'; const sv = new Server(); sv.listen(); + +export type ElysiaApp = typeof sv.elysia; diff --git a/src/routes/channels/[channelId]/messages.ts b/src/routes/channels/[channelId]/messages.ts new file mode 100644 index 0000000..f7e4249 --- /dev/null +++ b/src/routes/channels/[channelId]/messages.ts @@ -0,0 +1,25 @@ +import { t } from 'elysia'; +import type { ElysiaApp } from '../../..'; + +export default (app: ElysiaApp) => + app.post( + '/', + async ({ body, params }) => { + console.log(body, params); + }, + { + params: t.Object({ + channelId: t.Numeric({ + description: 'The channel id', + examples: ['123'] + }) + }), + body: t.Object({ + content: t.String({ description: 'The message content', minLength: 1, maxLength: 2000 }), + nonce: t.String({ description: 'Message internal identifier', minLength: 1, maxLength: 255 }) + }), + response: { + 200: t.Void() + } + } + );