@webscopeio/react-health-check

v2.1.5
Lightweight React hook for checking health of API services.
react health check hook detect offline api

React Health Check ๐Ÿฅ

Lightweight React hook for checking health of API services.

stable tslib checks license


Installation ๐Ÿง‘โ€๐Ÿ”ง

npm i @webscopeio/react-health-check

or

yarn add @webscopeio/react-health-check

Examples ๐Ÿ˜ฒ

Usage โ“

const { available, refresh } = useHealthCheck({
  service: {
    name: 'auth',
    url: 'https://example.com/auth/health',
  },
  onSuccess: ({ service, timestamp }) => {
    console.log(`Service "${service.name}" is available since "${timestamp}" ๐ŸŽ‰`);
  },
  onError: ({ service, timestamp }) => {
    console.log(`Service "${service.name}" is not available since "${timestamp}" ๐Ÿ˜”`);
  },
});

You can also create a global configuration so you don't have to define services and callbacks every time:

// App wrapper
<HealthCheckConfig
  value={{
    services: [
      {
        name: 'auth',
        url: 'https://example.com/auth/health',
      },
      {
        name: 'payment',
        url: 'https://example.com/payment/health',
      },
    ],
    onSuccess: ({ service, timestamp }) => {
      console.log(`Service "${service.name}" is available since "${timestamp}" ๐ŸŽ‰`);
    },
    onError: ({ service, timestamp }) => {
      console.log(`Service "${service.name}" is not available since "${timestamp}" ๐Ÿ˜”`);
    },
  }}
>
  <App />
</HealthCheckConfig>;

// Later in some child component
const { available } = useHealthCheck('auth');

Configuration ๐Ÿ› 

useHealthCheck() hook accepts a configuration object with keys:

Key Type Description
service Service<S = string> Object defining an API service to be checked.
onSuccess (state: ServiceState<S>) => void; Callback which should be called when API service becomes available again.
onError (state: ServiceState<S>) => void; Callback which should be called when API service becomes unavailable.
refreshInterval number Polling interval for health checks in milliseconds.
Default value: 5000
refreshWhileHidden boolean Determines whether polling should be paused while browser window isn't visible.
Default value: false

Global configuration accepts the same keys as useHealthCheck() hook with the exception of "service". You need to specify array of "services" when using global configuration.

License ๐Ÿ’ผ

MIT | Developed by Webscope.io

npm i @webscopeio/[email protected]

Metadata

  • MIT
  • Whatever
  • Jozef Hruลกka
  • released 9/3/2020

Downloads