elysia 2.0.0-beta.29 → 2.0.0-beta.30
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/adapter/bun/index.js +2 -2
- package/dist/adapter/bun/index.mjs +2 -2
- package/dist/base.d.ts +78 -36
- package/dist/base.js +27 -16
- package/dist/base.mjs +28 -17
- package/dist/compile/aot-capture.js +1 -1
- package/dist/compile/aot-capture.mjs +1 -1
- package/dist/compile/aot.d.ts +2 -2
- package/dist/compile/aot.js +5 -5
- package/dist/compile/aot.mjs +5 -6
- package/dist/compile/handler/descriptor.js +1 -1
- package/dist/compile/handler/descriptor.mjs +2 -2
- package/dist/compile/handler/index.js +3 -3
- package/dist/compile/handler/index.mjs +4 -4
- package/dist/compile/handler/jit.js +3 -3
- package/dist/compile/handler/jit.mjs +4 -4
- package/dist/error.d.ts +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -0
- package/dist/index.mjs +2 -2
- package/dist/package.js +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugin/aot/core.d.ts +1 -1
- package/dist/trace.d.ts +10 -1
- package/dist/type/bridge-live.d.ts +1 -1
- package/dist/type/bridge-live.js +1 -1
- package/dist/type/bridge-live.mjs +1 -1
- package/dist/type/bridge.d.ts +1 -1
- package/dist/type/typebox-value-live.d.ts +2 -2
- package/dist/type/typebox-value-live.js +2 -2
- package/dist/type/typebox-value-live.mjs +2 -2
- package/dist/type/typebox-value-ops.d.ts +2 -2
- package/dist/type/typebox-value-ops.js +2 -2
- package/dist/type/typebox-value-ops.mjs +2 -2
- package/dist/type/typebox-value.d.ts +6 -6
- package/dist/type/validator/index.js +5 -5
- package/dist/type/validator/index.mjs +6 -6
- package/dist/types.d.ts +6 -5
- package/dist/validator/index.js +2 -2
- package/dist/validator/index.mjs +3 -3
- package/dist/ws/route.js +1 -1
- package/dist/ws/route.mjs +2 -2
- package/package.json +1 -1
|
@@ -290,7 +290,7 @@ const BunAdapter = require_adapter_index.createAdapter({
|
|
|
290
290
|
};
|
|
291
291
|
const registerCleanup = (handler) => {
|
|
292
292
|
if (ext.cleanupEpoch !== registerCleanup) return false;
|
|
293
|
-
if (!epoch.setup) throw new Error("[Elysia] .
|
|
293
|
+
if (!epoch.setup) throw new Error("[Elysia] .onStop() called after its onStart epoch settled");
|
|
294
294
|
if (Array.isArray(handler)) epochCleanup.push(...handler);
|
|
295
295
|
else epochCleanup.push(handler);
|
|
296
296
|
return true;
|
|
@@ -477,7 +477,7 @@ const BunAdapter = require_adapter_index.createAdapter({
|
|
|
477
477
|
if (cleanupFailures) errors.push(...cleanupFailures);
|
|
478
478
|
const release = () => releaseLifecycle(app, ext, createdExt, stop, registerCleanup);
|
|
479
479
|
if (releasable && abandonedSetups) abandonedSetups.then((results) => {
|
|
480
|
-
for (const result of results) if (result.status === "rejected") console.error("[Elysia]
|
|
480
|
+
for (const result of results) if (result.status === "rejected") console.error("[Elysia] onStart abandoned by stop(true) failed:", result.reason);
|
|
481
481
|
release();
|
|
482
482
|
});
|
|
483
483
|
else if (releasable) release();
|
|
@@ -290,7 +290,7 @@ const BunAdapter = createAdapter({
|
|
|
290
290
|
};
|
|
291
291
|
const registerCleanup = (handler) => {
|
|
292
292
|
if (ext.cleanupEpoch !== registerCleanup) return false;
|
|
293
|
-
if (!epoch.setup) throw new Error("[Elysia] .
|
|
293
|
+
if (!epoch.setup) throw new Error("[Elysia] .onStop() called after its onStart epoch settled");
|
|
294
294
|
if (Array.isArray(handler)) epochCleanup.push(...handler);
|
|
295
295
|
else epochCleanup.push(handler);
|
|
296
296
|
return true;
|
|
@@ -477,7 +477,7 @@ const BunAdapter = createAdapter({
|
|
|
477
477
|
if (cleanupFailures) errors.push(...cleanupFailures);
|
|
478
478
|
const release = () => releaseLifecycle(app, ext, createdExt, stop, registerCleanup);
|
|
479
479
|
if (releasable && abandonedSetups) abandonedSetups.then((results) => {
|
|
480
|
-
for (const result of results) if (result.status === "rejected") console.error("[Elysia]
|
|
480
|
+
for (const result of results) if (result.status === "rejected") console.error("[Elysia] onStart abandoned by stop(true) failed:", result.reason);
|
|
481
481
|
release();
|
|
482
482
|
});
|
|
483
483
|
else if (releasable) release();
|
package/dist/base.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Context, ErrorContext, LifecycleContext } from "./context.js";
|
|
|
5
5
|
import { TraceCapability, TraceHandler } from "./trace.js";
|
|
6
6
|
import { AnySchema } from "./type/types.js";
|
|
7
7
|
import { AotFingerprint, CompilerSession, ProgramId } from "./compile/aot.js";
|
|
8
|
-
import { AddRoute, AddWSRoute, AfterHandler, AfterResponseHandler, AnyErrorConstructor, AnyLocalHook, BodyHandler, CompiledHandler, CreateEden, DefaultEphemeral, DefaultMetadata, DefaultSingleton, DefinitionBase, ElysiaConfig, ElysiaHandlerToResponseSchemaAmbiguous, EphemeralType, ErrorDefinitionEntry, ErrorHandler, EventScope, ExcludeElysiaResponse, ExtractErrorFromHandle, GlobalHookReturn, GracefulHandler, GuardHookSingleton, GuardLocalHook, HTTPMethod, HistoryEntry, HookContextSchema, HookContextSingleton, InlineHandler, InlineHandlerNonMacro, InputSchema, InputSchemaKey, InternalRoute, IntersectIfObjectSchema, JoinPath, LocalHook, LocalHookReturn, Macro, MacroOptionContext, MacroToContext, MacroToProperty, MapResponse, MaybeArray, MaybePromise, MergeElysiaInstances, MergeSchema, MergeScopedSchemas, MetadataBase, ObjectMacroDefs, OptionalHandler, ParentResponse, PluginHookReturn, PreHandler, Prettify, PublicRoute, ResolveUsedRouteErrors, RouteBase, ScopedHookReturn, ScopedMapDeriveReturn, SingletonBase, StaticMapAliases, TransformHandler, UnionResponseStatus, UnwrapRoute, WrapFn } from "./types.js";
|
|
8
|
+
import { AddRoute, AddWSRoute, AfterHandler, AfterResponseHandler, AnyErrorConstructor, AnyLocalHook, BodyHandler, CompiledHandler, CreateEden, DefaultEphemeral, DefaultMetadata, DefaultSingleton, DefinitionBase, ElysiaConfig, ElysiaHandlerToResponseSchemaAmbiguous, EphemeralType, ErrorDefinitionEntry, ErrorHandler, EventScope, ExcludeElysiaResponse, ExtractErrorFromHandle, GlobalHookReturn, GracefulHandler, GuardHookSingleton, GuardLocalHook, HTTPMethod, HistoryEntry, HookContextSchema, HookContextSingleton, InlineHandler, InlineHandlerNonMacro, InputSchema, InputSchemaKey, InternalRoute, IntersectIfObjectSchema, JoinPath, LegacyScope, LocalHook, LocalHookReturn, Macro, MacroOptionContext, MacroToContext, MacroToProperty, MapResponse, MaybeArray, MaybePromise, MergeElysiaInstances, MergeSchema, MergeScopedSchemas, MetadataBase, ObjectMacroDefs, OptionalHandler, ParentResponse, PluginHookReturn, PreHandler, Prettify, PublicRoute, ResolveUsedRouteErrors, RouteBase, ScopedHookReturn, ScopedMapDeriveReturn, SingletonBase, StaticMapAliases, TransformHandler, UnionResponseStatus, UnwrapRoute, WrapFn } from "./types.js";
|
|
9
9
|
import { RouteErrorHooks } from "./handler/utils.js";
|
|
10
10
|
import { WSCapability, WSHandlerResponse, WSLocalHook, WSMessageHandler, WSOptions, WSOptionsEntry } from "./ws/types.js";
|
|
11
11
|
import { RouteTable } from "./route-table.js";
|
|
@@ -204,10 +204,14 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
204
204
|
derive: Singleton['derive'];
|
|
205
205
|
}, Definitions, Metadata, Routes, Ephemeral, Volatile>;
|
|
206
206
|
headers(headers: Record<string, string>): this;
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
207
|
+
onRequest(fn: MaybeArray<PreHandler<{}, Singleton>>): this;
|
|
208
|
+
onParse(fn: MaybeArray<BodyHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>): this;
|
|
209
|
+
onParse(name: string): this;
|
|
210
|
+
/** @deprecated 1.x syntax, use `.onParse('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
211
|
+
onParse<const As extends 'local' | 'scoped' | 'global'>(options: {
|
|
212
|
+
as: As;
|
|
213
|
+
}, fn: MaybeArray<BodyHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>>): this;
|
|
214
|
+
onParse<const HookScope extends EventScope>(scope: HookScope, fn: MaybeArray<BodyHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>>): this;
|
|
211
215
|
parser<const Name extends string>(name: Name, fn: BodyHandler<MergeSchema<Volatile['schema'], MergeSchema<Ephemeral['schema'], Metadata['schema']>, BasePath> & Metadata['schemas'] & Ephemeral['schemas'] & Volatile['schemas'], Singleton & {
|
|
212
216
|
derive: Ephemeral['derive'] & Volatile['derive'];
|
|
213
217
|
}>): Elysia<BasePath, Scope, Singleton, Definitions, {
|
|
@@ -219,31 +223,43 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
219
223
|
response: Metadata['response'];
|
|
220
224
|
}, Routes, Ephemeral, Volatile>;
|
|
221
225
|
/**
|
|
222
|
-
* ###
|
|
226
|
+
* ### onStart | Life cycle event
|
|
223
227
|
* Called after server is ready for serving
|
|
224
228
|
*
|
|
225
229
|
* ---
|
|
226
230
|
* @example
|
|
227
231
|
* ```typescript
|
|
228
232
|
* new Elysia()
|
|
229
|
-
* .
|
|
233
|
+
* .onStart(({ server }) => {
|
|
230
234
|
* console.log(`Running at ${server?.url}`)
|
|
231
235
|
* })
|
|
232
236
|
* .listen(3000)
|
|
233
237
|
* ```
|
|
234
238
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
239
|
+
onStart(handler: MaybeArray<GracefulHandler<this>>): this;
|
|
240
|
+
onTransform(fn: MaybeArray<TransformHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>): this;
|
|
241
|
+
/** @deprecated 1.x syntax, use `.onTransform('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
242
|
+
onTransform<const As extends 'local' | 'scoped' | 'global'>(options: {
|
|
243
|
+
as: As;
|
|
244
|
+
}, fn: MaybeArray<TransformHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>>): this;
|
|
245
|
+
onTransform<const HookScope extends EventScope>(scope: HookScope, fn: MaybeArray<TransformHandler<MergeSchema<{}, {}, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>>): this;
|
|
246
|
+
onBeforeHandle<const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
247
|
+
onBeforeHandle<const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(scope: 'local', fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
248
|
+
onBeforeHandle<const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>>>(scope: 'plugin', fn: Handler): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
249
|
+
onBeforeHandle<const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'global'>>>(scope: 'global', fn: Handler): GlobalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
250
|
+
/** @deprecated 1.x syntax, use `.onBeforeHandle('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
251
|
+
onBeforeHandle<const As extends 'local' | 'scoped' | 'global', const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>>>(options: {
|
|
252
|
+
as: As;
|
|
253
|
+
}, fn: Handler): ScopedHookReturn<LegacyScope<As>, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
254
|
+
onBeforeHandle<const HookScope extends EventScope, const Handler extends MaybeArray<OptionalHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>>>(scope: HookScope, fn: Handler): ScopedHookReturn<HookScope, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
243
255
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
244
256
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'local', transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
245
257
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'plugin', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>) => MaybePromise<Derivative>): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
246
258
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'global', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'global'>) => MaybePromise<Derivative>): GlobalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
259
|
+
/** @deprecated 1.x syntax, use `.derive('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
260
|
+
derive<const As extends 'local' | 'scoped' | 'global', const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(options: {
|
|
261
|
+
as: As;
|
|
262
|
+
}, transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>) => MaybePromise<Derivative>): ScopedHookReturn<LegacyScope<As>, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
247
263
|
derive<const HookScope extends EventScope, const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: HookScope, transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>) => MaybePromise<Derivative>): ScopedHookReturn<HookScope, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
248
264
|
mapDerive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, {
|
|
249
265
|
derive: ExcludeElysiaResponse<Derivative>;
|
|
@@ -278,18 +294,34 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
278
294
|
parser: Metadata['parser'];
|
|
279
295
|
response: UnionResponseStatus<Metadata['response'], ExtractErrorFromHandle<Derivative>>;
|
|
280
296
|
}, Routes, Ephemeral, Volatile>;
|
|
297
|
+
/** @deprecated 1.x syntax, use `.mapDerive('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
298
|
+
mapDerive<const As extends 'local' | 'scoped' | 'global', const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(options: {
|
|
299
|
+
as: As;
|
|
300
|
+
}, transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>) => MaybePromise<Derivative>): ScopedMapDeriveReturn<LegacyScope<As>, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
281
301
|
mapDerive<const HookScope extends EventScope, const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: HookScope, transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>) => MaybePromise<Derivative>): ScopedMapDeriveReturn<HookScope, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
302
|
+
onAfterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
303
|
+
onAfterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(scope: 'local', fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
304
|
+
onAfterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>>>(scope: 'plugin', fn: Handler): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
305
|
+
onAfterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'global'>>>(scope: 'global', fn: Handler): GlobalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
306
|
+
/** @deprecated 1.x syntax, use `.onAfterHandle('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
307
|
+
onAfterHandle<const As extends 'local' | 'scoped' | 'global', const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>>>(options: {
|
|
308
|
+
as: As;
|
|
309
|
+
}, fn: Handler): ScopedHookReturn<LegacyScope<As>, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
310
|
+
onAfterHandle<const HookScope extends EventScope, const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>>>(scope: HookScope, fn: Handler): ScopedHookReturn<HookScope, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
287
311
|
mapResponse(fn: MaybeArray<MapResponse<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>): this;
|
|
312
|
+
/** @deprecated 1.x syntax, use `.mapResponse('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
313
|
+
mapResponse<const As extends 'local' | 'scoped' | 'global'>(options: {
|
|
314
|
+
as: As;
|
|
315
|
+
}, fn: MaybeArray<MapResponse<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>>): this;
|
|
288
316
|
mapResponse<const HookScope extends EventScope>(scope: HookScope, fn: MaybeArray<MapResponse<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>>): this;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
317
|
+
onAfterResponse(fn: AfterResponseHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>): this;
|
|
318
|
+
/** @deprecated 1.x syntax, use `.onAfterResponse('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
319
|
+
onAfterResponse<const As extends 'local' | 'scoped' | 'global'>(options: {
|
|
320
|
+
as: As;
|
|
321
|
+
}, fn: AfterResponseHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, LegacyScope<As>>): this;
|
|
322
|
+
onAfterResponse<const HookScope extends EventScope>(scope: HookScope, fn: AfterResponseHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, HookScope>): this;
|
|
323
|
+
onError<const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton>>>(fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
324
|
+
onError<const E extends AnyErrorConstructor & (abstract new (...args: any) => Error), const Fn extends (context: ErrorContext<{}, Singleton & {
|
|
293
325
|
derive: Scope extends 'local' ? Partial<Ephemeral['derive'] & Volatile['derive']> : Scope extends 'plugin' ? Ephemeral['derive'] & Partial<Volatile['derive']> : Ephemeral['derive'] & Volatile['derive'];
|
|
294
326
|
}> & {
|
|
295
327
|
error: InstanceType<E>;
|
|
@@ -309,26 +341,32 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
309
341
|
typebox: Definitions['typebox'];
|
|
310
342
|
error: [...Definitions['error'], ErrorDefinitionEntry<E, ReturnType<Fn>>];
|
|
311
343
|
}, Metadata, Routes, Ephemeral, Volatile>;
|
|
312
|
-
|
|
344
|
+
onError<const E extends AnyErrorConstructor & (abstract new (...args: any) => Error), const Value>(error: E, value: Value): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, {
|
|
313
345
|
derive: Volatile['derive'];
|
|
314
346
|
schema: Volatile['schema'];
|
|
315
347
|
schemas: Volatile['schemas'];
|
|
316
348
|
response: Volatile['response'];
|
|
317
349
|
error: [...Volatile['error'], ErrorDefinitionEntry<E, Value>];
|
|
318
350
|
}>;
|
|
319
|
-
|
|
351
|
+
onError<const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton & {
|
|
320
352
|
derive: Partial<Ephemeral['derive'] & Volatile['derive']>;
|
|
321
353
|
}>>>(scope: 'local', fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
322
|
-
|
|
354
|
+
onError<const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton & {
|
|
323
355
|
derive: Ephemeral['derive'] & Partial<Volatile['derive']>;
|
|
324
356
|
}>>>(scope: 'plugin', fn: Handler): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
325
|
-
|
|
357
|
+
onError<const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton & {
|
|
326
358
|
derive: Ephemeral['derive'] & Volatile['derive'];
|
|
327
359
|
}>>>(scope: 'global', fn: Handler): GlobalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
328
|
-
|
|
360
|
+
/** @deprecated 1.x syntax, use `.onError('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
361
|
+
onError<const As extends 'local' | 'scoped' | 'global', const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton & {
|
|
362
|
+
derive: Partial<Ephemeral['derive'] & Volatile['derive']>;
|
|
363
|
+
}>>>(options: {
|
|
364
|
+
as: As;
|
|
365
|
+
}, fn: Handler): ScopedHookReturn<LegacyScope<As>, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
366
|
+
onError<const HookScope extends EventScope, const Handler extends MaybeArray<ErrorHandler<[...Definitions['error'], ...Ephemeral['error'], ...Volatile['error']], {}, Singleton & {
|
|
329
367
|
derive: Partial<Ephemeral['derive'] & Volatile['derive']>;
|
|
330
368
|
}>>>(scope: HookScope, fn: Handler): ScopedHookReturn<HookScope, BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
331
|
-
|
|
369
|
+
onError<const S extends EventScope, const E extends AnyErrorConstructor & (abstract new (...args: any) => Error), const Fn extends (context: ErrorContext<{}, {
|
|
332
370
|
store: Singleton['store'];
|
|
333
371
|
decorator: Singleton['decorator'];
|
|
334
372
|
derive: {};
|
|
@@ -350,7 +388,7 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
350
388
|
response: Volatile['response'];
|
|
351
389
|
error: [...Volatile['error'], ErrorDefinitionEntry<E, ReturnType<Fn>>];
|
|
352
390
|
}>;
|
|
353
|
-
|
|
391
|
+
onError<const S extends EventScope, const E extends AnyErrorConstructor & (abstract new (...args: any) => Error), const Value>(scope: S, error: E, value: Value): S extends 'global' ? Elysia<BasePath, Scope, Singleton, {
|
|
354
392
|
typebox: Definitions['typebox'];
|
|
355
393
|
error: [...Definitions['error'], ErrorDefinitionEntry<E, Value>];
|
|
356
394
|
}, Metadata, Routes, Ephemeral, Volatile> : S extends 'plugin' ? Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, {
|
|
@@ -367,6 +405,10 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
367
405
|
error: [...Volatile['error'], ErrorDefinitionEntry<E, Value>];
|
|
368
406
|
}>;
|
|
369
407
|
trace(fn: TraceHandler<any, any>): this;
|
|
408
|
+
/** @deprecated 1.x syntax, use `.trace('plugin', fn)`, `'scoped'` is now `'plugin'` */
|
|
409
|
+
trace<const As extends 'local' | 'scoped' | 'global'>(options: {
|
|
410
|
+
as: As;
|
|
411
|
+
}, fn: TraceHandler<any, any>): this;
|
|
370
412
|
trace<const HookScope extends EventScope>(scope: HookScope, fn: TraceHandler<any, any>): this;
|
|
371
413
|
as(type: 'plugin'): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, {
|
|
372
414
|
derive: Ephemeral['derive'] & Volatile['derive'];
|
|
@@ -970,23 +1012,23 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
970
1012
|
*/
|
|
971
1013
|
wrap<T extends (...params: any) => MaybePromise<Response> = (request: Request, ...rest: any[]) => MaybePromise<Response>>(callback: WrapFn<T>): this;
|
|
972
1014
|
/**
|
|
973
|
-
* ###
|
|
1015
|
+
* ### onStop | Life cycle event
|
|
974
1016
|
* Called after server stop serving request
|
|
975
1017
|
*
|
|
976
1018
|
* ---
|
|
977
1019
|
* @example
|
|
978
1020
|
* ```typescript
|
|
979
1021
|
* new Elysia()
|
|
980
|
-
* .
|
|
1022
|
+
* .onStop((app) => {
|
|
981
1023
|
* closeDatabase()
|
|
982
1024
|
* })
|
|
983
1025
|
* ```
|
|
984
1026
|
*/
|
|
985
|
-
|
|
1027
|
+
onStop(handler: MaybeArray<GracefulHandler<this>>): this;
|
|
986
1028
|
/**
|
|
987
|
-
* Stop the underlying server (if any), then run every `
|
|
1029
|
+
* Stop the underlying server (if any), then run every `onStop` handler
|
|
988
1030
|
*
|
|
989
|
-
* Awaiting `stop()` from inside
|
|
1031
|
+
* Awaiting `stop()` from inside an `onStart`, `onStop` or WebSocket lifecycle
|
|
990
1032
|
* callback is only supported while that callback is still synchronous
|
|
991
1033
|
*
|
|
992
1034
|
* @param closeActiveConnections Pass `true` to terminate active
|
package/dist/base.js
CHANGED
|
@@ -69,6 +69,14 @@ const toModel = (key, value) => {
|
|
|
69
69
|
const assertScope = (scope) => {
|
|
70
70
|
if (scope !== void 0 && scope !== "local" && scope !== "plugin" && scope !== "global") throw new Error(`[Elysia] Invalid hook scope ${JSON.stringify(scope)}, expected 'local', 'plugin' or 'global' (1.x 'scoped' is 'plugin')`);
|
|
71
71
|
};
|
|
72
|
+
let warnedLegacyScope = false;
|
|
73
|
+
const legacyScope = ({ as }) => {
|
|
74
|
+
if (!warnedLegacyScope) {
|
|
75
|
+
warnedLegacyScope = true;
|
|
76
|
+
console.warn("[Elysia] { as: scope } is deprecated, pass the scope as the first argument instead ('scoped' is now 'plugin')");
|
|
77
|
+
}
|
|
78
|
+
return as === "scoped" ? "plugin" : as;
|
|
79
|
+
};
|
|
72
80
|
const hasHookKeys = (value) => {
|
|
73
81
|
for (const key in value) if (require_utils.schemaProperties.has(key) || require_utils.eventProperties.has(key) || key === "detail" || key === "tags") return true;
|
|
74
82
|
return false;
|
|
@@ -281,6 +289,7 @@ var Elysia = class Elysia {
|
|
|
281
289
|
}
|
|
282
290
|
#on(type, fn, scope = this["~config"]?.as) {
|
|
283
291
|
this.#assertMutable(type);
|
|
292
|
+
if (scope && typeof scope === "object") scope = legacyScope(scope) ?? this["~config"]?.as;
|
|
284
293
|
assertScope(scope);
|
|
285
294
|
const added = require_utils.nullObject();
|
|
286
295
|
added[type] = fn;
|
|
@@ -312,10 +321,10 @@ var Elysia = class Elysia {
|
|
|
312
321
|
#onBranch(type, scopeOrFn, fn) {
|
|
313
322
|
return fn ? this.#on(type, fn, scopeOrFn) : this.#on(type, scopeOrFn);
|
|
314
323
|
}
|
|
315
|
-
|
|
324
|
+
onRequest(fn) {
|
|
316
325
|
return this.#on("request", fn, "global");
|
|
317
326
|
}
|
|
318
|
-
|
|
327
|
+
onParse(scopeOrFnOrName, fn) {
|
|
319
328
|
if (fn === void 0 && typeof scopeOrFnOrName === "string") {
|
|
320
329
|
const named = this["~ext"]?.parser?.[scopeOrFnOrName];
|
|
321
330
|
return this.#onBranch("parse", named ?? scopeOrFnOrName);
|
|
@@ -328,15 +337,15 @@ var Elysia = class Elysia {
|
|
|
328
337
|
parsers[name] = fn;
|
|
329
338
|
return this;
|
|
330
339
|
}
|
|
331
|
-
|
|
332
|
-
this.#assertMutable("
|
|
340
|
+
onStart(handler) {
|
|
341
|
+
this.#assertMutable("onStart");
|
|
333
342
|
(this.#ext.setup ??= []).push(...[].concat(handler));
|
|
334
343
|
return this;
|
|
335
344
|
}
|
|
336
|
-
|
|
345
|
+
onTransform(scopeOrFn, fn) {
|
|
337
346
|
return this.#onBranch("transform", scopeOrFn, fn);
|
|
338
347
|
}
|
|
339
|
-
|
|
348
|
+
onBeforeHandle(scopeOrFn, fn) {
|
|
340
349
|
return this.#onBranch("beforeHandle", scopeOrFn, fn);
|
|
341
350
|
}
|
|
342
351
|
derive(scopeOrFn, fn) {
|
|
@@ -353,20 +362,20 @@ var Elysia = class Elysia {
|
|
|
353
362
|
mapDerive(scopeOrFn, fn) {
|
|
354
363
|
return this.#derive(scopeOrFn, fn, require_utils.mapDeriveEntry);
|
|
355
364
|
}
|
|
356
|
-
|
|
365
|
+
onAfterHandle(scopeOrFn, fn) {
|
|
357
366
|
return this.#onBranch("afterHandle", scopeOrFn, fn);
|
|
358
367
|
}
|
|
359
368
|
mapResponse(scopeOrFn, fn) {
|
|
360
369
|
return this.#onBranch("mapResponse", scopeOrFn, fn);
|
|
361
370
|
}
|
|
362
|
-
|
|
371
|
+
onAfterResponse(scopeOrFn, fn) {
|
|
363
372
|
return this.#onBranch("afterResponse", scopeOrFn, fn);
|
|
364
373
|
}
|
|
365
|
-
|
|
366
|
-
this.#assertMutable("
|
|
374
|
+
onError(scopeOrFnOrError, fnOrError, fn) {
|
|
375
|
+
this.#assertMutable("onError");
|
|
367
376
|
switch (arguments.length) {
|
|
368
377
|
case 1:
|
|
369
|
-
if (scopeOrFnOrError && typeof scopeOrFnOrError === "object" && !Array.isArray(scopeOrFnOrError)) throw new Error("[Elysia] .
|
|
378
|
+
if (scopeOrFnOrError && typeof scopeOrFnOrError === "object" && !Array.isArray(scopeOrFnOrError)) throw new Error("[Elysia] .onError({ CODE: Class }) was removed in 2.0 — use .onError(Class, handler)");
|
|
370
379
|
return this.#onBranch("error", scopeOrFnOrError);
|
|
371
380
|
case 2:
|
|
372
381
|
if (typeof scopeOrFnOrError === "function" && scopeOrFnOrError.prototype instanceof Object) return this.#onErrorClass(scopeOrFnOrError, fnOrError);
|
|
@@ -384,7 +393,8 @@ var Elysia = class Elysia {
|
|
|
384
393
|
}
|
|
385
394
|
as(target) {
|
|
386
395
|
this.#assertMutable("as");
|
|
387
|
-
|
|
396
|
+
if (target !== "plugin" && target !== "global") throw new Error(`[Elysia] Invalid .as() scope ${JSON.stringify(target)}, expected 'plugin' or 'global' (1.x 'scoped' is 'plugin')`);
|
|
397
|
+
this.#as(this["~hookChain"], target);
|
|
388
398
|
return this;
|
|
389
399
|
}
|
|
390
400
|
#as(node, scope) {
|
|
@@ -1015,13 +1025,14 @@ var Elysia = class Elysia {
|
|
|
1015
1025
|
#assertMutable(api) {
|
|
1016
1026
|
mutationEpoch++;
|
|
1017
1027
|
if (this["~generation"] === void 0) return;
|
|
1018
|
-
|
|
1028
|
+
const on = "on" + api[0].toUpperCase() + api.slice(1);
|
|
1029
|
+
throw new Error(`[Elysia] .${on in this ? on : api}() called after the app was sealed by its first request, listen or compile`);
|
|
1019
1030
|
}
|
|
1020
1031
|
#assertTableMutable(api) {
|
|
1021
1032
|
for (let app = this; app; app = app.scopeParent) app.#assertMutable(api);
|
|
1022
1033
|
}
|
|
1023
1034
|
get #isCacheable() {
|
|
1024
|
-
return this["~generation"] !== void 0 && this.#routerBuilt && !require_compile_aot.
|
|
1035
|
+
return this["~generation"] !== void 0 && this.#routerBuilt && !require_compile_aot.inAotBuild();
|
|
1025
1036
|
}
|
|
1026
1037
|
#cacheRoutes(generation) {
|
|
1027
1038
|
if (this.cachedGeneration === generation && this.cachedEpoch === mutationEpoch) return;
|
|
@@ -1607,9 +1618,9 @@ var Elysia = class Elysia {
|
|
|
1607
1618
|
(ext.hocOrigin ??= []).push(this.hash ?? {});
|
|
1608
1619
|
return this;
|
|
1609
1620
|
}
|
|
1610
|
-
|
|
1621
|
+
onStop(handler) {
|
|
1611
1622
|
if (this["~ext"]?.cleanupEpoch?.(handler)) return this;
|
|
1612
|
-
this.#assertMutable("
|
|
1623
|
+
this.#assertMutable("onStop");
|
|
1613
1624
|
(this.#ext.cleanup ??= []).push(...[].concat(handler));
|
|
1614
1625
|
return this;
|
|
1615
1626
|
}
|
package/dist/base.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { isBun } from "./universal/constants.mjs";
|
|
|
3
3
|
import { clearFlattenChainMemo, clonePlainDecorators, clonePlainDeep, coalesceSchemas, createErrorEventHandler, eventProperties, getLoosePath, guardNonPlainLeaves, hookToGuard, invalidateMacroEpoch, isEmpty, isHTMLBundle, isNotEmpty, isPlainObject, isRecordNumber, joinPath, macroOrigin, mapDeriveEntry, markPropertySingletons, markSingletons, mergeDeep, mergeResponse, nullObject, pluginHash, schemaProperties, serializeMacroSeed, throwLifecycleErrors } from "./utils.mjs";
|
|
4
4
|
import { Ref, isBridgeLive, warmTypebox } from "./type/bridge.mjs";
|
|
5
5
|
import { isProduction } from "./universal/is-production.mjs";
|
|
6
|
-
import { Capture, Compiled, beginCompilerSession, createAotFingerprint, endCompilerSession } from "./compile/aot.mjs";
|
|
6
|
+
import { Capture, Compiled, beginCompilerSession, createAotFingerprint, endCompilerSession, inAotBuild } from "./compile/aot.mjs";
|
|
7
7
|
import { traceCapabilityRequired, wsCapabilityRequired } from "./generation.mjs";
|
|
8
8
|
import { releaseAnalysisCaches } from "./compile/handler/descriptor.mjs";
|
|
9
9
|
import { chainResolver, clearHandlerAnalysisCaches, compileHandler, composeRouteHook, localMacroRoot, resolveLocalHook, runIntrospect } from "./compile/handler/index.mjs";
|
|
@@ -67,6 +67,14 @@ const toModel = (key, value) => {
|
|
|
67
67
|
const assertScope = (scope) => {
|
|
68
68
|
if (scope !== void 0 && scope !== "local" && scope !== "plugin" && scope !== "global") throw new Error(`[Elysia] Invalid hook scope ${JSON.stringify(scope)}, expected 'local', 'plugin' or 'global' (1.x 'scoped' is 'plugin')`);
|
|
69
69
|
};
|
|
70
|
+
let warnedLegacyScope = false;
|
|
71
|
+
const legacyScope = ({ as }) => {
|
|
72
|
+
if (!warnedLegacyScope) {
|
|
73
|
+
warnedLegacyScope = true;
|
|
74
|
+
console.warn("[Elysia] { as: scope } is deprecated, pass the scope as the first argument instead ('scoped' is now 'plugin')");
|
|
75
|
+
}
|
|
76
|
+
return as === "scoped" ? "plugin" : as;
|
|
77
|
+
};
|
|
70
78
|
const hasHookKeys = (value) => {
|
|
71
79
|
for (const key in value) if (schemaProperties.has(key) || eventProperties.has(key) || key === "detail" || key === "tags") return true;
|
|
72
80
|
return false;
|
|
@@ -279,6 +287,7 @@ var Elysia = class Elysia {
|
|
|
279
287
|
}
|
|
280
288
|
#on(type, fn, scope = this["~config"]?.as) {
|
|
281
289
|
this.#assertMutable(type);
|
|
290
|
+
if (scope && typeof scope === "object") scope = legacyScope(scope) ?? this["~config"]?.as;
|
|
282
291
|
assertScope(scope);
|
|
283
292
|
const added = nullObject();
|
|
284
293
|
added[type] = fn;
|
|
@@ -310,10 +319,10 @@ var Elysia = class Elysia {
|
|
|
310
319
|
#onBranch(type, scopeOrFn, fn) {
|
|
311
320
|
return fn ? this.#on(type, fn, scopeOrFn) : this.#on(type, scopeOrFn);
|
|
312
321
|
}
|
|
313
|
-
|
|
322
|
+
onRequest(fn) {
|
|
314
323
|
return this.#on("request", fn, "global");
|
|
315
324
|
}
|
|
316
|
-
|
|
325
|
+
onParse(scopeOrFnOrName, fn) {
|
|
317
326
|
if (fn === void 0 && typeof scopeOrFnOrName === "string") {
|
|
318
327
|
const named = this["~ext"]?.parser?.[scopeOrFnOrName];
|
|
319
328
|
return this.#onBranch("parse", named ?? scopeOrFnOrName);
|
|
@@ -326,15 +335,15 @@ var Elysia = class Elysia {
|
|
|
326
335
|
parsers[name] = fn;
|
|
327
336
|
return this;
|
|
328
337
|
}
|
|
329
|
-
|
|
330
|
-
this.#assertMutable("
|
|
338
|
+
onStart(handler) {
|
|
339
|
+
this.#assertMutable("onStart");
|
|
331
340
|
(this.#ext.setup ??= []).push(...[].concat(handler));
|
|
332
341
|
return this;
|
|
333
342
|
}
|
|
334
|
-
|
|
343
|
+
onTransform(scopeOrFn, fn) {
|
|
335
344
|
return this.#onBranch("transform", scopeOrFn, fn);
|
|
336
345
|
}
|
|
337
|
-
|
|
346
|
+
onBeforeHandle(scopeOrFn, fn) {
|
|
338
347
|
return this.#onBranch("beforeHandle", scopeOrFn, fn);
|
|
339
348
|
}
|
|
340
349
|
derive(scopeOrFn, fn) {
|
|
@@ -351,20 +360,20 @@ var Elysia = class Elysia {
|
|
|
351
360
|
mapDerive(scopeOrFn, fn) {
|
|
352
361
|
return this.#derive(scopeOrFn, fn, mapDeriveEntry);
|
|
353
362
|
}
|
|
354
|
-
|
|
363
|
+
onAfterHandle(scopeOrFn, fn) {
|
|
355
364
|
return this.#onBranch("afterHandle", scopeOrFn, fn);
|
|
356
365
|
}
|
|
357
366
|
mapResponse(scopeOrFn, fn) {
|
|
358
367
|
return this.#onBranch("mapResponse", scopeOrFn, fn);
|
|
359
368
|
}
|
|
360
|
-
|
|
369
|
+
onAfterResponse(scopeOrFn, fn) {
|
|
361
370
|
return this.#onBranch("afterResponse", scopeOrFn, fn);
|
|
362
371
|
}
|
|
363
|
-
|
|
364
|
-
this.#assertMutable("
|
|
372
|
+
onError(scopeOrFnOrError, fnOrError, fn) {
|
|
373
|
+
this.#assertMutable("onError");
|
|
365
374
|
switch (arguments.length) {
|
|
366
375
|
case 1:
|
|
367
|
-
if (scopeOrFnOrError && typeof scopeOrFnOrError === "object" && !Array.isArray(scopeOrFnOrError)) throw new Error("[Elysia] .
|
|
376
|
+
if (scopeOrFnOrError && typeof scopeOrFnOrError === "object" && !Array.isArray(scopeOrFnOrError)) throw new Error("[Elysia] .onError({ CODE: Class }) was removed in 2.0 \u2014 use .onError(Class, handler)");
|
|
368
377
|
return this.#onBranch("error", scopeOrFnOrError);
|
|
369
378
|
case 2:
|
|
370
379
|
if (typeof scopeOrFnOrError === "function" && scopeOrFnOrError.prototype instanceof Object) return this.#onErrorClass(scopeOrFnOrError, fnOrError);
|
|
@@ -382,7 +391,8 @@ var Elysia = class Elysia {
|
|
|
382
391
|
}
|
|
383
392
|
as(target) {
|
|
384
393
|
this.#assertMutable("as");
|
|
385
|
-
|
|
394
|
+
if (target !== "plugin" && target !== "global") throw new Error(`[Elysia] Invalid .as() scope ${JSON.stringify(target)}, expected 'plugin' or 'global' (1.x 'scoped' is 'plugin')`);
|
|
395
|
+
this.#as(this["~hookChain"], target);
|
|
386
396
|
return this;
|
|
387
397
|
}
|
|
388
398
|
#as(node, scope) {
|
|
@@ -1013,13 +1023,14 @@ var Elysia = class Elysia {
|
|
|
1013
1023
|
#assertMutable(api) {
|
|
1014
1024
|
mutationEpoch++;
|
|
1015
1025
|
if (this["~generation"] === void 0) return;
|
|
1016
|
-
|
|
1026
|
+
const on = "on" + api[0].toUpperCase() + api.slice(1);
|
|
1027
|
+
throw new Error(`[Elysia] .${on in this ? on : api}() called after the app was sealed by its first request, listen or compile`);
|
|
1017
1028
|
}
|
|
1018
1029
|
#assertTableMutable(api) {
|
|
1019
1030
|
for (let app = this; app; app = app.scopeParent) app.#assertMutable(api);
|
|
1020
1031
|
}
|
|
1021
1032
|
get #isCacheable() {
|
|
1022
|
-
return this["~generation"] !== void 0 && this.#routerBuilt && !
|
|
1033
|
+
return this["~generation"] !== void 0 && this.#routerBuilt && !inAotBuild();
|
|
1023
1034
|
}
|
|
1024
1035
|
#cacheRoutes(generation) {
|
|
1025
1036
|
if (this.cachedGeneration === generation && this.cachedEpoch === mutationEpoch) return;
|
|
@@ -1605,9 +1616,9 @@ var Elysia = class Elysia {
|
|
|
1605
1616
|
(ext.hocOrigin ??= []).push(this.hash ?? {});
|
|
1606
1617
|
return this;
|
|
1607
1618
|
}
|
|
1608
|
-
|
|
1619
|
+
onStop(handler) {
|
|
1609
1620
|
if (this["~ext"]?.cleanupEpoch?.(handler)) return this;
|
|
1610
|
-
this.#assertMutable("
|
|
1621
|
+
this.#assertMutable("onStop");
|
|
1611
1622
|
(this.#ext.cleanup ??= []).push(...[].concat(handler));
|
|
1612
1623
|
return this;
|
|
1613
1624
|
}
|
|
@@ -13,8 +13,8 @@ const require_type_validator_frozen_check = require('../type/validator/frozen-ch
|
|
|
13
13
|
const require_type_validator_custom_error = require('../type/validator/custom-error.js');
|
|
14
14
|
const require_type_validator_exact_mirror = require('../type/validator/exact-mirror.js');
|
|
15
15
|
const require_type_validator_string_codec_aot = require('../type/validator/string-codec-aot.js');
|
|
16
|
-
let typebox_value = require("typebox/value");
|
|
17
16
|
let typebox_schema = require("typebox/schema");
|
|
17
|
+
let typebox_value = require("typebox/value");
|
|
18
18
|
|
|
19
19
|
//#region src/compile/aot-capture.ts
|
|
20
20
|
function emptyContainers(node, depth) {
|
|
@@ -12,8 +12,8 @@ import { buildFrozenCheck } from "../type/validator/frozen-check.mjs";
|
|
|
12
12
|
import { captureCustomErrors } from "../type/validator/custom-error.mjs";
|
|
13
13
|
import { getExactMirror } from "../type/validator/exact-mirror.mjs";
|
|
14
14
|
import { captureStringCodecEntries, compileWithBuild } from "../type/validator/string-codec-aot.mjs";
|
|
15
|
-
import { Default } from "typebox/value";
|
|
16
15
|
import { Build, Compile } from "typebox/schema";
|
|
16
|
+
import { Default } from "typebox/value";
|
|
17
17
|
|
|
18
18
|
//#region src/compile/aot-capture.ts
|
|
19
19
|
function emptyContainers(node, depth) {
|
package/dist/compile/aot.d.ts
CHANGED
|
@@ -228,6 +228,7 @@ declare function captureSet(loc: {
|
|
|
228
228
|
path: string;
|
|
229
229
|
slot: ValidatorSlot;
|
|
230
230
|
}, partial: Partial<CapturedValidator>): void;
|
|
231
|
+
declare function inAotBuild(): boolean;
|
|
231
232
|
declare const Capture: {
|
|
232
233
|
readonly set: typeof captureSet;
|
|
233
234
|
readonly get: (loc: {
|
|
@@ -236,7 +237,6 @@ declare const Capture: {
|
|
|
236
237
|
slot: ValidatorSlot;
|
|
237
238
|
}) => CapturedValidator | undefined;
|
|
238
239
|
readonly handler: typeof captureHandler;
|
|
239
|
-
readonly isCapturing: () => boolean;
|
|
240
240
|
readonly isAotBuildEnv: () => boolean;
|
|
241
241
|
};
|
|
242
242
|
interface CaptureImpl {
|
|
@@ -282,4 +282,4 @@ interface CaptureImpl {
|
|
|
282
282
|
declare let captureImpl: CaptureImpl | undefined;
|
|
283
283
|
declare function setCaptureImpl(impl: CaptureImpl | undefined): void;
|
|
284
284
|
//#endregion
|
|
285
|
-
export { AOT_MANIFEST_FORMAT, AotFingerprint, Capture, CaptureImpl, CapturedHandler, CapturedMirror, CapturedValidator, CheckBuildResult, Compiled, CompiledProgramRegistration, CompiledSnapshot, CompilerSession, CompilerState, EMPTY_EXTERNALS, FrozenBothFactory, FrozenCheckFactory, FrozenHandler, FrozenMirror, FrozenMirrorFactory, FrozenValidator, HandlerManifest, ProgramId, ReconstructImpl, StringCodecNode, ValidatorManifest, ValidatorSlot, aotActivationError, aotDriftMessage, beginCompilerSession, captureImpl, createAotFingerprint, endCompilerSession, getCompilerSession, reconstruct, setCaptureImpl, warnAotDrift };
|
|
285
|
+
export { AOT_MANIFEST_FORMAT, AotFingerprint, Capture, CaptureImpl, CapturedHandler, CapturedMirror, CapturedValidator, CheckBuildResult, Compiled, CompiledProgramRegistration, CompiledSnapshot, CompilerSession, CompilerState, EMPTY_EXTERNALS, FrozenBothFactory, FrozenCheckFactory, FrozenHandler, FrozenMirror, FrozenMirrorFactory, FrozenValidator, HandlerManifest, ProgramId, ReconstructImpl, StringCodecNode, ValidatorManifest, ValidatorSlot, aotActivationError, aotDriftMessage, beginCompilerSession, captureImpl, createAotFingerprint, endCompilerSession, getCompilerSession, inAotBuild, reconstruct, setCaptureImpl, warnAotDrift };
|
package/dist/compile/aot.js
CHANGED
|
@@ -142,14 +142,14 @@ const CompilerState = {
|
|
|
142
142
|
const EMPTY_EXTERNALS = Object.freeze([]);
|
|
143
143
|
const aotActivationError = () => /* @__PURE__ */ new Error("Elysia AOT capture module is not activated.");
|
|
144
144
|
function captureHandler(v) {
|
|
145
|
-
if (!
|
|
145
|
+
if (!inAotBuild()) return;
|
|
146
146
|
const session = activeSession;
|
|
147
147
|
if (!session) return;
|
|
148
148
|
(session.handlerCapture ??= /* @__PURE__ */ new Map()).set(`${v.method}\0${v.path}`, v);
|
|
149
149
|
}
|
|
150
150
|
function captureSet(loc, partial) {
|
|
151
151
|
const { method, path, slot } = loc;
|
|
152
|
-
if (!
|
|
152
|
+
if (!inAotBuild()) return;
|
|
153
153
|
const capture = activeSession?.capture;
|
|
154
154
|
if (!capture) return;
|
|
155
155
|
const k = `${method}_${path}_${slot}`;
|
|
@@ -163,16 +163,15 @@ function captureSet(loc, partial) {
|
|
|
163
163
|
}
|
|
164
164
|
const captureGet = (loc) => activeSession?.capture?.get(`${loc.method}_${loc.path}_${loc.slot}`);
|
|
165
165
|
const isAotBuildEnv = () => !!require_universal_env.env.ELYSIA_AOT_BUILD;
|
|
166
|
-
|
|
166
|
+
function inAotBuild() {
|
|
167
167
|
if (activeSession?.capture === void 0 && !isAotBuildEnv()) return false;
|
|
168
168
|
if (captureImpl === void 0) throw aotActivationError();
|
|
169
169
|
return true;
|
|
170
|
-
}
|
|
170
|
+
}
|
|
171
171
|
const Capture = {
|
|
172
172
|
set: captureSet,
|
|
173
173
|
get: captureGet,
|
|
174
174
|
handler: captureHandler,
|
|
175
|
-
isCapturing: isValidatorCapturing,
|
|
176
175
|
isAotBuildEnv
|
|
177
176
|
};
|
|
178
177
|
let captureImpl;
|
|
@@ -198,6 +197,7 @@ Object.defineProperty(exports, 'captureImpl', {
|
|
|
198
197
|
exports.createAotFingerprint = createAotFingerprint;
|
|
199
198
|
exports.endCompilerSession = endCompilerSession;
|
|
200
199
|
exports.getCompilerSession = getCompilerSession;
|
|
200
|
+
exports.inAotBuild = inAotBuild;
|
|
201
201
|
exports.reconstruct = reconstruct;
|
|
202
202
|
exports.setCaptureImpl = setCaptureImpl;
|
|
203
203
|
exports.warnAotDrift = warnAotDrift;
|