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 | import { Effect, Predicate, Schema, SchemaGetter, SchemaIssue } from 'effect'; |
| 2 | |
| 3 | const errorMessage = (error: unknown): string => |
| 4 | Predicate.isError(error) ? error.message : String(error); |
| 5 | |
| 6 | export function JSONTransformSchema<To extends Schema.Top>(to: To) { |
| 7 | return Schema.String.pipe( |
| 8 | Schema.decodeTo(to, { |
| 9 | decode: SchemaGetter.transformEffect((str: string) => |
| 10 | Effect.try({ |
| 11 | try: (): To['Encoded'] => JSON.parse(str), |
| 12 | catch: error => new SchemaIssue.InvalidValue({ message: errorMessage(error) }, str), |
| 13 | }) |
| 14 | ), |
| 15 | encode: SchemaGetter.transformEffect((obj: To['Encoded']) => |
| 16 | Effect.try({ |
| 17 | try: () => JSON.stringify(obj), |
| 18 | catch: error => new SchemaIssue.InvalidValue({ message: errorMessage(error) }, obj), |
| 19 | }) |
| 20 | ), |
| 21 | }) |
| 22 | ); |
| 23 | } |