> For the complete documentation index, see [llms.txt](https://hichemtech.gitbook.io/reqease-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hichemtech.gitbook.io/reqease-docs/response-handler.md).

# 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:

```typescript
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:

```typescript
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`:

```json
{
    "label": "myCustomResponse",
    //more data
}
```

2. Develop a custom response handler with a label matching your custom response.
3. Register your custom response handlers within the Requester configuration using the `responseHandlersToRegister` option.

```javascript
{
    //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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hichemtech.gitbook.io/reqease-docs/response-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
