Skip to content

Commit

Permalink
📘 feat: infer 200 response from handle if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Oct 12, 2024
1 parent fad6445 commit cccd591
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.21 - 13 Oct 2024
Improvement:
- infer 200 response from handle if not specified

# 1.1.20 - 10 Oct 2024
Bug fix:
- merge guard and not specified hook responses status
Expand Down
17 changes: 6 additions & 11 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { Elysia, t } from '../src'

const app = new Elysia()
.guard({
response: {
400: t.String(),
500: t.String()
}
})
.get('/', () => '', {
response: t.String()
})
const main = new Elysia().get('/', () => 'a', {
response: { 200: t.Number({
default: () => 'a'
}), 500: t.String() }
})

console.log(app.routes[0].hooks)
type A = (typeof main)['_routes']['index']['get']['response']
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": "1.1.20",
"version": "1.1.21",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
34 changes: 18 additions & 16 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,24 +1196,26 @@ export type ComposeElysiaResponse<Response, Handle> = Handle extends (
: _ComposeElysiaResponse<Response, Replace<Awaited<Handle>, BunFile, File>>

type _ComposeElysiaResponse<Response, Handle> = Prettify<
{} extends Response
? {
200: Exclude<Handle, ElysiaCustomStatusResponse<any, any, any>>
} & {
[ErrorResponse in Extract<
Handle,
ElysiaCustomStatusResponse<any, any, any>
> as ErrorResponse extends ElysiaCustomStatusResponse<
any,
any,
any
>
? ErrorResponse['code']
: never]: ErrorResponse['response']
}
: Response
Prettify<
{
200: Exclude<
Handle,
ElysiaCustomStatusResponse<any, any, any>
>
} & ExtractErrorFromHandle<Handle> &
({} extends Response ? {} : Omit<Response, 200>)
>
>

type ExtractErrorFromHandle<Handle> = {
[ErrorResponse in Extract<
Handle,
ElysiaCustomStatusResponse<any, any, any>
> as ErrorResponse extends ElysiaCustomStatusResponse<any, any, any>
? ErrorResponse['code']
: never]: ErrorResponse['response']
}

export type MergeElysiaInstances<
Instances extends Elysia<any, any, any, any, any, any>[] = [],
Prefix extends string = '',
Expand Down
2 changes: 2 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ const a = app
}>()

expectTypeOf<app['index']['post']['response']>().toEqualTypeOf<{
200: string
201: string
}>()

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

expectTypeOf<app['true']['post']['response']>().toEqualTypeOf<{
200: boolean
202: boolean
}>()

Expand Down

0 comments on commit cccd591

Please sign in to comment.