An open API service indexing awesome lists of open source software.

https://github.com/CERN/react-openapi-generator-hook

OpenApi Generator provides an implementation of the functions and the model necessary to call HTTP endpoints. This repository aims to streamline the configuration of the tool and provides a hook to perform HTTP calls and handle responses efficently.
https://github.com/CERN/react-openapi-generator-hook

Last synced: about 1 month ago
JSON representation

OpenApi Generator provides an implementation of the functions and the model necessary to call HTTP endpoints. This repository aims to streamline the configuration of the tool and provides a hook to perform HTTP calls and handle responses efficently.

Awesome Lists containing this project

README

          

# `react-openapi-generator-hook`

A lightweight React hook utility for calling API methods generated from OpenAPI specifications. Simplify your frontend API integration by using a consistent, declarative hook-based approach โ€” without manually instantiating and managing API clients.

## ๐Ÿš€ Features

* โš›๏ธ React-first API interaction with minimal boilerplate
* ๐Ÿ”„ Built-in support for async states: `loading`, `data`, and `error`
* โš™๏ธ Support for multiple API configurations and endpoints
* โœ… Compatible with OpenAPI Generator's generated `*ApiFactory` functions

## ๐Ÿงฉ Installation

```bash
npm install react-openapi-generator-hook
```

## ๐Ÿ› ๏ธ Basic Usage

```tsx
import { useApi, OpenApiProvider } from 'react-openapi-generator-hook'
import { PetApiFactory, Configuration } from './generated'
import axios from 'axios'

// Setup config
const axiosInstance = axios.create({ withCredentials: true }) // custom headers here
const configMap = {
PETS: {
axiosInstance,
configuration: new Configuration({ accessToken: '1234567890' }),
baseUrl: 'https://petstore3.swagger.io/api/v3',
},
USERS: {
axiosInstance,
configuration: new Configuration({ accessToken: '0123456789' }),
baseUrl: 'https://users.cern.ch/api/v3',
}
}

// Wrap your app or your components

```
Within your component
```tsx
// E.g. PetComponent.tsx
const [{ data, error, loading }, fetchPet] = useApi({
apiFactory: PetApiFactory,
methodName: 'getPetById',
requestParameters: 4,
})

return (


{loading && 'Loading...'}
{error &&
Error: {error.message}
}
{data &&
Pet name: {data.name}
}

)
```

## ๐Ÿ“ฆ API

### `useApi`

A custom hook for invoking a specific API method from an OpenAPI-generated factory.

**Parameters:**

```ts
{
apiFactory: (config: Configuration) => any, // The API factory to use
methodName: string, // Name of the method to invoke
requestParameters?: any, // Parameters to pass to the method
},
{
manual?: boolean // if true, does not send request on component mount
configurationId?: string // (Optional) Configuration key to use
}
```

**Returns:**

```ts
[
{ data, error, loading }, // Result state
refetch // Function to manually re-trigger the call
]
```

### `OpenApiProvider`

A context provider that supplies configuration for all `useApi` calls.

**Props:**

* `defaultConfigurationId: string` โ€“ (optional) the default API to use
* `openApiConfigurationMap: Record` โ€“ a map of configurations:

* `axiosInstance`: Axios instance to use
* `configuration`: OpenAPI `Configuration` object (with accessToken, etc.)
* `baseUrl`: Base URL of the API

You can also extend this setup to dynamically refresh tokens or inject headers as needed via `axios`.

## ๐Ÿงช Example Project

A guided example project showing how to use the hook is available here:
๐Ÿ‘‰ [react-openapi-generator-hook-demo](./examples/demo)

## ๐Ÿงฑ Built With

* [React](https://reactjs.org)
* [Axios](https://axios-http.com)
* [OpenAPI Generator](https://openapi-generator.tech/)

## ๐Ÿ“ƒ License

MIT ยฉ CERN