Skip to content

Commit

Permalink
chore: update code to add type assertions for beforeTime property
Browse files Browse the repository at this point in the history
Updated the code to add type assertions for the `beforeTime` property resolve type checking errors
and ensure proper type inference
  • Loading branch information
PunGrumpy committed Nov 5, 2023
1 parent 8f0f1ee commit a3c93b0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logixlysia",
"version": "0.0.2-alpha.0",
"version": "0.0.2-alpha.1",
"description": "🪵 Logixlysia is a logger for Elysia",
"type": "module",
"module": "src/index.ts",
Expand Down
97 changes: 64 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,79 @@ import process from 'process'
import { durationString } from './utils/duration'
import { methodString } from './utils/method'

const logRequest = (
request: {
headers: { get: (key: string) => any }
method: string
url: string
},
store: {
beforeTime: bigint
}
) => {
const logStr = []

if (request.headers.get('X-Forwarded-For')) {
logStr.push(`[${pc.cyan(request.headers.get('X-Forwarded-For'))}]`)
}

logStr.push(methodString(request.method))
logStr.push(new URL(request.url).pathname)
logStr.push(durationString(store.beforeTime))

console.log(logStr.join(' '))
}

const logError = (
request: {
headers: { get: (key: string) => any }
method: string
url: string
},
error: {
status?: number
message?: string
},
store: {
beforeTime: bigint
}
) => {
const logStr = []

logStr.push(pc.red(methodString(request.method)))
logStr.push(new URL(request.url).pathname)
logStr.push(pc.red('Error'))

if (error.status !== undefined) {
logStr.push(String(error.status))
}

if (error.message !== undefined) {
logStr.push(error.message)
}

logStr.push(durationString(store.beforeTime))

console.log(logStr.join(' '))
}

export const logger = () =>
new Elysia({
name: 'logixlysia'
})
.onRequest(ctx => {
ctx.store = { ...ctx.store, beforeTime: process.hrtime.bigint() }
ctx.store = { beforeTime: process.hrtime.bigint() } as {
beforeTime: bigint
}
})
.onBeforeHandle(ctx => {
ctx.store = { ...ctx.store, beforeTime: process.hrtime.bigint() }
ctx.store = { beforeTime: process.hrtime.bigint() } as {
beforeTime: bigint
}
})
.onAfterHandle(({ request, store }) => {
const logStr: string[] = []
if (request.headers.get('X-Forwarded-For')) {
logStr.push(`[${pc.cyan(request.headers.get('X-Forwarded-For'))}]`)
}

logStr.push(methodString(request.method))

logStr.push(new URL(request.url).pathname)
const beforeTime: bigint = (store as any).beforeTime

logStr.push(durationString(beforeTime))

console.log(logStr.join(' '))
logRequest(request, store as { beforeTime: bigint })
})
.onError(({ request, error, store }) => {
const logStr: string[] = []

logStr.push(pc.red(methodString(request.method)))

logStr.push(new URL(request.url).pathname)

logStr.push(pc.red('Error'))

if ('status' in error) {
logStr.push(String(error.status))
}

logStr.push(error.message)
const beforeTime: bigint = (store as any).beforeTime

logStr.push(durationString(beforeTime))

console.log(logStr.join(' '))
logError(request, error, store as { beforeTime: bigint })
})

0 comments on commit a3c93b0

Please sign in to comment.