npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-result-handler

v1.0.3

Published

result handler for api response

Downloads

11

Readme

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