Response Handler

In ReqEase, response handlers play a vital role in processing and rendering the responses received from HTTP requests. These handlers are designed to provide a smooth and user-friendly experience when dealing with various types of server responses. Let's dive into the world of response handlers.

Base Response Handler

The foundation of all response handlers in ReqEase is the ResponseHandler class:

export class ResponseHandler {
    static label: string;
    requester: Requester;
    response: Responses.Response | BaseCustomResponse;
    retry: () => void = () => {};

    protected prepare() {}

    constructor(requester: Requester, response: Responses.Response | BaseCustomResponse) {
        this.response = response;
        this.requester = requester;
    }

    public renderResponse() {}
}
  • static label: string: Each response handler has a label to identify its type.

  • requester: Requester: A reference to the Requester instance responsible for the HTTP request.

  • response: Responses.Response | BaseCustomResponse: The received response to be handled.

  • retry: () => void: A function to retry the request if needed.

Message Response Handler

One of the built-in response handlers is the MessageResponseHandler, used for handling messages sent by the server. Here's an overview:

export class MessageResponseHandler extends ResponseHandler {
    static label = "message";
    response: Responses.Message.MessageResponse;
    modalHandler?: ReturnType<<T extends ModalHandler>() => T> | undefined;
    message: ToastMessage | FormMessage = null;
    retry: () => void = () => {};

    prepare() {
        // Prepare the response based on its type
    }

    renderResponse(): void {
        // Render the prepared response
    }
}
  • static label = "message": This label identifies the handler as a message response handler.

  • response: Responses.Message.MessageResponse: This response handler specializes in handling message responses.

  • modalHandler: A reference to the modal handler if the response type requires a modal.

  • message: An instance of a message renderer for rendering messages.

Custom Response Handlers

If your server sends responses in a format not recognized by the built-in handlers, you have the flexibility to create custom response types and their respective handlers. To achieve this:

  1. Create a custom response that extends BaseCustomResponse:

{
    "label": "myCustomResponse",
    //more data
}
  1. Develop a custom response handler with a label matching your custom response.

  2. Register your custom response handlers within the Requester configuration using the responseHandlersToRegister option.

{
    //other options
    requester: {
        //other options
        response: {
            responseHandlersToRegister: [MyCustomResponseHandler]
            //other options
        }
    }
}

By following these steps, you can tailor response handling in ReqEase to accommodate unique server responses, ensuring a seamless user experience in your web application. Customization allows you to adapt to the specific requirements of your project and extend ReqEase's capabilities as needed.

Last updated