1.1.0 • Published 9 months ago

make-response v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

JSON Response Structures in API Communication

JSON response structures are vital components in diverse API scenarios, serving as effective conduits for communication between client applications and servers. They enable streamlined information exchange and enhance the user experience.

Standard Response Format

The Standard Response format provides a reliable means of communicating the results of API requests. It consistently conveys the success status of the request and allows for the inclusion of optional data payloads. This format ensures a common understanding of API interactions.

Examples of JSON Response Structures

  1. Standard Response:

    {
        "status": "success",
        "message": "Request was successful.",
        "data": {
            // Actual response data goes here
        }
    }
  2. Error Response:

    {
        "status": "error",
        "message": "An error occurred while processing the request.",
        "error_code": "123",
        "data": {
            // Additional error details
        }
    }
  3. Pagination Response:

    {
        "status": "success",
        "message": "Request was successful.",
        "data": {
            "current_page": 1,
            "total_pages": 10,
            "items": [
                // List of items on this page
            ]
        }
    }

These examples showcase the versatility of JSON response structures, illustrating how they can effectively convey different types of information and outcomes in API interactions.

Example Usage

const { standard_response, error_response } = require('make-response');

// You can now use the standard_response and error_response functions to generate response objects.
// Example:
const successResponse = standard_response("Request was successful.", { /* Actual response data */ });
const errorResponse = error_response("An error occurred while processing the request.", "123", { /* Additional error details */ });

These examples showcase the versatility of JSON response structures, illustrating how they can effectively convey different types of information and outcomes in API interactions.