Skip to content

Commit 0e77530

Browse files
committed
🔧 fix: use StaticDecode to unwrap TImport
1 parent 57ff9cd commit 0e77530

File tree

8 files changed

+103
-108
lines changed

8 files changed

+103
-108
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 1.3.0
22
Feature:
3-
- add `jsonAccelerator`, `exactMirror`
3+
- add `exactMirror`
44
- add `systemRouter` config
55
- `standalone Validator`
66
- add `Elysia.Ref` for referencing schema with autocompletion instead of `t.Ref`

example/a.ts

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
1-
import { Elysia, t } from '../src'
2-
import { hasType } from '../src/schema'
3-
import { req, upload } from '../test/utils'
1+
import Elysia, { t } from '../src'
2+
3+
function addTwo(num: number) {
4+
return num + 2
5+
}
46

57
const app = new Elysia()
6-
.post('/', ({ body }) => 'ok', {
7-
body: t.Union([
8-
t.Object({
9-
hello: t.String(),
10-
file: t.File({
11-
type: 'image'
12-
}),
13-
a: t.File({
14-
type: 'image'
15-
})
16-
}),
17-
t.Object({
18-
world: t.String(),
19-
image: t.File({
20-
type: 'image'
21-
})
22-
}),
23-
t.Object({
24-
world: t.String()
25-
})
26-
])
8+
.get('', async ({ query: { foo } }) => addTwo(foo), {
9+
query: t.Object({
10+
foo: t
11+
.Transform(t.String())
12+
.Decode((x) => 12)
13+
.Encode((x) => x.toString())
14+
})
2715
})
28-
.listen(3000)
29-
30-
app.compile()
31-
32-
// app.handle(
33-
// upload('/', {
34-
// hello: 'world',
35-
// file: 'aris-yuzu.jpg'
36-
// }).request
37-
// )
38-
// .then((x) => x.text())
39-
// .then(console.log)
40-
41-
// // console.log(app.router)
16+
.listen(1234)

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.3.0-exp.74",
4+
"version": "1.3.0-exp.75",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,7 +3697,7 @@ export default class Elysia<
36973697
BasePath,
36983698
// @ts-expect-error - This is truly ideal
36993699
Prettify2<Singleton & NewElysia['~Singleton']>,
3700-
Prettify2<Definitions & NewElysia['~Singleton']>,
3700+
Prettify2<Definitions & NewElysia['~Definitions']>,
37013701
Prettify2<Metadata & NewElysia['~Metadata']>,
37023702
BasePath extends ``
37033703
? Routes & NewElysia['~Routes']
@@ -5415,20 +5415,16 @@ export default class Elysia<
54155415
Volatile['schema'],
54165416
MergeSchema<Ephemeral['schema'], Metadata['schema']>
54175417
>
5418-
>,
5419-
const Macro extends Metadata['macro']
5418+
>
54205419
>(
54215420
path: Path,
54225421
options: WSLocalHook<
54235422
LocalSchema,
54245423
Schema,
54255424
Singleton & {
54265425
derive: Ephemeral['derive'] & Volatile['derive']
5427-
resolve: Ephemeral['resolve'] &
5428-
Volatile['resolve'] &
5429-
MacroToContext<Metadata['macroFn'], Macro>
5430-
},
5431-
Macro
5426+
resolve: Ephemeral['resolve'] & Volatile['resolve']
5427+
}
54325428
>
54335429
): Elysia<
54345430
BasePath,

src/types.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,19 +404,23 @@ export type UnwrapSchema<
404404
: Schema extends TSchema
405405
? Schema extends OptionalField
406406
? Partial<
407+
StaticDecode<
408+
TImport<
409+
Definitions & {
410+
readonly __elysia: Schema
411+
},
412+
'__elysia'
413+
>
414+
>
415+
>
416+
: StaticDecode<
407417
TImport<
408418
Definitions & {
409419
readonly __elysia: Schema
410420
},
411421
'__elysia'
412-
>['static']
422+
>
413423
>
414-
: TImport<
415-
Definitions & {
416-
readonly __elysia: Schema
417-
},
418-
'__elysia'
419-
>['static']
420424
: Schema extends `${infer Key}[]`
421425
? Definitions extends Record<
422426
Key,

src/ws/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export class ElysiaWS<Context = unknown, Route extends RouteSchema = {}>
5353
id?: string
5454
validator?: TypeCheck<TSchema>
5555
}>,
56-
public data: Context,
56+
public data: Prettify<
57+
Omit<Context, 'body' | 'error' | 'status' | 'redirect'>
58+
>,
5759
public body: Route['body'] = undefined
5860
) {
5961
this.validator = raw.data?.validator

src/ws/types.ts

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
DocumentDecoration,
99
ErrorHandler,
1010
InputSchema,
11+
MacroToContext,
1112
MapResponse,
1213
MaybeArray,
1314
MaybePromise,
15+
MetadataBase,
1416
OptionalHandler,
1517
Prettify,
1618
RouteSchema,
@@ -116,62 +118,46 @@ export type WSParseHandler<Route extends RouteSchema, Context = {}> = (
116118
message: unknown
117119
) => MaybePromise<Route['body'] | void | undefined>
118120

119-
export type AnyWSLocalHook = WSLocalHook<any, any, any, any>
120-
121-
type WSLocalHookKey =
122-
| keyof TypedWebSocketHandler<any, any>
123-
| 'detail'
124-
| 'upgrade'
125-
| 'parse'
126-
| 'transform'
127-
| 'beforeHandle'
128-
| 'afterHandle'
129-
| 'mapResponse'
130-
| 'afterResponse'
131-
| 'error'
132-
| 'tags'
133-
| keyof InputSchema<any>
121+
export type AnyWSLocalHook = WSLocalHook<any, any, any>
134122

135123
export type WSLocalHook<
136124
LocalSchema extends InputSchema,
137125
Schema extends RouteSchema,
138-
Singleton extends SingletonBase,
139-
Macro extends BaseMacro
140-
> = Macro &
141-
(LocalSchema extends any ? LocalSchema : Prettify<LocalSchema>) & {
142-
detail?: DocumentDecoration
143-
/**
144-
* Headers to register to websocket before `upgrade`
145-
*/
146-
upgrade?: Record<string, unknown> | ((context: Context) => unknown)
147-
parse?: MaybeArray<WSParseHandler<Schema>>
126+
Singleton extends SingletonBase
127+
> = (LocalSchema extends any ? LocalSchema : Prettify<LocalSchema>) & {
128+
detail?: DocumentDecoration
129+
/**
130+
* Headers to register to websocket before `upgrade`
131+
*/
132+
upgrade?: Record<string, unknown> | ((context: Context) => unknown)
133+
parse?: MaybeArray<WSParseHandler<Schema>>
148134

149-
/**
150-
* Transform context's value
151-
*/
152-
transform?: MaybeArray<TransformHandler<Schema, Singleton>>
153-
/**
154-
* Execute before main handler
155-
*/
156-
beforeHandle?: MaybeArray<OptionalHandler<Schema, Singleton>>
157-
/**
158-
* Execute after main handler
159-
*/
160-
afterHandle?: MaybeArray<OptionalHandler<Schema, Singleton>>
161-
/**
162-
* Execute after main handler
163-
*/
164-
mapResponse?: MaybeArray<MapResponse<Schema, Singleton>>
165-
/**
166-
* Execute after response is sent
167-
*/
168-
afterResponse?: MaybeArray<AfterResponseHandler<Schema, Singleton>>
169-
/**
170-
* Catch error
171-
*/
172-
error?: MaybeArray<ErrorHandler<{}, Schema, Singleton>>
173-
tags?: DocumentDecoration['tags']
174-
} & TypedWebSocketHandler<
135+
/**
136+
* Transform context's value
137+
*/
138+
transform?: MaybeArray<TransformHandler<Schema, Singleton>>
139+
/**
140+
* Execute before main handler
141+
*/
142+
beforeHandle?: MaybeArray<OptionalHandler<Schema, Singleton>>
143+
/**
144+
* Execute after main handler
145+
*/
146+
afterHandle?: MaybeArray<OptionalHandler<Schema, Singleton>>
147+
/**
148+
* Execute after main handler
149+
*/
150+
mapResponse?: MaybeArray<MapResponse<Schema, Singleton>>
151+
/**
152+
* Execute after response is sent
153+
*/
154+
afterResponse?: MaybeArray<AfterResponseHandler<Schema, Singleton>>
155+
/**
156+
* Catch error
157+
*/
158+
error?: MaybeArray<ErrorHandler<{}, Schema, Singleton>>
159+
tags?: DocumentDecoration['tags']
160+
} & TypedWebSocketHandler<
175161
Omit<Context<Schema, Singleton>, 'body'> & {
176162
body: never
177163
},

test/types/type-system.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import { expectTypeOf } from 'expect-type'
5757
expectTypeOf<typeof body>().toEqualTypeOf<{
5858
images: File[]
5959
}>()
60-
6160
},
6261
{
6362
body: t.Object({
@@ -69,3 +68,36 @@ import { expectTypeOf } from 'expect-type'
6968
}
7069
)
7170
}
71+
72+
// use StaticDecode to unwrap type parameter
73+
{
74+
function addTwo(num: number) {
75+
return num + 2
76+
}
77+
78+
new Elysia().get('', async ({ query: { foo } }) => addTwo(foo), {
79+
query: t.Object({
80+
foo: t
81+
.Transform(t.String())
82+
.Decode((x) => 12)
83+
.Encode((x) => x.toString())
84+
})
85+
})
86+
}
87+
88+
// handle Elysia.Ref
89+
{
90+
const Model = new Elysia().model({
91+
hello: t.Number()
92+
})
93+
94+
new Elysia().use(Model).get(
95+
'',
96+
async ({ body }) => {
97+
expectTypeOf<typeof body>().toEqualTypeOf<number>()
98+
},
99+
{
100+
body: Model.Ref('hello')
101+
}
102+
)
103+
}

0 commit comments

Comments
 (0)