|
1 | 1 | import { defineSitemapEventHandler } from '#imports' |
2 | 2 | import type { SitemapUrl } from '#sitemap/types' |
| 3 | +import type { H3Event } from 'h3' |
3 | 4 |
|
4 | 5 | const API_V2_TYPES = ['dataset'] |
5 | 6 |
|
6 | | -export default defineSitemapEventHandler(async (event) => { |
| 7 | +interface TotalResponse { |
| 8 | + total: number |
| 9 | +} |
| 10 | + |
| 11 | +interface PaginatedResponse { |
| 12 | + data: Array<Record<string, string>> |
| 13 | + next_page: string | null |
| 14 | + page: number |
| 15 | + total: number |
| 16 | +} |
| 17 | + |
| 18 | +export default defineSitemapEventHandler(async (event: H3Event) => { |
7 | 19 | const config = useRuntimeConfig() |
8 | 20 |
|
9 | 21 | const { type, nbSitemapSections = 1 } = getQuery(event) |
10 | 22 | if (!type) |
11 | 23 | return new Response(null, { status: 404 }) |
12 | 24 | let { section = 1 } = getQuery(event) |
13 | | - section = parseInt(section) |
| 25 | + section = parseInt(String(section)) |
| 26 | + const nbSections = parseInt(String(nbSitemapSections)) |
14 | 27 |
|
15 | 28 | const pageSize = 200 |
16 | 29 | const selfWebUrlKey = type == 'dataservice' ? 'self_web_url' : 'page' |
17 | 30 |
|
18 | 31 | // computing starting api page and max page for this sitemap section |
19 | 32 | let currentPage = 1 |
20 | | - let maxPage = null |
21 | | - await $fetch<{ path: string }[]>(`${config.public.apiBase}/api/1/${type}s/`, { |
| 33 | + let maxPage = 1 |
| 34 | + await $fetch<TotalResponse>(`${config.public.apiBase}/api/1/${type}s/`, { |
22 | 35 | headers: { 'X-Fields': 'total' }, |
23 | 36 | }).then((result) => { |
24 | 37 | const total = result.total |
25 | | - currentPage = Math.floor(((total / nbSitemapSections / pageSize)) * (section - 1)) + 1 |
26 | | - maxPage = Math.floor(((total / nbSitemapSections / pageSize)) * section) |
27 | | - if (section == nbSitemapSections) |
| 38 | + currentPage = Math.floor(((total / nbSections / pageSize)) * (section - 1)) + 1 |
| 39 | + maxPage = Math.floor(((total / nbSections / pageSize)) * section) |
| 40 | + if (section == nbSections) |
28 | 41 | maxPage += 1 // add last incomplete page on the last sitemap |
29 | 42 | }) |
30 | 43 |
|
31 | | - const apiVersion = API_V2_TYPES.includes(type) ? 2 : 1 |
32 | | - let nextPageUrl = config.public.apiBase + `/api/${apiVersion}/${type}s/?page_size=${pageSize}&page=${currentPage}` |
33 | | - const pages = [] |
| 44 | + const apiVersion = API_V2_TYPES.includes(String(type)) ? 2 : 1 |
| 45 | + let nextPageUrl: string | null = config.public.apiBase + `/api/${apiVersion}/${type}s/?page_size=${pageSize}&page=${currentPage}` |
| 46 | + const pages: SitemapUrl[] = [] |
34 | 47 |
|
35 | 48 | do { |
36 | | - await $fetch<{ path: string }[]>(nextPageUrl, { |
| 49 | + await $fetch<PaginatedResponse>(nextPageUrl, { |
37 | 50 | headers: { 'X-Fields': `data{${selfWebUrlKey}},next_page,page,total` }, |
38 | 51 | }).then((result) => { |
39 | 52 | nextPageUrl = result.next_page |
40 | 53 | currentPage = result.page |
41 | | - pages.push(...result.data.map(p => ({ |
| 54 | + pages.push(...result.data.map((p: Record<string, string>) => ({ |
42 | 55 | loc: p[selfWebUrlKey], |
43 | | - _sitemap: nbSitemapSections == 1 ? `${type}s` : `${type}s_${section}`, |
| 56 | + _sitemap: nbSections == 1 ? `${type}s` : `${type}s_${section}`, |
44 | 57 | } satisfies SitemapUrl))) |
45 | 58 | }) |
46 | 59 | } while (nextPageUrl && currentPage < maxPage) |
|
0 commit comments