Comparing version 0.1.2 to 0.2.0-beta.0
@@ -5,3 +5,3 @@ /// <reference types="bun-types" /> | ||
import type { Context } from './context'; | ||
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, MergeIfNotNull, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, ElysiaRoute } from './types'; | ||
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, MergeIfNotNull, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, ElysiaRoute, MaybePromise, IsNever } from './types'; | ||
export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> { | ||
@@ -17,2 +17,3 @@ config: ElysiaConfig; | ||
protected routes: InternalRoute<Instance>[]; | ||
private lazyLoadModules; | ||
constructor(config?: Partial<ElysiaConfig>); | ||
@@ -47,3 +48,5 @@ private _addHandler; | ||
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<NewInstance & Instance> : this; | ||
use<NewElysia extends Elysia<any> = Elysia<any>, Params extends Elysia = Elysia<any>>(plugin: (app: Params extends Elysia<infer ParamsInstance> ? IsAny<ParamsInstance> extends true ? this : Params : Params) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<NewInstance & Instance> : this; | ||
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<{ | ||
default: (elysia: Elysia<any>) => MaybePromise<Elysia<LazyLoadElysia>>; | ||
}>): IsNever<LazyLoadElysia> extends false ? Elysia<LazyLoadElysia & Instance> : NewElysia extends Elysia<infer NewInstance> ? IsNever<NewInstance> extends true ? Elysia<Instance> : Elysia<NewInstance & Instance> : NewElysia extends Promise<Elysia<infer NewInstance>> ? Elysia<NewInstance & Instance> : this; | ||
get<Schema extends TypedSchema = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'GET', Schema, Instance, Path, Response>; | ||
@@ -95,2 +98,7 @@ post<Schema extends TypedSchema = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'POST', Schema, Instance, Path, Response>; | ||
stop: () => Promise<void>; | ||
get modules(): Promise<Elysia<ElysiaInstance<{ | ||
store: Record<typeof SCHEMA, {}>; | ||
request: {}; | ||
schema: {}; | ||
}>>[]>; | ||
} | ||
@@ -102,2 +110,2 @@ export { Elysia }; | ||
export type { Context, PreContext } from './context'; | ||
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 } from './types'; | ||
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, ElysiaRoute, ExtractPath } from './types'; |
@@ -27,2 +27,3 @@ import { Router } from './router'; | ||
this.routes = []; | ||
this.lazyLoadModules = []; | ||
this.stop = async () => { | ||
@@ -37,2 +38,3 @@ if (!this.server) | ||
strictPath: false, | ||
queryLimit: 48, | ||
...config | ||
@@ -177,3 +179,16 @@ }; | ||
use(plugin) { | ||
return plugin(this); | ||
if (plugin instanceof Promise) { | ||
this.lazyLoadModules.push(plugin.then((plugin) => { | ||
if (typeof plugin === 'function') | ||
return plugin(this); | ||
return plugin.default(this); | ||
})); | ||
return this; | ||
} | ||
const instance = plugin(this); | ||
if (instance instanceof Promise) { | ||
this.lazyLoadModules.push(instance); | ||
return this; | ||
} | ||
return instance; | ||
} | ||
@@ -269,2 +284,3 @@ get(path, handler, hook) { | ||
response = await response; | ||
response = mapEarlyResponse(response, set); | ||
if (response) | ||
@@ -367,4 +383,14 @@ return response; | ||
response = await response; | ||
if (handler.validator?.response?.Check(response) === false) | ||
throw createValidationError('response', handler.validator.response, response); | ||
if (handler.validator?.response) | ||
if (response instanceof Response) { | ||
let res; | ||
if (handler.validator.response.schema.type === 'object') | ||
res = await response.clone().json(); | ||
else | ||
res = await response.clone().text(); | ||
if (handler.validator.response.Check(res) === false) | ||
throw createValidationError('response', handler.validator.response, response); | ||
} | ||
else if (handler.validator.response.Check(response) === false) | ||
throw createValidationError('response', handler.validator.response, response); | ||
for (let i = 0; i < hooks.afterHandle.length; i++) { | ||
@@ -436,4 +462,10 @@ let newResponse = hooks.afterHandle[i](context, response); | ||
callback(this.server); | ||
Promise.all(this.lazyLoadModules).then(() => { | ||
Bun.gc(true); | ||
}); | ||
return this; | ||
} | ||
get modules() { | ||
return Promise.all(this.lazyLoadModules); | ||
} | ||
} | ||
@@ -440,0 +472,0 @@ export { Elysia }; |
@@ -0,3 +1,8 @@ | ||
import type { ComposedHandler } from '../src'; | ||
export interface FindResult { | ||
store: Record<string, any>; | ||
store: Partial<{ | ||
[k in string]: ComposedHandler; | ||
}> & Partial<{ | ||
wildcardStore?: Map<number, any> | null; | ||
}>; | ||
params: Record<string, any>; | ||
@@ -4,0 +9,0 @@ } |
@@ -171,3 +171,6 @@ import { getPath } from './utils'; | ||
const params = {}; | ||
params[node.parametricChild.paramName] = url.slice(pathPartEndIndex, urlLength); | ||
let paramData = url.slice(pathPartEndIndex, urlLength); | ||
if (paramData.includes('%')) | ||
paramData = decodeURI(paramData); | ||
params[node.parametricChild.paramName] = paramData; | ||
return { | ||
@@ -182,3 +185,6 @@ store: node.parametricChild.store, | ||
if (route) { | ||
route.params[node.parametricChild.paramName] = url.slice(pathPartEndIndex, slashIndex); | ||
let paramData = url.slice(pathPartEndIndex, slashIndex); | ||
if (paramData.includes('%')) | ||
paramData = decodeURI(paramData); | ||
route.params[node.parametricChild.paramName] = paramData; | ||
return route; | ||
@@ -185,0 +191,0 @@ } |
@@ -22,3 +22,3 @@ /// <reference types="bun-types" /> | ||
} | ||
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => CatchResponse extends Route['response'] ? CatchResponse | Promise<CatchResponse> | Response : Route['response'] | Promise<Route['response']> | Response; | ||
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => Route['response'] extends CatchResponse ? CatchResponse | Promise<CatchResponse> | Response : Route['response'] | Promise<Route['response']> | Response; | ||
export type NoReturnHandler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance> = (context: Context<Route, Instance['store']> & Instance['request']) => void | Promise<void>; | ||
@@ -50,3 +50,3 @@ export type LifeCycleEvent = 'start' | 'request' | 'parse' | 'transform' | 'beforeHandle' | 'afterHandle' | 'error' | 'stop'; | ||
} | ||
export type BeforeRequestHandler<Store extends ElysiaInstance['store'] = ElysiaInstance['store']> = (context: PreContext<Store>) => void | Promise<void> | Response | Promise<Response>; | ||
export type BeforeRequestHandler<Store extends ElysiaInstance['store'] = ElysiaInstance['store']> = (context: PreContext<Store>) => any; | ||
export interface RegisteredHook<Instance extends ElysiaInstance = ElysiaInstance> { | ||
@@ -123,3 +123,3 @@ schema?: TypedSchema; | ||
store: Instance['store'] & Record<typeof SCHEMA, Record<Path, Record<Method, RouteToSchema<Schema, Instance, Path> & { | ||
response: CatchResponse extends RouteToSchema<Schema, Instance, Path>['response'] ? CatchResponse : RouteToSchema<Schema, Instance, Path>['response']; | ||
response: RouteToSchema<Schema, Instance, Path>['response'] extends CatchResponse ? CatchResponse : RouteToSchema<Schema, Instance, Path>['response']; | ||
}>>>; | ||
@@ -150,2 +150,3 @@ schema: Instance['schema']; | ||
strictPath: boolean; | ||
queryLimit: number; | ||
serve?: Partial<Serve>; | ||
@@ -195,2 +196,4 @@ } | ||
export type IsAny<T> = unknown extends T ? [keyof T] extends [never] ? false : true : false; | ||
export type MaybePromise<T> = T | Promise<T>; | ||
export type IsNever<T> = [T] extends [never] ? true : false; | ||
export {}; |
import { TypeCheck } from '@sinclair/typebox/compiler'; | ||
import type { TSchema } from '@sinclair/typebox'; | ||
import type { DeepMergeTwoTypes, LifeCycleStore, LocalHook } from './types'; | ||
import type { DeepMergeTwoTypes, LifeCycleStore, LocalHook, RegisteredHook } from './types'; | ||
export declare const SCHEMA: unique symbol; | ||
export declare const mergeObjectArray: <T>(a: T | T[], b: T | T[]) => T[]; | ||
export declare const mergeHook: (a: LocalHook<any> | LifeCycleStore<any>, b: LocalHook<any>) => LocalHook<any, any>; | ||
export declare const mergeHook: (a: LocalHook<any> | LifeCycleStore<any>, b: LocalHook<any>) => RegisteredHook<any>; | ||
export declare const clone: <T extends Object | any[] = Object | any[]>(value: T) => T; | ||
@@ -8,0 +8,0 @@ export declare const getPath: (url: string, queryIndex?: number) => string; |
@@ -19,3 +19,3 @@ import { TypeCompiler } from '@sinclair/typebox/compiler'; | ||
} | ||
: null, | ||
: undefined, | ||
transform: mergeObjectArray(a.transform ?? [], b?.transform ?? []), | ||
@@ -46,3 +46,3 @@ beforeHandle: mergeObjectArray(a.beforeHandle ?? [], b?.beforeHandle ?? []), | ||
if (sep === -1) { | ||
const equal = paths.indexOf('=', 1); | ||
const equal = paths.indexOf('='); | ||
let value = paths.slice(equal + 1); | ||
@@ -54,3 +54,3 @@ const hashIndex = value.indexOf('#'); | ||
value = decodeURI(value); | ||
query[paths.slice(1, equal)] = value; | ||
query[paths.slice(1, equal)] = decodeURI(value); | ||
break; | ||
@@ -57,0 +57,0 @@ } |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.1.2", | ||
"version": "0.2.0-beta.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63903
1384