@bogeychan/elysia-logger
Advanced tools
Comparing version 0.1.0-rc.1 to 0.1.0-rc.2
@@ -0,1 +1,3 @@ | ||
import type { RouteSchema } from "elysia"; | ||
import type { ElysiaLoggerContext, _INTERNAL_ElysiaLoggerPluginAutoLoggingState } from "../types"; | ||
export declare const formatters: { | ||
@@ -8,3 +10,9 @@ log(object: Record<string, unknown>): { | ||
}; | ||
export declare function isContext(object: unknown): {}; | ||
export declare function isRequest(object: unknown): string; | ||
export declare function isContext(object: unknown): object is ElysiaLoggerContext<RouteSchema, { | ||
request: {}; | ||
store: _INTERNAL_ElysiaLoggerPluginAutoLoggingState; | ||
derive: {}; | ||
resolve: {}; | ||
decorator: {}; | ||
}>; | ||
export declare function isRequest(object: unknown): object is Request; |
@@ -5,9 +5,14 @@ import { serializeRequest } from "./serializers"; | ||
if (isContext(object)) { | ||
const context = object; | ||
const log = { | ||
request: context.request, | ||
request: object.request, | ||
}; | ||
if (context.store.responseTime) { | ||
log.responseTime = context.store.responseTime; | ||
if (object.isError) { | ||
log.code = object.code; | ||
log.message = object.error.message; | ||
} | ||
else { | ||
if (object.store.responseTime) { | ||
log.responseTime = object.store.responseTime; | ||
} | ||
} | ||
return log; | ||
@@ -23,7 +28,18 @@ } | ||
const context = object; | ||
return context.request && context.store; | ||
switch (undefined) { | ||
case context.request: | ||
case context.store: | ||
case context.isError: | ||
return false; | ||
} | ||
return true; | ||
} | ||
export function isRequest(object) { | ||
const request = object; | ||
return request.url && request.method; | ||
switch (undefined) { | ||
case request.url: | ||
case request.method: | ||
return false; | ||
} | ||
return true; | ||
} |
@@ -16,5 +16,5 @@ import type { pino } from "pino"; | ||
export type ErrorContext<T extends Record<string, Error> = {}, Route extends RouteSchema = RouteSchema, Singleton extends SingletonBase = SingletonBase, Ephemeral extends EphemeralType = EphemeralType, Volatile extends EphemeralType = EphemeralType> = Parameters<ErrorHandler<T, Route, Singleton, Ephemeral, Volatile>>[0]; | ||
export type ElysiaLoggerContext = ({ | ||
export type ElysiaLoggerContext<Route extends RouteSchema = {}, Singleton extends SingletonBase = SingletonBase> = ({ | ||
isError: false; | ||
} & Context) | ({ | ||
} & Context<Route, Singleton>) | ({ | ||
isError: true; | ||
@@ -21,0 +21,0 @@ } & ErrorContext); |
{ | ||
"name": "@bogeychan/elysia-logger", | ||
"version": "0.1.0-rc.1", | ||
"version": "0.1.0-rc.2", | ||
"description": "A plugin for Elysia.js for logging using the pino library", | ||
@@ -42,3 +42,3 @@ "author": { | ||
"elysia": "1.0.27", | ||
"pino-pretty": "^10.3.1", | ||
"pino-pretty": "^11.2.1", | ||
"tsd": "^0.30.7", | ||
@@ -48,3 +48,3 @@ "typescript": "5" | ||
"dependencies": { | ||
"pino": "^9.1.0" | ||
"pino": "^9.2.0" | ||
}, | ||
@@ -51,0 +51,0 @@ "homepage": "https://github.com/bogeychan/elysia-logger", |
@@ -1,6 +0,9 @@ | ||
import type { Context } from "elysia"; | ||
import type { RouteSchema } from "elysia"; | ||
import type { LoggerOptions } from "pino"; | ||
import { serializeRequest } from "./serializers"; | ||
import type { _INTERNAL_ElysiaLoggerPluginAutoLoggingState } from "../types"; | ||
import type { | ||
ElysiaLoggerContext, | ||
_INTERNAL_ElysiaLoggerPluginAutoLoggingState, | ||
} from "../types"; | ||
@@ -10,19 +13,13 @@ export const formatters = { | ||
if (isContext(object)) { | ||
const context = object as unknown as Context< | ||
{}, | ||
{ | ||
request: {}; | ||
store: _INTERNAL_ElysiaLoggerPluginAutoLoggingState; | ||
derive: {}; | ||
resolve: {}; | ||
decorator: {}; | ||
} | ||
>; | ||
const log: Record<string, any> = { | ||
request: context.request, | ||
request: object.request, | ||
}; | ||
if (context.store.responseTime) { | ||
log.responseTime = context.store.responseTime; | ||
if (object.isError) { | ||
log.code = object.code; | ||
log.message = object.error.message; | ||
} else { | ||
if (object.store.responseTime) { | ||
log.responseTime = object.store.responseTime; | ||
} | ||
} | ||
@@ -32,3 +29,3 @@ | ||
} else if (isRequest(object)) { | ||
return serializeRequest(object as unknown as Request); | ||
return serializeRequest(object); | ||
} | ||
@@ -39,10 +36,30 @@ return object; | ||
export function isContext(object: unknown) { | ||
const context = object as Context; | ||
return context.request && context.store; | ||
export function isContext(object: unknown): object is ElysiaLoggerContext< | ||
RouteSchema, | ||
{ | ||
request: {}; | ||
store: _INTERNAL_ElysiaLoggerPluginAutoLoggingState; | ||
derive: {}; | ||
resolve: {}; | ||
decorator: {}; | ||
} | ||
> { | ||
const context = object as Partial<ElysiaLoggerContext>; | ||
switch (undefined) { | ||
case context.request: | ||
case context.store: | ||
case context.isError: | ||
return false; | ||
} | ||
return true; | ||
} | ||
export function isRequest(object: unknown) { | ||
const request = object as Request; | ||
return request.url && request.method; | ||
export function isRequest(object: unknown): object is Request { | ||
const request = object as Partial<Request>; | ||
switch (undefined) { | ||
case request.url: | ||
case request.method: | ||
return false; | ||
} | ||
return true; | ||
} |
@@ -43,4 +43,7 @@ import type { pino } from "pino"; | ||
export type ElysiaLoggerContext = | ||
| ({ isError: false } & Context) | ||
export type ElysiaLoggerContext< | ||
Route extends RouteSchema = {}, | ||
Singleton extends SingletonBase = SingletonBase | ||
> = | ||
| ({ isError: false } & Context<Route, Singleton>) | ||
| ({ isError: true } & ErrorContext); | ||
@@ -47,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28000
659
Updatedpino@^9.2.0