Skip to content

Commit

Permalink
🔧 fix: check for object-like prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Sep 21, 2023
1 parent 5e9edfb commit 26264bc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ import type {
AddSuffixCapitalize,
TraceReporter,
TraceHandler,
MaybeArray
MaybeArray,
GracefulHandler
} from './types'

/**
Expand Down Expand Up @@ -397,8 +398,8 @@ export default class Elysia<
* .listen(8080)
* ```
*/
onStart(handler: MaybeArray<PreHandler<ParentSchema, Decorators>>) {
this.on('start', handler)
onStart(handler: MaybeArray<GracefulHandler<this, Decorators>>) {
this.on('start', handler as any)

return this
}
Expand Down Expand Up @@ -893,8 +894,8 @@ export default class Elysia<
* })
* ```
*/
onStop(handler: VoidHandler<ParentSchema, Decorators>) {
this.on('stop', handler)
onStop(handler: MaybeArray<GracefulHandler<this, Decorators>>) {
this.on('stop', handler as any)

return this
}
Expand Down Expand Up @@ -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!)

Expand Down Expand Up @@ -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)
}
}

/**
Expand Down
38 changes: 29 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import type {
ParseError,
ValidationError
} from './error'
import Elysia from '.'

export type ElysiaConfig<T extends string = '', Scoped extends boolean = false> = {
export type ElysiaConfig<
T extends string = '',
Scoped extends boolean = false
> = {
name?: string
seed?: unknown
serve?: Partial<Serve>
Expand Down Expand Up @@ -228,7 +232,7 @@ export type UnwrapGroupGuardRoute<

export interface LifeCycleStore {
type?: ContentType
start: PreHandler<any, any>[]
start: GracefulHandler<any, any>[]
request: PreHandler<any, any>[]
parse: BodyHandler<any, any>[]
transform: VoidHandler<any, any>[]
Expand All @@ -237,7 +241,7 @@ export interface LifeCycleStore {
onResponse: VoidHandler<any, any>[]
trace: TraceHandler<any, any>[]
error: ErrorHandler<any, any, any>[]
stop: VoidHandler<any, any>[]
stop: GracefulHandler<any, any>[]
}

export type LifeCycleEvent =
Expand Down Expand Up @@ -465,6 +469,22 @@ export type PreHandler<
context: Prettify<PreContext<Route, Decorators>>
) => MaybePromise<Route['response'] | void>

export type GracefulHandler<
Instance extends Elysia<any, any, any, any, any, any>,
Decorators extends DecoratorBase = {
request: {}
store: {}
}
> = (
data: {
app: Instance
} & Prettify<
Decorators['request'] & {
store: Decorators['store']
}
>
) => any

export type ErrorHandler<
T extends Record<string, Error> = {},
Route extends RouteSchema = {},
Expand Down Expand Up @@ -506,12 +526,12 @@ export type ErrorHandler<
error: Readonly<InternalServerError>
set: Context['set']
}
| {
request: Request
code: 'INVALID_COOKIE_SIGNATURE'
error: Readonly<InvalidCookieSignature>
set: Context['set']
}
| {
request: Request
code: 'INVALID_COOKIE_SIGNATURE'
error: Readonly<InvalidCookieSignature>
set: Context['set']
}
| {
[K in keyof T]: {
request: Request
Expand Down
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string, any>,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 26264bc

Please sign in to comment.