import {z} from 'zod' export type DependentFieldValue = {dynamicFeedId?: unknown | undefined, elementId?: unknown | undefined, fieldId?: number | undefined}; export type RequestValue = {excludeFromUserAttributeIds?: Array | undefined, key?: string | undefined, userAttributeIds?: Array | undefined}; export type FieldFilter = {boolValue?: boolean | undefined, dependentFieldValue?: DependentFieldValue | undefined, fieldId?: number | undefined, matchType?: ('LHS_MATCH_TYPE_UNKNOWN' | 'EQUALS_OR_UNRESTRICTED' | 'EQUALS' | 'UNRESTRICTED' | 'NOT_EQUALS') | undefined, requestValue?: RequestValue | undefined, stringValue?: string | undefined, valueType?: ('RHS_VALUE_TYPE_UNKNOWN' | 'STRING' | 'REQUEST' | 'BOOL' | 'DEPENDENT') | undefined}; export type RuleBlock = {fieldFilter?: Array | undefined}; export type CustomRule = {name?: string | undefined, priority?: number | undefined, ruleBlocks?: Array | undefined}; export type CustomValueField = {fieldId?: number | undefined, requestKey?: string | undefined}; export type ProximityFilter = {fieldId?: number | undefined, radiusBucketType?: ('RADIUS_BUCKET_TYPE_UNKNOWN' | 'SMALL' | 'MEDIUM' | 'LARGE' | 'MULTI_REGIONAL' | 'NATIONAL') | undefined, radiusUnitType?: ('RADIUS_UNIT_TYPE_UNKNOWN' | 'KILOMETERS' | 'MILES') | undefined, radiusValue?: number | undefined}; export type RemarketingValueAttribute = {fieldId?: number | undefined, userAttributeIds?: Array | undefined}; export type DynamicRules = {autoTargetedFieldIds?: Array | undefined, customRules?: Array | undefined, customValueFields?: Array | undefined, proximityFilter?: ProximityFilter | undefined, remarketingValueAttributes?: Array | undefined, rotationType?: ('ROTATION_TYPE_UNKNOWN' | 'RANDOM' | 'OPTIMIZED' | 'WEIGHTED') | undefined, ruleType?: ('RULE_SET_TYPE_UNKNOWN' | 'OPEN' | 'AUTO' | 'CUSTOM' | 'PROXIMITY_TARGETING') | undefined, weightFieldId?: number | undefined}; export type DynamicProfileFeedSettings = {dynamicFeedId?: unknown | undefined, dynamicRules?: DynamicRules | undefined, quantity?: number | undefined}; export type DynamicProfileVersion = {dynamicProfileFeedSettings?: Array | undefined, versionId?: unknown | undefined}; export type LastModifiedInfo = {time?: unknown | undefined}; export type DynamicProfile = {active?: DynamicProfileVersion | undefined, archiveStatus?: ('ARCHIVE_STATUS_UNKNOWN' | 'UNARCHIVED' | 'ARCHIVED') | undefined, createInfo?: LastModifiedInfo | undefined, description?: string | undefined, draft?: DynamicProfileVersion | undefined, dynamicProfileId?: unknown | undefined, kind?: string | undefined, lastModifiedInfo?: LastModifiedInfo | undefined, name?: string | undefined, status?: ('STATUS_UNKNOWN' | 'ACTIVE' | 'INACTIVE' | 'DELETED') | undefined, studioAdvertiserId?: unknown | undefined}; export type CreateApiStudioDynamicProfilesArgs = {body: DynamicProfile}; export type CreateApiStudioDynamicProfilesOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const dependentFieldValue = z.object({dynamicFeedId: z.string().optional(), elementId: z.string().optional(), fieldId: z.number().int().optional()}); export const requestValue = z.object({excludeFromUserAttributeIds: z.array(z.string()).optional(), key: z.string().optional(), userAttributeIds: z.array(z.string()).optional()}); export const fieldFilter = z.object({boolValue: z.boolean().optional(), dependentFieldValue: dependentFieldValue.optional(), fieldId: z.number().int().optional(), matchType: z.enum(["LHS_MATCH_TYPE_UNKNOWN", "EQUALS_OR_UNRESTRICTED", "EQUALS", "UNRESTRICTED", "NOT_EQUALS"]).optional(), requestValue: requestValue.optional(), stringValue: z.string().optional(), valueType: z.enum(["RHS_VALUE_TYPE_UNKNOWN", "STRING", "REQUEST", "BOOL", "DEPENDENT"]).optional()}); export const ruleBlock = z.object({fieldFilter: z.array(fieldFilter).optional()}); export const customRule = z.object({name: z.string().optional(), priority: z.number().int().optional(), ruleBlocks: z.array(ruleBlock).optional()}); export const customValueField = z.object({fieldId: z.number().int().optional(), requestKey: z.string().optional()}); export const proximityFilter = z.object({fieldId: z.number().int().optional(), radiusBucketType: z.enum(["RADIUS_BUCKET_TYPE_UNKNOWN", "SMALL", "MEDIUM", "LARGE", "MULTI_REGIONAL", "NATIONAL"]).optional(), radiusUnitType: z.enum(["RADIUS_UNIT_TYPE_UNKNOWN", "KILOMETERS", "MILES"]).optional(), radiusValue: z.number().int().optional()}); export const remarketingValueAttribute = z.object({fieldId: z.number().int().optional(), userAttributeIds: z.array(z.string()).optional()}); export const dynamicRules = z.object({autoTargetedFieldIds: z.array(z.number().int()).optional(), customRules: z.array(customRule).optional(), customValueFields: z.array(customValueField).optional(), proximityFilter: proximityFilter.optional(), remarketingValueAttributes: z.array(remarketingValueAttribute).optional(), rotationType: z.enum(["ROTATION_TYPE_UNKNOWN", "RANDOM", "OPTIMIZED", "WEIGHTED"]).optional(), ruleType: z.enum(["RULE_SET_TYPE_UNKNOWN", "OPEN", "AUTO", "CUSTOM", "PROXIMITY_TARGETING"]).optional(), weightFieldId: z.number().int().optional()}); export const dynamicProfileFeedSettings = z.object({dynamicFeedId: z.string().optional(), dynamicRules: dynamicRules.optional(), quantity: z.number().int().optional()}); export const dynamicProfileVersion = z.object({dynamicProfileFeedSettings: z.array(dynamicProfileFeedSettings).optional(), versionId: z.string().optional()}); export const lastModifiedInfo = z.object({time: z.string().optional()}); export const dynamicProfile = z.object({active: dynamicProfileVersion.optional(), archiveStatus: z.enum(["ARCHIVE_STATUS_UNKNOWN", "UNARCHIVED", "ARCHIVED"]).optional(), createInfo: lastModifiedInfo.optional(), description: z.string().optional(), draft: dynamicProfileVersion.optional(), dynamicProfileId: z.string().optional(), kind: z.string().optional(), lastModifiedInfo: lastModifiedInfo.optional(), name: z.string().optional(), status: z.enum(["STATUS_UNKNOWN", "ACTIVE", "INACTIVE", "DELETED"]).optional(), studioAdvertiserId: 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 }; /** * Inserts a new dynamic profile. * * POST /studio/dynamicProfiles */ export const createApiStudioDynamicProfiles = async (args: CreateApiStudioDynamicProfilesArgs, options?: CreateApiStudioDynamicProfilesOptions) => { const {baseUrl = "https://dfareporting.googleapis.com/dfareporting/v5", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/studio/dynamicProfiles${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(dynamicProfile, await res.json()) };