import {z} from 'zod' export type GetApiCmV1ReportsArgs = {ad_account_id: string}; export type GetApiCmV1ReportsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const adcloudcommonDateInterval = z.object({start: z.string().optional(), end: z.string().optional()}); export const dspReportDimension = z.enum(["UNKNOWN_REPORT_DIMENSION", "DATE", "APP_OR_SITE", "CAMPAIGN", "AD_GROUP", "CREATIVE_GROUP", "CREATIVE", "EXCHANGE", "SUB_PUBLISHER", "TRAFFIC", "SKAN"]); export const reportOptionalMetric = z.enum(["UNKNOWN_REPORT_METRIC", "VIDEO_PLAY_PROGRESS", "ENGAGED_VIEWS", "ENGAGED_CLICKS"]); export const dspReport = z.object({id: z.string().optional(), ad_account_id: z.string(), product_id: z.string().optional(), date_range: adcloudcommonDateInterval, dimensions: z.array(dspReportDimension), optional_metrics: z.array(reportOptionalMetric).optional(), expire_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const messagesListReportsResponse = z.object({reports: z.array(dspReport).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List up reports. * * GET /cm/v1/reports */ export const getApiCmV1Reports = async (args: GetApiCmV1ReportsArgs, options?: GetApiCmV1ReportsOptions) => { const {baseUrl = "https://api.moloco.cloud", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ad_account_id != null) query.append("ad_account_id", String(args.ad_account_id)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/cm/v1/reports${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(messagesListReportsResponse, await res.json()) };