(
options: Parameters<typeof fetchAdRequest>[0],
{
onTransition,
throwOnError = false,
}: {
onTransition?: FetchAdByPlacementOptions['onTransition'];
throwOnError?: boolean;
} = {},
)
| 219 | }; |
| 220 | |
| 221 | const fetchAdSlot = async ( |
| 222 | options: Parameters<typeof fetchAdRequest>[0], |
| 223 | { |
| 224 | onTransition, |
| 225 | throwOnError = false, |
| 226 | }: { |
| 227 | onTransition?: FetchAdByPlacementOptions['onTransition']; |
| 228 | throwOnError?: boolean; |
| 229 | } = {}, |
| 230 | ): Promise<Ad | null> => { |
| 231 | onTransition?.({ type: 'pre-fetch' }); |
| 232 | let result: Awaited<ReturnType<typeof fetchAdRequest>>; |
| 233 | try { |
| 234 | result = await fetchAdRequest({ ...options, throwOnError }); |
| 235 | } catch (error) { |
| 236 | onTransition?.({ type: 'fetch-error' }); |
| 237 | throw error; |
| 238 | } |
| 239 | const { ad, failed } = result; |
| 240 | if (failed) { |
| 241 | onTransition?.({ type: 'fetch-error' }); |
| 242 | } else { |
| 243 | onTransition?.(ad ? { type: 'post-fetch', ad } : { type: 'no-fill' }); |
| 244 | } |
| 245 | return ad; |
| 246 | }; |
| 247 | |
| 248 | export const resolveAdFetchOptions = ({ |
| 249 | placement = AdPlacement.Feed, |
no test coverage detected