-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useRoadizHydraCollectionFetch
- Loading branch information
1 parent
803b889
commit 3e0e421
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { FetchOptions } from 'ofetch' | ||
import { hydraCollectionFetch } from '~/utils/hydra-collection-fetch' | ||
|
||
export function useRoadizHydraCollectionFetch<T>(url: string, request?: FetchOptions) { | ||
const fetch = useRoadizFetchFactory() | ||
|
||
return hydraCollectionFetch<T>(url, request, fetch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { HydraCollection } from '@roadiz/types' | ||
import type { FetchOptions } from 'ofetch' | ||
|
||
// Fetch hydra collection recursively | ||
export async function hydraCollectionFetch<T>(url: string, request?: FetchOptions, fetch = $fetch) { | ||
const result = [] as Array<T> | ||
let page = request?.params?.page || 1 | ||
let active = true | ||
|
||
do { | ||
const response = await fetch<HydraCollection<T>>(url, { | ||
...request, | ||
method: 'GET', | ||
params: { | ||
...request?.params, | ||
page, | ||
}, | ||
}) | ||
|
||
if (response?.['hydra:member']) { | ||
result.push(...response['hydra:member']) | ||
|
||
active = !!response?.['hydra:view']?.['hydra:next'] | ||
page++ | ||
} else { | ||
active = false | ||
} | ||
} while (active) | ||
|
||
return result | ||
} |