import {z} from 'zod' export type TransformationLanguage = 'ATTRIBUTE_MAPPER' | 'JSONATA' | 'JQ' | 'CEL' | 'SQL' | 'TEMPLATE' | 'CUSTOM'; export type ResourceDescriptorId = string; export type GetApiTransformationsArgs = {page?: number | undefined, size?: number | undefined, language?: TransformationLanguage | undefined, sourceDescriptorId?: ResourceDescriptorId | undefined, targetDescriptorId?: ResourceDescriptorId | undefined}; export type GetApiTransformationsOptions = {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 page = z.object({limit: z.number().int().gte(0), offset: z.number().int().gte(0), page: z.number().int().gte(0), size: z.number().int().gte(0), total: z.number().int().gte(0), totalPages: z.number().int().gte(0), hasMore: z.boolean()}); export const transformationPage = z.object({data: z.array(transformationDefinition), pagination: page}); 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 }; /** * List transformations * * GET /transformations */ export const getApiTransformations = async (args: GetApiTransformationsArgs, options?: GetApiTransformationsOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.language != null) query.append("language", String(args.language)) if (args.sourceDescriptorId != null) query.append("sourceDescriptorId", String(args.sourceDescriptorId)) if (args.targetDescriptorId != null) query.append("targetDescriptorId", String(args.targetDescriptorId)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/transformations${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(transformationPage, await res.json()) };