Skip to content

Commit b10ce3b

Browse files
authored
refactor: Enhance API type definitions for route extraction (#36388)
1 parent 9adebfa commit b10ce3b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

apps/meteor/app/api/server/ApiClass.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ export type Prettify<T> = {
6666

6767
type ExtractValidation<T> = T extends ValidateFunction<infer TSchema> ? TSchema : never;
6868

69-
export type ExtractRoutesFromAPI<T> =
70-
T extends APIClass<any, infer TOperations> ? (TOperations extends MinimalRoute ? Prettify<ConvertToRoute<TOperations>> : never) : never;
69+
type UnionToIntersection<U> = (U extends any ? (x: U) => any : never) extends (x: infer I) => any ? I : never;
70+
71+
export type ExtractRoutesFromAPI<T> = Prettify<
72+
UnionToIntersection<
73+
T extends APIClass<any, infer TOperations> ? (TOperations extends MinimalRoute ? Prettify<ConvertToRoute<TOperations>> : never) : never
74+
>
75+
>;
7176

7277
type ConvertToRoute<TRoute extends MinimalRoute> = {
7378
[K in TRoute['path']]: {
@@ -578,7 +583,7 @@ export class APIClass<
578583
TSubPathPattern extends string,
579584
TOptions extends TypedOptions,
580585
TPathPattern extends `${TBasePath}/${TSubPathPattern}`,
581-
>(method: Method, subpath: TSubPathPattern, options: TOptions): void {
586+
>(method: MinimalRoute['method'], subpath: TSubPathPattern, options: TOptions): void {
582587
const path = `/${this.apiPath}/${subpath}`.replaceAll('//', '/') as TPathPattern;
583588
this.typedRoutes = this.typedRoutes || {};
584589
this.typedRoutes[path] = this.typedRoutes[subpath] || {};
@@ -629,7 +634,7 @@ export class APIClass<
629634
}
630635

631636
private method<TSubPathPattern extends string, TOptions extends TypedOptions, TPathPattern extends `${TBasePath}/${TSubPathPattern}`>(
632-
method: Method,
637+
method: MinimalRoute['method'],
633638
subpath: TSubPathPattern,
634639
options: TOptions,
635640
action: TypedAction<TOptions, TSubPathPattern>,

apps/meteor/app/api/server/definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type NonEnterpriseTwoFactorOptions = {
8888
twoFactorOptions: ITwoFactorOptions;
8989
};
9090

91-
export type Options = SharedOptions<Method>;
91+
export type Options = SharedOptions<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'>;
9292

9393
export type SharedOptions<TMethod extends string> = (
9494
| {

0 commit comments

Comments
 (0)