From d73069ab0ad6c2af261d68fa678d40d817c79854 Mon Sep 17 00:00:00 2001 From: saltyaom Date: Fri, 15 Nov 2024 20:02:11 +0700 Subject: [PATCH] :tada: feat: exp.35 --- example/a.ts | 16 ++++++++++++++-- package.json | 2 +- src/error.ts | 2 +- src/index.ts | 2 +- src/types.ts | 40 +++++++++++++++++++++++++++++++--------- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/example/a.ts b/example/a.ts index f083c830..e0679590 100644 --- a/example/a.ts +++ b/example/a.ts @@ -6,6 +6,18 @@ const a = new Elysia() a: t.Ref('a') }), }) - .get('/', ({ body: { a: { a: { a } } } }) => a, { - body: 'a' + .model((model) => ({ + ...model, + b: t.Object({ + a: model.a, + b: t.Ref('b') + }) + })) + .get('/', () => 'a', { + query: t.Object({ + a: t.String() + }) }) + .listen(3000) + +a._routes.index.get.response diff --git a/package.json b/package.json index 48ce4b95..0888dd33 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "elysia", "description": "Ergonomic Framework for Human", - "version": "1.2.0-exp.35", + "version": "1.2.0-exp.36", "author": { "name": "saltyAom", "url": "https://github.com/SaltyAom", diff --git a/src/error.ts b/src/error.ts index b401a4ba..8d9ca91f 100644 --- a/src/error.ts +++ b/src/error.ts @@ -249,7 +249,7 @@ export class ValidationError extends Error { { type: 'validation', on: type, - summary: errors[0]?.summary, + summary: mapValueError(error).summary, property: accessor, message: error?.message, expected, diff --git a/src/index.ts b/src/index.ts index 1b344d8b..79d622ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3859,7 +3859,7 @@ export default class Elysia< query: Schema['query'] headers: Schema['headers'] response: ComposeElysiaResponse< - Schema['response'], + Schema, Handle > } diff --git a/src/types.ts b/src/types.ts index 458576d8..c6ecce7b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -34,6 +34,7 @@ import type { ComposerGeneralHandlerOptions } from './compose' import type { ElysiaAdapter } from './adapter' import type { WSLocalHook } from './ws/types' import type { WebSocketHandler } from './ws/bun' +import { TypeRegistryValidationFunction } from '@sinclair/typebox/build/cjs/type/registry/type' type PartialServe = Partial @@ -616,8 +617,8 @@ export type InlineHandler< > } ? key - : never]: // @ts-expect-error type is checked in key mapping - ResolveResolutions< + : never]: ResolveResolutions< + // @ts-expect-error type is checked in key mapping // @ts-expect-error type is checked in key mapping ReturnType['resolve'] >[key] @@ -1430,18 +1431,39 @@ export type CreateEden< ? _CreateEden<'index', Property> : _CreateEden -export type ComposeElysiaResponse = Handle extends ( - ...a: any[] -) => infer A - ? _ComposeElysiaResponse, BunFile, File>> - : _ComposeElysiaResponse, BunFile, File>> +export type ComposeElysiaResponse< + Schema extends RouteSchema, + Handle +> = Handle extends (...a: any[]) => infer A + ? _ComposeElysiaResponse, BunFile, File>> + : _ComposeElysiaResponse, BunFile, File>> + +export type EmptyRouteSchema = { + body: unknown + headers: unknown + query: unknown + params: unknown + cookie: unknown + response: {} +} -type _ComposeElysiaResponse = Prettify< +type _ComposeElysiaResponse = Prettify< Prettify< { 200: Exclude> } & ExtractErrorFromHandle & - ({} extends Response ? {} : Omit) + ({} extends Schema['response'] + ? {} + : Omit) & + (EmptyRouteSchema extends Schema ? {} : { 422: { + type: 'validation' + on: string + summary?: string + message?: string + found?: unknown + property?: string + expected?: string + } }) > >