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