Samuelreichoer
Fetch Data server and client side module recommended way
@kapa.ai Can you refactor my code to use asyncQuery?
async function fetchFn(url: string): Promise<FetchResult> {
const { data, error } = await useFetch(url)
return { data, error }
}
export function useCraftQuery<T extends ElementType>(elementType: T) {
const queryBuilder = useCraftUrlBuilder(elementType)
return {
...queryBuilder,
async one() {
const url = queryBuilder.buildUrl('one')
return await fetchFn(url)
},
async all() {
const url = queryBuilder.buildUrl('all')
return await fetchFn(url)
},
}
}
10 replies