Skip to content

Commit

Permalink
autoroutes & test endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tnfAngel committed May 24, 2024
1 parent f917093 commit 0fcb43f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"cSpell.words": [
"elysia"
"autoroutes",
"elysia",
"elysiajs"
]
}
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@elysiajs/cors": "^1.0.2",
"elysia": "^1.0.21",
"elysia-autoroutes": "^0.5.0",
"tslib": "^2.6.2"
},
"devDependencies": {
Expand Down
12 changes: 10 additions & 2 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,7 @@ export class Server {
public constructor() {
this.initCORS();
this.initErrorListener();
this.initEndpoints();
this.initRoutes();
}

private initCORS(): void {
Expand Down Expand Up @@ -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}`));
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import { Server } from './classes/Server.ts';
const sv = new Server();

sv.listen();

export type ElysiaApp = typeof sv.elysia;
25 changes: 25 additions & 0 deletions src/routes/channels/[channelId]/messages.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
);

0 comments on commit 0fcb43f

Please sign in to comment.