From 8ba6fa008b9c42168885e4f1c6d322f5a9e8bf9f Mon Sep 17 00:00:00 2001 From: Pascal Klesse Date: Tue, 10 Sep 2024 15:42:43 +0200 Subject: [PATCH] fix: fix async query --- src/runtime/composables/gql-async-query.ts | 44 +++++++++++----------- src/runtime/composables/use-helper.ts | 18 ++++++++- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/runtime/composables/gql-async-query.ts b/src/runtime/composables/gql-async-query.ts index a890c66..2397a9d 100644 --- a/src/runtime/composables/gql-async-query.ts +++ b/src/runtime/composables/gql-async-query.ts @@ -14,37 +14,37 @@ export async function gqlAsyncQuery(method: string, options: IGraphQLOp const _nuxtApp = useNuxtApp(); const { accessTokenState } = useAuthState(); const { checkTokenAndRenew } = useAuth(); - const { hashCode } = useHelper(); + const { generateUniqueHash } = useHelper(); // Check parameters if (!method) { throw new Error('No method detected'); } - const config = { - asyncDataOptions: { - lazy: false, - }, - fields: null, - log: false, - variables: null, - ...options, - hashPasswords: options.hashPasswords ?? true, - }; - - // check if config.variables is a function - if (typeof config.variables === 'function') { - config.variables = config.variables(); - } - - if (config.hashPasswords) { - config.variables = await hashPasswords(config.variables); - } - return useAsyncData( - method + hashCode(config.variables), + method + generateUniqueHash, async () => { // Get config + const config = { + asyncDataOptions: { + lazy: false, + }, + fields: null, + log: false, + variables: null, + ...options, + hashPasswords: options.hashPasswords ?? true, + }; + + // check if config.variables is a function + if (typeof config.variables === 'function') { + config.variables = config.variables(); + } + + if (config.hashPasswords) { + config.variables = await hashPasswords(config.variables); + } + const fields = config.fields as unknown as string[]; if (config.log) { diff --git a/src/runtime/composables/use-helper.ts b/src/runtime/composables/use-helper.ts index 3bbd3b0..2571207 100644 --- a/src/runtime/composables/use-helper.ts +++ b/src/runtime/composables/use-helper.ts @@ -138,5 +138,21 @@ export function useHelper() { return hash; } - return { getDifferences, groupBy, hashCode, isValidMongoID, removeFields, removeNullOrUndefined, slugifyDomain }; + function generateUniqueHash(): string { + const now = Date.now(); + const random = Math.random(); + const input = now.toString() + random.toString(); + + let hash = 0; + + for (let i = 0; i < input.length; i++) { + const char = input.charCodeAt(i); + hash = (hash << 5) - hash + char; + hash |= 0; + } + + return hash.toString(16); + } + + return { generateUniqueHash, getDifferences, groupBy, hashCode, isValidMongoID, removeFields, removeNullOrUndefined, slugifyDomain }; }