import {z} from 'zod' export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type OnlyId = {id?: string | undefined}; export type CustomAttribute = {attributeDefinitionId?: string | undefined, booleanValue?: boolean | undefined, dateValue?: number | undefined, entityId?: string | undefined, entityReferences?: Array | undefined, numberValue?: unknown | undefined, selectedValueId?: string | undefined, selectedValues?: Array | undefined, stringValue?: string | undefined}; export type Campaign = {id?: string | undefined, version?: string | undefined, campaignEndDate?: number | undefined, campaignName: string, campaignNumber?: string | undefined, campaignStartDate?: number | undefined, campaignType?: ('ADVERTISEMENT' | 'BULKMAIL' | 'EMAIL' | 'EVENT' | 'EXPOSITION' | 'FAIR' | 'OTHER' | 'PUBLICRELATION' | 'TELEMARKETING' | 'WEBINAR') | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, description?: string | undefined, lastModifiedDate?: number | undefined, responsibleUserId?: string | undefined}; export type UpdateApiCampaignIdIdArgs = {id: string, dryRun?: boolean | undefined, body: Campaign}; export type UpdateApiCampaignIdIdOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const onlyId = z.object({id: z.string().optional()}); export const customAttribute = z.object({attributeDefinitionId: z.string().optional(), booleanValue: z.boolean().optional(), dateValue: z.number().int().optional(), entityId: z.string().optional(), entityReferences: z.array(entityReference).optional(), numberValue: z.string().optional(), selectedValueId: z.string().optional(), selectedValues: z.array(onlyId).optional(), stringValue: z.string().optional()}); export const campaign = z.object({id: z.string().optional(), version: z.string().optional(), campaignEndDate: z.number().int().optional(), campaignName: z.string(), campaignNumber: z.string().optional(), campaignStartDate: z.number().int().optional(), campaignType: z.enum(["ADVERTISEMENT", "BULKMAIL", "EMAIL", "EVENT", "EXPOSITION", "FAIR", "OTHER", "PUBLICRELATION", "TELEMARKETING", "WEBINAR"]).optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), description: z.string().optional(), lastModifiedDate: z.number().int().optional(), responsibleUserId: 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 }; /** * update campaign * * PUT /campaign/id/{id} */ export const updateApiCampaignIdId = async (args: UpdateApiCampaignIdIdArgs, options: UpdateApiCampaignIdIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/campaign/id/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(campaign, await res.json()) };