import {z} from 'zod' export type ReportExportPartner = 'UNKNOWN_PARTNER' | 'SINGULAR' | 'APPSFLYER' | 'TENJIN' | 'BIDALGO' | 'APPANNIE' | 'APPSUMER' | 'ADJUST' | 'KOCHAVA' | 'AIRBRIDGE' | 'BRANCH' | 'JUSTTRACK'; export type DspReportExport = {id?: string | undefined, badge?: string | undefined, workplace_id: string, ad_account_id: string, product_id: string, partner: ReportExportPartner, created_at?: string | undefined, updated_at?: string | undefined}; export type CreateApiCmV1ReportExportsArgs = {ad_account_id: string, product_id?: string | undefined, body: DspReportExport}; export type CreateApiCmV1ReportExportsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const reportExportPartner = z.enum(["UNKNOWN_PARTNER", "SINGULAR", "APPSFLYER", "TENJIN", "BIDALGO", "APPANNIE", "APPSUMER", "ADJUST", "KOCHAVA", "AIRBRIDGE", "BRANCH", "JUSTTRACK"]); export const dspReportExport = z.object({id: z.string().optional(), badge: z.string().optional(), workplace_id: z.string(), ad_account_id: z.string(), product_id: z.string(), partner: reportExportPartner, created_at: z.string().optional(), updated_at: z.string().optional()}); export const messagesCreateReportExportResponse = z.object({report_export: dspReportExport.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 export. * * POST /cm/v1/report-exports */ export const createApiCmV1ReportExports = async (args: CreateApiCmV1ReportExportsArgs, options?: CreateApiCmV1ReportExportsOptions) => { 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 (args.product_id != null) query.append("product_id", String(args.product_id)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/cm/v1/report-exports${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(messagesCreateReportExportResponse, await res.json()) };