Comparing version 0.3.0-beta.2 to 0.3.0-beta.3
@@ -1,1 +0,1 @@ | ||
import{Type as e}from"@sinclair/typebox";import{TypeSystem as t}from"@sinclair/typebox/system";let i=e=>{if("string"==typeof e)switch(e.slice(-1)){case"k":return 1024*+e.slice(0,e.length-1);case"m":return 1048576*+e.slice(0,e.length-1);default:return+e}return e},r=(e,t)=>{if(!(t instanceof Blob)||e.minSize&&t.size<i(e.minSize)||e.maxSize&&t.size>i(e.maxSize))return!1;if(e.extension){if("string"==typeof e.extension){if(!t.type.startsWith(e.extension))return!1}else{for(let i=0;i<e.extension.length;i++)if(t.type.startsWith(e.extension[i]))return!0;return!1}}return!0};export const ElysiaType={File:t.CreateType("File",r),Files:t.CreateType("Files",(e,t)=>{if(!Array.isArray(t)||e.minItems&&t.length<e.minItems||e.maxItems&&t.length>e.maxItems)return!1;for(let i=0;i<t.length;i++)if(!r(e,t[i]))return!1;return!0})};e.File=e=>ElysiaType.File({default:"File",...e,extension:e?.type,type:"string",format:"binary"}),e.Files=e=>ElysiaType.Files({default:"Files",...e,extension:e?.type,type:"string",format:"binary"});export{e as t}; | ||
import{Type as e}from"@sinclair/typebox";import{TypeSystem as t}from"@sinclair/typebox/system";let i=e=>{if("string"==typeof e)switch(e.slice(-1)){case"k":return 1024*+e.slice(0,e.length-1);case"m":return 1048576*+e.slice(0,e.length-1);default:return+e}return e},r=(e,t)=>{if(!(t instanceof Blob)||e.minSize&&t.size<i(e.minSize)||e.maxSize&&t.size>i(e.maxSize))return!1;if(e.extension){if("string"==typeof e.extension){if(!t.type.startsWith(e.extension))return!1}else{for(let i=0;i<e.extension.length;i++)if(t.type.startsWith(e.extension[i]))return!0;return!1}}return!0};export const ElysiaType={File:t.CreateType("File",r),Files:t.CreateType("Files",(e,t)=>{if(!Array.isArray(t)||e.minItems&&t.length<e.minItems||e.maxItems&&t.length>e.maxItems)return!1;for(let i=0;i<t.length;i++)if(!r(e,t[i]))return!1;return!0})};e.File=e=>ElysiaType.File({default:"File",...e,extension:e?.type,type:"string",format:"binary"}),e.Files=e=>ElysiaType.Files({...e,default:"Files",extension:e?.type,type:"array",items:{...e,default:"Files",type:"string",format:"binary"}});export{e as t}; |
@@ -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, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, MaybePromise, IsNever, TypedRouteToEden, MergeUnionObjects } from './types'; | ||
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, MergeUnionObjects, TypedRouteToEden } from './types'; | ||
import { type TSchema } from '@sinclair/typebox'; | ||
@@ -55,3 +55,3 @@ 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 TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
get<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -62,7 +62,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'GET']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'GET']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
post<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
post<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -73,7 +73,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'POST']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'POST']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
put<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
put<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -84,7 +84,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'PUT']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'PUT']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
patch<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
patch<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -95,7 +95,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'PATCH']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'PATCH']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
delete<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
delete<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -106,7 +106,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'DELETE']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'DELETE']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
options<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
options<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -117,7 +117,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'OPTIONS']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'OPTIONS']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
all<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
all<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -128,7 +128,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'ALL']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'ALL']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
head<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
head<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -139,7 +139,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'HEAD']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'HEAD']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
trace<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
trace<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -150,7 +150,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'TRACE']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'TRACE']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
connect<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
connect<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler<Schema, Instance, Path>>(path: Path, handler: Handler, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -161,7 +161,7 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in 'CONNECT']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in 'CONNECT']: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
}>>; | ||
}>; | ||
route<Schema extends TypedSchema<Exclude<keyof Instance['meta'][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>): Elysia<{ | ||
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, hook?: LocalHook<Schema, Instance, Path>): Elysia<{ | ||
request: Instance['request']; | ||
@@ -172,3 +172,3 @@ store: Instance['store']; | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path>; | ||
[method in Method]: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path, ReturnType<Handler>>; | ||
}; | ||
@@ -213,3 +213,3 @@ }>>; | ||
}>; | ||
schema<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, NewInstance = Elysia<{ | ||
schema<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>>, NewInstance = Elysia<{ | ||
request: Instance['request']; | ||
@@ -236,2 +236,2 @@ store: Instance['store']; | ||
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, ElysiaRoute, ExtractPath, IsPathParameter, IsAny, IsNever, UnknownFallback, WithArray, ObjectValues, PickInOrder, MaybePromise, MergeIfNotNull } 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, ExtractPath, IsPathParameter, IsAny, IsNever, UnknownFallback, WithArray, ObjectValues, PickInOrder, MaybePromise, MergeIfNotNull, ElysiaDefaultMeta, TypedRouteToEden, AnyTypedSchema, RouteToSchema, DeepMergeTwoTypes, ConnectedKeysType } from './types'; |
@@ -12,3 +12,3 @@ import { type TSchema } from '@sinclair/typebox'; | ||
hook?: LocalHook<import("./types").TypedSchema<string>, import("./types").ElysiaInstance<{ | ||
store: Record<any, any>; | ||
store: Record<string, unknown>; | ||
request: {}; | ||
@@ -15,0 +15,0 @@ schema: {}; |
@@ -11,11 +11,12 @@ /// <reference types="bun-types" /> | ||
export type ObjectValues<T extends object> = T[keyof T]; | ||
export type ElysiaDefaultMeta = Record<typeof SCHEMA, Partial<OpenAPIV3.PathsObject>> & Record<typeof DEFS, { | ||
[x in string]: TSchema; | ||
}> & Record<typeof EXPOSED, Record<string, Record<string, unknown>>>; | ||
export type ElysiaInstance<Instance extends { | ||
store?: Record<any, any>; | ||
request?: Record<any, any>; | ||
store?: Record<string, unknown>; | ||
request?: Record<string, unknown>; | ||
schema?: TypedSchema; | ||
meta?: Record<typeof SCHEMA, Partial<OpenAPIV3.PathsObject>> & Record<typeof DEFS, { | ||
[x in string]: TSchema; | ||
}> & Record<typeof EXPOSED, Record<string, Record<string, unknown>>>; | ||
meta?: ElysiaDefaultMeta; | ||
} = { | ||
store: Record<any, any>; | ||
store: Record<string, unknown>; | ||
request: {}; | ||
@@ -30,3 +31,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 ? 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 Handler<Route extends OverwritableTypeRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = unknown> = (context: Context<Route, Instance['store']> & Instance['request']) => IsUnknown<Route['response']> extends false ? Response | MaybePromise<Route['response']> : Response | MaybePromise<CatchResponse>; | ||
export type NoReturnHandler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance> = (context: Context<Route, Instance['store']> & Instance['request']) => void | Promise<void>; | ||
@@ -129,49 +130,17 @@ export type LifeCycleEvent = 'start' | 'request' | 'parse' | 'transform' | 'beforeHandle' | 'afterHandle' | 'error' | 'stop'; | ||
} | ||
export type RouteToSchema<Schema extends TypedSchema, InstanceSchema extends ElysiaInstance['schema'], Definitions extends ElysiaInstance['meta'][typeof DEFS], Path extends string = string, FinalSchema extends MergeSchema<Schema, InstanceSchema> = MergeSchema<Schema, InstanceSchema>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToRoute<FinalSchema, Definitions> : Omit<TypedSchemaToRoute<FinalSchema, Definitions>, 'params'> & { | ||
export type RouteToSchema<Schema extends TypedSchema, InstanceSchema extends ElysiaInstance['schema'], Definitions extends ElysiaInstance['meta'][typeof DEFS], Path extends string = string> = MergeSchema<Schema, InstanceSchema> extends infer Typed extends TypedSchema ? undefined extends Typed['params'] ? Omit<TypedSchemaToRoute<Typed, Definitions>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string>; | ||
}; | ||
} : TypedSchemaToRoute<Typed, Definitions> : never; | ||
export type MergeUnionObjects<T> = {} & { | ||
[P in keyof T]: T[P]; | ||
}; | ||
export type ElysiaRoute<Method extends string = string, Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Elysia<{ | ||
request: Instance['request']; | ||
store: Instance['store']; | ||
schema: Instance['schema']; | ||
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & Record<typeof SCHEMA, MergeUnionObjects<Instance['meta'][typeof SCHEMA] & { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance['meta'][typeof DEFS], Path> extends { | ||
body: infer Body extends AnyTypedSchema['body']; | ||
headers: infer Headers extends AnyTypedSchema['headers']; | ||
query: infer Query extends AnyTypedSchema['query']; | ||
params: infer Params extends AnyTypedSchema['params']; | ||
response: infer Response extends AnyTypedSchema['response']; | ||
} ? { | ||
body: Body; | ||
headers: Headers; | ||
query: Query; | ||
params: Params extends NonNullable<Params> ? Params : Record<ExtractPath<Path>, string>; | ||
response: undefined extends Response ? { | ||
'200': CatchResponse; | ||
} : Response; | ||
} : never; | ||
}; | ||
}>>; | ||
}>; | ||
export type TypedRouteToEden<Schema extends TypedSchema = TypedSchema, Definitions extends TypedSchema<string> = ElysiaInstance['meta'][typeof DEFS], Path extends string = string, Typed extends AnyTypedSchema = TypedSchemaToEden<Schema, Definitions>> = Schema['params'] extends NonNullable<Schema['params']> ? Typed extends AnyTypedSchema ? { | ||
export type TypedRouteToEden<Schema extends TypedSchema = TypedSchema, Definitions extends TypedSchema<string> = ElysiaInstance['meta'][typeof DEFS], Path extends string = string, Catch = unknown> = TypedSchemaToEden<Schema, Definitions> extends infer Typed extends AnyTypedSchema ? { | ||
body: Typed['body']; | ||
headers: Typed['headers']; | ||
query: Typed['query']; | ||
params: Typed['params'] extends NonNullable<Typed['params']> ? Typed['params'] : Record<ExtractPath<Path>, string>; | ||
response: undefined extends Response ? { | ||
'200': Response; | ||
} : Response; | ||
} : AnyTypedSchema : Typed extends AnyTypedSchema ? { | ||
body: Typed['body']; | ||
headers: Typed['headers']; | ||
query: Typed['query']; | ||
params: never; | ||
response: undefined extends Response ? { | ||
'200': Response; | ||
} : Response; | ||
} : AnyTypedSchema; | ||
params: IsUnknown<Typed['params']> extends true ? Record<ExtractPath<Path>, string> : Typed['params']; | ||
response: undefined extends Typed['response'] ? { | ||
'200': Catch; | ||
} : Typed['response']; | ||
} : never; | ||
export type TypedSchemaToEden<Schema extends TypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS]> = { | ||
@@ -223,3 +192,3 @@ body: UnwrapSchema<Schema['body'], Definitions>; | ||
export type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE' | 'ALL'; | ||
export type ErrorCode = 'NOT_FOUND' | 'INTERNAL_SERVER_ERROR' | 'VALIDATION' | 'UNKNOWN'; | ||
export type ErrorCode = (string & {}) | 'NOT_FOUND' | 'INTERNAL_SERVER_ERROR' | 'VALIDATION' | 'UNKNOWN'; | ||
export type ErrorHandler = (params: { | ||
@@ -257,5 +226,6 @@ request: Request; | ||
}] ? MergeTwoObjects<T, U> : T | U; | ||
export type IsAny<T> = unknown extends T ? [keyof T] extends [never] ? false : true : false; | ||
export type IsAny<T> = 0 extends 1 & T ? true : false; | ||
export type IsNever<T> = [T] extends [never] ? true : false; | ||
export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false; | ||
export type MaybePromise<T> = T | Promise<T>; | ||
export type IsNever<T> = [T] extends [never] ? true : false; | ||
export type FunctionProperties<T> = { | ||
@@ -262,0 +232,0 @@ [K in keyof T as T[K] extends Record<any, any> | ((...args: any[]) => any) ? IsAny<T[K]> extends true ? never : K : never]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends Record<string, any> ? FunctionProperties<T[K]> : never; |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.3.0-beta.2", | ||
"version": "0.3.0-beta.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
@@ -141,10 +141,14 @@ import { Type, type SchemaOptions } from '@sinclair/typebox' | ||
ElysiaType.Files({ | ||
...arg, | ||
default: 'Files', | ||
...arg, | ||
extension: arg?.type, | ||
// TODO: Migrate this to array of binary string | ||
type: 'string', | ||
format: 'binary' | ||
type: 'array', | ||
items: { | ||
...arg, | ||
default: 'Files', | ||
type: 'string', | ||
format: 'binary' | ||
} | ||
}) | ||
export { Type as t } |
156
src/index.ts
@@ -55,6 +55,4 @@ import type { Serve, Server } from 'bun' | ||
IsNever, | ||
AnyTypedSchema, | ||
TypedRouteToEden, | ||
MergeUnionObjects, | ||
ExtractPath | ||
TypedRouteToEden | ||
} from './types' | ||
@@ -685,6 +683,10 @@ import { type TSchema } from '@sinclair/typebox' | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -705,3 +707,4 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
@@ -741,6 +744,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -761,3 +768,4 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
@@ -797,6 +805,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -817,4 +829,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -853,6 +866,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -873,4 +890,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -909,6 +927,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -929,4 +951,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -965,6 +988,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -985,4 +1012,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1016,6 +1044,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -1036,4 +1068,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1072,6 +1105,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -1092,4 +1129,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1128,6 +1166,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -1148,4 +1190,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1184,6 +1227,10 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -1204,4 +1251,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1241,7 +1289,11 @@ } | ||
Path extends string = string, | ||
Response = unknown | ||
Handler extends LocalHandler<Schema, Instance, Path> = LocalHandler< | ||
Schema, | ||
Instance, | ||
Path | ||
> | ||
>( | ||
method: Method, | ||
path: Path, | ||
handler: LocalHandler<Schema, Instance, Path, Response>, | ||
handler: Handler, | ||
hook?: LocalHook<Schema, Instance, Path> | ||
@@ -1262,4 +1314,5 @@ ): Elysia<{ | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> | ||
Path, | ||
ReturnType<Handler> | ||
> | ||
} | ||
@@ -1429,5 +1482,5 @@ } | ||
Schema extends TypedSchema< | ||
Exclude<keyof Instance['store'][typeof DEFS], number | symbol> | ||
Exclude<keyof Instance['meta'][typeof DEFS], number | symbol> | ||
> = TypedSchema< | ||
Exclude<keyof Instance['store'][typeof DEFS], number | symbol> | ||
Exclude<keyof Instance['meta'][typeof DEFS], number | symbol> | ||
>, | ||
@@ -1853,3 +1906,2 @@ NewInstance = Elysia<{ | ||
SchemaValidator, | ||
ElysiaRoute, | ||
ExtractPath, | ||
@@ -1864,3 +1916,9 @@ IsPathParameter, | ||
MaybePromise, | ||
MergeIfNotNull | ||
MergeIfNotNull, | ||
ElysiaDefaultMeta, | ||
TypedRouteToEden, | ||
AnyTypedSchema, | ||
RouteToSchema, | ||
DeepMergeTwoTypes, | ||
ConnectedKeysType | ||
} from './types' |
203
src/types.ts
@@ -13,12 +13,17 @@ import type { Elysia } from '.' | ||
export type ElysiaDefaultMeta = Record< | ||
typeof SCHEMA, | ||
Partial<OpenAPIV3.PathsObject> | ||
> & | ||
Record<typeof DEFS, { [x in string]: TSchema }> & | ||
Record<typeof EXPOSED, Record<string, Record<string, unknown>>> | ||
export type ElysiaInstance< | ||
Instance extends { | ||
store?: Record<any, any> | ||
request?: Record<any, any> | ||
store?: Record<string, unknown> | ||
request?: Record<string, unknown> | ||
schema?: TypedSchema | ||
meta?: Record<typeof SCHEMA, Partial<OpenAPIV3.PathsObject>> & | ||
Record<typeof DEFS, { [x in string]: TSchema }> & | ||
Record<typeof EXPOSED, Record<string, Record<string, unknown>>> | ||
meta?: ElysiaDefaultMeta | ||
} = { | ||
store: Record<any, any> | ||
store: Record<string, unknown> | ||
request: {} | ||
@@ -40,17 +45,15 @@ schema: {} | ||
export type Handler< | ||
Route extends TypedRoute = TypedRoute, | ||
Route extends OverwritableTypeRoute, | ||
Instance extends ElysiaInstance = ElysiaInstance, | ||
CatchResponse = Route['response'] | ||
CatchResponse = unknown | ||
> = ( | ||
context: Context<Route, Instance['store']> & Instance['request'] | ||
) => // Catch function | ||
Route['response'] extends (models: Record<string, TSchema>) => TSchema | ||
? undefined extends ReturnType<Route['response']> | ||
? MaybePromise<CatchResponse> | Response | ||
: MaybePromise<ReturnType<Route['response']>> | Response | ||
: // Catch non-function | ||
undefined extends Route['response'] | ||
? MaybePromise<CatchResponse> | Response | ||
: MaybePromise<Route['response']> | Response | ||
) => IsUnknown<Route['response']> extends false | ||
? Response | MaybePromise<Route['response']> | ||
: Response | MaybePromise<CatchResponse> | ||
// undefined extends Route['response'] | ||
// ? MaybePromise<CatchResponse> | Response | ||
// : MaybePromise<Route['response']> | Response | ||
export type NoReturnHandler< | ||
@@ -285,105 +288,13 @@ Route extends TypedRoute = TypedRoute, | ||
Definitions extends ElysiaInstance['meta'][typeof DEFS], | ||
Path extends string = string, | ||
FinalSchema extends MergeSchema<Schema, InstanceSchema> = MergeSchema< | ||
Schema, | ||
InstanceSchema | ||
> | ||
> = FinalSchema['params'] extends NonNullable<Schema['params']> | ||
? TypedSchemaToRoute<FinalSchema, Definitions> | ||
: Omit<TypedSchemaToRoute<FinalSchema, Definitions>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string> | ||
} | ||
Path extends string = string | ||
> = MergeSchema<Schema, InstanceSchema> extends infer Typed extends TypedSchema | ||
? undefined extends Typed['params'] | ||
? Omit<TypedSchemaToRoute<Typed, Definitions>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string> | ||
} | ||
: TypedSchemaToRoute<Typed, Definitions> | ||
: never | ||
export type MergeUnionObjects<T> = {} & { [P in keyof T]: T[P] } | ||
// export type ElysiaRouteSchema< | ||
// Method extends string, | ||
// Path extends string, | ||
// CatchResponse, | ||
// Schema extends TypedSchema<any>, | ||
// Parent extends ElysiaInstance<any>['meta'][typeof SCHEMA], | ||
// Definitions extends ElysiaInstance['meta'][typeof DEFS] | ||
// > = Record< | ||
// typeof SCHEMA, | ||
// MergeUnionObjects< | ||
// Parent & { | ||
// [path in Path]: { | ||
// [method in Method]: TypedRouteToEden< | ||
// Schema, | ||
// Definitions, | ||
// Path | ||
// > extends { | ||
// body: infer Body extends AnyTypedSchema['body'] | ||
// headers: infer Headers extends AnyTypedSchema['headers'] | ||
// query: infer Query extends AnyTypedSchema['query'] | ||
// params: infer Params extends AnyTypedSchema['params'] | ||
// response: infer Response extends AnyTypedSchema['response'] | ||
// } | ||
// ? { | ||
// body: Body | ||
// headers: Headers | ||
// query: Query | ||
// params: Params extends NonNullable<Params> | ||
// ? Params | ||
// : Record<ExtractPath<Path>, string> | ||
// response: undefined extends Response | ||
// ? { | ||
// '200': CatchResponse | ||
// } | ||
// : Response | ||
// } | ||
// : never | ||
// } | ||
// } | ||
// > | ||
// > | ||
export type ElysiaRoute< | ||
Method extends string = string, | ||
Schema extends TypedSchema = TypedSchema, | ||
Instance extends ElysiaInstance<any> = ElysiaInstance, | ||
Path extends string = string, | ||
CatchResponse = unknown | ||
> = Elysia<{ | ||
request: Instance['request'] | ||
store: Instance['store'] | ||
schema: Instance['schema'] | ||
meta: Record<typeof DEFS, Instance['meta'][typeof DEFS]> & | ||
Record<typeof EXPOSED, Instance['meta'][typeof EXPOSED]> & | ||
Record< | ||
typeof SCHEMA, | ||
MergeUnionObjects< | ||
Instance['meta'][typeof SCHEMA] & { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden< | ||
Schema, | ||
Instance['meta'][typeof DEFS], | ||
Path | ||
> extends { | ||
body: infer Body extends AnyTypedSchema['body'] | ||
headers: infer Headers extends AnyTypedSchema['headers'] | ||
query: infer Query extends AnyTypedSchema['query'] | ||
params: infer Params extends AnyTypedSchema['params'] | ||
response: infer Response extends AnyTypedSchema['response'] | ||
} | ||
? { | ||
body: Body | ||
headers: Headers | ||
query: Query | ||
params: Params extends NonNullable<Params> | ||
? Params | ||
: Record<ExtractPath<Path>, string> | ||
response: undefined extends Response | ||
? { | ||
'200': CatchResponse | ||
} | ||
: Response | ||
} | ||
: never | ||
} | ||
} | ||
> | ||
> | ||
}> | ||
export type TypedRouteToEden< | ||
@@ -393,20 +304,7 @@ Schema extends TypedSchema = TypedSchema, | ||
Path extends string = string, | ||
Typed extends AnyTypedSchema = TypedSchemaToEden<Schema, Definitions> | ||
> = Schema['params'] extends NonNullable<Schema['params']> | ||
? Typed extends AnyTypedSchema | ||
? { | ||
body: Typed['body'] | ||
headers: Typed['headers'] | ||
query: Typed['query'] | ||
params: Typed['params'] extends NonNullable<Typed['params']> | ||
? Typed['params'] | ||
: Record<ExtractPath<Path>, string> | ||
response: undefined extends Response | ||
? { | ||
'200': Response | ||
} | ||
: Response | ||
} | ||
: AnyTypedSchema | ||
: Typed extends AnyTypedSchema | ||
Catch = unknown | ||
> = TypedSchemaToEden< | ||
Schema, | ||
Definitions | ||
> extends infer Typed extends AnyTypedSchema | ||
? { | ||
@@ -416,10 +314,12 @@ body: Typed['body'] | ||
query: Typed['query'] | ||
params: never | ||
response: undefined extends Response | ||
params: IsUnknown<Typed['params']> extends true | ||
? Record<ExtractPath<Path>, string> | ||
: Typed['params'] | ||
response: undefined extends Typed['response'] | ||
? { | ||
'200': Response | ||
'200': Catch | ||
} | ||
: Response | ||
: Typed['response'] | ||
} | ||
: AnyTypedSchema | ||
: never | ||
@@ -561,2 +461,3 @@ export type TypedSchemaToEden< | ||
export type ErrorCode = | ||
| (string & {}) | ||
// ? Default 404 | ||
@@ -630,10 +531,22 @@ | 'NOT_FOUND' | ||
export type IsAny<T> = unknown extends T | ||
? [keyof T] extends [never] | ||
? false | ||
: true | ||
/** | ||
* @link https://stackoverflow.com/a/49928360/1490091 | ||
*/ | ||
export type IsAny<T> = 0 extends 1 & T ? true : false | ||
/** | ||
* Returns a boolean for whether the the type is `never`. | ||
*/ | ||
export type IsNever<T> = [T] extends [never] ? true : false | ||
/** | ||
* Returns a boolean for whether the the type is `unknown`. | ||
*/ | ||
export type IsUnknown<T> = IsAny<T> extends true | ||
? false | ||
: unknown extends T | ||
? true | ||
: false | ||
export type MaybePromise<T> = T | Promise<T> | ||
export type IsNever<T> = [T] extends [never] ? true : false | ||
@@ -640,0 +553,0 @@ export type FunctionProperties<T> = { |
@@ -44,3 +44,3 @@ import { Kind, TSchema, Type } from '@sinclair/typebox' | ||
: undefined, | ||
transform: mergeObjectArray(a.transform ?? [], b?.transform ?? []), | ||
transform: mergeObjectArray(a.transform ?? [], b?.transform ?? []) as any, | ||
beforeHandle: mergeObjectArray( | ||
@@ -47,0 +47,0 @@ a.beforeHandle ?? [], |
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
144596
3857