Skip to content

Commit 815aabf

Browse files
committed
FIX use api fetch
1 parent 729794e commit 815aabf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

app/src/composables/useApiFetch.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ import { FetchError } from 'ofetch'
2020

2121
// export const useApiFetch = useApiData
2222

23+
function parseQuery(query: Record<string, any>, prefix: string = '') {
24+
let parsedQuery: any = {}
25+
for (const q in query) {
26+
const key = prefix ? `${prefix}[${q}]` : q
27+
if (typeof query[q] === 'object') {
28+
parsedQuery = { ...parsedQuery, ...parseQuery(query[q], key) }
29+
} else {
30+
parsedQuery[key] = query[q]
31+
}
32+
}
33+
return parsedQuery
34+
}
35+
2336
export function useApiFetch<P extends AllPaths<Paths>, M extends IgnoreCase<keyof Paths[`/${P}`] & HttpMethod>>(
2437
path: MaybeRefOrGetter<P>,
2538
opts?: Omit<UseOpenApiDataOptions<Paths[`/${P}`], M>, 'method'> & {
@@ -35,15 +48,18 @@ export function useApiFetch<P extends AllPaths<Paths>, M extends IgnoreCase<keyo
3548
body?: Paths[`/${P}`][Lowercase<M>]['requestBody']['content']['application/json'],
3649
): /* @ts-ignore */
3750
AsyncData<OpenApiResponse<Paths[`/${P}`][Lowercase<M>]> | undefined, FetchError<OpenApiError<Paths[`/${P}`][Lowercase<M>]>>> {
51+
const query = parseQuery((opts as any)?.query || {})
3852
if (!body) {
3953
return useApiData(path, {
4054
...opts,
55+
query,
4156
cache: false,
4257
client: true,
4358
} as any)
4459
}
4560
return useApiData(path, {
4661
...opts,
62+
query,
4763
cache: false,
4864
client: true,
4965
body: {

0 commit comments

Comments
 (0)