Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

1.3 KB
Raw Blame
/**
 * Function to transform the response from the API to the expected format
 * This takes in the zod schema to be transformed into and the response from the API
 *
 * Users need to manually map the fields to the expected format
 *
 * @example
 * ```ts
 * transform(response)
 *   .with(ConnectedAccountRetrieveResponseSchema)
 *   .using((raw) => ({ ... }))
 * ```
 */
import { ZodTypeAny, z } from 'zod/v3';
import logger from './logger';

export function transform<RawInput>(raw: RawInput) {
  return {
    with<Schema extends ZodTypeAny>(schema: Schema) {
      return {
        using(
          transformer: (input: RawInput) => z.infer<Schema>,
          options?: { label?: string }
        ): z.infer<Schema> {
          const transformed = transformer(raw);
          const result = schema.safeParse(transformed);

          if (!result.success) {
            const label = options?.label ? ` for ${options.label}` : '';
            const issues = result.error.issues
              .map(issue => `  - ${issue.path.join('.') || 'parameter'}: ${issue.message}`)
              .join('\n');
            logger.warn(`Transform validation failed${label}:\n${issues}`);
            return transformed;
          }

          return result.data;
        },
      };
    },
  };
}