MCPcopy Create free account
hub / github.com/dailydotdev/apps / fetchAdRequest

Function fetchAdRequest

packages/shared/src/lib/ads.ts:183–219  ·  view source on GitHub ↗
({
  path,
  params,
  pageViewId = getAdPageViewId(),
  throwOnError = false,
}: {
  path: string;
  params?: URLSearchParams;
  pageViewId?: string;
  throwOnError?: boolean;
})

Source from the content-addressed store, hash-verified

181};
182
183const fetchAdRequest = async ({
184 path,
185 params,
186 pageViewId = getAdPageViewId(),
187 throwOnError = false,
188}: {
189 path: string;
190 params?: URLSearchParams;
191 pageViewId?: string;
192 throwOnError?: boolean;
193}): Promise<{ ad: Ad | null; failed: boolean }> => {
194 const headers: Record<string, string> = {};
195 if (pageViewId) {
196 headers['X-Page-View-Id'] = pageViewId;
197 }
198 const hints = await getBrowserClientHints(
199 params?.get('consent_exists') === AdConsentExists.True,
200 );
201 if (hints) {
202 headers['X-User-Agent-Client-Hints'] = JSON.stringify(hints);
203 }
204 const query = params?.toString();
205 const res = await fetch(`${apiUrl}${path}${query ? `?${query}` : ''}`, {
206 credentials: 'include',
207 ...(Object.keys(headers).length > 0 && { headers }),
208 });
209
210 if (throwOnError && !res.ok) {
211 throw new Error(`Ad request failed with status ${res.status}`);
212 }
213
214 const ads = (await res.json()) as Ad[];
215 return {
216 ad: addGenerationIdHeader({ ad: ads[0] ?? null, res }),
217 failed: res.ok === false,
218 };
219};
220
221const fetchAdSlot = async (
222 options: Parameters<typeof fetchAdRequest>[0],

Callers 1

fetchAdSlotFunction · 0.85

Calls 3

getAdPageViewIdFunction · 0.90
getBrowserClientHintsFunction · 0.90
addGenerationIdHeaderFunction · 0.85

Tested by

no test coverage detected