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-api-client-provider

v1.2.0

Published

A customizable API client for React that uses axios under the hood

Downloads

9

Readme

React API Client Provider

Version

A customizable API client for React that uses axios under the hood. This is useful if you want a centralized client for handling errors and other operations and have it be accessible through a custom hook throughout the react application.

This is not a fancy library, it just encapsulates an instance of an axios client to make it available through the app. The configuration is very customizable as the client exposes axios options directly.

Installation

npm i react-api-client-provider

Usage

Import the APIClientProvider and encapsulate the part of your app that you want to share the API client instance with and pass it the optional default configuration of your choice.

import { APIClientProvider } from "react-api-client-provider";

const MyComponent = () => {
	return (
		<APIClientProvider>
			<App />
		</APIClientProvider>
	);
};

Properties

| Property | Type | Required | Description | | --- | --- | --- | --- | | baseURL | string | false | Set this up to have the client call the same baseURL for every request, if not set, you should provide the full url on each request | | defaultHeaders | {[key: string]: string} | false | If you want the client to pass in default headers in every request | | handleErrors | (err: Error | AxiosError | any) => any | false | For a custom error handling function when an API call fails | | requestInterceptors | (config: AxiosRequestConfig) => AxiosRequestConfig | false | Any request interceptors you want to put in place for client requests (check axios docs) | | responseInterceptors | (config: AxiosResponse) => AxiosResponse | false | Any response interceptors you want to put in place for client responses (check axios docs) | | options | any | false | Any additional custom options that you want to make accessible through the useClient hook's options field |

Custom Hooks

useClient

The useClient hook allows child components to access the client instance and the optional options if setup during initialization. It then allows them to use client methods directly.

import { useClient } from "react-api-client-provider";

const MyComponent = () => {
	const { client } = useClient();

	const handleClick = async () => {
		await client.GET({ url: YOUR_URL });
	};

	return <button onClick={handleClick}>Get data</button>;
};

useAPIClientCallOnLoad

This hook allows you to leverage the hook functionalities to avoid setting the regular data, isLoading, isError states whenever making an API call using useEffect when a component mounts. It generates the states for you, makes the request and returns you the different states.

Please keep in mind that when using this hook, specifying the method is mandatory.

import { useAPIClientCallOnLoad } from "react-api-client-provider";

const MyComponent = () => {
	const { data, isLoading, isError } = useAPIClientCallOnLoad({
		url: YOUR_URL,
		method: "GET",
	});

	// Rest of your code
};

For typescript users, you can specify a generic type on the hook so that your returned data is typed the way you want.

useAPIClientStateCall

This hook allows you to leverage the hook functionalities to avoid setting the regular data, isLoading, isError states yourselves when making a manual API call. The hook will handle state changes by providing you with a method to activate the API call.

Please keep in mind that when using this hook, specifying the method is mandatory.

import { useAPIClientStateCall } from "react-api-client-provider";

const MyComponent = () => {
	const { data, isLoading, isError, makeRequest } = useAPIClientStateCall({
		url: YOUR_URL,
		method: "GET",
	});

	return <button onClick={makeRequest}>Get data</button>;
};

For typescript users, you can specify a generic type on the hook so that your returned data is typed the way you want.

Client shorthand methods

The client has a basic request method but there are shorthands for the different other API request methods, GET, POST, PUT, PATCH, DELETE.