Simplifying Request Handling and Error Management in Effect Typescript

I need to model some requests to start migrating a legacy application written in a different stack.
- Every request must adhere to a specific structure: it must have an action field in the body and other optional fields
- the response is always a JSON and it is successful if the response has the property success set to true, otherwise it is a failure
- both the success the failure are specific to the request

I'm modelling them with a Schema.TaggedRequest, like this:
export class MyRequest extends Schema.TaggedRequest<MyRequest>()(
    "MyRequest",
    {
        payload: {
            action: pipe(
                Schema.tag("my_request"),
                Schema.withConstructorDefault(() => "my_request" as const),
            ),
        },
        success: Schema.Void,
        failure: Schema.Never,
    },
) {}

Now I created a custom client (i'll add the code in the response as this thread) with a send method that receives the request and handles most of the transformation and based on the request passed I get type safety.

I have using a single method to send multiple request, I always have to add the request to Schema.Union and this leads to a bit of boilerplate that needs to be manually updated.

Is it possible to simplify the code without rely on the multiple Schema.Unions? Optionally, can I simplify how the failure is handled, maybe directly in the client if success is false?
Was this page helpful?