@compass/http

This package provides simplified HTTP request functionalities, allowing you to make requests using different methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS).

Usage

Import it in the action:

import http from "@compass/http";

API Reference

http.get(url: string, options?: RequestConfig)

Sends a GET request to the specified URL.

  • url (required, string): The URL where the request should be sent.

  • options (optional, RequestConfig): The request configuration options.

Returns a Promise that resolves with the response.

http.post(url: string, data?: any, options?: RequestConfig)

Sends a POST request to the specified URL.

  • url (required, string): The URL where the request should be sent.

  • data (optional, any): The data to be sent as the request body.

  • options (optional, RequestConfig): The request configuration options.

Returns a Promise that resolves with the response. RequestConfig

An object that configures the request. It accepts the following properties:

  • url (optional, string): The URL where the request should be sent.

  • method (optional, 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'): The HTTP method for the request.

  • headers (optional, object): Any headers you want to add to your request.

  • params (optional, any): URL parameters to be sent with the request.

  • data (optional, any): The data to be sent as the request body. Used primarily with 'POST', 'PUT' and 'PATCH' requests.

Examples

To send a GET request:

import http from "@compass/http";

export default async function() {
    try {
        const data = await http.get('https://example.com/api/items');
        log(data);

        return {
            items: data,
        }
    } catch (error) {
        log(error);
    }
}

To send a POST request:

import http from "@compass/http";

export default async function() {
    const postData = {
        name: 'John',
        email: 'john@example.com'
    };

    try {
        const data = await http.post('https://example.com/api/users', postData);
        log(data);

        return {
            user: data,
        }
    } catch (error) {
        log(error);
    }
}

Remember that error handling is important to take care of any potential errors that might occur during the request.

Last updated