Skip to content

Commit cccd591

Browse files
committed
📘 feat: infer 200 response from handle if not specified
1 parent fad6445 commit cccd591

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.21 - 13 Oct 2024
2+
Improvement:
3+
- infer 200 response from handle if not specified
4+
15
# 1.1.20 - 10 Oct 2024
26
Bug fix:
37
- merge guard and not specified hook responses status

example/a.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { Elysia, t } from '../src'
22

3-
const app = new Elysia()
4-
.guard({
5-
response: {
6-
400: t.String(),
7-
500: t.String()
8-
}
9-
})
10-
.get('/', () => '', {
11-
response: t.String()
12-
})
3+
const main = new Elysia().get('/', () => 'a', {
4+
response: { 200: t.Number({
5+
default: () => 'a'
6+
}), 500: t.String() }
7+
})
138

14-
console.log(app.routes[0].hooks)
9+
type A = (typeof main)['_routes']['index']['get']['response']

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.1.20",
4+
"version": "1.1.21",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/types.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,24 +1196,26 @@ export type ComposeElysiaResponse<Response, Handle> = Handle extends (
11961196
: _ComposeElysiaResponse<Response, Replace<Awaited<Handle>, BunFile, File>>
11971197

11981198
type _ComposeElysiaResponse<Response, Handle> = Prettify<
1199-
{} extends Response
1200-
? {
1201-
200: Exclude<Handle, ElysiaCustomStatusResponse<any, any, any>>
1202-
} & {
1203-
[ErrorResponse in Extract<
1204-
Handle,
1205-
ElysiaCustomStatusResponse<any, any, any>
1206-
> as ErrorResponse extends ElysiaCustomStatusResponse<
1207-
any,
1208-
any,
1209-
any
1210-
>
1211-
? ErrorResponse['code']
1212-
: never]: ErrorResponse['response']
1213-
}
1214-
: Response
1199+
Prettify<
1200+
{
1201+
200: Exclude<
1202+
Handle,
1203+
ElysiaCustomStatusResponse<any, any, any>
1204+
>
1205+
} & ExtractErrorFromHandle<Handle> &
1206+
({} extends Response ? {} : Omit<Response, 200>)
1207+
>
12151208
>
12161209

1210+
type ExtractErrorFromHandle<Handle> = {
1211+
[ErrorResponse in Extract<
1212+
Handle,
1213+
ElysiaCustomStatusResponse<any, any, any>
1214+
> as ErrorResponse extends ElysiaCustomStatusResponse<any, any, any>
1215+
? ErrorResponse['code']
1216+
: never]: ErrorResponse['response']
1217+
}
1218+
12171219
export type MergeElysiaInstances<
12181220
Instances extends Elysia<any, any, any, any, any, any>[] = [],
12191221
Prefix extends string = '',

test/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ const a = app
12481248
}>()
12491249

12501250
expectTypeOf<app['index']['post']['response']>().toEqualTypeOf<{
1251+
200: string
12511252
201: string
12521253
}>()
12531254

@@ -1256,6 +1257,7 @@ const a = app
12561257
}>()
12571258

12581259
expectTypeOf<app['true']['post']['response']>().toEqualTypeOf<{
1260+
200: boolean
12591261
202: boolean
12601262
}>()
12611263

0 commit comments

Comments
 (0)