From 694627a194821262d24c1485daf9e29969fe6045 Mon Sep 17 00:00:00 2001 From: timothe Date: Mon, 10 Jun 2024 15:06:36 +0200 Subject: [PATCH] chore: update sitemap content fetched --- server/api/sitemap.ts | 62 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/server/api/sitemap.ts b/server/api/sitemap.ts index fa071d5..5598250 100644 --- a/server/api/sitemap.ts +++ b/server/api/sitemap.ts @@ -1,7 +1,61 @@ -import type { SitemapUrlInput } from '@nuxtjs/sitemap/dist/runtime/types' +import type { HydraCollection, JsonLdObject, RoadizRequestParams, RoadizTranslation } from '@roadiz/types' +import { asSitemapUrl, defineSitemapEventHandler } from '#imports' +import { hydraCollectionFetch } from '~/utils/hydra-collection-fetch' +import { useApiUrl } from '~/composables/use-api-url' -export default defineSitemapEventHandler(async (_event) => { - const result: SitemapUrlInput[] = [] +type ReachableEntity = JsonLdObject & { url?: string } - return result +const apiFetch = $fetch.create({ + method: 'GET', + headers: { + 'accept-encoding': 'gzip, deflate', + Accept: 'application/ld+json', + }, + baseURL: useApiUrl(), // Auto imports within the server folder aren't supported +}) + +function fetchAllByLocale(path: string, _locale = 'fr', params: RoadizRequestParams = {}): Promise { + return hydraCollectionFetch( + path, + { + params: { + properties: ['url'], + _locale, + ...params, + }, + }, + apiFetch, + ) +} + +function fetchResourcesByLocale(locale: string) { + const nodes = fetchAllByLocale('/nodes_sources', locale, { + 'node.nodeType.reachable': true, + 'node.visible': true, + noIndex: false, + }) + + // const today = new Date() + // today.setFullYear(today.getFullYear() - 2) + // const events = fetchAllByLocale('/events', locale, { + // 'sortingDateTime[after]': today.getFullYear() + '-' + today.getMonth() + '-' + today.getDate(), + // }) + // + // const peoples = fetchAllByLocale('/people', locale, { + // 'exists[description]': true, + // 'order[familyName]': 'asc', + // }) + + return [nodes] +} + +export default defineSitemapEventHandler(async () => { + const locales = await apiFetch>('/translations', { + params: { available: true }, + }).then((response) => response['hydra:member'].map(({ locale }) => locale)) + + const resourcesLocalized = locales.map((locale) => fetchResourcesByLocale(locale)).flat() + const resources = (await Promise.all(resourcesLocalized)).flat() + + return resources.filter((r) => r?.url).map((resource) => asSitemapUrl(resource.url as string)) })