-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from wobsoriano/mutations
- Loading branch information
Showing
10 changed files
with
158 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { type inferRouterProxyClient } from '@trpc/client' | ||
import { type AnyRouter } from '@trpc/server' | ||
import { createRecursiveProxy } from '@trpc/server/shared' | ||
// @ts-expect-error: Nuxt auto-imports | ||
import { getCurrentInstance, onScopeDispose, useAsyncData, unref, isRef } from '#imports' | ||
import { getQueryKeyInternal } from './getQueryKey' | ||
|
||
export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: string, client: inferRouterProxyClient<TRouter>) { | ||
return createRecursiveProxy((opts) => { | ||
const args = opts.args | ||
|
||
const pathCopy = [name, ...opts.path] | ||
|
||
// The last arg is for instance `.useMutation` or `.useQuery()` | ||
const lastArg = pathCopy.pop()! | ||
|
||
// The `path` ends up being something like `post.byId` | ||
const path = pathCopy.join('.') | ||
|
||
const [input, otherOptions] = args | ||
|
||
if (lastArg === '_def') { | ||
return { | ||
path: pathCopy, | ||
}; | ||
} | ||
|
||
if (['useQuery', 'useLazyQuery'].includes(lastArg)) { | ||
const { trpc, queryKey: customQueryKey, ...asyncDataOptions } = otherOptions || {} as any | ||
|
||
let controller: AbortController | ||
|
||
if (trpc?.abortOnUnmount) { | ||
if (getCurrentInstance()) { | ||
onScopeDispose(() => { | ||
controller?.abort?.() | ||
}) | ||
} | ||
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController | ||
} | ||
|
||
const queryKey = customQueryKey || getQueryKeyInternal(path, unref(input)) | ||
const watch = isRef(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch | ||
const isLazy = lastArg === 'useLazyQuery' ? true : (asyncDataOptions.lazy || false) | ||
|
||
return useAsyncData(queryKey, () => (client as any)[path].query(unref(input), { | ||
signal: controller?.signal, | ||
...trpc | ||
}), { | ||
...asyncDataOptions, | ||
watch, | ||
lazy: isLazy | ||
}) | ||
} | ||
|
||
return (client as any)[path][lastArg](...args) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
AnyQueryProcedure, | ||
AnyRouter, | ||
DeepPartial, | ||
inferProcedureInput, | ||
} from '@trpc/server'; | ||
import { hash } from 'ohash' | ||
import { DecorateProcedure } from './types'; | ||
|
||
export type GetQueryParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
TProcedureInput = inferProcedureInput<TProcedureOrRouter>, | ||
> = DeepPartial<TProcedureInput>; | ||
|
||
type GetParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
> = [ | ||
procedureOrRouter: DecorateProcedure<TProcedureOrRouter, AnyRouter> | string, | ||
params: GetQueryParams<TProcedureOrRouter>, | ||
]; | ||
|
||
type GetQueryKeyParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
> = GetParams<TProcedureOrRouter>; | ||
|
||
/** | ||
* Method to extract the query key for a procedure | ||
* @param procedure - procedure | ||
* @param input - input to procedure | ||
* @link https://trpc-nuxt.vercel.app/get-started/tips/mutation | ||
*/ | ||
export function getQueryKey< | ||
TProcedure extends AnyQueryProcedure, | ||
>(..._params: GetQueryKeyParams<TProcedure>): string { | ||
const [procedure, input] = _params; | ||
|
||
if (typeof procedure === 'string') { | ||
// TODO: Warn here if string is passed that it will be deprecated in the future. | ||
return getQueryKeyInternal(procedure, input); | ||
} | ||
|
||
// @ts-expect-error: we don't expose _def on the type layer | ||
const path = procedure._def().path as string[]; | ||
const dotPath = path.join('.'); | ||
|
||
return getQueryKeyInternal(dotPath, input) | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function getQueryKeyInternal ( | ||
path: string, | ||
input: unknown | ||
): string { | ||
return input === undefined ? path : `${path}-${hash(input || '')}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c0d944c
There was a problem hiding this comment.
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:
trpc-nuxt – ./
trpc-nuxt.vercel.app
trpc-nuxt-wobsoriano.vercel.app
trpc-nuxt-git-main-wobsoriano.vercel.app