1 | type Datocms = { |
2 | apiKey: string |
3 | } |
4 |
|
5 | export async function main( |
6 | resource: Datocms, |
7 | pagination?: { |
8 | limit?: number |
9 | offset?: number |
10 | } |
11 | ) { |
12 | const queryParams = new URLSearchParams() |
13 |
|
14 | if (pagination?.limit) { |
15 | queryParams.append('page[limit]', pagination.limit.toString()) |
16 | } |
17 |
|
18 | if (pagination?.offset) { |
19 | queryParams.append('page[offset]', pagination.offset.toString()) |
20 | } |
21 |
|
22 | const endpoint = `https://site-api.datocms.com/items?${queryParams.toString()}` |
23 |
|
24 | const response = await fetch(endpoint, { |
25 | method: 'GET', |
26 | headers: { |
27 | Authorization: `Bearer ${resource.apiKey}`, |
28 | Accept: 'application/json', |
29 | 'X-Api-Version': '3' |
30 | } |
31 | }) |
32 |
|
33 | if (!response.ok) { |
34 | throw new Error(`HTTP error! status: ${response.status}`) |
35 | } |
36 |
|
37 | const data = await response.json() |
38 |
|
39 | return data |
40 | } |
41 |
|