logixlysia 4.0.0 → 4.1.1
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 +14 -0
- package/README.md +3 -0
- package/package.json +8 -7
- package/src/index.ts +18 -8
- package/src/utils/status.ts +14 -11
- package/tests/utils/status.test.ts +46 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [4.1.1](https://github.com/PunGrumpy/logixlysia/compare/v4.1.0...v4.1.1) (2025-01-31)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **package:** wrong place @elysiajs/swagger and move to devDependencies ([a5e1a96](https://github.com/PunGrumpy/logixlysia/commit/a5e1a96606f53883a50331a94c1ba844dff67dc3))
|
|
9
|
+
|
|
10
|
+
## [4.1.0](https://github.com/PunGrumpy/logixlysia/compare/v4.0.0...v4.1.0) (2025-01-31)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **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)
|
|
16
|
+
|
|
3
17
|
## [4.0.0](https://github.com/PunGrumpy/logixlysia/compare/v3.7.0...v4.0.0) (2024-10-28)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "logixlysia",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "🦊 Logixlysia is a logger for Elysia",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -77,21 +77,22 @@
|
|
|
77
77
|
],
|
|
78
78
|
"dependencies": {
|
|
79
79
|
"chalk": "^5.3.0",
|
|
80
|
-
"elysia": "^1.1.
|
|
80
|
+
"elysia": "^1.1.23"
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
|
+
"@elysiajs/swagger": "^1.2.0",
|
|
83
84
|
"@elysiajs/eden": "^1.1.3",
|
|
84
|
-
"@eslint/js": "^9.
|
|
85
|
+
"@eslint/js": "^9.13.0",
|
|
85
86
|
"@trunkio/launcher": "^1.3.2",
|
|
86
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
87
|
-
"@typescript-eslint/parser": "^8.
|
|
88
|
-
"bun-types": "^1.1.
|
|
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.
|
|
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
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
package/src/utils/status.ts
CHANGED
|
@@ -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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
+
})
|