Skip to content

Commit

Permalink
🎉 feat: optimize resolve handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Nov 20, 2024
1 parent 86d7928 commit 5199c61
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/adapter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ export interface ElysiaAdapter {
ws?(
app: AnyElysia,
path: string,
handler: WSLocalHook<any, any, any, any, any, any, any>
handler: WSLocalHook<any, any, any, any, any>
): unknown
}
86 changes: 22 additions & 64 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2560,7 +2560,11 @@ export default class Elysia<
keyof UnwrapTypeModule<Definitions['typebox']> & string
>,
const Schema extends MergeSchema<
UnwrapRoute<Input, Definitions['typebox'], JoinPath<BasePath, Prefix>>,
UnwrapRoute<
Input,
Definitions['typebox'],
JoinPath<BasePath, Prefix>
>,
Metadata['schema']
>,
const Resolutions extends MaybeArray<
Expand Down Expand Up @@ -2734,11 +2738,7 @@ export default class Elysia<
keyof UnwrapTypeModule<Definitions['typebox']> & string
>,
const Schema extends MergeSchema<
UnwrapRoute<
LocalSchema,
Definitions['typebox'],
BasePath
>,
UnwrapRoute<LocalSchema, Definitions['typebox'], BasePath>,
Metadata['schema']
>,
const Type extends LifeCycleType,
Expand All @@ -2755,16 +2755,9 @@ export default class Elysia<
hook: { as: Type } & LocalHook<
LocalSchema,
Schema,
{
decorator: Singleton['decorator']
store: Singleton['store']
derive: Singleton['derive'] &
Ephemeral['derive'] &
Volatile['derive']
resolve: Singleton['resolve'] &
Ephemeral['resolve'] &
Volatile['resolve'] &
ResolveResolutions<Resolutions>
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
},
Definitions['error'],
Metadata['macro'],
Expand Down Expand Up @@ -2852,11 +2845,7 @@ export default class Elysia<
keyof UnwrapTypeModule<Definitions['typebox']> & string
>,
const Schema extends MergeSchema<
UnwrapRoute<
LocalSchema,
Definitions['typebox'],
BasePath
>,
UnwrapRoute<LocalSchema, Definitions['typebox'], BasePath>,
Metadata['schema']
>,
const Resolutions extends MaybeArray<
Expand All @@ -2872,16 +2861,9 @@ export default class Elysia<
hook: LocalHook<
LocalSchema,
Schema,
{
decorator: Singleton['decorator']
store: Singleton['store']
derive: Singleton['derive'] &
Ephemeral['derive'] &
Volatile['derive']
resolve: Singleton['resolve'] &
Ephemeral['resolve'] &
Volatile['resolve'] &
ResolveResolutions<Resolutions>
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
},
Definitions['error'],
Metadata['macro'],
Expand All @@ -2901,11 +2883,7 @@ export default class Elysia<
>
schema: Prettify<
MergeSchema<
UnwrapRoute<
LocalSchema,
Definitions['typebox'],
BasePath
>,
UnwrapRoute<LocalSchema, Definitions['typebox'], BasePath>,
MergeSchema<
Volatile['schema'],
MergeSchema<Ephemeral['schema'], Metadata['schema']>
Expand All @@ -2921,11 +2899,7 @@ export default class Elysia<
>,
const NewElysia extends AnyElysia,
const Schema extends MergeSchema<
UnwrapRoute<
LocalSchema,
Definitions['typebox'],
BasePath
>,
UnwrapRoute<LocalSchema, Definitions['typebox'], BasePath>,
Metadata['schema']
>
>(
Expand Down Expand Up @@ -2960,11 +2934,7 @@ export default class Elysia<
>,
const NewElysia extends AnyElysia,
const Schema extends MergeSchema<
UnwrapRoute<
LocalSchema,
Definitions['typebox'],
BasePath
>,
UnwrapRoute<LocalSchema, Definitions['typebox'], BasePath>,
Metadata['schema']
>,
const Resolutions extends MaybeArray<
Expand All @@ -2980,16 +2950,9 @@ export default class Elysia<
schema: LocalHook<
LocalSchema,
Schema,
{
decorator: Singleton['decorator']
store: Singleton['store']
derive: Singleton['derive'] &
Ephemeral['derive'] &
Volatile['derive']
resolve: Singleton['resolve'] &
Ephemeral['resolve'] &
Volatile['resolve'] &
ResolveResolutions<Resolutions>
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
},
Definitions['error'],
Metadata['macro'],
Expand Down Expand Up @@ -3805,9 +3768,7 @@ export default class Elysia<
const Resolutions extends MaybeArray<
ResolveHandler<
Schema,
{
decorator: Singleton['decorator']
store: Singleton['store']
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
}
Expand Down Expand Up @@ -3914,8 +3875,7 @@ export default class Elysia<
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] &
Volatile['resolve'] &
ResolveResolutions<Resolutions>
Volatile['resolve']
},
JoinPath<BasePath, Path>,
Metadata['macroFn'],
Expand Down Expand Up @@ -4774,9 +4734,7 @@ export default class Elysia<
const Resolutions extends MaybeArray<
ResolveHandler<
Schema,
{
decorator: Singleton['decorator']
store: Singleton['store']
Singleton & {
derive: Ephemeral['derive'] & Volatile['derive']
resolve: Ephemeral['resolve'] & Volatile['resolve']
}
Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ export type DocumentDecoration = Partial<OpenAPIV3.OperationObject> & {

export type ResolveHandler<
in out Route extends RouteSchema,
Singleton extends SingletonBase,
in out Singleton extends SingletonBase,
in out Derivative extends Record<string, unknown> | void = Record<
string,
unknown
Expand Down Expand Up @@ -1047,7 +1047,7 @@ export type LocalHook<
Singleton extends SingletonBase,
Errors extends Record<string, Error>,
Extension extends BaseMacro,
Resolutions extends MaybeArray<ResolveHandler<Schema, Singleton>>
Resolutions extends MaybeArray<ResolveHandler<any, any>>
> = (LocalSchema extends {} ? LocalSchema : Isolate<LocalSchema>) &
Extension & {
/**
Expand Down Expand Up @@ -1121,7 +1121,6 @@ export type LocalHook<
*/
error?: MaybeArray<ErrorHandler<Errors, Schema, Singleton>>
tags?: DocumentDecoration['tags']
websocket?: WSLocalHook<any, any, any, any, any, any, any>
}

export type ComposedHandler = (context: Context) => MaybePromise<Response>
Expand All @@ -1133,7 +1132,7 @@ export interface InternalRoute {
handler: Handler
compile(): Function
hooks: LocalHook<any, any, any, any, any, any>
websocket?: WSLocalHook<any, any, any, any, any, any, any>
websocket?: WSLocalHook<any, any, any, any, any>
}

export type SchemaValidator = {
Expand Down
75 changes: 18 additions & 57 deletions src/ws/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
OptionalHandler,
Prettify,
ResolveHandler,
ResolvePath,
ResolveResolutions,
RouteSchema,
SingletonBase,
Expand Down Expand Up @@ -126,19 +125,7 @@ export type WSLocalHook<
Schema extends RouteSchema,
Singleton extends SingletonBase,
Extension extends BaseMacro,
Resolutions extends MaybeArray<ResolveHandler<Schema, Singleton>>,
Path extends string = '',
TypedRoute extends RouteSchema = Prettify<
Schema extends {
params: Record<string, unknown>
}
? Schema
: Schema & {
params: undefined extends Schema['params']
? ResolvePath<Path>
: Schema['params']
}
>
Resolutions extends MaybeArray<ResolveHandler<Schema, Singleton>>
> = (LocalSchema extends {} ? LocalSchema : Isolate<LocalSchema>) &
Extension & {
/**
Expand All @@ -158,35 +145,21 @@ export type WSLocalHook<
* Headers to register to websocket before `upgrade`
*/
upgrade?: Record<string, unknown> | ((context: Context) => unknown)
parse?: MaybeArray<WSParseHandler<TypedRoute>>
parse?: MaybeArray<WSParseHandler<Schema>>

/**
* Transform context's value
*/
transform?: MaybeArray<
TransformHandler<
TypedRoute,
{
decorator: Singleton['decorator']
store: Singleton['decorator']
derive: Singleton['derive']
resolve: {}
}
>
>
transform?: MaybeArray<TransformHandler<Schema, Singleton>>
resolve?: Resolutions
/**
* Execute before main handler
*/
beforeHandle?: MaybeArray<
OptionalHandler<
TypedRoute,
{
decorator: Singleton['decorator']
store: Singleton['decorator']
derive: Singleton['derive']
resolve: Singleton['resolve'] &
ResolveResolutions<Resolutions>
Schema,
Singleton & {
resolve: ResolveResolutions<Resolutions>
}
>
>
Expand All @@ -195,13 +168,9 @@ export type WSLocalHook<
*/
afterHandle?: MaybeArray<
AfterHandler<
TypedRoute,
{
decorator: Singleton['decorator']
store: Singleton['decorator']
derive: Singleton['derive']
resolve: Singleton['resolve'] &
ResolveResolutions<Resolutions>
Schema,
Singleton & {
resolve: ResolveResolutions<Resolutions>
}
>
>
Expand All @@ -210,13 +179,9 @@ export type WSLocalHook<
*/
mapResponse?: MaybeArray<
MapResponse<
TypedRoute,
{
decorator: Singleton['decorator']
store: Singleton['decorator']
derive: Singleton['derive']
resolve: Singleton['resolve'] &
ResolveResolutions<Resolutions>
Schema,
Singleton & {
resolve: ResolveResolutions<Resolutions>
}
>
>
Expand All @@ -225,25 +190,21 @@ export type WSLocalHook<
*/
afterResponse?: MaybeArray<
AfterResponseHandler<
TypedRoute,
{
decorator: Singleton['decorator']
store: Singleton['decorator']
derive: Singleton['derive']
resolve: Singleton['resolve'] &
ResolveResolutions<Resolutions>
Schema,
Singleton & {
resolve: ResolveResolutions<Resolutions>
}
>
>
/**
* Catch error
*/
error?: MaybeArray<ErrorHandler<{}, TypedRoute, Singleton>>
error?: MaybeArray<ErrorHandler<{}, Schema, Singleton>>
tags?: DocumentDecoration['tags']
} & TypedWebSocketHandler<
Omit<
Context<
TypedRoute,
Schema,
Singleton & {
resolve: ResolveResolutions<Resolutions>
}
Expand All @@ -252,5 +213,5 @@ export type WSLocalHook<
> & {
body: never
},
TypedRoute
Schema
>

0 comments on commit 5199c61

Please sign in to comment.