import {z} from 'zod' export type TransformationId = string; export type GetApiTransformationsTransformationIdArgs = {transformationId: TransformationId}; export type GetApiTransformationsTransformationIdOptions = {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 }; /** * Get a transformation * * GET /transformations/{transformationId} */ export const getApiTransformationsTransformationId = async (args: GetApiTransformationsTransformationIdArgs, options?: GetApiTransformationsTransformationIdOptions) => { const {baseUrl = "https://api.example.com/api/connector/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };