import {z} from 'zod' export type TransformationId = string; export type TransformationLanguage = 'ATTRIBUTE_MAPPER' | 'JSONATA' | 'JQ' | 'CEL' | 'SQL' | 'TEMPLATE' | 'CUSTOM'; export type ResourceDescriptorId = string; export type StringMap = {[key: string]: string}; export type TransformationStep = {stepType: 'MAP' | 'CONSTANT' | 'DEFAULT' | 'REQUIRE' | 'REDACT' | 'HASH' | 'LOOKUP' | 'CUSTOM', sourcePath?: string | undefined, targetPath?: string | undefined, value?: (string | null) | undefined, expression?: string | undefined, metadata?: StringMap | undefined}; export type TransformationUpdateRequest = {displayName?: string | undefined, description?: string | undefined, language?: TransformationLanguage | undefined, sourceDescriptorId?: ResourceDescriptorId | undefined, targetDescriptorId?: ResourceDescriptorId | undefined, steps?: Array | undefined, expression?: string | undefined, metadata?: StringMap | undefined}; export type PatchApiTransformationsTransformationIdArgs = {transformationId: TransformationId, body: TransformationUpdateRequest}; export type PatchApiTransformationsTransformationIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transformationLanguage = z.enum(["ATTRIBUTE_MAPPER", "JSONATA", "JQ", "CEL", "SQL", "TEMPLATE", "CUSTOM"]); export const resourceDescriptorId = z.string(); export const stringMap = z.record(z.string(), z.string()); export const transformationStep = z.object({stepType: z.enum(["MAP", "CONSTANT", "DEFAULT", "REQUIRE", "REDACT", "HASH", "LOOKUP", "CUSTOM"]), sourcePath: z.string().optional(), targetPath: z.string().optional(), value: z.string().nullable().optional(), expression: z.string().optional(), metadata: stringMap.optional()}); export const transformationId = z.string(); export const transformationDefinition = z.object({displayName: z.string(), description: z.string().optional(), language: transformationLanguage, sourceDescriptorId: resourceDescriptorId.optional(), targetDescriptorId: resourceDescriptorId.optional(), steps: z.array(transformationStep).optional(), expression: z.string().optional(), metadata: stringMap.optional(), transformationId: transformationId, createdAt: z.string(), updatedAt: z.string()}); 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 a transformation * * PATCH /transformations/{transformationId} */ export const patchApiTransformationsTransformationId = async (args: PatchApiTransformationsTransformationIdArgs, options?: PatchApiTransformationsTransformationIdOptions) => { const {baseUrl = "https://api.example.com/api/connector/v1", 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/transformations/${encodeURIComponent(String(args.transformationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(transformationDefinition, await res.json()) };