elysia
Advanced tools
Comparing version 0.2.0-rc.0 to 0.2.0-rc.1
/// <reference types="bun-types" /> | ||
import { Elysia } from '.'; | ||
import type { TypedRoute } from './types'; | ||
type UnwrapFn<T> = T extends (...params: any) => any ? ReturnType<T> : T; | ||
export interface Context<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> { | ||
request: Request; | ||
query: UnwrapFn<Route['query']> extends undefined ? Record<string, unknown> : UnwrapFn<Route['query']>; | ||
params: UnwrapFn<Route['params']>; | ||
body: UnwrapFn<Route['body']>; | ||
query: Route['query'] extends undefined ? Record<string, unknown> : Route['query']; | ||
params: Route['params']; | ||
body: Route['body']; | ||
store: Store; | ||
@@ -18,2 +17,1 @@ set: { | ||
export type PreContext<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> = Omit<Context<Route, Store>, 'query' | 'params' | 'body'>; | ||
export {}; |
@@ -6,3 +6,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, MaybePromise, IsNever, InferSchema } 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'; | ||
import { type TSchema } from '@sinclair/typebox'; | ||
@@ -52,13 +52,13 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> { | ||
}>): 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 InferSchema<Instance> = InferSchema<Instance>, 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>; | ||
post<Schema extends InferSchema<Instance> = InferSchema<Instance>, 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>; | ||
put<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PUT', Schema, Instance, Path, Response>; | ||
patch<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PATCH', Schema, Instance, Path, Response>; | ||
delete<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'DELETE', Schema, Instance, Path, Response>; | ||
options<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'OPTIONS', Schema, Instance, Path, Response>; | ||
all<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'ALL', Schema, Instance, Path, Response>; | ||
head<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'HEAD', Schema, Instance, Path, Response>; | ||
trace<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'TRACE', Schema, Instance, Path, Response>; | ||
connect<Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'CONNECT', Schema, Instance, Path, Response>; | ||
route<Method extends HTTPMethod = HTTPMethod, Schema extends InferSchema<Instance> = InferSchema<Instance>, Path extends string = string, Response = unknown>(method: Method, path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<Method, Schema, Instance, Path, Response>; | ||
get<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, 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>; | ||
post<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, 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>; | ||
put<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PUT', Schema, Instance, Path, Response>; | ||
patch<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PATCH', Schema, Instance, Path, Response>; | ||
delete<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'DELETE', Schema, Instance, Path, Response>; | ||
options<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'OPTIONS', Schema, Instance, Path, Response>; | ||
all<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'ALL', Schema, Instance, Path, Response>; | ||
head<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'HEAD', Schema, Instance, Path, Response>; | ||
trace<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'TRACE', Schema, Instance, Path, Response>; | ||
connect<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'CONNECT', Schema, Instance, Path, Response>; | ||
route<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Method extends HTTPMethod = HTTPMethod, Path extends string = string, Response = unknown>(method: Method, path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<Method, Schema, Instance, Path, Response>; | ||
state<Key extends string | number | symbol = keyof Instance['store'], Value = Instance['store'][keyof Instance['store']], ReturnValue = Value extends () => infer Returned ? Returned extends Promise<infer AsyncReturned> ? AsyncReturned : Returned : Value, NewInstance = Elysia<{ | ||
@@ -90,3 +90,3 @@ store: Instance['store'] & { | ||
}>; | ||
schema<Schema extends InferSchema<Instance> = InferSchema<Instance>, NewInstance = Elysia<{ | ||
schema<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, NewInstance = Elysia<{ | ||
request: Instance['request']; | ||
@@ -101,3 +101,3 @@ store: Instance['store']; | ||
get modules(): Promise<Elysia<ElysiaInstance<{ | ||
store: Record<typeof SCHEMA | typeof DEFS, {}>; | ||
store: Record<any, any> & Record<typeof SCHEMA, {}> & Record<typeof DEFS, {}>; | ||
request: {}; | ||
@@ -104,0 +104,0 @@ schema: {}; |
@@ -5,8 +5,3 @@ import { TSchema } from '@sinclair/typebox'; | ||
export declare const toOpenAPIPath: (path: string) => string; | ||
export declare const mapProperties: (name: string, schema: TSchema | string | undefined, models: Record<string, TSchema>) => { | ||
in: string; | ||
name: string; | ||
type: any; | ||
required: any; | ||
}[]; | ||
export declare const mapProperties: (name: string, schema: TSchema | string | undefined, models: Record<string, TSchema>) => any[]; | ||
export declare const registerSchemaPath: ({ schema, path, method, hook, models }: { | ||
@@ -17,3 +12,3 @@ schema: OpenAPIV2.PathsObject; | ||
hook?: LocalHook<import("./types").TypedSchema<string>, import("./types").ElysiaInstance<{ | ||
store: Record<typeof import("./utils").SCHEMA | typeof import("./utils").DEFS, {}>; | ||
store: Record<any, any> & Record<typeof import("./utils").SCHEMA, {}> & Record<typeof import("./utils").DEFS, {}>; | ||
request: {}; | ||
@@ -20,0 +15,0 @@ schema: {}; |
@@ -15,2 +15,3 @@ import { Kind } from '@sinclair/typebox'; | ||
return Object.entries(schema?.properties ?? []).map(([key, value]) => ({ | ||
...value, | ||
in: name, | ||
@@ -31,5 +32,11 @@ name: key, | ||
if (Kind in responseSchema) { | ||
const { type, properties, required, ...rest } = responseSchema; | ||
responseSchema = { | ||
'200': { | ||
schema: responseSchema | ||
...rest, | ||
schema: { | ||
type, | ||
properties, | ||
required | ||
} | ||
} | ||
@@ -40,4 +47,6 @@ }; | ||
Object.entries(responseSchema).forEach(([key, value]) => { | ||
if (typeof value === 'string') | ||
if (typeof value === 'string') { | ||
const { type, properties, required, ...rest } = models[value]; | ||
responseSchema[key] = { | ||
...rest, | ||
schema: { | ||
@@ -47,8 +56,22 @@ $ref: `#/definitions/${value}` | ||
}; | ||
} | ||
else { | ||
const { type, properties, required, ...rest } = value; | ||
responseSchema[key] = { | ||
...rest, | ||
schema: { | ||
type, | ||
properties, | ||
required | ||
} | ||
}; | ||
} | ||
}); | ||
} | ||
} | ||
else if (typeof responseSchema === 'string') | ||
else if (typeof responseSchema === 'string') { | ||
const { type, properties, required, ...rest } = models[responseSchema]; | ||
responseSchema = { | ||
'200': { | ||
...rest, | ||
schema: { | ||
@@ -59,2 +82,3 @@ $ref: `#/definitions/${responseSchema}` | ||
}; | ||
} | ||
const parameters = [ | ||
@@ -61,0 +85,0 @@ ...mapProperties('header', headerSchema, models), |
@@ -18,3 +18,3 @@ /// <reference types="bun-types" /> | ||
} = { | ||
store: Record<typeof SCHEMA | typeof DEFS, {}>; | ||
store: Record<any, any> & Record<typeof SCHEMA, {}> & Record<typeof DEFS, {}>; | ||
request: {}; | ||
@@ -27,3 +27,3 @@ schema: {}; | ||
} | ||
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => Route['response'] extends (models: Record<string, TSchema>) => TSchema ? ReturnType<Route['response']> extends CatchResponse ? MaybePromise<CatchResponse> | Response : MaybePromise<ReturnType<Route['response']>> | Response : Route['response'] extends CatchResponse ? MaybePromise<CatchResponse> | Response : MaybePromise<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 (models: Record<string, TSchema>) => TSchema ? undefined extends ReturnType<Route['response']> ? MaybePromise<CatchResponse> | Response : MaybePromise<ReturnType<Route['response']>> | Response : undefined extends Route['response'] ? MaybePromise<CatchResponse> | Response : MaybePromise<Route['response']> | Response; | ||
export type NoReturnHandler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance> = (context: Context<Route, Instance['store']> & Instance['request']) => void | Promise<void>; | ||
@@ -70,6 +70,5 @@ export type LifeCycleEvent = 'start' | 'request' | 'parse' | 'transform' | 'beforeHandle' | 'afterHandle' | 'error' | 'stop'; | ||
} | ||
export type InferSchema<Instance extends ElysiaInstance> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>; | ||
export type UnwrapSchema<Schema extends TSchema | undefined | string, Instance extends ElysiaInstance = ElysiaInstance, Fallback = unknown> = Schema extends string ? Instance['store'][typeof DEFS] extends { | ||
[name in Schema]: infer NamedSchema extends TSchema; | ||
} ? UnwrapSchema<NamedSchema> : keyof Instance['store'][typeof DEFS] : NonNullable<Schema> extends TSchema ? Static<NonNullable<Schema>> : Fallback; | ||
} ? Static<NamedSchema> : Fallback : Schema extends TSchema ? Static<NonNullable<Schema>> : Fallback; | ||
export type TypedSchemaToRoute<Schema extends TypedSchema, Instance extends ElysiaInstance> = { | ||
@@ -84,2 +83,9 @@ body: UnwrapSchema<Schema['body'], Instance>; | ||
}; | ||
export type AnyTypedSchema = { | ||
body: unknown; | ||
headers: Record<string, any> | undefined; | ||
query: Record<string, any> | undefined; | ||
params: Record<string, any> | undefined; | ||
response: TSchema | unknown | undefined; | ||
}; | ||
export type SchemaValidator = { | ||
@@ -92,3 +98,3 @@ body?: TypeCheck<any>; | ||
}; | ||
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, Typed extends TypedSchemaToRoute<Schema, Instance> = TypedSchemaToRoute<Schema, Instance>> = Handler<Typed['params'] extends {} ? Omit<Typed, 'response'> & { | ||
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, Typed extends AnyTypedSchema = TypedSchemaToRoute<Schema, Instance>> = Handler<Typed['params'] extends {} ? Omit<Typed, 'response'> & { | ||
response: void | Typed['response']; | ||
@@ -122,9 +128,33 @@ } : Omit<Omit<Typed, 'response'> & { | ||
}; | ||
export type ElysiaRoute<Method extends string = string, Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown, Typed extends RouteToSchema<Schema, Instance, Path> = RouteToSchema<Schema, Instance, Path>> = Elysia<{ | ||
export type ElysiaRoute<Method extends string = string, Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Elysia<{ | ||
request: Instance['request']; | ||
store: Instance['store'] & Record<typeof SCHEMA, Record<Path, Record<Method, Typed & { | ||
response: Typed['response'] extends CatchResponse ? CatchResponse : Typed['response']; | ||
}>>>; | ||
store: Instance['store'] & { | ||
[SCHEMA]: { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance, Path> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? { | ||
'200': CatchResponse; | ||
} : FinalSchema['response']; | ||
} : never; | ||
}; | ||
}; | ||
}; | ||
schema: Instance['schema']; | ||
}>; | ||
export type TypedRouteToEden<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, FinalSchema extends MergeSchema<Schema, Instance['schema']> = MergeSchema<Schema, Instance['schema']>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToEden<FinalSchema, Instance> : Omit<TypedSchemaToEden<FinalSchema, Instance>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string>; | ||
}; | ||
export type TypedSchemaToEden<Schema extends TypedSchema, Instance extends ElysiaInstance> = { | ||
body: UnwrapSchema<Schema['body'], Instance>; | ||
headers: UnwrapSchema<Schema['headers'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: Schema['response'] extends TSchema | string ? { | ||
'200': UnwrapSchema<Schema['response'], Instance>; | ||
} : Schema['response'] extends { | ||
[x in string]: TSchema | string; | ||
} ? { | ||
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance>; | ||
} : unknown; | ||
}; | ||
export type LocalHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Handler<RouteToSchema<Schema, Instance, Path>, Instance, CatchResponse>; | ||
@@ -131,0 +161,0 @@ export interface TypedRoute { |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.2.0-rc.0", | ||
"version": "0.2.0-rc.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
72697
1521