Fix globals fetch to handle singleton collection shape
This commit is contained in:
@@ -45,6 +45,15 @@ export async function directusOne<T>(collection: string, q: DirectusQuery = {}):
|
||||
return data[0]
|
||||
}
|
||||
|
||||
export async function directusSingleton<T>(collection: string, q: DirectusQuery = {}): Promise<T> {
|
||||
const url = new URL(`${DIRECTUS_URL}/items/${collection}`)
|
||||
for (const field of q.fields ?? []) url.searchParams.append('fields[]', field)
|
||||
const res = await fetch(url.toString())
|
||||
if (!res.ok) throw new Error(`Directus error ${res.status} on /items/${collection}`)
|
||||
const { data } = (await res.json()) as DirectusItemResponse<T>
|
||||
return data
|
||||
}
|
||||
|
||||
export function assetUrl(fileId: string | null | undefined, key?: string): string {
|
||||
if (!fileId) return ''
|
||||
const url = new URL(`${DIRECTUS_URL}/assets/${fileId}`)
|
||||
|
||||
Reference in New Issue
Block a user