UNPKG

elysia-autoload

Version:

Plugin for Elysia which autoload all routes in directory and code-generate types for Eden with Bun.build support

93 lines (89 loc) 3.07 kB
import Elysia, { RouteSchema, AnyElysia, RouteBase, LocalHook, InputSchema, SingletonBase, BaseMacro, Elysia as Elysia$1 } from 'elysia'; type PathToObject<Path extends string, Type extends RouteBase> = Path extends `${infer Head}/${infer Rest}` ? Head extends "" ? PathToObject<Rest, Type> : { [K in Head]: PathToObject<Rest, Type>; } : { [K in Path]: Type; }; declare namespace ElysiaMatch { type RouteEnd = Record<string, RouteSchema>; type Fx = (...args: any[]) => AnyElysia; type All = AnyElysia | Fx; type Extract<T extends All> = T extends Fx ? ReturnType<T> : T; } type FlattenIndexRoutes<T> = T extends object ? { [K in keyof T as K extends "index" ? T[K] extends ElysiaMatch.RouteEnd ? never : K : K]: FlattenIndexRoutes<T[K]>; } & (T extends { index: infer I; } ? I extends ElysiaMatch.RouteEnd ? FlattenIndexRoutes<I> : T : T) : T; type ElysiaWithBaseUrl<BaseUrl extends string, ElysiaType extends ElysiaMatch.All> = ElysiaMatch.Extract<ElysiaType> extends Elysia<infer BasePath, infer Singleton, infer Definitions, infer Metadata, infer Routes, infer Ephemeral, infer Volatile> ? Elysia<BasePath, Singleton, Definitions, Metadata, FlattenIndexRoutes<PathToObject<BaseUrl, Routes>>, Ephemeral, Volatile> : never; type SoftString<T extends string> = T | (string & {}); type SchemaHandler = ({ path, url, }: { path: string; url: string; }) => LocalHook<InputSchema, RouteSchema, SingletonBase, Record<string, Error>, BaseMacro, "">; interface TypesOptions { output?: string | string[]; typeName?: string; useExport?: boolean; } interface AutoloadOptions { pattern?: string; dir?: string; prefix?: string; schema?: SchemaHandler; types?: TypesOptions | true; /** * Throws an error if no matches are found. * @default true */ failGlob?: boolean; /** * import a specific `export` from a file * @example import first export * ```ts * import: (file) => Object.keys(file).at(0) || "default", * ``` * @default "default" */ import?: SoftString<"default"> | ((file: any) => string); /** * Skip imports where needed `export` not defined * @default false */ skipImportErrors?: boolean; /** * Glob pattern(s) to ignore when discovering files */ ignore?: string | string[]; /** * Path to tsconfig.json file for module resolution detection * @default "tsconfig.json" */ tsconfigPath?: string; } declare function autoload(options?: AutoloadOptions): Promise<Elysia$1<string, { decorator: {}; store: {}; derive: {}; resolve: {}; }, { typebox: {}; error: {}; }, { schema: {}; standaloneSchema: {}; macro: {}; macroFn: {}; parser: {}; }, {}, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; }, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; }>>; export { type AutoloadOptions, type ElysiaWithBaseUrl, type SchemaHandler, type SoftString, type TypesOptions, autoload };