Skip to content

Commit a964826

Browse files
committed
🔧 fix: soundness for guard, group
1 parent d6f84eb commit a964826

File tree

3 files changed

+57
-78
lines changed

3 files changed

+57
-78
lines changed

src/index.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3918,9 +3918,7 @@ export default class Elysia<
39183918
Omit<Input, NonResolvableMacroKey>,
39193919
Definitions['typebox']
39203920
>,
3921-
const BeforeHandle extends MaybeArray<
3922-
OptionalHandler<Schema, Singleton>
3923-
>,
3921+
const BeforeHandle extends MaybeArray<OptionalHandler<Schema, Singleton>> ,
39243922
const AfterHandle extends MaybeArray<AfterHandler<Schema, Singleton>>,
39253923
const ErrorHandle extends MaybeArray<
39263924
ErrorHandler<Definitions['error'], Schema, Singleton>
@@ -3934,10 +3932,9 @@ export default class Elysia<
39343932
Schema & MacroContext,
39353933
Singleton & {
39363934
derive: Ephemeral['derive'] & Volatile['derive']
3937-
resolve: Ephemeral['resolve'] &
3938-
Volatile['resolve'] &
3939-
// @ts-ignore
3940-
MacroContext['response']
3935+
resolve: Ephemeral['resolve'] & Volatile['resolve'] &
3936+
// @ts-ignore
3937+
MacroContext['response']
39413938
},
39423939
keyof Metadata['parser'],
39433940
BeforeHandle,
@@ -3968,7 +3965,10 @@ export default class Elysia<
39683965
parser: Metadata['parser']
39693966
response: Metadata['response'] &
39703967
// @ts-ignore
3971-
MacroContext['response']
3968+
MacroContext['response'] &
3969+
ElysiaHandlerToResponseSchemaAmbiguous<BeforeHandle> &
3970+
ElysiaHandlerToResponseSchemaAmbiguous<AfterHandle> &
3971+
ElysiaHandlerToResponseSchemaAmbiguous<ErrorHandle>
39723972
},
39733973
{},
39743974
Ephemeral,
@@ -4144,9 +4144,7 @@ export default class Elysia<
41444144
>,
41454145
const GuardType extends GuardSchemaType,
41464146
const AsType extends LifeCycleType,
4147-
const BeforeHandle extends MaybeArray<
4148-
OptionalHandler<Schema, Singleton>
4149-
>,
4147+
const BeforeHandle extends MaybeArray<OptionalHandler<Schema, Singleton>> ,
41504148
const AfterHandle extends MaybeArray<AfterHandler<Schema, Singleton>>,
41514149
const ErrorHandle extends MaybeArray<
41524150
ErrorHandler<Definitions['error'], Schema, Singleton>
@@ -4158,10 +4156,9 @@ export default class Elysia<
41584156
Schema & MacroContext,
41594157
Singleton & {
41604158
derive: Ephemeral['derive'] & Volatile['derive']
4161-
resolve: Ephemeral['resolve'] &
4162-
Volatile['resolve'] &
4163-
// @ts-ignore
4164-
MacroContext['response']
4159+
resolve: Ephemeral['resolve'] & Volatile['resolve'] &
4160+
// @ts-ignore
4161+
MacroContext['response']
41654162
},
41664163
keyof Metadata['parser'],
41674164
BeforeHandle,
@@ -4504,7 +4501,10 @@ export default class Elysia<
45044501
parser: Metadata['parser']
45054502
response: Metadata['response'] &
45064503
// @ts-ignore
4507-
MacroContext['response']
4504+
MacroContext['response'] &
4505+
ElysiaHandlerToResponseSchemaAmbiguous<BeforeHandle> &
4506+
ElysiaHandlerToResponseSchemaAmbiguous<AfterHandle> &
4507+
ElysiaHandlerToResponseSchemaAmbiguous<ErrorHandle>
45084508
},
45094509
{},
45104510
Ephemeral,
@@ -4527,7 +4527,9 @@ export default class Elysia<
45274527
>
45284528
schema: Volatile['schema']
45294529
standaloneSchema: Volatile['standaloneSchema']
4530-
response: Volatile['response']
4530+
response: Volatile['response'] &
4531+
// @ts-ignore
4532+
MacroContext['response']
45314533
}
45324534
>
45334535

