1 | |
2 | type Adhook = { |
3 | token: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Adhook, |
8 | facebookAccountId: string | undefined, |
9 | instagramAccountId: string | undefined, |
10 | catalogId: string | undefined, |
11 | q: string | undefined, |
12 | adhookToken: string, |
13 | Origin: string |
14 | ) { |
15 | const url = new URL(`https://app.adhook.io/v1/promotions/facebook/products`) |
16 |
|
17 | for (const [k, v] of [ |
18 | ['facebookAccountId', facebookAccountId], |
19 | ['instagramAccountId', instagramAccountId], |
20 | ['catalogId', catalogId], |
21 | ['q', q] |
22 | ]) { |
23 | if (v !== undefined && v !== '' && k !== undefined) { |
24 | url.searchParams.append(k, v) |
25 | } |
26 | } |
27 |
|
28 | const response = await fetch(url, { |
29 | method: 'GET', |
30 | headers: { |
31 | adhookToken: adhookToken, |
32 | Authorization: `Bearer ${auth.token}`, |
33 | Origin: Origin |
34 | }, |
35 | body: undefined |
36 | }) |
37 |
|
38 | if (!response.ok) { |
39 | const text = await response.text() |
40 | throw new Error(`${response.status} ${text}`) |
41 | } |
42 |
|
43 | return await response.json() |
44 | } |
45 |
|