Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
fix: return type of mutation$
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Apr 21, 2023
1 parent 8274e09 commit 1fc2455
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-hairs-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prpc/react': patch
---

fix: return type of mutation$
56 changes: 24 additions & 32 deletions packages/react/src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getParams,
} from '@prpc/core'

export function mutation$<
export type ExpectedMutationReturn<
Mw extends IMiddleware[],
Fn extends ExpectedFn<
ZObj extends void | undefined
Expand All @@ -28,9 +28,7 @@ export function mutation$<
Mw
>,
ZObj extends zod.ZodSchema | void | undefined = void | undefined
>(
params: ObjectParams<ZObj, Mw, Fn, true>
): (
> = (
mutationOpts?: UseMutationOptions<
InferReturnType<Fn>,
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>,
Expand All @@ -41,32 +39,36 @@ export function mutation$<
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>,
AsParam<Fn, false>
>
export function mutation$<
Mw extends IMiddleware[],
Fn extends ExpectedFn<
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined,
Mw
>,
ZObj extends zod.ZodSchema | void | undefined = void | undefined
>(
params: ObjectParams<ZObj, Mw, Fn, true>
): ExpectedMutationReturn<Mw, Fn, ZObj>

export function mutation$<
Mw extends IMiddleware[],
Fn extends ExpectedFn<undefined, Mw>
Fn extends ExpectedFn<void | undefined, Mw>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
>(
queryFn: Fn,
key: string,
..._middlewares: Mw
): (
mutationOpts?: UseMutationOptions<
InferReturnType<Fn>,
PRPCClientError<any>,
AsParam<Fn, false>
>
) => UseMutationResult<
InferReturnType<Fn>,
PRPCClientError<any>,
AsParam<Fn, false>
>
>(queryFn: Fn, key: string, ..._middlewares: Mw): ExpectedMutationReturn<Mw, Fn>

export function mutation$<
ZObj extends zod.ZodSchema | undefined,
Mw extends IMiddleware[],
Fn extends ExpectedFn<
ZObj extends zod.ZodSchema ? zod.infer<ZObj> : undefined,
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined,
Mw
>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -75,17 +77,7 @@ export function mutation$<
key: string,
_schema?: ZObj,
..._middlewares: Mw
): (
mutationOpts?: UseMutationOptions<
InferReturnType<Fn>,
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>,
AsParam<Fn, false>
>
) => UseMutationResult<
InferReturnType<Fn>,
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>,
AsParam<Fn, false>
>
): ExpectedMutationReturn<Mw, Fn, ZObj>

export function mutation$<
ZObj extends zod.ZodSchema | undefined,
Expand Down
79 changes: 24 additions & 55 deletions packages/react/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getParams,
} from '@prpc/core'

export function query$<
export type ExpectedQueryReturn<
Mw extends IMiddleware[],
Fn extends ExpectedFn<
ZObj extends void | undefined
Expand All @@ -34,9 +34,7 @@ export function query$<
Mw
>,
ZObj extends zod.ZodSchema | void | undefined = void | undefined
>(
params: ObjectParams<ZObj, Mw, Fn>
): {
> = {
(
input: WithVoid<
ZObj extends void | undefined
Expand Down Expand Up @@ -64,6 +62,18 @@ export function query$<
) => Promise<{ prpcState: DehydratedState }>
callRaw: (input: AsParam<Fn, false>) => Promise<InferReturnType<Fn>>
}
export function query$<
Mw extends IMiddleware[],
Fn extends ExpectedFn<
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined,
Mw
>,
ZObj extends zod.ZodSchema | void | undefined = void | undefined
>(params: ObjectParams<ZObj, Mw, Fn>): ExpectedQueryReturn<Mw, Fn, ZObj>

export function query$<
ZObj extends zod.ZodSchema | void | undefined,
Expand All @@ -73,66 +83,25 @@ export function query$<
queryFn: Fn,
key: string,
..._middlewares: Mw
): {
(
input: WithVoid<
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined
>,
queryOpts?: UseQueryOptions<InferReturnType<Fn>, PRPCClientError<any>>
): UseQueryResult<InferReturnType<Fn>, PRPCClientError<any>>
prefetch: (
queryClient: QueryClient,
input: AsParam<Fn, false>
) => Promise<InferReturnType<Fn>>
fullyDehydrate: (
queryClient: QueryClient,
input: AsParam<Fn, false>,
fn?: (input: AsParam<Fn, false>) => Promise<InferReturnType<Fn>>
) => Promise<{ prpcState: DehydratedState }>
callRaw: (input: AsParam<Fn, false>) => Promise<InferReturnType<Fn>>
}
): ExpectedQueryReturn<Mw, Fn, ZObj>

export function query$<
ZObj extends zod.ZodSchema | void | undefined,
Mw extends IMiddleware[],
Fn extends ExpectedFn<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : void, Mw>
Fn extends ExpectedFn<
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined,
Mw
>
>(
queryFn: Fn,
key: string,
_schema?: ZObj,
..._middlewares: Mw
): {
(
input: WithVoid<
ZObj extends void | undefined
? void | undefined
: ZObj extends zod.ZodSchema
? zod.infer<ZObj>
: void | undefined
>,
queryOpts?: UseQueryOptions<
InferReturnType<Fn>,
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>
>
): UseQueryResult<
InferReturnType<Fn>,
PRPCClientError<ZObj extends zod.ZodSchema ? zod.infer<ZObj> : any>
>
prefetch: (
queryClient: QueryClient,
input: AsParam<Fn, false>
) => Promise<InferReturnType<Fn>>
fullyDehydrate: (
queryClient: QueryClient,
input: AsParam<Fn, false>,
fn?: (input: AsParam<Fn, false>) => Promise<InferReturnType<Fn>>
) => Promise<{ prpcState: DehydratedState }>
callRaw: (input: AsParam<Fn, false>) => Promise<InferReturnType<Fn>>
}
): ExpectedQueryReturn<Mw, Fn, ZObj>

export function query$<
ZObj extends zod.ZodSchema | undefined,
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/reuseable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
type IMiddleware,
type ObjectParams,
} from '@prpc/core'
import { mutation$ } from './mutation'
import { query$ } from './query'
import { type ExpectedMutationReturn, mutation$ } from './mutation'
import { type ExpectedQueryReturn, query$ } from './query'
import type zod from 'zod'

export function reuseable$<Mw extends IMiddleware[]>(...mw: Mw) {
Expand All @@ -26,7 +26,7 @@ export function reuseable$<Mw extends IMiddleware[]>(...mw: Mw) {
>(
params: Omit<ObjectParams<ZObj, Mw, Fn>, 'middlewares'>,
...rest: any[]
) => {
): ExpectedQueryReturn<Mw, Fn, ZObj> => {
const { queryFn, key } =
typeof params === 'object'
? params
Expand All @@ -46,7 +46,7 @@ export function reuseable$<Mw extends IMiddleware[]>(...mw: Mw) {
>(
params: Omit<ObjectParams<ZObj, Mw, Fn, true>, 'middlewares'>,
...rest: any[]
) => {
): ExpectedMutationReturn<Mw, Fn, ZObj> => {
let queryFn
let key
if (typeof params === 'object') {
Expand Down

1 comment on commit 1fc2455

@vercel
Copy link

@vercel vercel bot commented on 1fc2455 Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

prpc-docs – ./

prpc.vercel.app
prpc-docs-orjdev.vercel.app
prpc-docs-git-main-orjdev.vercel.app
prpc-docs.vercel.app

Please sign in to comment.