From 26264bc8bcccd19889702340b20e819318f9a7d2 Mon Sep 17 00:00:00 2001 From: saltyaom Date: Thu, 21 Sep 2023 11:05:58 +0700 Subject: [PATCH] :wrench: fix: check for object-like prototype --- CHANGELOG.md | 5 +++++ package.json | 2 +- src/index.ts | 33 ++++++++++++++++++++++++--------- src/types.ts | 38 +++++++++++++++++++++++++++++--------- src/utils.ts | 8 +++++--- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88a5a1d9..92d415ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.7.4 - 21 Sep 2023 +Bug fix: +- check for class-like object +- add `GraceHandler` to access both `app` and `context` + # 0.7.3 - 21 Sep 2023 Bug fix: - resolve 200 by default when type is not provided diff --git a/package.json b/package.json index a204438f..bbce0a53 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "elysia", "description": "Ergonomic Framework for Human", - "version": "0.7.3", + "version": "0.7.4", "author": { "name": "saltyAom", "url": "https://github.com/SaltyAom", diff --git a/src/index.ts b/src/index.ts index 4ec8bc89..33d1cd8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,7 +74,8 @@ import type { AddSuffixCapitalize, TraceReporter, TraceHandler, - MaybeArray + MaybeArray, + GracefulHandler } from './types' /** @@ -397,8 +398,8 @@ export default class Elysia< * .listen(8080) * ``` */ - onStart(handler: MaybeArray>) { - this.on('start', handler) + onStart(handler: MaybeArray>) { + this.on('start', handler as any) return this } @@ -893,8 +894,8 @@ export default class Elysia< * }) * ``` */ - onStop(handler: VoidHandler) { - this.on('stop', handler) + onStop(handler: MaybeArray>) { + this.on('stop', handler as any) return this } @@ -3081,8 +3082,15 @@ export default class Elysia< this.server = Bun?.serve(serve) - for (let i = 0; i < this.event.start.length; i++) - this.event.start[i](this as any) + if (this.event.start.length) { + const context = Object.assign(this.decorators, { + store: this.store, + app: this + }) + + for (let i = 0; i < this.event.start.length; i++) + this.event.start[i](context) + } if (callback) callback(this.server!) @@ -3116,8 +3124,15 @@ export default class Elysia< this.server.stop() - for (let i = 0; i < this.event.stop.length; i++) - await this.event.stop[i](this as any) + if (this.event.stop.length) { + const context = Object.assign(this.decorators, { + store: this.store, + app: this + }) + + for (let i = 0; i < this.event.stop.length; i++) + await this.event.stop[i](context) + } } /** diff --git a/src/types.ts b/src/types.ts index 2ae90544..6a70ef03 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,8 +15,12 @@ import type { ParseError, ValidationError } from './error' +import Elysia from '.' -export type ElysiaConfig = { +export type ElysiaConfig< + T extends string = '', + Scoped extends boolean = false +> = { name?: string seed?: unknown serve?: Partial @@ -228,7 +232,7 @@ export type UnwrapGroupGuardRoute< export interface LifeCycleStore { type?: ContentType - start: PreHandler[] + start: GracefulHandler[] request: PreHandler[] parse: BodyHandler[] transform: VoidHandler[] @@ -237,7 +241,7 @@ export interface LifeCycleStore { onResponse: VoidHandler[] trace: TraceHandler[] error: ErrorHandler[] - stop: VoidHandler[] + stop: GracefulHandler[] } export type LifeCycleEvent = @@ -465,6 +469,22 @@ export type PreHandler< context: Prettify> ) => MaybePromise +export type GracefulHandler< + Instance extends Elysia, + Decorators extends DecoratorBase = { + request: {} + store: {} + } +> = ( + data: { + app: Instance + } & Prettify< + Decorators['request'] & { + store: Decorators['store'] + } + > +) => any + export type ErrorHandler< T extends Record = {}, Route extends RouteSchema = {}, @@ -506,12 +526,12 @@ export type ErrorHandler< error: Readonly set: Context['set'] } - | { - request: Request - code: 'INVALID_COOKIE_SIGNATURE' - error: Readonly - set: Context['set'] - } + | { + request: Request + code: 'INVALID_COOKIE_SIGNATURE' + error: Readonly + set: Context['set'] + } | { [K in keyof T]: { request: Request diff --git a/src/utils.ts b/src/utils.ts index b9661419..5b1c6274 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,8 @@ import { Kind, TSchema } from '@sinclair/typebox' import { Value } from '@sinclair/typebox/value' import { TypeCheck, TypeCompiler } from '@sinclair/typebox/compiler' +import { isNotEmpty } from './handler' + import type { LifeCycleStore, LocalHook, @@ -15,7 +17,9 @@ const isObject = (item: any): item is Object => const isClass = (v: Object) => (typeof v === 'function' && /^\s*class\s+/.test(v.toString())) || // Handle import * as Sentry from '@sentry/bun' - v.toString() === '[object Module]' + v.toString() === '[object Module]' || + // If object prototype is not pure, then probably a class-like object + isNotEmpty(Object.getPrototypeOf(v)) export const mergeDeep = < const A extends Record, @@ -48,8 +52,6 @@ export const mergeDeep = < continue } - console.log("B") - target[key as keyof typeof target] = mergeDeep( (target as any)[key] as any, value