import {z} from 'zod' export type CommonEnablingState = 'UNSPECIFIED' | 'ENABLED' | 'DISABLED'; export type DspAdvertiserInfo = {id?: string | undefined, title?: string | undefined, description?: string | undefined}; export type DspCreativeType = 'UNKNOWN_TYPE' | 'IMAGE' | 'VIDEO' | 'VAST_VIDEO' | 'NATIVE' | 'NATIVE_IMAGE' | 'NATIVE_VIDEO' | 'CUSTOM_HTML' | 'TEMPLATE_HTML' | 'RICH_CUSTOM_HTML' | 'DYNAMIC_HTML' | 'AD_TAG'; export type DspCreativeImageSrc = {image_url?: string | undefined, filename?: string | undefined, width?: number | undefined, height?: number | undefined, size_in_bytes?: number | undefined}; export type DspCreativeHtmlRequirementAttributesFlag = 'UNKNOWN_FLAG' | 'MRAID2' | 'IN_BANNER_VIDEO'; export type CreativeHtmlRequirementAttributesCustomCloseType = 'UNKNOWN_CUSTOM_CLOSE_TYPE' | 'CUSTOM_CLOSE_TYPE_NONE' | 'CUSTOM_CLOSE_TYPE_ENABLED' | 'CUSTOM_CLOSE_TYPE_CONTROLLABLE'; export type CreativeHtmlRequirementAttributesAutoPlayAudioType = 'UNKNOWN_AUTO_PLAY_AUDIO_TYPE' | 'AUTO_PLAY_AUDIO_TYPE_MUTED' | 'AUTO_PLAY_AUDIO_TYPE_AUTO_PLAY' | 'AUTO_PLAY_AUDIO_TYPE_CONTROLLABLE'; export type DspCreativeHtmlRequirementAttributes = {flags?: Array | undefined, use_custom_close?: CreativeHtmlRequirementAttributesCustomCloseType | undefined, auto_play_audio?: CreativeHtmlRequirementAttributesAutoPlayAudioType | undefined}; export type DspCreativeHtmlSizeAttributesFixed = {width?: number | undefined, height?: number | undefined}; export type DspCreativeHtmlSizeAttributes = {interstitial?: boolean | undefined, fixed?: Array | undefined}; export type DspCreativeRichCustomHtmlSrcPlayableADSourceType = 'UNKNOWN_PLAYABLE_AD_SOURCE_TYPE' | 'PLAYABLE_AD_SOURCE_TYPE_URL' | 'PLAYABLE_AD_SOURCE_TYPE_TAG'; export type CreativeRichCustomHtmlSrcPlayableADSourceData = {type?: DspCreativeRichCustomHtmlSrcPlayableADSourceType | undefined, value?: string | undefined}; export type DspCreativeRichCustomHtmlSrc = {entry_html?: string | undefined, html_requirement_attributes: DspCreativeHtmlRequirementAttributes, html_size_attributes?: DspCreativeHtmlSizeAttributes | undefined, max_video_duration_seconds?: number | undefined, playable_ad_source?: CreativeRichCustomHtmlSrcPlayableADSourceData | undefined, filename?: string | undefined}; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunction = 'UNKNOWN_POLICY' | 'RANDOM_SELECTION' | 'MOST_RECENT_VIEW' | 'IN_CART' | 'GLOBAL_TREND' | 'TREND' | 'COMBINED_EVENTS_RECENCY' | 'FREQUENTLY_BOUGHT_TOGETHER' | 'FREQUENTLY_SEEN_TOGETHER' | 'KEYWORD_SEARCH_WITH_CLIENT_RESULT' | 'COMBINED_EVENTS_RECENCY_WITH_KEYWORD_SEARCH' | 'MODEL' | 'GAME' | 'RMP_AUDIENCE_EXTENSION' | 'MCM_ITEM_RECOMMENDATION' | 'NEXT_ITEM_SELECTION'; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigTrendConfig = {name?: string | undefined}; export type PolicySourcerType = 'UNKNOWN_SOURCER' | 'SOURCER_TEST_CATALOG_DEPENDENT' | 'SOURCER_TEST_CATALOG_INDEPENDENT' | 'SOURCER_DEFAULT' | 'SOURCER_USER_SIGNAL' | 'SOURCER_TRENDS' | 'SOURCER_RELATED_ITEMS' | 'SOURCER_AUCTION_ITEMS'; export type PolicyEvaluatorType = 'UNKNOWN_EVALUATOR' | 'EVALUATOR_TEST_CATALOG_DEPENDENT' | 'EVALUATOR_TEST_CATALOG_INDEPENDENT' | 'EVALUATOR_RANDOM' | 'EVALUATOR_LR'; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigModelConfig = {sourcer_type?: PolicySourcerType | undefined, evaluator_type?: PolicyEvaluatorType | undefined, evaluator_suffix?: string | undefined, user_signal_window_hours?: number | undefined}; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigGameConfig = {game_id?: string | undefined}; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigMcmItemRecommendationConfig = {mcm_platform_id?: string | undefined, inventory_id?: string | undefined}; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigNextItemSelectionConfig = {require_video?: boolean | undefined, skip_internal_review?: boolean | undefined}; export type DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfig = {trend?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigTrendConfig | undefined, model?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigModelConfig | undefined, game?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigGameConfig | undefined, mcm_item_recommendation?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigMcmItemRecommendationConfig | undefined, next_item_selection?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigNextItemSelectionConfig | undefined}; export type DspCreativeDynamicHtmlSrcPolicyItemCapperFrequency = {period?: string | undefined, max_exposures?: number | undefined}; export type DspCreativeDynamicHtmlSrcPolicyItemCapper = {frequency?: DspCreativeDynamicHtmlSrcPolicyItemCapperFrequency | undefined}; export type DspCreativeDynamicHtmlSrcPolicy = {function?: DspCreativeDynamicHtmlSrcPolicyPolicyFunction | undefined, function_config?: DspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfig | undefined, num_items?: number | undefined, catalog?: string | undefined, item_capper?: DspCreativeDynamicHtmlSrcPolicyItemCapper | undefined}; export type DspCreativeDynamicHtmlSrc = {template_id: string, template_env?: {[key: string]: string} | undefined, rendered_entry_html: string, html_requirement_attributes?: DspCreativeHtmlRequirementAttributes | undefined, html_size_attributes?: DspCreativeHtmlSizeAttributes | undefined, policy?: DspCreativeDynamicHtmlSrcPolicy | undefined}; export type DspCreativeVideoSrcTranscodingInfoStatus = 'UNKNOWN_STATUS' | 'IN_PROGRESS' | 'SUCCEEDED' | 'FAILED'; export type DspCreativeVideoSrcTranscodingInfo = {status?: DspCreativeVideoSrcTranscodingInfoStatus | undefined, failed_reason?: string | undefined}; export type DspCreativeVideoSrc = {video_url: string, filename?: string | undefined, width: number, height: number, length_seconds: number, fps?: number | undefined, size_in_bytes?: number | undefined, companion_images?: Array | undefined, auto_endcard?: boolean | undefined, playable_endcard?: DspCreativeRichCustomHtmlSrc | undefined, dynamic_html_endcard?: DspCreativeDynamicHtmlSrc | undefined, transcoding_info?: DspCreativeVideoSrcTranscodingInfo | undefined}; export type DspCreativeVastVideoSrc = {vast_tag: string, width?: number | undefined, height?: number | undefined, length_seconds?: number | undefined}; export type DspCreativeNativeSrc = {icon_image: DspCreativeImageSrc, main_image: DspCreativeImageSrc, shrunk_main_image?: DspCreativeImageSrc | undefined, title: string, text: string, cta_text: string, sponsored_by?: string | undefined, star_rating?: number | undefined, video?: DspCreativeVideoSrc | undefined, vast_video?: DspCreativeVastVideoSrc | undefined}; export type DspCreativeCustomHtmlSrc = {html: string, width: number, height: number, image_url: string}; export type DspCreativeTemplateHtmlSrc = {template_id: string, template_env?: {[key: string]: string} | undefined, rendered_entry_html: string, html_requirement_attributes?: DspCreativeHtmlRequirementAttributes | undefined, html_size_attributes?: DspCreativeHtmlSizeAttributes | undefined, max_video_duration_seconds?: number | undefined}; export type AdcloudcommonCreativeFeatureType = 'UNKNOWN_CREATIVE_FEATURE_TYPE' | 'GENERAL' | 'DYNAMIC_CREATIVE'; export type DspCreative = {id?: string | undefined, ad_account_id?: string | undefined, product_id?: string | undefined, title: string, enabling_state?: CommonEnablingState | undefined, advertiser_info?: DspAdvertiserInfo | undefined, type?: DspCreativeType | undefined, original_filename: string, size_in_bytes?: number | undefined, custom_key_values?: {[key: string]: string} | undefined, image?: DspCreativeImageSrc | undefined, video?: DspCreativeVideoSrc | undefined, vast_video?: DspCreativeVastVideoSrc | undefined, native?: DspCreativeNativeSrc | undefined, custom_html?: DspCreativeCustomHtmlSrc | undefined, template_html?: DspCreativeTemplateHtmlSrc | undefined, rich_custom_html?: DspCreativeRichCustomHtmlSrc | undefined, dynamic_html?: DspCreativeDynamicHtmlSrc | undefined, feature_type?: AdcloudcommonCreativeFeatureType | undefined, creator_tag?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined}; export type CreateApiCmV1CreativesArgs = {ad_account_id: string, product_id: string, body: DspCreative}; export type CreateApiCmV1CreativesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const commonEnablingState = z.enum(["UNSPECIFIED", "ENABLED", "DISABLED"]); export const dspAdvertiserInfo = z.object({id: z.string().optional(), title: z.string().optional(), description: z.string().optional()}); export const dspCreativeType = z.enum(["UNKNOWN_TYPE", "IMAGE", "VIDEO", "VAST_VIDEO", "NATIVE", "NATIVE_IMAGE", "NATIVE_VIDEO", "CUSTOM_HTML", "TEMPLATE_HTML", "RICH_CUSTOM_HTML", "DYNAMIC_HTML", "AD_TAG"]); export const dspCreativeImageSrc = z.object({image_url: z.string().optional(), filename: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), size_in_bytes: z.number().int().optional()}); export const dspCreativeHtmlRequirementAttributesFlag = z.enum(["UNKNOWN_FLAG", "MRAID2", "IN_BANNER_VIDEO"]); export const creativeHtmlRequirementAttributesCustomCloseType = z.enum(["UNKNOWN_CUSTOM_CLOSE_TYPE", "CUSTOM_CLOSE_TYPE_NONE", "CUSTOM_CLOSE_TYPE_ENABLED", "CUSTOM_CLOSE_TYPE_CONTROLLABLE"]); export const creativeHtmlRequirementAttributesAutoPlayAudioType = z.enum(["UNKNOWN_AUTO_PLAY_AUDIO_TYPE", "AUTO_PLAY_AUDIO_TYPE_MUTED", "AUTO_PLAY_AUDIO_TYPE_AUTO_PLAY", "AUTO_PLAY_AUDIO_TYPE_CONTROLLABLE"]); export const dspCreativeHtmlRequirementAttributes = z.object({flags: z.array(dspCreativeHtmlRequirementAttributesFlag).optional(), use_custom_close: creativeHtmlRequirementAttributesCustomCloseType.optional(), auto_play_audio: creativeHtmlRequirementAttributesAutoPlayAudioType.optional()}); export const dspCreativeHtmlSizeAttributesFixed = z.object({width: z.number().int().optional(), height: z.number().int().optional()}); export const dspCreativeHtmlSizeAttributes = z.object({interstitial: z.boolean().optional(), fixed: z.array(dspCreativeHtmlSizeAttributesFixed).optional()}); export const dspCreativeRichCustomHtmlSrcPlayableADSourceType = z.enum(["UNKNOWN_PLAYABLE_AD_SOURCE_TYPE", "PLAYABLE_AD_SOURCE_TYPE_URL", "PLAYABLE_AD_SOURCE_TYPE_TAG"]); export const creativeRichCustomHtmlSrcPlayableADSourceData = z.object({type: dspCreativeRichCustomHtmlSrcPlayableADSourceType.optional(), value: z.string().optional()}); export const dspCreativeRichCustomHtmlSrc = z.object({entry_html: z.string().optional(), html_requirement_attributes: dspCreativeHtmlRequirementAttributes, html_size_attributes: dspCreativeHtmlSizeAttributes.optional(), max_video_duration_seconds: z.number().int().optional(), playable_ad_source: creativeRichCustomHtmlSrcPlayableADSourceData.optional(), filename: z.string().optional()}); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunction = z.enum(["UNKNOWN_POLICY", "RANDOM_SELECTION", "MOST_RECENT_VIEW", "IN_CART", "GLOBAL_TREND", "TREND", "COMBINED_EVENTS_RECENCY", "FREQUENTLY_BOUGHT_TOGETHER", "FREQUENTLY_SEEN_TOGETHER", "KEYWORD_SEARCH_WITH_CLIENT_RESULT", "COMBINED_EVENTS_RECENCY_WITH_KEYWORD_SEARCH", "MODEL", "GAME", "RMP_AUDIENCE_EXTENSION", "MCM_ITEM_RECOMMENDATION", "NEXT_ITEM_SELECTION"]); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigTrendConfig = z.object({name: z.string().optional()}); export const policySourcerType = z.enum(["UNKNOWN_SOURCER", "SOURCER_TEST_CATALOG_DEPENDENT", "SOURCER_TEST_CATALOG_INDEPENDENT", "SOURCER_DEFAULT", "SOURCER_USER_SIGNAL", "SOURCER_TRENDS", "SOURCER_RELATED_ITEMS", "SOURCER_AUCTION_ITEMS"]); export const policyEvaluatorType = z.enum(["UNKNOWN_EVALUATOR", "EVALUATOR_TEST_CATALOG_DEPENDENT", "EVALUATOR_TEST_CATALOG_INDEPENDENT", "EVALUATOR_RANDOM", "EVALUATOR_LR"]); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigModelConfig = z.object({sourcer_type: policySourcerType.optional(), evaluator_type: policyEvaluatorType.optional(), evaluator_suffix: z.string().optional(), user_signal_window_hours: z.number().int().optional()}); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigGameConfig = z.object({game_id: z.string().optional()}); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigMcmItemRecommendationConfig = z.object({mcm_platform_id: z.string().optional(), inventory_id: z.string().optional()}); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigNextItemSelectionConfig = z.object({require_video: z.boolean().optional(), skip_internal_review: z.boolean().optional()}); export const dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfig = z.object({trend: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigTrendConfig.optional(), model: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigModelConfig.optional(), game: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigGameConfig.optional(), mcm_item_recommendation: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigMcmItemRecommendationConfig.optional(), next_item_selection: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfigNextItemSelectionConfig.optional()}); export const dspCreativeDynamicHtmlSrcPolicyItemCapperFrequency = z.object({period: z.string().optional(), max_exposures: z.number().int().optional()}); export const dspCreativeDynamicHtmlSrcPolicyItemCapper = z.object({frequency: dspCreativeDynamicHtmlSrcPolicyItemCapperFrequency.optional()}); export const dspCreativeDynamicHtmlSrcPolicy = z.object({function: dspCreativeDynamicHtmlSrcPolicyPolicyFunction.optional(), function_config: dspCreativeDynamicHtmlSrcPolicyPolicyFunctionConfig.optional(), num_items: z.number().int().optional(), catalog: z.string().optional(), item_capper: dspCreativeDynamicHtmlSrcPolicyItemCapper.optional()}); export const dspCreativeDynamicHtmlSrc = z.object({template_id: z.string(), template_env: z.record(z.string(), z.string()).optional(), rendered_entry_html: z.string(), html_requirement_attributes: dspCreativeHtmlRequirementAttributes.optional(), html_size_attributes: dspCreativeHtmlSizeAttributes.optional(), policy: dspCreativeDynamicHtmlSrcPolicy.optional()}); export const dspCreativeVideoSrcTranscodingInfoStatus = z.enum(["UNKNOWN_STATUS", "IN_PROGRESS", "SUCCEEDED", "FAILED"]); export const dspCreativeVideoSrcTranscodingInfo = z.object({status: dspCreativeVideoSrcTranscodingInfoStatus.optional(), failed_reason: z.string().optional()}); export const dspCreativeVideoSrc = z.object({video_url: z.string(), filename: z.string().optional(), width: z.number().int(), height: z.number().int(), length_seconds: z.number().int(), fps: z.number().int().optional(), size_in_bytes: z.number().int().optional(), companion_images: z.array(dspCreativeImageSrc).optional(), auto_endcard: z.boolean().optional(), playable_endcard: dspCreativeRichCustomHtmlSrc.optional(), dynamic_html_endcard: dspCreativeDynamicHtmlSrc.optional(), transcoding_info: dspCreativeVideoSrcTranscodingInfo.optional()}); export const dspCreativeVastVideoSrc = z.object({vast_tag: z.string(), width: z.number().int().optional(), height: z.number().int().optional(), length_seconds: z.number().int().optional()}); export const dspCreativeNativeSrc = z.object({icon_image: dspCreativeImageSrc, main_image: dspCreativeImageSrc, shrunk_main_image: dspCreativeImageSrc.optional(), title: z.string(), text: z.string(), cta_text: z.string(), sponsored_by: z.string().optional(), star_rating: z.number().optional(), video: dspCreativeVideoSrc.optional(), vast_video: dspCreativeVastVideoSrc.optional()}); export const dspCreativeCustomHtmlSrc = z.object({html: z.string(), width: z.number().int(), height: z.number().int(), image_url: z.string()}); export const dspCreativeTemplateHtmlSrc = z.object({template_id: z.string(), template_env: z.record(z.string(), z.string()).optional(), rendered_entry_html: z.string(), html_requirement_attributes: dspCreativeHtmlRequirementAttributes.optional(), html_size_attributes: dspCreativeHtmlSizeAttributes.optional(), max_video_duration_seconds: z.number().int().optional()}); export const adcloudcommonCreativeFeatureType = z.enum(["UNKNOWN_CREATIVE_FEATURE_TYPE", "GENERAL", "DYNAMIC_CREATIVE"]); export const dspCreative = z.object({id: z.string().optional(), ad_account_id: z.string().optional(), product_id: z.string().optional(), title: z.string(), enabling_state: commonEnablingState.optional(), advertiser_info: dspAdvertiserInfo.optional(), type: dspCreativeType.optional(), original_filename: z.string(), size_in_bytes: z.number().int().optional(), custom_key_values: z.record(z.string(), z.string()).optional(), image: dspCreativeImageSrc.optional(), video: dspCreativeVideoSrc.optional(), vast_video: dspCreativeVastVideoSrc.optional(), native: dspCreativeNativeSrc.optional(), custom_html: dspCreativeCustomHtmlSrc.optional(), template_html: dspCreativeTemplateHtmlSrc.optional(), rich_custom_html: dspCreativeRichCustomHtmlSrc.optional(), dynamic_html: dspCreativeDynamicHtmlSrc.optional(), feature_type: adcloudcommonCreativeFeatureType.optional(), creator_tag: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const messagesCreateCreativeResponse = z.object({creative: dspCreative.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 Creative. * * POST /cm/v1/creatives */ export const createApiCmV1Creatives = async (args: CreateApiCmV1CreativesArgs, options?: CreateApiCmV1CreativesOptions) => { 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/creatives${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(messagesCreateCreativeResponse, await res.json()) };