({
path,
params,
pageViewId = getAdPageViewId(),
throwOnError = false,
}: {
path: string;
params?: URLSearchParams;
pageViewId?: string;
throwOnError?: boolean;
})
| 158 | }; |
| 159 | |
| 160 | const fetchAdRequest = async ({ |
| 161 | path, |
| 162 | params, |
| 163 | pageViewId = getAdPageViewId(), |
| 164 | throwOnError = false, |
| 165 | }: { |
| 166 | path: string; |
| 167 | params?: URLSearchParams; |
| 168 | pageViewId?: string; |
| 169 | throwOnError?: boolean; |
| 170 | }): Promise<{ ad: Ad | null; failed: boolean }> => { |
| 171 | const headers: Record<string, string> = {}; |
| 172 | if (pageViewId) { |
| 173 | headers['X-Page-View-Id'] = pageViewId; |
| 174 | } |
| 175 | const hints = await getBrowserClientHints( |
| 176 | params?.get('consent_exists') === AdConsentExists.True, |
| 177 | ); |
| 178 | if (hints) { |
| 179 | headers['X-User-Agent-Client-Hints'] = JSON.stringify(hints); |
| 180 | } |
| 181 | const query = params?.toString(); |
| 182 | const res = await fetch(`${apiUrl}${path}${query ? `?${query}` : ''}`, { |
| 183 | credentials: 'include', |
| 184 | ...(Object.keys(headers).length > 0 && { headers }), |
| 185 | }); |
| 186 | |
| 187 | if (throwOnError && !res.ok) { |
| 188 | throw new Error(`Ad request failed with status ${res.status}`); |
| 189 | } |
| 190 | |
| 191 | const ads = (await res.json()) as Ad[]; |
| 192 | return { |
| 193 | ad: addGenerationIdHeader({ ad: ads[0] ?? null, res }), |
| 194 | failed: res.ok === false, |
| 195 | }; |
| 196 | }; |
| 197 | |
| 198 | const fetchAdSlot = async ( |
| 199 | options: Parameters<typeof fetchAdRequest>[0], |
no test coverage detected