From a333f99844ca58a29087e727a95792fa513a5f1b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 08:27:39 +0000 Subject: [PATCH 1/5] --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/container.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 7234009..8b4f7d1 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -28,7 +28,7 @@ jobs: steps: - name: 'Checkout' - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: 'Setup tags' id: setup-tags From 48bf70c5526a542e26ad3b811cc96439b2b7d4a0 Mon Sep 17 00:00:00 2001 From: tnfAngel <57068341+tnfAngel@users.noreply.github.com> Date: Wed, 22 May 2024 09:28:34 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | Bin 12 -> 20 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/README.md b/README.md index 06d3e0f7d41c7d8256151e1824d2cec68ab6adf2..d7813f78949a7fa624145b59a458f669531fc23a 100644 GIT binary patch literal 20 bcmY#ZD9KB6%u7$rQE<*kEKzU_@Z Date: Wed, 22 May 2024 09:37:08 +0100 Subject: [PATCH 3/5] initial server class --- .vscode/settings.json | 5 +++++ src/classes/Server.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 4 +++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 src/classes/Server.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7becf87 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "elysia" + ] +} \ No newline at end of file diff --git a/src/classes/Server.ts b/src/classes/Server.ts new file mode 100644 index 0000000..3eee698 --- /dev/null +++ b/src/classes/Server.ts @@ -0,0 +1,42 @@ +import { Elysia } from 'elysia'; + +export class Server { + public static readonly port = process.env['PORT'] ?? 4000; + + public elysia: Elysia = new Elysia({ precompile: true }); + + public constructor() { + this.initCORS(); + this.initErrorListener(); + this.initEndpoints(); + + this.elysia.listen(Server.port, ({ port }) => console.info(`Listening on: http://localhost:${port}`)); + } + + private initCORS(): void {} + + private initErrorListener(): void { + this.elysia.onError(({ code, error }) => { + if (code === 'NOT_FOUND') { + return 'Not Found'; + } + + if (code === 'VALIDATION') { + return 'Validation Error'; + } + + if (code === 'PARSE') { + return 'Parse Error'; + } + + if (error instanceof Error || code === 'INTERNAL_SERVER_ERROR') { + console.error(error); + return 'Internal Server Error'; + } + + return error; + }); + } + + private initEndpoints(): void {} +} diff --git a/src/index.ts b/src/index.ts index 7bc4a71..d06af5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,3 @@ -console.log('Hello world'); +import { Server } from './classes/Server.ts'; + +new Server(); From f917093f3721a533a72bd035cd0564bcd4ff5dc7 Mon Sep 17 00:00:00 2001 From: tnfAngel <57068341+tnfAngel@users.noreply.github.com> Date: Wed, 22 May 2024 11:59:07 +0100 Subject: [PATCH 4/5] cors --- bun.lockb | Bin 10399 -> 10399 bytes package.json | 56 +++++++++++++++++++++--------------------- src/classes/Server.ts | 11 ++++++--- src/index.ts | 4 ++- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/bun.lockb b/bun.lockb index 5474b3adbb8cb329ba73487766d6719644221044..504e720a628ab558b91e6a4f81bb30e8979ccfe4 100644 GIT binary patch delta 20 YcmbOqI6rVhrVKk10~l;BkkRJ>06Z}SnE(I) delta 20 bcmbOqI6rVhrVKk{oS~k9p3&w48GRlAN2LYj diff --git a/package.json b/package.json index d2a463f..bd0095a 100644 --- a/package.json +++ b/package.json @@ -1,30 +1,30 @@ { - "name": "@tnfangel-chat/api", - "version": "1.0.0", - "description": "tnfAngel Chat API Service", - "type": "module", - "private": true, - "scripts": { - "build": "bun run build:bundle", - "build:bundle": "bun build --target bun --format esm --minify --outdir ./dist/ ./src/index.ts", - "build:standalone": "bun build --compile --target bun --format esm --minify --outfile ./dist/aurora ./src/index.ts", - "start": "bun run build && bun ./dist/index.js", - "dev": "NODE_ENV=development bun --hot src/index.ts", - "production:build": "bun run build:standalone", - "lint": "bunx --bun @biomejs/biome check --apply ." - }, - "author": "tnfAngel", - "dependencies": { - "@elysiajs/cors": "^1.0.2", - "elysia": "^1.0.21", - "tslib": "^2.6.2" - }, - "devDependencies": { - "@biomejs/biome": "^1.7.3", - "@types/bun": "^1.1.3", - "typescript": "5.4.5" - }, - "peerDependencies": { - "typescript": "^5.0.0" - } + "name": "@tnfangel-chat/api", + "version": "1.0.0", + "description": "tnfAngel Chat API Service", + "type": "module", + "private": true, + "scripts": { + "build": "bun run build:bundle", + "build:bundle": "bun build --target bun --format esm --minify --outdir ./dist/ ./src/index.ts", + "build:standalone": "bun build --compile --target bun --format esm --minify --outfile ./dist/aurora ./src/index.ts", + "start": "bun run build && bun ./dist/index.js", + "dev": "NODE_ENV=development bun --hot src/index.ts", + "production:build": "bun run build:standalone", + "lint": "bunx --bun @biomejs/biome check --apply ." + }, + "author": "tnfAngel", + "dependencies": { + "@elysiajs/cors": "^1.0.2", + "elysia": "^1.0.21", + "tslib": "^2.6.2" + }, + "devDependencies": { + "@biomejs/biome": "^1.7.3", + "@types/bun": "^1.1.3", + "typescript": "5.4.5" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } } diff --git a/src/classes/Server.ts b/src/classes/Server.ts index 3eee698..3e96543 100644 --- a/src/classes/Server.ts +++ b/src/classes/Server.ts @@ -1,3 +1,4 @@ +import cors from '@elysiajs/cors'; import { Elysia } from 'elysia'; export class Server { @@ -9,11 +10,11 @@ export class Server { this.initCORS(); this.initErrorListener(); this.initEndpoints(); - - this.elysia.listen(Server.port, ({ port }) => console.info(`Listening on: http://localhost:${port}`)); } - private initCORS(): void {} + private initCORS(): void { + this.elysia.use(cors()); + } private initErrorListener(): void { this.elysia.onError(({ code, error }) => { @@ -39,4 +40,8 @@ export class Server { } private initEndpoints(): void {} + + 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 d06af5d..69ab611 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ import { Server } from './classes/Server.ts'; -new Server(); +const sv = new Server(); + +sv.listen(); From 0fcb43f8df27e9c481870d0f5798453c5efc688e Mon Sep 17 00:00:00 2001 From: tnfAngel <57068341+tnfAngel@users.noreply.github.com> Date: Fri, 24 May 2024 09:50:57 +0100 Subject: [PATCH 5/5] autoroutes & test endpoint --- .vscode/settings.json | 4 +++- bun.lockb | Bin 10399 -> 10802 bytes package.json | 1 + src/classes/Server.ts | 12 ++++++++-- src/index.ts | 2 ++ src/routes/channels/[channelId]/messages.ts | 25 ++++++++++++++++++++ 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/routes/channels/[channelId]/messages.ts 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 504e720a628ab558b91e6a4f81bb30e8979ccfe4..cced51bc591f8ff1058262c06c3361ec71fc919b 100644 GIT binary patch delta 1881 zcmd5-YfM~46rQ=e7xuwrm*>78Q2DVnd)cL39)+@`XeC{3#XgqWh|m^f*IH-+EwKm^ zN@8qE@g%LSCe&g~p&PA|#%ha+v7*KrtbYu&Xlnlmu_30>plzt<+}#bPiTc+`X1;S~ z&N+AH%$e^F*M6|&sO;Ib@mt||$6e3dL(zBkPFwHYy}aLBH+io4=wNf|;P>asPJ5Wr zY(rsJPoXWY#K%m=dOO_K4x1w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8u}dCWoQq0 zvs|UT*Q5!rO5Z{AQoGNjHlIqzd|6&c-}p>kPsuuy`{^j=2KoVWBcjrCf>_qs7N6ni2$u3)S;++%T;U< zTg4r5+7}5lqkNe7KFYyb6~V;aqi0y?rPC4JBf^*Lu&Xh;7J1t#LO^ID8liL1uymHu z&1kB-(pSzL{bi_iciW>9-_@1qNF*6sg16`izO6^zUH;^)23V41OOes`SWqf4dM=id zmKaUPQandjVUtJl6WbBWa1hkV3RnqGyJ<9^zA^>7huaJ-Np>fA({3`a^hi zB~smqhZq{p(|WMG(tlRE3;iEoyR%E?$;75aCo-yZDDQ8tpVTv7&aPW-%k+jG)G96Q z$Ge@QH#az>bq9@BkU=Ni(nA|74RPo3zL(BzTivkk)uD@HV(dMg+Twk93^=plc!zXD zqUm_%z8g44(OF;a>00I!p0%Rw?zG!{_2%b$GVvr8BS8u@Z>9Y$8okooQ*gE1@A?}= CKq46c 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() + } + } + );