api-result-handler

1.0.3 • Public • Published

api-result-handler

api-result-handler is a Node.js package providing utilities for handling results in your back-end applications. It offers a series of classes and functions to facilitate the manipulation of successes, errors, and other types of results in your Node.js APIs.

Installation

To install api-result-handler, you can use npm:

npm install api-result-handler

Utilisation

// Import the necessary classes from the package
const { SuccessResult, OkResult, ErrorResult, BadRequestResult } = require('result-handler-utils');

// Create a new generic success
const success = new SuccessResult(200, 'Operation successful', { data: 'example' });

// Creat a specific success
const ok = new OkResult('Operation successful', { data: 'example' }, { metadata: 'example' });

// Create a bad request error
const error = new BadRequestResult('The request is malformed');

// Use the results in your application
console.log(success);
console.log(ok);
console.log(error);

Description

  1. result class:
    • Result: A generic class representing a result.
    • ResultBuilder: builder class to create a result.
  2. success classes::
    • SuccessResult: A class representing a generic success result.
    • SuccessResultBuilder: builder class to create a success result.
    • OkResult: A class representing a specific success result with code 200.
    • CreatedResult: A class representing a success result for a resource creation operation.
    • NoContentResult: A class representing a success result for a request that has no content to return.
    • And all other HTTP status code success results.
  3. error classes:
    • ErrorResult: A class representing a generic error result.
    • ErrorResultBuilder: builder class to create an error result.
    • BadRequestResult: A class representing a specific error result for a bad request.
    • UnauthorizedResult: A class representing a specific error result for an unauthorized request.
    • NotFoundResult: A class representing a specific error result for a resource not found.
    • ConflictResult: A class representing a specific error result for a conflict in the request.
    • IMATeapotResult: A class representing a specific error result for a request to brew coffee.
    • InternalServerErrorResult: A class representing a specific error result for an internal server error.
    • NotImplementedResult: A class representing a specific error result for a not implemented request.
    • And all other HTTP status code error results.
  4. info result:
    • InfoResult: A class representing an informational result.
    • InfoResultBuilder: builder class to create an info result.
  5. warning result:
    • WarningResult: A class representing a warning result.
    • WarningResultBuilder: builder class to create a warning result.

Result Object Structure

A Result object has the following structure:

  • isSuccess: A boolean indicating whether the operation succeeded or failed.
  • resultType: The type of result (e.g., "success", "error", etc.).
  • statusCode: The HTTP status code associated with the result.
  • message: A descriptive message explaining the result.
  • data (optional): The data associated with the result.
  • metadata (optional): Additional metadata associated with the result.

Features

  1. Typed Result Provision: The api-result-handler API package provides classes and interfaces for creating typed results, ensuring consistency in the structure and manipulation of results returned by your API.
  2. Facilitation of Front-Back Communication: By exposing clear result types and enumerations such as ResultType, the API package facilitates communication between the back-end and the front-end, enabling smooth integration and efficient result handling.
  3. Consistent Result Management: The package provides classes like SuccessResult, ErrorResult, etc., to facilitate consistent management of various types of results (successes, errors, warnings, etc.) in your API. This ensures a standardized and predictable response to each request.
  4. Customization of Messages and Metadata: Result classes allow customization of messages and metadata associated with each result. This enables your API to provide informative and contextual responses to clients, enhancing the user experience.
  5. Centralized Error Handling: By using the error classes provided by the package, your API can centralize error handling, ensuring an appropriate response to each error and providing useful details for debugging and issue resolution.
  6. Extension and Customization: The package is designed to be extensible and customizable according to the specific needs of your application. You can extend existing features or add new result classes to address specific use cases.
  7. Comprehensive Documentation: The package comes with comprehensive documentation describing each class, interface, and enumeration available, along with usage examples to assist developers in integrating and using the package effectively.

Front-End Package

To complement this package, a front-end package named ui-result-handler is also available. It provides data models to define the return types of requests in the front-end.

You can find more information about ui-result-handler in its README. and to install ui-result-handler, you can use npm:

npm install ui-result-handler

Package Sidebar

Install

npm i api-result-handler

Weekly Downloads

14

Version

1.0.3

License

ISC

Unpacked Size

59 kB

Total Files

17

Last publish

Collaborators

  • neruso