The expFetch method is part of the Http class in the experro-storefront module. It provides an easy way to make HTTP requests with support for SSR (Server-Side Rendering) and customizable headers, request bodies, and methods.
Method Signature
Parameters
The method accepts an object of type ExpHttpRequest with the following fields:
If no headers are provided, the following default headers will be used:
Response
The method returns a Promise that resolves to the response data from the HTTP request.
Usage Examples
Example 1: Basic GET Request
This example demonstrates how to make a simple GET request without enabling SSR.
Example 2: POST Request with Body and SSR Enabled
In this example, we make a POST request with a request body and enable SSR by setting the enableSSR flag to true. The key must be unique to properly enable SSR.
SSR and key
The key is crucial when enabling SSR. It must be unique to ensure that the request is tracked and handled correctly on the server side. You cannot reuse the same
key for different requests when SSR is enabled.
If no custom headers are provided, the request will use the default header { 'content-type': 'application/json' }
Request Body
The body parameter is optional and should only be included for methods like POST, PUT, PATCH, or DELETE.
The expFetch method is a flexible and efficient way to make HTTP requests, with support for SSR, custom headers, and various HTTP methods. By providing an easy-to-use interface, it simplifies API calls in your application, allowing you to focus on the core logic while handling request details seamlessly.