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

ui-result-handler

v1.0.3

Published

result handler interface for front-end

Downloads

2

Readme

ui-result-handler

ui-result-handler is a package providing utilities for handling results in front-end applications. It offers a set of types and interfaces to represent various types of results, such as successes, errors, warnings, and information, and provides metadata for additional context.

Installation

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

npm install ui-result-handler

Usage

// Import the necessary types from the package
import { Result, ResultType, Metadata } from 'ui-result-handler';

interface User {
    name: string;
    email: string;
}

interface Login {
    user: User;
    token: string;
}

// Define a sample result
const response: Result<Login> = await fetch('https://api.example.com/users/1');
console.log(response);

// {
//   isSuccess: true,
//   resultType: 'success',
//   statusCode: 200,
//   message: 'Operation successful',
//   value: {
//     user: {
//       name: 'example',
//       email: '[email protected]'
//     },
//     token: 'example'
//   },
//   metadata: {
//     totalItems: 1
//   }
// };

// Show a notification based on the result type
function genericShowNotification(result: Result<any>): void {
    // Logic to show notification based on the result type
}

function specificShowNotification(severity: ResultType, message: string): void {
    // Logic to show notification with specific severity and message
}

// Call genericShowNotification with the response
genericShowNotification(response);

// Call specificShowNotification with a specific result type and message
specificShowNotification(ResultType.success, 'notification example message');

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", "warn", "info").
  • 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.

A ResultType enum is also provided, which includes the following values:

  • success: Indicates a successful operation.
  • error: Indicates an error occurred during the operation.
  • warn: Indicates a warning or non-fatal issue occurred during the operation.
  • info: Indicates an informational message about the operation.

Features

  1. Type Inference from Back-End: Automatically retrieves the result type from the back-end to display information to the user. This ensures seamless integration between the back-end and front-end, allowing for consistent handling and display of result data.
  2. Export of ResultType Enum: Exports the ResultType enum, allowing for easy integration with notification tools whose types correspond to those of the result. This simplifies the process of displaying notifications based on the outcome of API requests.
  3. Rendering Helpers: Provides utility functions or components for rendering results in your front-end application, allowing for consistent and user-friendly display of result information to users.
  4. Centralized Error Handling: Offers functionalities or components to centrally manage error handling in your front-end application, ensuring consistent error display throughout the application, such as a global alert component that automatically displays errors when they occur.
  5. Integration with Third-Party Notification Libraries: Integrates with popular third-party notification libraries like Toastify or SweetAlert, facilitating the display of notifications to users based on the type of result returned by the API.
  6. Result Validation: Provides validation functions to check if a given result meets certain expectations or criteria, such as verifying if a result is a success, an error, or a warning, and taking appropriate action in your front-end application.
  7. Loading State Management: Adds features to manage loading states during API calls, providing visual indicators to inform users that operations are in progress, and hiding or disabling features during loading.
  8. Message Customization: Allows customization of result messages to fit specific needs of your front-end application, such as options for localization into different languages or customization to match your application's branding.

API Package

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

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

npm install uapi-result-handler