-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from deadlinecode/patch-type-gen
Fixes for type gen
- Loading branch information
Showing
1 changed file
with
54 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,64 @@ | ||
import type Elysia from "elysia"; | ||
import type { RouteBase } from "elysia"; | ||
|
||
type RemoveLastChar<T extends string> = T extends `${infer V}/` ? V : T; | ||
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 }; | ||
|
||
type RoutesWithPrefix<Routes extends RouteBase, Prefix extends string> = { | ||
[K in keyof Routes as `${Prefix}${RemoveLastChar<K & string>}`]: Routes[K]; | ||
}; | ||
type RouteEndType = Record< | ||
string, | ||
{ | ||
body: any; | ||
params: any; | ||
query: any; | ||
headers: any; | ||
response: any; | ||
} | ||
>; | ||
|
||
type FlattenIndexRoutes<T> = T extends object | ||
? { | ||
[K in keyof T as K extends "index" | ||
? T[K] extends RouteEndType | ||
? never | ||
: K | ||
: K]: FlattenIndex<T[K]>; | ||
} & (T extends { index: infer I } | ||
? I extends RouteEndType | ||
? FlattenIndex<I> | ||
: {} | ||
: {}) | ||
: T; | ||
|
||
export type ElysiaWithBaseUrl< | ||
BaseUrl extends string, | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
ElysiaType extends Elysia<any, any, any, any, any, any, any, any>, | ||
BaseUrl extends string, | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
ElysiaType extends Elysia<any, any, any, any, any, any, any, any> | ||
> = ElysiaType extends Elysia< | ||
infer BasePath, | ||
infer Scoped, | ||
infer Singleton, | ||
infer Definitions, | ||
infer Metadata, | ||
infer Routes, | ||
infer Ephemeral, | ||
infer Volatile | ||
infer BasePath, | ||
infer Scoped, | ||
infer Singleton, | ||
infer Definitions, | ||
infer Metadata, | ||
infer Routes, | ||
infer Ephemeral, | ||
infer Volatile | ||
> | ||
? Elysia< | ||
BasePath, | ||
Scoped, | ||
Singleton, | ||
Definitions, | ||
Metadata, | ||
RoutesWithPrefix<Routes, BaseUrl>, | ||
Ephemeral, | ||
Volatile | ||
> | ||
: never; | ||
? Elysia< | ||
BasePath, | ||
Scoped, | ||
Singleton, | ||
Definitions, | ||
Metadata, | ||
FlattenIndexRoutes<PathToObject<BaseUrl, Routes>>, | ||
Ephemeral, | ||
Volatile | ||
> | ||
: never; | ||
|
||
export type SoftString<T extends string> = T | (string & {}); |