(
options: Parameters<typeof fetchAdRequest>[0],
{
onTransition,
throwOnError = false,
}: {
onTransition?: FetchAdByPlacementOptions['onTransition'];
throwOnError?: boolean;
} = {},
)
| 196 | }; |
| 197 | |
| 198 | const fetchAdSlot = async ( |
| 199 | options: Parameters<typeof fetchAdRequest>[0], |
| 200 | { |
| 201 | onTransition, |
| 202 | throwOnError = false, |
| 203 | }: { |
| 204 | onTransition?: FetchAdByPlacementOptions['onTransition']; |
| 205 | throwOnError?: boolean; |
| 206 | } = {}, |
| 207 | ): Promise<Ad | null> => { |
| 208 | onTransition?.({ type: 'pre-fetch' }); |
| 209 | let result: Awaited<ReturnType<typeof fetchAdRequest>>; |
| 210 | try { |
| 211 | result = await fetchAdRequest({ ...options, throwOnError }); |
| 212 | } catch (error) { |
| 213 | onTransition?.({ type: 'fetch-error' }); |
| 214 | throw error; |
| 215 | } |
| 216 | const { ad, failed } = result; |
| 217 | if (failed) { |
| 218 | onTransition?.({ type: 'fetch-error' }); |
| 219 | } else { |
| 220 | onTransition?.(ad ? { type: 'post-fetch', ad } : { type: 'no-fill' }); |
| 221 | } |
| 222 | return ad; |
| 223 | }; |
| 224 | |
| 225 | export const resolveAdFetchOptions = ({ |
| 226 | placement = AdPlacement.Feed, |
no test coverage detected