logixlysia 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.1.0](https://github.com/PunGrumpy/logixlysia/compare/v4.0.0...v4.1.0) (2025-01-31)
4
+
5
+
6
+ ### Features
7
+
8
+ * **logging:** add status string parsing and error handling ([32800ac](https://github.com/PunGrumpy/logixlysia/commit/32800acb0fbfdf957e2975b5773b0a350e3af0d8)), closes [#71](https://github.com/PunGrumpy/logixlysia/issues/71)
9
+
3
10
  ## [4.0.0](https://github.com/PunGrumpy/logixlysia/compare/v3.7.0...v4.0.0) (2024-10-28)
4
11
 
5
12
 
package/README.md CHANGED
@@ -45,6 +45,9 @@ app.listen(3000)
45
45
  > [!NOTE]
46
46
  > You can discover more about example in the [example](example) directory.
47
47
 
48
+ > [!TIP]
49
+ > Also, you can play my example with Swagger UI on `http://localhost:3000/swagger`.
50
+
48
51
  ## `📚` Documentation
49
52
 
50
53
  ### Options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -76,22 +76,23 @@
76
76
  "middleware"
77
77
  ],
78
78
  "dependencies": {
79
+ "@elysiajs/swagger": "^1.2.0",
79
80
  "chalk": "^5.3.0",
80
- "elysia": "^1.1.19"
81
+ "elysia": "^1.1.23"
81
82
  },
82
83
  "devDependencies": {
83
84
  "@elysiajs/eden": "^1.1.3",
84
- "@eslint/js": "^9.12.0",
85
+ "@eslint/js": "^9.13.0",
85
86
  "@trunkio/launcher": "^1.3.2",
86
- "@typescript-eslint/eslint-plugin": "^8.8.1",
87
- "@typescript-eslint/parser": "^8.8.1",
88
- "bun-types": "^1.1.30",
87
+ "@typescript-eslint/eslint-plugin": "^8.11.0",
88
+ "@typescript-eslint/parser": "^8.11.0",
89
+ "bun-types": "^1.1.33",
89
90
  "eslint-plugin-simple-import-sort": "^12.1.1",
90
91
  "globals": "^15.11.0",
91
92
  "husky": "^9.1.6",
92
93
  "lint-staged": "^15.2.10",
93
94
  "prettier": "^3.3.3",
94
- "typescript-eslint": "^8.8.1"
95
+ "typescript-eslint": "^8.11.0"
95
96
  },
96
97
  "peerDependencies": {
97
98
  "typescript": "^5.2.2"
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ import { Elysia } from 'elysia'
3
3
  import { createLogger } from './core'
4
4
  import { startServer } from './plugins'
5
5
  import { HttpError, Options, Server } from './types'
6
+ import { getStatusCode } from './utils/status'
6
7
 
7
8
  export default function logixlysia(options?: Options): Elysia {
8
9
  const log = createLogger(options)
@@ -11,19 +12,29 @@ export default function logixlysia(options?: Options): Elysia {
11
12
  name: 'Logixlysia'
12
13
  })
13
14
  .onStart(ctx => {
14
- const showStartupMessage = options?.config?.showStartupMessage ?? true
15
- if (showStartupMessage) startServer(ctx.server as Server, options)}
16
- )
15
+ const showStartupMessage = options?.config?.showStartupMessage ?? true
16
+ if (showStartupMessage) startServer(ctx.server as Server, options)
17
+ })
17
18
  .onRequest(ctx => {
18
19
  ctx.store = { beforeTime: process.hrtime.bigint() }
19
20
  })
20
- .onAfterHandle({ as: 'global' }, ({ request, store }) => {
21
- log.log('INFO', request, { status: 200 }, store as { beforeTime: bigint })
21
+ .onAfterHandle({ as: 'global' }, ({ request, set, store }) => {
22
+ const status = getStatusCode(set.status || 200)
23
+ log.log(
24
+ 'INFO',
25
+ request,
26
+ {
27
+ status,
28
+ message: set.headers?.['x-message'] || ''
29
+ },
30
+ store as { beforeTime: bigint }
31
+ )
22
32
  })
23
- .onError({ as: 'global' }, ({ request, error, store }) => {
33
+ .onError({ as: 'global' }, ({ request, error, set, store }) => {
34
+ const status = getStatusCode(set.status || 500)
24
35
  log.handleHttpError(
25
36
  request,
26
- error as HttpError,
37
+ { ...error, status } as HttpError,
27
38
  store as { beforeTime: bigint }
28
39
  )
29
40
  })
@@ -31,4 +42,3 @@ export default function logixlysia(options?: Options): Elysia {
31
42
 
32
43
  export { createLogger, handleHttpError } from './core'
33
44
  export { logToTransports } from './transports'
34
-
@@ -1,18 +1,21 @@
1
1
  import chalk from 'chalk'
2
+ import { StatusMap } from 'elysia'
3
+
4
+ export function getStatusCode(status: string | number): number {
5
+ if (typeof status === 'number') return status
6
+ return (StatusMap as Record<string, number>)[status] || 500
7
+ }
2
8
 
3
9
  export default function statusString(
4
10
  status: number,
5
11
  useColors: boolean
6
12
  ): string {
7
- const color =
8
- status >= 500
9
- ? 'red'
10
- : status >= 400
11
- ? 'yellow'
12
- : status >= 300
13
- ? 'cyan'
14
- : status >= 200
15
- ? 'green'
16
- : 'white'
17
- return useColors ? chalk[color](status.toString()) : status.toString()
13
+ const statusStr = status.toString()
14
+ if (!useColors) return statusStr
15
+
16
+ if (status >= 500) return chalk.red(statusStr)
17
+ if (status >= 400) return chalk.yellow(statusStr)
18
+ if (status >= 300) return chalk.cyan(statusStr)
19
+ if (status >= 200) return chalk.green(statusStr)
20
+ return chalk.white(statusStr)
18
21
  }
@@ -0,0 +1,46 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { Elysia } from 'elysia'
3
+
4
+ import logixlysia from '../../src/index'
5
+
6
+ test('handles numeric status codes', async () => {
7
+ const app = new Elysia().use(logixlysia()).get('/rate-limited', ({ set }) => {
8
+ set.status = 429
9
+ return 'Rate Limited'
10
+ })
11
+
12
+ const res = await app.handle(new Request('http://localhost/rate-limited'))
13
+ expect(res.status).toBe(429)
14
+ })
15
+
16
+ test('handles string status codes', async () => {
17
+ const app = new Elysia().use(logixlysia()).get('/not-found', ({ set }) => {
18
+ set.status = 'Not Found'
19
+ return 'Resource not found'
20
+ })
21
+
22
+ const res = await app.handle(new Request('http://localhost/not-found'))
23
+ expect(res.status).toBe(404)
24
+ })
25
+
26
+ test('handles custom error status', async () => {
27
+ const app = new Elysia().use(logixlysia()).get('/error', ({ set }) => {
28
+ set.status = 418
29
+ const error = new Error('Custom error')
30
+ throw error
31
+ })
32
+
33
+ const res = await app.handle(new Request('http://localhost/error'))
34
+ expect(res.status).toBe(418)
35
+ })
36
+
37
+ test('handles custom error status with message', async () => {
38
+ const app = new Elysia().use(logixlysia()).get('/error', ({ set }) => {
39
+ set.status = "I'm a teapot"
40
+ const error = new Error('Custom error')
41
+ throw error
42
+ })
43
+
44
+ const res = await app.handle(new Request('http://localhost/error'))
45
+ expect(res.status).toBe(418)
46
+ })