Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invoke type regression #4110

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-boxes-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---

Fix return type of the `invoke` method from returning type function to return the type of resolved data
12 changes: 6 additions & 6 deletions packages/blitz-rpc/src/client/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {RpcClient} from "./rpc"
export async function invoke<T extends (...args: any) => any, TInput = FirstParam<T>>(
queryFn: T,
params: TInput,
): Promise<T>
): Promise<PromiseReturnType<T>>
export async function invoke<T extends (...args: any) => any, TInput = FirstParam<T>>(
queryFn: T,
params: TInput,
isServer: boolean,
): Promise<T>
): Promise<PromiseReturnType<T>>
export async function invoke<T extends (...args: any) => any, TInput = FirstParam<T>>(
queryFn: T,
params: TInput,
isServer = typeof window === "undefined" ? true : false,
): Promise<T> {
): Promise<PromiseReturnType<T>> {
if (typeof queryFn === "undefined") {
throw new Error(
"invoke is missing the first argument - it must be a query or mutation function",
Expand All @@ -28,15 +28,15 @@ export async function invoke<T extends (...args: any) => any, TInput = FirstPara
)
})
const ctx = await getBlitzContext()
return queryFn(params, ctx)
return queryFn(params, ctx) as PromiseReturnType<T>
}

if (isClient) {
const fn = queryFn as unknown as RpcClient
return fn(params, {fromInvoke: true}) as ReturnType<T>
return fn(params, {fromInvoke: true}) as PromiseReturnType<T>
} else {
const fn = queryFn as unknown as RpcClient
return fn(params) as ReturnType<T>
return fn(params) as PromiseReturnType<T>
}
}

Expand Down