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

Function fetchAdRequest

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

156};
157
158const 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
193export const resolveAdFetchOptions = ({
194 placement = AdPlacement.Feed,

Callers 2

fetchAdByPlacementFunction · 0.85
fetchAdFunction · 0.85

Calls 3

getAdPageViewIdFunction · 0.90
getBrowserClientHintsFunction · 0.90
addGenerationIdHeaderFunction · 0.85

Tested by

no test coverage detected