Skip to content

Commit

Permalink
Merge pull request #3 from tnfAngel-Chat/dev
Browse files Browse the repository at this point in the history
merge dev to stable
  • Loading branch information
tnfAngel authored May 24, 2024
2 parents e80508a + 0fcb43f commit 6b10d08
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": [
"autoroutes",
"elysia",
"elysiajs"
]
}
Binary file modified README.md
Binary file not shown.
Binary file modified bun.lockb
Binary file not shown.
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{
"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",
"elysia-autoroutes": "^0.5.0",
"tslib": "^2.6.2"
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@types/bun": "^1.1.3",
"typescript": "5.4.5"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
55 changes: 55 additions & 0 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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;

public elysia: Elysia = new Elysia({ precompile: true });

public constructor() {
this.initCORS();
this.initErrorListener();
this.initRoutes();
}

private initCORS(): void {
this.elysia.use(cors());
}

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 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}`));
}
}
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
console.log('Hello world');
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 6b10d08

Please sign in to comment.