Skip to content

Commit

Permalink
Merge pull request #17 from deadlinecode/patch-type-gen
Browse files Browse the repository at this point in the history
Fixes for type gen
  • Loading branch information
kravetsone authored Aug 17, 2024
2 parents d38b124 + 3b45341 commit 880e051
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions src/types.ts
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]>;

Check failure on line 30 in src/types.ts

View workflow job for this annotation

GitHub Actions / publish_package

Cannot find name 'FlattenIndex'.
} & (T extends { index: infer I }
? I extends RouteEndType
? FlattenIndex<I>

Check failure on line 33 in src/types.ts

View workflow job for this annotation

GitHub Actions / publish_package

Cannot find name 'FlattenIndex'.
: {}
: {})
: 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 & {});

0 comments on commit 880e051

Please sign in to comment.