From 273bda980b3bd518ecd6f81ec1552e9b156593c3 Mon Sep 17 00:00:00 2001 From: cawa-93 Date: Tue, 31 May 2022 15:20:21 +0300 Subject: [PATCH] feat: extract `getQueryKey` helper This method will be useful to get a key that can be used to reset the internal nuxt cache --- src/runtime/client.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/runtime/client.ts b/src/runtime/client.ts index 176df2e..6d6f5ec 100644 --- a/src/runtime/client.ts +++ b/src/runtime/client.ts @@ -31,6 +31,18 @@ export type TError = TRPCClientErrorLike export type TQueryValues = inferProcedures + + +/** + * Calculates the key used for `useAsyncData` call + * @param pathAndInput + */ +export function getQueryKey< + TPath extends keyof TQueryValues & string + >(pathAndInput: [path: TPath, ...args: inferHandlerInput]) { + return `${pathAndInput[0]}-${objectHash(pathAndInput[1] ? JSON.stringify(pathAndInput[1]) : '')}` +} + export async function useAsyncQuery< TPath extends keyof TQueryValues & string, TOutput extends TQueryValues[TPath]['output'] = TQueryValues[TPath]['output'], @@ -41,7 +53,7 @@ export async function useAsyncQuery< options: AsyncDataOptions = {}, ): Promise, PickKeys>, TError>> { const { $client } = useNuxtApp() - const key = `${pathAndInput[0]}-${objectHash(pathAndInput[1] ? JSON.stringify(pathAndInput[1]) : '')}` + const key = getQueryKey(pathAndInput) const serverError = useState(`error-${key}`, () => null) const { error, data, ...rest } = await useAsyncData( key,