import {z} from 'zod' export type TransformAndValidateRequest = {content: {messageType: string, content: {[key: string]: unknown}}, source?: string | undefined, swiftVersion?: ('2025' | '2026') | undefined, validateBic?: boolean | undefined}; export type CreateApiWebapiTransformAndValidateArgs = {body: TransformAndValidateRequest}; export type CreateApiWebapiTransformAndValidateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transformAndValidateResponse = z.object({errorOccurred: z.boolean().optional(), isValid: z.boolean().optional(), error: z.string().nullable().optional(), transformationResult: z.object({Result: z.string().optional(), errorOccurred: z.boolean().optional()}).optional(), validationResult: z.object({isValid: z.boolean().optional(), errorOccurred: z.boolean().optional()}).optional(), swiftMessage: 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 }; /** * Transform and validate an outbound Swift message * * POST /webapi/transform-and-validate */ export const createApiWebapiTransformAndValidate = async (args: CreateApiWebapiTransformAndValidateArgs, options?: CreateApiWebapiTransformAndValidateOptions) => { const {baseUrl = "https://your-environment.appiancloud.com/suite", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("appian-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webapi/transform-and-validate${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(transformAndValidateResponse, await res.json()) };