Skip to content

Commit 54bf6ac

Browse files
committed
🎉 feat(macro): add introspect
1 parent 6214d52 commit 54bf6ac

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Improvement:
33
- ValidationError: add `messageValue` as an alias of `errorValue`
44
- ValidationError.detail now accept optional 2nd parameter `allowUnsafeValidatorDetails`
5+
- macro: add `introspect`
56

67
Bug fix:
78
- [#1537](https://github.com/elysiajs/elysia/issues/1537) websocket: ping/pong not being called

example/a.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import { Elysia } from '../src'
22
import { req } from '../test/utils'
33

4-
new Elysia()
5-
.ws("/", {
6-
ping() {
7-
console.log("onping")
8-
},
4+
const app = new Elysia()
5+
.macro('a', {
6+
introspect(option) {
7+
console.log('a', option)
8+
},
9+
beforeHandle() {
10+
console.log('before handle a')
11+
}
12+
})
13+
.macro({
14+
b: {
15+
introspect(option) {
16+
console.log('b', option)
17+
},
18+
beforeHandle() {
19+
console.log('before handle a')
20+
}
21+
}
22+
})
23+
.get('/', () => 'hello world', {
24+
a: true,
25+
b: true,
26+
detail: {
27+
description: 'a'
28+
}
29+
})
930

10-
pong() {
11-
console.log("onpong")
12-
},
13-
14-
async message(ws) {
15-
console.log("onmessage")
16-
console.log(ws.body)
17-
},
18-
})
19-
.listen(3005)
20-
21-
const ws = new WebSocket("ws://localhost:3005")
22-
23-
ws.addEventListener("open", () => {
24-
ws.ping()
25-
ws.send("df")
26-
ws.ping()
27-
})
31+
app.handle(req('/'))

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5235,6 +5235,10 @@ export default class Elysia<
52355235
>,
52365236
const Property extends MaybeValueOrVoidFunction<
52375237
MacroProperty<
5238+
Metadata['macro'] &
5239+
InputSchema<keyof Definitions['typebox'] & string> & {
5240+
[name in Name]?: boolean
5241+
},
52385242
Schema & MacroContext,
52395243
Singleton & {
52405244
derive: Partial<Ephemeral['derive'] & Volatile['derive']>
@@ -5277,6 +5281,8 @@ export default class Elysia<
52775281
const Input extends Metadata['macro'] &
52785282
InputSchema<keyof Definitions['typebox'] & string>,
52795283
const NewMacro extends Macro<
5284+
Metadata['macro'] &
5285+
InputSchema<keyof Definitions['typebox'] & string>,
52805286
Input,
52815287
IntersectIfObjectSchema<
52825288
MergeSchema<
@@ -5321,6 +5327,7 @@ export default class Elysia<
53215327
const NewMacro extends MaybeFunction<
53225328
Macro<
53235329
Input,
5330+
// @ts-ignore trust me bro
53245331
IntersectIfObjectSchema<
53255332
MergeSchema<
53265333
UnwrapRoute<Input, Definitions['typebox'], BasePath>,
@@ -5417,6 +5424,13 @@ export default class Elysia<
54175424
continue
54185425
}
54195426

5427+
if (k === 'introspect') {
5428+
value?.(localHook)
5429+
5430+
delete localHook[key]
5431+
continue
5432+
}
5433+
54205434
if (k === 'detail') {
54215435
if (!localHook.detail) localHook.detail = {}
54225436
localHook.detail = mergeDeep(localHook.detail, value, {

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,7 @@ export type BaseMacro = Record<
19441944
export type MaybeValueOrVoidFunction<T> = T | ((...a: any) => void | T)
19451945

19461946
export interface MacroProperty<
1947+
in out Macro extends BaseMacro = {},
19471948
in out TypedRoute extends RouteSchema = {},
19481949
in out Singleton extends SingletonBase = {
19491950
decorator: {}
@@ -1966,9 +1967,16 @@ export interface MacroProperty<
19661967
afterResponse?: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>
19671968
resolve?: MaybeArray<ResolveHandler<TypedRoute, Singleton>>
19681969
detail?: DocumentDecoration
1970+
/**
1971+
* Introspect hook option for documentation generation or analysis
1972+
*
1973+
* @param option
1974+
*/
1975+
introspect?(option: Prettify<Macro>): unknown
19691976
}
19701977

19711978
export interface Macro<
1979+
in out Macro extends BaseMacro = {},
19721980
in out Input extends BaseMacro = {},
19731981
in out TypedRoute extends RouteSchema = {},
19741982
in out Singleton extends SingletonBase = {
@@ -1980,7 +1988,7 @@ export interface Macro<
19801988
in out Errors extends Record<string, Error> = {}
19811989
> {
19821990
[K: keyof any]: MaybeValueOrVoidFunction<
1983-
Input & MacroProperty<TypedRoute, Singleton, Errors>
1991+
Input & MacroProperty<Macro, TypedRoute, Singleton, Errors>
19841992
>
19851993
}
19861994

0 commit comments

Comments
 (0)