From 72de87f651e8040da4012b52bc8f13336fed02c0 Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Tue, 5 Nov 2024 00:01:06 +0300 Subject: [PATCH] fix: throw should be inferred properly --- .../better-auth/src/client/path-to-object.ts | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/packages/better-auth/src/client/path-to-object.ts b/packages/better-auth/src/client/path-to-object.ts index 5fdfcf1037..7b8adc3dab 100644 --- a/packages/better-auth/src/client/path-to-object.ts +++ b/packages/better-auth/src/client/path-to-object.ts @@ -30,41 +30,49 @@ export type PathToObject< ? { [K in CamelCase]: Fn } : never; -type InferSignUpEmailCtx = { +type InferSignUpEmailCtx< + ClientOpts extends ClientOptions, + FetchOptions extends BetterFetchOption, +> = { email: string; name: string; password: string; image?: string; callbackURL?: string; - fetchOptions?: BetterFetchOption; + fetchOptions?: FetchOptions; } & UnionToIntersection>; -type InferUserUpdateCtx = { +type InferUserUpdateCtx< + ClientOpts extends ClientOptions, + FetchOptions extends BetterFetchOption, +> = { image?: string; name?: string; - fetchOptions?: BetterFetchOption; + fetchOptions?: FetchOptions; } & Partial< UnionToIntersection> >; -type InferCtx> = C["body"] extends Record< - string, - any -> +type InferCtx< + C extends Context, + FetchOptions extends BetterFetchOption, +> = C["body"] extends Record ? C["body"] & { fetchOptions?: BetterFetchOption; } : C["query"] extends Record ? { query: C["query"]; - fetchOptions?: Omit< - BetterFetchOption, - "query" - >; + fetchOptions?: FetchOptions; } - : { - fetchOptions?: BetterFetchOption; - }; + : C["query"] extends Record | undefined + ? { + query?: C["query"]; + fetchOptions?: FetchOptions; + } + : { + fetchOptions?: FetchOptions; + }; type MergeRoutes = UnionToIntersection; @@ -102,26 +110,38 @@ export type InferRoute = API extends { T["path"], T extends (ctx: infer C) => infer R ? C extends Context - ? ( - ...data: HasRequiredKeys> extends true + ? < + FetchOptions extends BetterFetchOption< + C["body"], + C["query"], + C["params"] + >, + >( + ...data: HasRequiredKeys< + InferCtx + > extends true ? [ Prettify< T["path"] extends `/sign-up/email` - ? InferSignUpEmailCtx - : InferCtx + ? InferSignUpEmailCtx + : InferCtx >, - BetterFetchOption?, + FetchOptions?, ] : [ Prettify< T["path"] extends `/update-user` - ? InferUserUpdateCtx - : InferCtx + ? InferUserUpdateCtx + : InferCtx >?, - BetterFetchOption?, + FetchOptions?, ] ) => Promise< - BetterFetchResponse, COpts>> + BetterFetchResponse< + InferReturn, COpts>, + unknown, + FetchOptions["throw"] extends true ? true : false + > > : never : never