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

Function fetchAdRequest

packages/shared/src/lib/ads.ts:160–196  ·  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

158};
159
160const 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
198const fetchAdSlot = async (
199 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