next-drupal
Version:
Helpers for Next.js + Drupal.
68 lines (55 loc) • 1.41 kB
text/typescript
import { buildHeaders, buildUrl, deserialize } from "./utils"
import type { AccessToken, DrupalMenuItem } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"
export async function getMenu<T extends DrupalMenuItem>(
name: string,
options?: {
deserialize?: boolean
accessToken?: AccessToken
} & JsonApiWithLocaleOptions
): Promise<{
items: T[]
tree: T[]
}> {
options = {
deserialize: true,
...options,
}
const localePrefix =
options?.locale && options.locale !== options.defaultLocale
? `/${options.locale}`
: ""
const url = buildUrl(`${localePrefix}/jsonapi/menu_items/${name}`)
const response = await fetch(url.toString(), {
headers: await buildHeaders(options),
})
if (!response.ok) {
throw new Error(response.statusText)
}
const data = await response.json()
const items = options.deserialize ? deserialize(data) : data
const { items: tree } = buildMenuTree(items)
return {
items,
tree,
}
}
function buildMenuTree(
links: DrupalMenuItem[],
parent: DrupalMenuItem["id"] = ""
) {
if (!links?.length) {
return {
items: [],
}
}
const children = links.filter((link) => link.parent === parent)
return children.length
? {
items: children.map((link) => ({
...link,
...buildMenuTree(links, link.id),
})),
}
: {}
}