��# API
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tnfAngel-Chat/dev
merge dev to stable
- Loading branch information
Showing
8 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"autoroutes", | ||
"elysia", | ||
"elysiajs" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
); |