import {z} from 'zod' export type AdcloudcommonCurrency = 'UNKNOWN_CURRENCY' | 'USD' | 'KRW' | 'JPY' | 'EUR' | 'GBP'; export type TargetFuturePreferenceInfoProductScope = 'UNKNOWN' | 'ALL' | 'ANDROID' | 'IOS'; export type TargetFuturePreferenceInfoCustomProducts = {product_ids?: Array | undefined}; export type AdAccountTargetFuturePreferenceInfo = {product_scope?: TargetFuturePreferenceInfoProductScope | undefined, custom_products?: TargetFuturePreferenceInfoCustomProducts | undefined}; export type DspAdAccount = {id?: string | undefined, title: string, description?: string | undefined, timezone?: string | undefined, currency?: AdcloudcommonCurrency | undefined, custom_key_values?: {[key: string]: string} | undefined, billing_account_id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, targets_future_preferences?: {[key: string]: AdAccountTargetFuturePreferenceInfo} | undefined}; export type CreateApiCmV1AdAccountsArgs = {body: DspAdAccount}; export type CreateApiCmV1AdAccountsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const adcloudcommonCurrency = z.enum(["UNKNOWN_CURRENCY", "USD", "KRW", "JPY", "EUR", "GBP"]); export const targetFuturePreferenceInfoProductScope = z.enum(["UNKNOWN", "ALL", "ANDROID", "IOS"]); export const targetFuturePreferenceInfoCustomProducts = z.object({product_ids: z.array(z.string()).optional()}); export const adAccountTargetFuturePreferenceInfo = z.object({product_scope: targetFuturePreferenceInfoProductScope.optional(), custom_products: targetFuturePreferenceInfoCustomProducts.optional()}); export const dspAdAccount = z.object({id: z.string().optional(), title: z.string(), description: z.string().optional(), timezone: z.string().optional(), currency: adcloudcommonCurrency.optional(), custom_key_values: z.record(z.string(), z.string()).optional(), billing_account_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), targets_future_preferences: z.record(z.string(), adAccountTargetFuturePreferenceInfo).optional()}); export const messagesCreateAdAccountResponse = z.object({ad_account: dspAdAccount.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 AdAccount. * * POST /cm/v1/ad-accounts */ export const createApiCmV1AdAccounts = async (args: CreateApiCmV1AdAccountsArgs, options?: CreateApiCmV1AdAccountsOptions) => { 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 (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/cm/v1/ad-accounts${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(messagesCreateAdAccountResponse, await res.json()) };