Skip to content

Commit

Permalink
fix: fix async query
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Sep 10, 2024
1 parent 76e1146 commit 8ba6fa0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
44 changes: 22 additions & 22 deletions src/runtime/composables/gql-async-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ export async function gqlAsyncQuery<T = any>(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) {
Expand Down
18 changes: 17 additions & 1 deletion src/runtime/composables/use-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

0 comments on commit 8ba6fa0

Please sign in to comment.