src/types.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,67 +1784,6 @@ export type GuardLocalHook<
17841784
tags?: DocumentDecoration['tags']
17851785
}
17861786

1787-
export type GuardLocalHook<
1788-
Input extends BaseMacro | undefined,
1789-
Schema extends RouteSchema,
1790-
Singleton extends SingletonBase,
1791-
Parser extends keyof any,
1792-
BeforeHandle extends MaybeArray<OptionalHandler<any, any>>,
1793-
AfterHandle extends MaybeArray<AfterHandler<any, any>>,
1794-
ErrorHandle extends MaybeArray<ErrorHandler<any, any, any>>,
1795-
GuardType extends GuardSchemaType = 'standalone',
1796-
AsType extends LifeCycleType = 'local',
1797-
> = (Input extends any ? Input : Prettify<Input>) & {
1798-
/**
1799-
* @default 'override'
1800-
*/
1801-
as?: AsType
1802-
/**
1803-
* @default 'standalone'
1804-
* @since 1.3.0
1805-
*/
1806-
schema?: GuardType
1807-
1808-
detail?: DocumentDecoration
1809-
/**
1810-
* Short for 'Content-Type'
1811-
*
1812-
* Available:
1813-
* - 'none': do not parse body
1814-
* - 'text' / 'text/plain': parse body as string
1815-
* - 'json' / 'application/json': parse body as json
1816-
* - 'formdata' / 'multipart/form-data': parse body as form-data
1817-
* - 'urlencoded' / 'application/x-www-form-urlencoded: parse body as urlencoded
1818-
* - 'arraybuffer': parse body as readable stream
1819-
*/
1820-
parse?: MaybeArray<BodyHandler<Schema, Singleton> | ContentType | Parser>
1821-
/**
1822-
* Transform context's value
1823-
*/
1824-
transform?: MaybeArray<TransformHandler<Schema, Singleton>>
1825-
/**
1826-
* Execute before main handler
1827-
*/
1828-
beforeHandle?: BeforeHandle
1829-
/**
1830-
* Execute after main handler
1831-
*/
1832-
afterHandle?: AfterHandle
1833-
/**
1834-
* Execute after main handler
1835-
*/
1836-
mapResponse?: MaybeArray<MapResponse<Schema, Singleton>>
1837-
/**
1838-
* Execute after response is sent
1839-
*/
1840-
afterResponse?: MaybeArray<AfterResponseHandler<Schema, Singleton>>
1841-
/**
1842-
* Catch error
1843-
*/
1844-
error?: ErrorHandle
1845-
tags?: DocumentDecoration['tags']
1846-
}
1847-
18481787
export type ComposedHandler = (context: Context) => MaybePromise<Response>
18491788

18501789
export interface InternalRoute {

test/types/lifecycle/soundness.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,25 @@ import { Prettify } from '../../../src/types'
19821982
)
19831983
)
19841984
)
1985+
1986+
expectTypeOf<(typeof app)['~Routes']['get']['response']>().toEqualTypeOf<{
1987+
200: {
1988+
playing: boolean
1989+
name: 'lilith'
1990+
limit: number
1991+
}
1992+
400: 'Bad Request'
1993+
401: 'Unauthorized'
1994+
422: {
1995+
type: 'validation'
1996+
on: string
1997+
summary?: string
1998+
message?: string
1999+
found?: unknown
2000+
property?: string
2001+
expected?: string
2002+
}
2003+
}>()
19852004
}
19862005

19872006
{
@@ -2024,6 +2043,25 @@ import { Prettify } from '../../../src/types'
20242043
)
20252044
)
20262045
)
2046+
2047+
expectTypeOf<(typeof app)['~Routes']['get']['response']>().toEqualTypeOf<{
2048+
200: {
2049+
playing: boolean
2050+
name: 'lilith'
2051+
limit: number
2052+
}
2053+
400: 'Bad Request'
2054+
401: 'Unauthorized'
2055+
422: {
2056+
type: 'validation'
2057+
on: string
2058+
summary?: string
2059+
message?: string
2060+
found?: unknown
2061+
property?: string
2062+
expected?: string
2063+
}
2064+
}>()
20272065
}
20282066

20292067
// Inherit macro context

0 commit comments

Comments
 (0)