import {z} from 'zod' export type AdcloudcommonDateInterval = {start?: string | undefined, end?: string | undefined}; export type DspReportDimension = 'UNKNOWN_REPORT_DIMENSION' | 'DATE' | 'APP_OR_SITE' | 'CAMPAIGN' | 'AD_GROUP' | 'CREATIVE_GROUP' | 'CREATIVE' | 'EXCHANGE' | 'SUB_PUBLISHER' | 'TRAFFIC' | 'SKAN'; export type ReportOptionalMetric = 'UNKNOWN_REPORT_METRIC' | 'VIDEO_PLAY_PROGRESS' | 'ENGAGED_VIEWS' | 'ENGAGED_CLICKS'; export type DspReport = {id?: string | undefined, ad_account_id: string, product_id?: string | undefined, date_range: AdcloudcommonDateInterval, dimensions: Array, optional_metrics?: Array | undefined, expire_at?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined}; export type CreateApiCmV1ReportsArgs = {ad_account_id: string, body: DspReport}; export type CreateApiCmV1ReportsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const messagesCreateReportResponse = z.object({id: z.string().optional(), href: z.string().optional(), status: z.string().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 }; /** * Create a new report. * * POST /cm/v1/reports */ export const createApiCmV1Reports = async (args: CreateApiCmV1ReportsArgs, options?: CreateApiCmV1ReportsOptions) => { const {baseUrl = "https://api.moloco.cloud", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(messagesCreateReportResponse, await res.json()) };