import {z} from 'zod' export type GetApiCmV1CustomerSetsArgs = {ad_account_id: string}; export type GetApiCmV1CustomerSetsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dspCustomerSetIdType = z.enum(["UNKNOWN_ID_TYPE", "GOOGLE_ADID", "APPLE_IDFA"]); export const dspCustomerSetStatus = z.enum(["UNKNOWN_STATUS", "PREPARING", "READY", "FAILED"]); export const fileUpdateInfoUpdateStatus = z.enum(["UNKNOWN_STATUS", "IN_PROGRESS", "SUCCESS", "FAILED"]); export const customerSetFileUpdateInfo = z.object({status: fileUpdateInfoUpdateStatus.optional(), timestamp: z.string().optional(), file_name: z.string().optional()}); export const customerSetStatusDetailConnectionMethod = z.enum(["UNKNOWN_METHOD", "CLOUD_API", "MCP_UPLOAD", "AF_AUDIENCE_BUILDER", "BESPOKE"]); export const customerSetStatusDetailErrorId = z.enum(["UNKNOWN_ERROR_ID", "TAGGING_ERROR", "REQUEST_ERROR", "PREPROCESSING_ERROR", "GENERIC_ERROR"]); export const customerSetCustomerSetStatusDetail = z.object({connection_method: customerSetStatusDetailConnectionMethod.optional(), error_id: customerSetStatusDetailErrorId.optional(), error_description: z.string().optional(), csv_file_size: z.number().int().optional(), aud_size: z.number().int().optional()}); export const customerSetFileUpdateHistory = z.object({status: dspCustomerSetStatus.optional(), status_detail: customerSetCustomerSetStatusDetail.optional(), updated_at: z.string().optional()}); export const dspCustomerSet = z.object({id: z.string().optional(), title: z.string(), description: z.string().optional(), id_type: dspCustomerSetIdType, status: dspCustomerSetStatus.optional(), data_file_path: z.string(), is_upper: z.boolean().optional(), auto_delete_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), recent_file_update_info: customerSetFileUpdateInfo.optional(), status_detail: customerSetCustomerSetStatusDetail.optional(), file_update_histories: z.array(customerSetFileUpdateHistory).optional()}); export const messagesListCustomerSetsResponse = z.object({customer_sets: z.array(dspCustomerSet).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 CustomerSets. * * GET /cm/v1/customer-sets */ export const getApiCmV1CustomerSets = async (args: GetApiCmV1CustomerSetsArgs, options?: GetApiCmV1CustomerSetsOptions) => { 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/customer-sets${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(messagesListCustomerSetsResponse, await res.json()) };