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

react-healthy

v2.0.0

Published

Incrementally check and alert your users to the health of your APIs

Downloads

711

Readme

Build status Build size Open Issues

🩺🚀 React Healthy v2.0 🚀🩺

About

react-healthy provides multiple ways of alerting your users to potential API issues that might be affecting their experience in your application.


Installation

You can install the react-healthy package through any node package manager.

npm i react-healthy

or

yarn add react-healthy

Usage

react-healthy comes with two pre-built components that you can utilize in your application. If you'd like to use your own components, you can utilize the useHealthCheck hook to manage the logic for you.

💡 All components and hooks might take in specific types that will be referenced throughout this documentation. These types can be found in the src/types.ts file or at the bottom of this README

Healthy

The <Healthy /> component is a banner that will display at the top of your application, alerting your users to any issues with your APIs.

<Healthy /> takes in a HealthyConfig object and handles the rest.

Basic Usage

Simply import the <Healthy /> component in your main container (your Root file or wherever you are utilizing ReactDOM.render), and pass in the config object.

// other imports
import App from './App'
import {Healthy, HealthyConfig} from 'react-healthy'

const healthyConfig: HealthyConfig = {
  apis: [
    {
      name: 'Good API',
      endpoint: 'https://httpstat.us/200',
    },
    {
      name: 'Bad API',
      endpoint: 'https://httpstat.us/404',
    },
  ],
}

ReactDOM.render(
  <>
    <Healthy config={healthyConfig} />
    <App />
  </>,
  document.getElementById('root')
)

StatusPage

The <StatusPage /> is a page level component that displays a list of your APIs and their statuses.

The <StatusPage /> component takes in a StatusPageConfig object and handles the rest.

Basic Usage

The <StatusPage /> component can be used anywhere, including as a main route's component. However you'd like!

// other imports
import {StatusPage, StatusPageConfig} from 'react-healthy'

export const MyApp = () => {
  const statusPageConfig: StatusPageConfig = {
    apis: [
      {
        name: 'Good API',
        endpoint: 'https://httpstat.us/200',
      },
      {
        name: 'Bad API',
        endpoint: 'https://httpstat.us/404',
      },
    ],
  }

  return (
    <>
      <StatusPage config={statusPageConfig} />>
    </>
  )
}

useHealthCheck

If you want to use your own components or handle the errors in your own way, you can use the useHealthCheck hook. The hook takes in a HealthCheckConfig similar to the <Healthy /> and <StatusPage /> components. Two values are returned from this hook; pageHasErrors and apisWithErrors.

Basic Usage

// other imports
import App from './App'
import {useHealthCheck, HealthCheckConfig} from 'react-healthy'

const healthCheckConfig: HealthCheckConfig = {
  apis: [
    {
      name: 'Good API',
      endpoint: 'https://httpstat.us/200',
    },
    {
      name: 'Bad API',
      endpoint: 'https://httpstat.us/404',
    },
  ],
}

const MyApp = () => {
  const {pageHasErrors, apisWithErrors} = useHealthCheck(healthCheckConfig)

  return (
    <>
      {pageHasErrors ? <div>Errors!</div> : <div>No Errors!</div>}
      {apisWithErrors.map(api => (
        <div>{api.name} has errors!</div>
      ))}
    </>
  )
}

Docs Table of Contents


Types

Api

interface Api {
  /** Name of the API - to be displayed in the banner */
  name: string
  /** Endpoint of the API to ping */
  endpoint: string
}

ApiResponse

interface ApiResponse {
  /* The API that sent the response */
  api: Api
  /* The response object */
  response?: {
    /* The status code */
    status: number
    /* The status text */
    statusText: string
  }
}

HealthyConfig

type HealthyConfig = {
  /* An array of APIs to check */
  apis: Api[]
  /** A callback that's called after the component handles the error, for additional error handling */
  onError?: (api: ApiResponse) => void
  /** The interval at which to call the APIs in milliseconds; default is 30 seconds (30000) */
  interval?: number
  /** CSS class names to assign to the banner, banner content, and close button */
  classes?: {
    banner?: string
    content?: string
    closeButton?: string
  }
  /** Messages for the banner */
  messages?: BannerMessages
  /** Whether or not to show a close icon - default is false */
  closeable?: boolean
}

StatusPageConfig

type StatusPageConfig = {
  /* An array of APIs to check */
  apis: Api[]
  /** The interval at which to call the APIs in milliseconds; default is 30 seconds (30000) */
  interval?: number
  /** Messages for the banner and row */
  messages?: Pick<Messages, 'row' | 'statusPage'>
  /** A callback that's called after the component handles the error, for additional error handling */
  onError?: (api: ApiResponse) => void
}

HealthCheckConfig

type HealthCheckConfig = {
  /* An array of APIs to check */
  apis: Api[]
  /** The interval at which to call the APIs in milliseconds; default is 30 seconds (30000) */
  interval?: number
  /** A callback that's called after the component handles the error, for additional error handling */
  onError?: (api: ApiResponse) => void
}