From cccd5913b77ef387bc54a534bf18afff00c81e31 Mon Sep 17 00:00:00 2001 From: saltyaom Date: Sun, 13 Oct 2024 00:23:53 +0700 Subject: [PATCH] :blue_book: feat: infer 200 response from handle if not specified --- CHANGELOG.md | 4 ++++ example/a.ts | 17 ++++++----------- package.json | 2 +- src/types.ts | 34 ++++++++++++++++++---------------- test/types/index.ts | 2 ++ 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf2cc909..5c4e80dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/example/a.ts b/example/a.ts index 5b61377e..e43d3c2d 100644 --- a/example/a.ts +++ b/example/a.ts @@ -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'] diff --git a/package.json b/package.json index 2bfa7ad1..0e64be2f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/types.ts b/src/types.ts index 579b194e..815cace4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1196,24 +1196,26 @@ export type ComposeElysiaResponse = Handle extends ( : _ComposeElysiaResponse, BunFile, File>> type _ComposeElysiaResponse = Prettify< - {} extends Response - ? { - 200: Exclude> - } & { - [ErrorResponse in Extract< - Handle, - ElysiaCustomStatusResponse - > as ErrorResponse extends ElysiaCustomStatusResponse< - any, - any, - any - > - ? ErrorResponse['code'] - : never]: ErrorResponse['response'] - } - : Response + Prettify< + { + 200: Exclude< + Handle, + ElysiaCustomStatusResponse + > + } & ExtractErrorFromHandle & + ({} extends Response ? {} : Omit) + > > +type ExtractErrorFromHandle = { + [ErrorResponse in Extract< + Handle, + ElysiaCustomStatusResponse + > as ErrorResponse extends ElysiaCustomStatusResponse + ? ErrorResponse['code'] + : never]: ErrorResponse['response'] +} + export type MergeElysiaInstances< Instances extends Elysia[] = [], Prefix extends string = '', diff --git a/test/types/index.ts b/test/types/index.ts index 4ca7a9af..b2b62db5 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -1248,6 +1248,7 @@ const a = app }>() expectTypeOf().toEqualTypeOf<{ + 200: string 201: string }>() @@ -1256,6 +1257,7 @@ const a = app }>() expectTypeOf().toEqualTypeOf<{ + 200: boolean 202: boolean }>()