elysia 0.4.14 → 0.5.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/compose.d.ts +17 -0
- package/dist/cjs/compose.js +257 -0
- package/dist/cjs/context.d.ts +20 -0
- package/dist/cjs/context.js +1 -0
- package/dist/cjs/custom-types.d.ts +30 -0
- package/dist/cjs/custom-types.js +1 -0
- package/dist/cjs/error.d.ts +24 -0
- package/dist/cjs/error.js +1 -0
- package/dist/cjs/handler.d.ts +6 -0
- package/dist/cjs/handler.js +1 -0
- package/dist/cjs/index.d.ts +445 -0
- package/dist/cjs/index.js +5 -0
- package/dist/cjs/schema.d.ts +15 -0
- package/dist/cjs/schema.js +1 -0
- package/dist/cjs/types.d.ts +263 -0
- package/dist/cjs/types.js +1 -0
- package/dist/cjs/utils.d.ts +11 -0
- package/dist/cjs/utils.js +1 -0
- package/dist/cjs/ws/index.d.ts +28 -0
- package/dist/cjs/ws/index.js +1 -0
- package/dist/cjs/ws/types.d.ts +55 -0
- package/dist/cjs/ws/types.js +1 -0
- package/dist/compose.d.ts +12 -4
- package/dist/compose.js +185 -57
- package/dist/context.d.ts +4 -3
- package/dist/custom-types.d.ts +5 -1
- package/dist/custom-types.js +1 -1
- package/dist/error.d.ts +20 -11
- package/dist/error.js +1 -1
- package/dist/handler.js +1 -1
- package/dist/index.d.ts +78 -37
- package/dist/index.js +5 -1
- package/dist/schema.d.ts +3 -6
- package/dist/schema.js +1 -1
- package/dist/types.d.ts +78 -54
- package/dist/utils.d.ts +1 -5
- package/dist/utils.js +1 -1
- package/dist/ws/index.js +1 -1
- package/dist/ws/types.d.ts +2 -0
- package/package.json +20 -11
- package/dist/validation.d.ts +0 -16
- package/dist/validation.js +0 -1
- package/src/compose.ts +0 -295
- package/src/context.ts +0 -31
- package/src/custom-types.ts +0 -179
- package/src/error.ts +0 -32
- package/src/handler.ts +0 -335
- package/src/index.ts +0 -2462
- package/src/schema.ts +0 -209
- package/src/types.ts +0 -593
- package/src/utils.ts +0 -180
- package/src/validation.ts +0 -26
- package/src/ws/index.ts +0 -230
- package/src/ws/types.ts +0 -199
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
import type { Serve, Server } from 'bun';
|
|
3
|
+
import { SCHEMA, EXPOSED, DEFS } from './utils';
|
|
4
|
+
import type { Context } from './context';
|
|
5
|
+
import type { ElysiaWSOptions, WSTypedSchema } from './ws';
|
|
6
|
+
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, MaybePromise, IsNever, Prettify, TypedWSRouteToEden, UnwrapSchema, ExtractPath, TypedSchemaToRoute, DeepWritable } from './types';
|
|
7
|
+
import { Static, type TSchema } from '@sinclair/typebox';
|
|
8
|
+
import type { ValidationError, ParseError, NotFoundError, InternalServerError } from './error';
|
|
9
|
+
import type { Permission } from '@elysiajs/fn';
|
|
10
|
+
export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
|
|
11
|
+
config: ElysiaConfig;
|
|
12
|
+
store: Instance['store'];
|
|
13
|
+
meta: Instance['meta'];
|
|
14
|
+
protected decorators: ElysiaInstance['request'];
|
|
15
|
+
event: LifeCycleStore<Instance>;
|
|
16
|
+
server: Server | null;
|
|
17
|
+
private $schema;
|
|
18
|
+
private router;
|
|
19
|
+
protected routes: InternalRoute<Instance>[];
|
|
20
|
+
private staticRouter;
|
|
21
|
+
private wsRouter;
|
|
22
|
+
private lazyLoadModules;
|
|
23
|
+
constructor(config?: Partial<ElysiaConfig>);
|
|
24
|
+
private _addHandler;
|
|
25
|
+
onStart(handler: VoidLifeCycle<Instance>): this;
|
|
26
|
+
onRequest<Route extends OverwritableTypeRoute = TypedRoute>(handler: BeforeRequestHandler<Route, Instance>): this;
|
|
27
|
+
onParse(parser: BodyParser<any, Instance>): this;
|
|
28
|
+
onTransform<Route extends OverwritableTypeRoute = TypedRoute>(handler: NoReturnHandler<Route, Instance>): this;
|
|
29
|
+
onBeforeHandle<Route extends OverwritableTypeRoute = TypedRoute>(handler: Handler<Route, Instance>): this;
|
|
30
|
+
onAfterHandle<Route extends OverwritableTypeRoute = TypedRoute>(handler: AfterRequestHandler<Route, Instance>): this;
|
|
31
|
+
onError(errorHandler: ErrorHandler): this;
|
|
32
|
+
onStop(handler: VoidLifeCycle<Instance>): this;
|
|
33
|
+
on<Event extends LifeCycleEvent = LifeCycleEvent>(type: Event, handler: LifeCycle<Instance>[Event]): this;
|
|
34
|
+
group<NewElysia extends Elysia<any> = Elysia<any>, Prefix extends string = string>(prefix: Prefix, run: (group: Elysia<{
|
|
35
|
+
request: Instance['request'];
|
|
36
|
+
store: Omit<Instance['store'], typeof SCHEMA> & ElysiaInstance['store'];
|
|
37
|
+
schema: Instance['schema'];
|
|
38
|
+
meta: Omit<Instance['meta'], typeof SCHEMA> & ElysiaInstance['meta'];
|
|
39
|
+
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<{
|
|
40
|
+
request: Instance['request'];
|
|
41
|
+
schema: Instance['schema'];
|
|
42
|
+
store: Instance['store'];
|
|
43
|
+
meta: Instance['meta'] & (Omit<NewInstance['meta'], typeof SCHEMA> & Record<typeof SCHEMA, {
|
|
44
|
+
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['meta'][typeof SCHEMA][key];
|
|
45
|
+
}>);
|
|
46
|
+
}> : this;
|
|
47
|
+
group<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>>, NewElysia extends Elysia<any> = Elysia<any>, Prefix extends string = string>(prefix: Prefix, schema: LocalHook<Schema, Instance>, run: (group: Elysia<{
|
|
48
|
+
request: Instance['request'];
|
|
49
|
+
store: Omit<Instance['store'], typeof SCHEMA> & ElysiaInstance['store'];
|
|
50
|
+
schema: Schema & Instance['schema'];
|
|
51
|
+
meta: Omit<Instance['meta'], typeof SCHEMA> & ElysiaInstance['meta'];
|
|
52
|
+
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<{
|
|
53
|
+
request: Instance['request'];
|
|
54
|
+
schema: Instance['schema'];
|
|
55
|
+
store: Instance['store'];
|
|
56
|
+
meta: Instance['meta'] & (Omit<NewInstance['meta'], typeof SCHEMA> & Record<typeof SCHEMA, {
|
|
57
|
+
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['meta'][typeof SCHEMA][key];
|
|
58
|
+
}>);
|
|
59
|
+
}> : this;
|
|
60
|
+
guard<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, NewElysia extends Elysia<any> = Elysia<any>>(hook: LocalHook<Schema, Instance>, run: (group: Elysia<{
|
|
61
|
+
request: Instance['request'];
|
|
62
|
+
store: Instance['store'];
|
|
63
|
+
schema: Schema & Instance['schema'];
|
|
64
|
+
meta: Instance['meta'];
|
|
65
|
+
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<{
|
|
66
|
+
request: Instance['request'];
|
|
67
|
+
store: Instance['store'];
|
|
68
|
+
schema: Instance['schema'];
|
|
69
|
+
meta: Instance['meta'] & NewInstance['schema'];
|
|
70
|
+
}> : this;
|
|
71
|
+
use<NewElysia extends MaybePromise<Elysia<any>> = Elysia<any>, Params extends Elysia = Elysia<any>, LazyLoadElysia extends never | ElysiaInstance = never>(plugin: MaybePromise<(app: Params extends Elysia<infer ParamsInstance> ? IsAny<ParamsInstance> extends true ? this : Params : Params) => MaybePromise<NewElysia>> | Promise<{
|
|
72
|
+
default: (elysia: Elysia<any>) => MaybePromise<Elysia<LazyLoadElysia>>;
|
|
73
|
+
}>): IsNever<LazyLoadElysia> extends false ? Elysia<{
|
|
74
|
+
request: Instance['request'] & LazyLoadElysia['request'];
|
|
75
|
+
store: Instance['store'] & LazyLoadElysia['store'];
|
|
76
|
+
schema: Instance['schema'] & LazyLoadElysia['schema'];
|
|
77
|
+
meta: Instance['meta'] & LazyLoadElysia['meta'];
|
|
78
|
+
}> : NewElysia extends Elysia<infer NewInstance> ? IsNever<NewInstance> extends true ? Elysia<Instance> : Elysia<{
|
|
79
|
+
request: Instance['request'] & NewInstance['request'];
|
|
80
|
+
store: Instance['store'] & NewInstance['store'];
|
|
81
|
+
schema: Instance['schema'] & NewInstance['schema'];
|
|
82
|
+
meta: Instance['meta'] & NewInstance['meta'];
|
|
83
|
+
}> : NewElysia extends Promise<Elysia<infer NewInstance>> ? Elysia<{
|
|
84
|
+
request: Instance['request'] & NewInstance['request'];
|
|
85
|
+
store: Instance['store'] & NewInstance['store'];
|
|
86
|
+
schema: Instance['schema'] & NewInstance['schema'];
|
|
87
|
+
meta: Instance['meta'] & NewInstance['meta'];
|
|
88
|
+
}> : this;
|
|
89
|
+
if<Condition extends boolean, NewElysia extends MaybePromise<Elysia<any>> = Elysia<any>, Params extends Elysia = Elysia<any>, LazyLoadElysia extends never | ElysiaInstance = never>(condition: Condition, run: MaybePromise<(app: Params extends Elysia<infer ParamsInstance> ? IsAny<ParamsInstance> extends true ? this : Params : Params) => MaybePromise<NewElysia>> | Promise<{
|
|
90
|
+
default: (elysia: Elysia<any>) => MaybePromise<Elysia<LazyLoadElysia>>;
|
|
91
|
+
}>): IsNever<LazyLoadElysia> extends false ? Elysia<{
|
|
92
|
+
request: Instance['request'] & LazyLoadElysia['request'];
|
|
93
|
+
store: Instance['store'] & LazyLoadElysia['store'];
|
|
94
|
+
schema: Instance['schema'] & LazyLoadElysia['schema'];
|
|
95
|
+
meta: Instance['meta'] & LazyLoadElysia['meta'];
|
|
96
|
+
}> : NewElysia extends Elysia<infer NewInstance> ? IsNever<NewInstance> extends true ? Elysia<Instance> : Elysia<{
|
|
97
|
+
request: Instance['request'] & NewInstance['request'];
|
|
98
|
+
store: Instance['store'] & NewInstance['store'];
|
|
99
|
+
schema: Instance['schema'] & NewInstance['schema'];
|
|
100
|
+
meta: Instance['meta'] & NewInstance['meta'];
|
|
101
|
+
}> : NewElysia extends Promise<Elysia<infer NewInstance>> ? Elysia<{
|
|
102
|
+
request: Instance['request'] & NewInstance['request'];
|
|
103
|
+
store: Instance['store'] & NewInstance['store'];
|
|
104
|
+
schema: Instance['schema'] & NewInstance['schema'];
|
|
105
|
+
meta: Instance['meta'] & NewInstance['meta'];
|
|
106
|
+
}> : this;
|
|
107
|
+
get<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
108
|
+
request: Instance['request'];
|
|
109
|
+
store: Instance['store'];
|
|
110
|
+
schema: Instance['schema'];
|
|
111
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
112
|
+
[path in Path]: {
|
|
113
|
+
get: {
|
|
114
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
115
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
116
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
117
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
118
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
119
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
120
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
121
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
122
|
+
} : {
|
|
123
|
+
'200': ReturnType<Handler>;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
}>>;
|
|
128
|
+
}>;
|
|
129
|
+
post<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
130
|
+
request: Instance['request'];
|
|
131
|
+
store: Instance['store'];
|
|
132
|
+
schema: Instance['schema'];
|
|
133
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
134
|
+
[path in Path]: {
|
|
135
|
+
post: {
|
|
136
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
137
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
138
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
139
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
140
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
141
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
142
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
143
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
144
|
+
} : {
|
|
145
|
+
'200': ReturnType<Handler>;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
}>>;
|
|
150
|
+
}>;
|
|
151
|
+
put<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
152
|
+
request: Instance['request'];
|
|
153
|
+
store: Instance['store'];
|
|
154
|
+
schema: Instance['schema'];
|
|
155
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
156
|
+
[path in Path]: {
|
|
157
|
+
put: {
|
|
158
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
159
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
160
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
161
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
162
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
163
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
164
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
165
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
166
|
+
} : {
|
|
167
|
+
'200': ReturnType<Handler>;
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
}>>;
|
|
172
|
+
}>;
|
|
173
|
+
patch<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
174
|
+
request: Instance['request'];
|
|
175
|
+
store: Instance['store'];
|
|
176
|
+
schema: Instance['schema'];
|
|
177
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
178
|
+
[path in Path]: {
|
|
179
|
+
patch: {
|
|
180
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
181
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
182
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
183
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
184
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
185
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
186
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
187
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
188
|
+
} : {
|
|
189
|
+
'200': ReturnType<Handler>;
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
}>>;
|
|
194
|
+
}>;
|
|
195
|
+
delete<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
196
|
+
request: Instance['request'];
|
|
197
|
+
store: Instance['store'];
|
|
198
|
+
schema: Instance['schema'];
|
|
199
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
200
|
+
[path in Path]: {
|
|
201
|
+
delete: {
|
|
202
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
203
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
204
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
205
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
206
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
207
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
208
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
209
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
210
|
+
} : {
|
|
211
|
+
'200': ReturnType<Handler>;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
}>>;
|
|
216
|
+
}>;
|
|
217
|
+
options<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
218
|
+
request: Instance['request'];
|
|
219
|
+
store: Instance['store'];
|
|
220
|
+
schema: Instance['schema'];
|
|
221
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
222
|
+
[path in Path]: {
|
|
223
|
+
options: {
|
|
224
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
225
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
226
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
227
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
228
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
229
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
230
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
231
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
232
|
+
} : {
|
|
233
|
+
'200': ReturnType<Handler>;
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
}>>;
|
|
238
|
+
}>;
|
|
239
|
+
all<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
240
|
+
request: Instance['request'];
|
|
241
|
+
store: Instance['store'];
|
|
242
|
+
schema: Instance['schema'];
|
|
243
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
244
|
+
[path in Path]: {
|
|
245
|
+
all: {
|
|
246
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
247
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
248
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
249
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
250
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
251
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
252
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
253
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
254
|
+
} : {
|
|
255
|
+
'200': ReturnType<Handler>;
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
}>>;
|
|
260
|
+
}>;
|
|
261
|
+
head<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
262
|
+
request: Instance['request'];
|
|
263
|
+
store: Instance['store'];
|
|
264
|
+
schema: Instance['schema'];
|
|
265
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
266
|
+
[path in Path]: {
|
|
267
|
+
head: {
|
|
268
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
269
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
270
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
271
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
272
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
273
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
274
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
275
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
276
|
+
} : {
|
|
277
|
+
'200': ReturnType<Handler>;
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
}>>;
|
|
282
|
+
}>;
|
|
283
|
+
trace<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
284
|
+
request: Instance['request'];
|
|
285
|
+
store: Instance['store'];
|
|
286
|
+
schema: Instance['schema'];
|
|
287
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
288
|
+
[path in Path]: {
|
|
289
|
+
trace: {
|
|
290
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
291
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
292
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
293
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
294
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
295
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
296
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
297
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
298
|
+
} : {
|
|
299
|
+
'200': ReturnType<Handler>;
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
}>>;
|
|
304
|
+
}>;
|
|
305
|
+
connect<Path extends string, Handler extends LocalHandler<Schema, Instance, Path>, Schema extends TypedSchema<Extract<keyof Instance['meta'][typeof DEFS], string>> = {}>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{
|
|
306
|
+
request: Instance['request'];
|
|
307
|
+
store: Instance['store'];
|
|
308
|
+
schema: Instance['schema'];
|
|
309
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
310
|
+
[path in Path]: {
|
|
311
|
+
connect: {
|
|
312
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
313
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
314
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
315
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
316
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
317
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
318
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
319
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
320
|
+
} : {
|
|
321
|
+
'200': ReturnType<Handler>;
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
}>>;
|
|
326
|
+
}>;
|
|
327
|
+
ws<Path extends string = '', Schema extends TypedSchema = {}>(path: Path, options: Path extends '' ? never : this extends Elysia<infer Instance> ? ElysiaWSOptions<Path, Schema extends WSTypedSchema ? Schema : WSTypedSchema, Instance['meta'][typeof DEFS]> : never): Elysia<{
|
|
328
|
+
request: Instance['request'];
|
|
329
|
+
store: Instance['store'];
|
|
330
|
+
schema: Instance['schema'];
|
|
331
|
+
meta: Instance['meta'] & Record<typeof SCHEMA, Record<Path, {
|
|
332
|
+
[method in 'subscribe']: TypedWSRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>;
|
|
333
|
+
}>>;
|
|
334
|
+
}>;
|
|
335
|
+
route<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Method extends HTTPMethod = HTTPMethod, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(method: Method, path: Path, handler: Handler, { config, ...hook }?: LocalHook<Schema, Instance, Path> & {
|
|
336
|
+
config: {
|
|
337
|
+
allowMeta?: boolean;
|
|
338
|
+
};
|
|
339
|
+
}): Elysia<{
|
|
340
|
+
request: Instance['request'];
|
|
341
|
+
store: Instance['store'];
|
|
342
|
+
schema: Instance['schema'];
|
|
343
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, Prettify<Instance['meta'][typeof SCHEMA] & {
|
|
344
|
+
[path in Path]: {
|
|
345
|
+
[method in Method]: {
|
|
346
|
+
body: UnwrapSchema<Schema['body'], Instance['meta'][typeof DEFS]>;
|
|
347
|
+
headers: UnwrapSchema<Schema['headers'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
348
|
+
query: UnwrapSchema<Schema['query'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : undefined;
|
|
349
|
+
params: UnwrapSchema<Schema['params'], Instance['meta'][typeof DEFS]> extends infer Result ? Result extends Record<string, any> ? Result : undefined : Record<ExtractPath<Path>, string>;
|
|
350
|
+
response: Schema['response'] extends TSchema | string ? {
|
|
351
|
+
'200': UnwrapSchema<Schema['response'], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
352
|
+
} : Schema['response'] extends Record<string, TSchema | string> ? {
|
|
353
|
+
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance['meta'][typeof DEFS], ReturnType<Handler>>;
|
|
354
|
+
} : {
|
|
355
|
+
'200': ReturnType<Handler>;
|
|
356
|
+
};
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
}>>;
|
|
360
|
+
}>;
|
|
361
|
+
state<const Key extends string | number | symbol, const Value>(name: Key, value: Value): Elysia<{
|
|
362
|
+
store: Instance['store'] & {
|
|
363
|
+
[key in Key]: Value;
|
|
364
|
+
};
|
|
365
|
+
request: Instance['request'];
|
|
366
|
+
schema: Instance['schema'];
|
|
367
|
+
meta: Instance['meta'];
|
|
368
|
+
}>;
|
|
369
|
+
state<const NewStore extends Record<string, unknown>>(store: NewStore): Elysia<{
|
|
370
|
+
store: Instance['store'] & DeepWritable<NewStore>;
|
|
371
|
+
request: Instance['request'];
|
|
372
|
+
schema: Instance['schema'];
|
|
373
|
+
meta: Instance['meta'];
|
|
374
|
+
}>;
|
|
375
|
+
decorate<const Name extends string, const Value>(name: Name, value: Value): Elysia<{
|
|
376
|
+
store: Instance['store'];
|
|
377
|
+
request: Instance['request'] & {
|
|
378
|
+
[key in Name]: Value;
|
|
379
|
+
};
|
|
380
|
+
schema: Instance['schema'];
|
|
381
|
+
meta: Instance['meta'];
|
|
382
|
+
}>;
|
|
383
|
+
decorate<const Decorators extends Record<string, unknown>>(name: Decorators): Elysia<{
|
|
384
|
+
store: Instance['store'];
|
|
385
|
+
request: Instance['request'] & DeepWritable<Decorators>;
|
|
386
|
+
schema: Instance['schema'];
|
|
387
|
+
meta: Instance['meta'];
|
|
388
|
+
}>;
|
|
389
|
+
derive<Returned extends Object = Object>(transform: (context: Context<TypedSchemaToRoute<Instance['schema'], Instance['meta'][typeof DEFS]>, Instance['store']> & Instance['request']) => MaybePromise<Returned> extends {
|
|
390
|
+
store: any;
|
|
391
|
+
} ? never : Returned): Elysia<{
|
|
392
|
+
store: Instance['store'];
|
|
393
|
+
request: Instance['request'] & Awaited<Returned>;
|
|
394
|
+
schema: Instance['schema'];
|
|
395
|
+
meta: Instance['meta'];
|
|
396
|
+
}>;
|
|
397
|
+
fn<PluginInstalled extends boolean = IsAny<Permission> extends true ? false : true, T extends PluginInstalled extends true ? Record<string, unknown> | ((app: Instance['request'] & {
|
|
398
|
+
store: Instance['store'];
|
|
399
|
+
permission: Permission;
|
|
400
|
+
}) => Record<string, unknown>) : "Please install '@elysiajs/fn' before using Elysia Fn" = PluginInstalled extends true ? Record<string, unknown> | ((app: Instance['request'] & {
|
|
401
|
+
store: Instance['store'];
|
|
402
|
+
permission: Permission;
|
|
403
|
+
}) => Record<string, unknown>) : "Please install '@elysiajs/fn' before using Elysia Fn">(value: T): PluginInstalled extends true ? Elysia<{
|
|
404
|
+
store: Instance['store'];
|
|
405
|
+
request: Instance['request'];
|
|
406
|
+
schema: Instance['schema'];
|
|
407
|
+
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED] & (T extends (store: any) => infer Returned ? Returned : T)> & Record<typeof SCHEMA, Instance['meta'][typeof SCHEMA]>;
|
|
408
|
+
}> : this;
|
|
409
|
+
schema<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>>, NewInstance = Elysia<{
|
|
410
|
+
request: Instance['request'];
|
|
411
|
+
store: Instance['store'];
|
|
412
|
+
schema: MergeSchema<Schema, Instance['schema']>;
|
|
413
|
+
meta: Instance['meta'];
|
|
414
|
+
}>>(schema: Schema): NewInstance;
|
|
415
|
+
handle: (request: Request) => Promise<Response>;
|
|
416
|
+
compile: () => this;
|
|
417
|
+
fetch(request: Request): MaybePromise<Response>;
|
|
418
|
+
handleError: (request: Request, error: Error | ValidationError | ParseError | NotFoundError | InternalServerError, set: Context['set']) => Promise<Response>;
|
|
419
|
+
listen: (options: string | number | Partial<Serve>, callback?: ListenCallback) => this;
|
|
420
|
+
stop: () => Promise<void>;
|
|
421
|
+
get modules(): Promise<Elysia<any>[]>;
|
|
422
|
+
model<Name extends string, Model extends TSchema>(name: Name, model: Model): Elysia<{
|
|
423
|
+
store: Instance['store'];
|
|
424
|
+
request: Instance['request'];
|
|
425
|
+
schema: Instance['schema'];
|
|
426
|
+
meta: Instance['meta'] & Record<typeof DEFS, {
|
|
427
|
+
[name in Name]: Static<Model>;
|
|
428
|
+
}>;
|
|
429
|
+
}>;
|
|
430
|
+
model<Recorder extends Record<string, TSchema>>(record: Recorder): Elysia<{
|
|
431
|
+
store: Instance['store'];
|
|
432
|
+
request: Instance['request'];
|
|
433
|
+
schema: Instance['schema'];
|
|
434
|
+
meta: Instance['meta'] & Record<typeof DEFS, {
|
|
435
|
+
[key in keyof Recorder]: Static<Recorder[key]>;
|
|
436
|
+
}>;
|
|
437
|
+
}>;
|
|
438
|
+
}
|
|
439
|
+
export { Elysia };
|
|
440
|
+
export { t } from './custom-types';
|
|
441
|
+
export { ws } from './ws';
|
|
442
|
+
export { SCHEMA, DEFS, EXPOSED, getSchemaValidator, mergeDeep, mergeHook, mergeObjectArray, getResponseSchemaValidator } from './utils';
|
|
443
|
+
export { ParseError, NotFoundError, ValidationError, InternalServerError } from './error';
|
|
444
|
+
export type { Context, PreContext } from './context';
|
|
445
|
+
export type { Handler, RegisteredHook, BeforeRequestHandler, TypedRoute, OverwritableTypeRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, ComposedHandler, InternalRoute, BodyParser, ErrorHandler, ErrorCode, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, AfterRequestHandler, HookHandler, TypedSchemaToRoute, UnwrapSchema, LifeCycleStore, VoidLifeCycle, SchemaValidator, ExtractPath, IsPathParameter, IsAny, IsNever, UnknownFallback, WithArray, ObjectValues, MaybePromise, MergeIfNotNull, ElysiaDefaultMeta, AnyTypedSchema, DeepMergeTwoTypes } from './types';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import{nanoid as e}from"nanoid";import{Memoirist as t}from"memoirist";import{mapResponse as r}from"./handler";import{SCHEMA as s,EXPOSED as a,DEFS as o,mergeHook as i,getSchemaValidator as n,getResponseSchemaValidator as h,mergeDeep as u}from"./utils";import{registerSchemaPath as d}from"./schema";import{composeGeneralHandler as l,composeHandler as c}from"./compose";import{ws as p}from"./ws";export default class f{config;store={};meta={[s]:Object.create(null),[o]:Object.create(null),[a]:Object.create(null)};decorators={};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new t;routes=[];staticRouter={handlers:[],variables:"",map:{}};wsRouter;lazyLoadModules=[];constructor(e){this.config={fn:"/~fn",...e}}_addHandler(e,t,r,a,{allowMeta:u=!1}={allowMeta:!1}){t=""===t?t:47===t.charCodeAt(0)?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i({...this.event},a)});let l=this.meta[o];if(a?.type)switch(a.type){case"text":a.type="text/plain";break;case"json":a.type="application/json";break;case"formdata":a.type="multipart/form-data";break;case"urlencoded":a.type="application/x-www-form-urlencoded"}d({schema:this.meta[s],contentType:a?.type,hook:a,method:e,path:t,models:this.meta[o]});let p={body:n(a?.body??this.$schema?.body,l),headers:n(a?.headers??this.$schema?.headers,l,!0),params:n(a?.params??this.$schema?.params,l),query:n(a?.query??this.$schema?.query,l),response:h(a?.response??this.$schema?.response,l)},f=i(this.event,a),m=c({path:t,method:e,hooks:f,validator:p,handler:r,handleError:this.handleError,meta:u?this.meta:void 0});if(-1===t.indexOf(":")&&-1===t.indexOf("*")){let r=this.staticRouter.handlers.length;this.staticRouter.handlers.push(m),this.staticRouter.variables+=`const st${r} = staticRouter.handlers[${r}]
|
|
2
|
+
`,this.staticRouter.map[t]||(this.staticRouter.map[t]=""),this.staticRouter.map[t]+="ALL"===e?`default:
|
|
3
|
+
`:`case '${e}':
|
|
4
|
+
`,this.staticRouter.map[t]+=`return st${r}(ctx)
|
|
5
|
+
`}else this.router.add(e,t,m)}onStart(e){return this.event.start.push(e),this}onRequest(e){return this.event.request.push(e),this}onParse(e){return this.event.parse.splice(this.event.parse.length-1,0,e),this}onTransform(e){return this.event.transform.push(e),this}onBeforeHandle(e){return this.event.beforeHandle.push(e),this}onAfterHandle(e){return this.event.afterHandle.push(e),this}onError(e){return this.event.error.push(e),this}onStop(e){return this.event.stop.push(e),this}on(e,t){switch(e){case"start":this.event.start.push(t);break;case"request":this.event.request.push(t);break;case"parse":this.event.parse.push(t);break;case"transform":this.event.transform.push(t);break;case"beforeHandle":this.event.beforeHandle.push(t);break;case"afterHandle":this.event.afterHandle.push(t);break;case"error":this.event.error.push(t);break;case"stop":this.event.stop.push(t)}return this}group(e,t,r){let s=new f;s.store=this.store,this.wsRouter&&s.use(p());let a="object"==typeof t,n=(a?r:t)(s);return this.decorators=u(this.decorators,s.decorators),n.event.request.length&&(this.event.request=[...this.event.request,...n.event.request]),this.model(n.meta[o]),Object.values(s.routes).forEach(({method:r,path:o,handler:h,hooks:u})=>{if(a){let a=`${e}${o}`,d=s.wsRouter?.find("subscribe",a);if(d){let e=s.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(a,e[2])}this._addHandler(r,a,h,i(t,{...u,error:u.error?Array.isArray(u.error)?[...u.error,...n.event.error]:[u.error,...n.event.error]:n.event.error}))}else{let t=`${e}${o}`,a=s.wsRouter?.find("subscribe",t);if(a){let e=s.wsRouter.history.find(([e,t])=>o===t);if(!e)return;return this.ws(t,e[2])}this._addHandler(r,t,h,i(u,{error:n.event.error}))}}),s.wsRouter&&this.wsRouter&&s.wsRouter.history.forEach(([t,r,s])=>{"/"===r?this.wsRouter?.add(t,e,s):this.wsRouter?.add(t,`${e}${r}`,s)}),this}guard(e,t){let r=new f;r.store=this.store,this.wsRouter&&r.use(p());let s=t(r);return this.decorators=u(this.decorators,r.decorators),s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),this.model(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:a,handler:o,hooks:n})=>{let h=r.wsRouter?.find("subscribe",a);if(h){let e=r.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(a,e[2])}this._addHandler(t,a,o,i(e,{...n,error:n.error?Array.isArray(n.error)?[...n.error,...s.event.error]:[n.error,...s.event.error]:s.event.error}))}),r.wsRouter&&this.wsRouter&&r.wsRouter.history.forEach(([e,t,r])=>{this.wsRouter?.add(e,t,r)}),this}use(e){if(e instanceof Promise)return this.lazyLoadModules.push(e.then(e=>"function"==typeof e?e(this):e.default(this)).then(e=>e.compile())),this;let t=e(this);return t instanceof Promise?(this.lazyLoadModules.push(t),this):t}if(e,t){return e?this.use(t):this}get(e,t,r){return this._addHandler("GET",e,t,r),this}post(e,t,r){return this._addHandler("POST",e,t,r),this}put(e,t,r){return this._addHandler("PUT",e,t,r),this}patch(e,t,r){return this._addHandler("PATCH",e,t,r),this}delete(e,t,r){return this._addHandler("DELETE",e,t,r),this}options(e,t,r){return this._addHandler("OPTIONS",e,t,r),this}all(e,t,r){return this._addHandler("ALL",e,t,r),this}head(e,t,r){return this._addHandler("HEAD",e,t,r),this}trace(e,t,r){return this._addHandler("TRACE",e,t,r),this}connect(e,t,r){return this._addHandler("CONNECT",e,t,r),this}ws(t,r){if(!this.wsRouter)throw Error("Can't find WebSocket. Please register WebSocket plugin first by importing 'elysia/ws'");return this.wsRouter.add("subscribe",t,r),this.get(t,t=>{if(!this.server?.upgrade(t.request,{headers:"function"==typeof r.headers?r.headers(t):r.headers,data:{...t,id:e(),message:n(r.schema?.body,this.meta[o]),transformMessage:r.transform?Array.isArray(r.transformMessage)?r.transformMessage:[r.transformMessage]:[]}}))return t.set.status=400,"Expected a websocket connection"},{parse:r.parse,beforeHandle:r.beforeHandle,transform:r.transform,schema:{headers:r.schema?.headers,params:r.schema?.params,query:r.schema?.query}}),this}route(e,t,r,{config:s,...a}={config:{allowMeta:!1}}){return this._addHandler(e,t,r,a,s),this}state(e,t){return"object"==typeof e?(this.store=u(this.store,e),this):(e in this.store||(this.store[e]=t),this)}decorate(e,t){return"object"==typeof e?(this.decorators=u(this.decorators,e),this):(e in this.decorators||(this.decorators[e]=t),this)}derive(e){return"AsyncFunction"===e.constructor.name?this.onTransform(async t=>{Object.assign(t,await e(t))}):this.onTransform(t=>{Object.assign(t,e(t))})}fn(e){return this.use(async()=>{let{fn:t}=await import("@elysiajs/fn");return t({app:this,value:e,path:this.config.fn})})}schema(e){let t=this.meta[o];return this.$schema={body:n(e.body,t),headers:n(e?.headers,t,!0),params:n(e?.params,t),query:n(e?.query,t),response:n(e?.response,t)},this}handle=async e=>this.fetch(e);compile=()=>(this.fetch=l(this),this);fetch(e){return(this.fetch=l(this))(e)}handleError=async(e,t,s)=>{for(let a=0;a<this.event.error.length;a++){let o=this.event.error[a]({request:e,code:t.code??"UNKNOWN",error:t,set:s});if(o instanceof Promise&&(o=await o),null!=o)return r(o,s)}return new Response(t.message,{headers:s.headers,status:t.status??500})};listen=(e,t)=>{if(!Bun)throw Error("Bun to run");if(this.compile(),"string"==typeof e&&Number.isNaN(e=+e))throw Error("Port must be a numeric value");let r=this.fetch,s=(process.env.ENV??process.env.NODE_ENV)!=="production",a="object"==typeof e?{...this.config.serve,...e,development:s,fetch:r}:{...this.config.serve,port:e,fetch:r};if("production"!==process.env.ENV){let e=`$$Elysia:${a.port}`;globalThis[e]?(this.server=globalThis[e],this.server.reload(a)):globalThis[e]=this.server=Bun.serve(a)}else this.server=Bun.serve(a);for(let e=0;e<this.event.start.length;e++)this.event.start[e](this);return t&&t(this.server),Promise.all(this.lazyLoadModules).then(()=>{Bun.gc(!0)}),this};stop=async()=>{if(!this.server)throw Error("Elysia isn't running. Call `app.listen` to start the server.");this.server.stop();for(let e=0;e<this.event.stop.length;e++)await this.event.stop[e](this)};get modules(){return Promise.all(this.lazyLoadModules)}model(e,t){return"object"==typeof e?Object.entries(e).forEach(([e,t])=>{e in this.meta[o]||(this.meta[o][e]=t)}):this.meta[o][e]=t,this}}export{t}from"./custom-types";export{ws}from"./ws";export{SCHEMA,DEFS,EXPOSED,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,getResponseSchemaValidator}from"./utils";export{ParseError,NotFoundError,ValidationError,InternalServerError}from"./error";export{f as Elysia};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type TSchema } from '@sinclair/typebox';
|
|
2
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
3
|
+
import type { HTTPMethod, LocalHook } from './types';
|
|
4
|
+
export declare const toOpenAPIPath: (path: string) => string;
|
|
5
|
+
export declare const mapProperties: (name: string, schema: TSchema | string | undefined, models: Record<string, TSchema>) => any[];
|
|
6
|
+
export declare const capitalize: (word: string) => string;
|
|
7
|
+
export declare const generateOperationId: (method: string, paths: string) => string;
|
|
8
|
+
export declare const registerSchemaPath: ({ schema, contentType, path, method, hook, models }: {
|
|
9
|
+
schema: Partial<OpenAPIV3.PathsObject>;
|
|
10
|
+
contentType?: string | string[] | undefined;
|
|
11
|
+
path: string;
|
|
12
|
+
method: HTTPMethod;
|
|
13
|
+
hook?: LocalHook<any, any> | undefined;
|
|
14
|
+
models: Record<string, TSchema>;
|
|
15
|
+
}) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Kind as e}from"@sinclair/typebox";import t from"lodash.clonedeep";export const toOpenAPIPath=e=>e.split("/").map(e=>e.startsWith(":")?`{${e.slice(1,e.length)}}`:e).join("/");export const mapProperties=(e,t,r)=>{if(void 0===t)return[];if("string"==typeof t){if(t in r)t=r[t];else throw Error(`Can't find model ${t}`)}return Object.entries(t?.properties??[]).map(([r,o])=>({...o,in:e,name:r,type:o?.type,required:t.required?.includes(r)??!1}))};let r=(e,t)=>{let r={};for(let o of e)r[o]={schema:"string"==typeof t?{$ref:`#/components/schemas/${t}`}:{...t}};return r};export const capitalize=e=>e.charAt(0).toUpperCase()+e.slice(1);export const generateOperationId=(e,t)=>{let r=e.toLowerCase();if("/"===t)return r+"Index";for(let e of t.split("/"))123===e.charCodeAt(0)?r+="By"+capitalize(e.slice(1,-1)):r+=capitalize(e);return r};export const registerSchemaPath=({schema:o,contentType:i=["application/json","multipart/form-data","text/plain"],path:n,method:p,hook:s,models:a})=>{s&&(s=t(s)),n=toOpenAPIPath(n);let c="string"==typeof i?[i]:i??["application/json"],l=s?.body,d=s?.params,f=s?.headers,m=s?.query,h=s?.response;if("object"==typeof h){if(e in h){let{type:e,properties:t,required:o,...i}=h;h={200:{...i,description:i.description,content:r(c,"object"===e||"array"===e?{type:e,properties:t,required:o}:h)}}}else Object.entries(h).forEach(([e,t])=>{if("string"==typeof t){let{type:o,properties:i,required:n,...p}=a[t];h[e]={...p,description:p.description,content:r(c,t)}}else{let{type:o,properties:i,required:n,...p}=t;h[e]={...p,description:p.description,content:r(c,{type:o,properties:i,required:n})}}})}else if("string"==typeof h){let{type:e,properties:t,required:o,...i}=a[h];h={200:{...i,content:r(c,h)}}}let y=[...mapProperties("header",f,a),...mapProperties("path",d,a),...mapProperties("query",m,a)];o[n]={...o[n]?o[n]:{},[p.toLowerCase()]:{...f||d||m||l?{parameters:y}:{},...h?{responses:h}:{},operationId:s?.detail?.operationId??generateOperationId(p,n),...s?.detail,...l?{requestBody:{content:r(c,"string"==typeof l?{$ref:`#/components/schemas/${l}`}:l)}}:null}}};
|