import { buildHeaders, buildUrl, deserialize } from "./utils" import type { GetStaticPropsContext } from "next" import type { AccessToken, JsonApiResource } from "../types" import type { JsonApiWithLocaleOptions } from "../types/deprecated" export async function getSearchIndex( name: string, options?: { deserialize?: boolean accessToken?: AccessToken } & JsonApiWithLocaleOptions ): Promise { options = { deserialize: true, ...options, } const localePrefix = options?.locale && options.locale !== options.defaultLocale ? `/${options.locale}` : "" const url = buildUrl(`${localePrefix}/jsonapi/index/${name}`, options.params) const response = await fetch(url.toString(), { headers: await buildHeaders(options), }) if (!response.ok) { throw new Error(response.statusText) } const json = await response.json() return options.deserialize ? deserialize(json) : json } export async function getSearchIndexFromContext( name: string, context: GetStaticPropsContext, options?: { deserialize?: boolean accessToken?: AccessToken } & JsonApiWithLocaleOptions ): Promise { options = { deserialize: true, ...options, } return await getSearchIndex(name, { ...options, locale: context.locale, defaultLocale: context.defaultLocale, }) }