1.4.8 • Published 1 year ago
react-simple-api v1.4.8
react-simple-api
Query and cache API data
Install
npm install react-simple-api
or
yarn add react-simple-api
Usage
In the index.tsx
import { ApiContextProvider } from 'react-simple-api'
import { App } from './App
ReactDOM.render(
<React.StrictMode>
<ApiContextProvider>
<App/>
</ApiContextProvider>
</React.StrictMode>,
document.getElementById('root')
)
Query data
import { useApi, APIMethod } from 'react-simple-api'
interface Cat {
type: string
user: string
}
const {
data,
isLoading,
isRetrying
isError,
error
} = useApi<Cat[], {}>({
apiId: 'cat-api',
apiUrl: 'https://cat-fact.herokuapp.com/facts',
method: APIMethod.GET,
retry: 4
})
Types
enum APIMethod {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
PATCH = 'PATCH',
DELETE = 'DELETE',
}
useApi = <TResponse, TData>({
apiId: string // cache identifier,
apiUrl: string,
method: APIMethod,
data?: TData,
headers?: Record<string, string>
cacheExpiry?: number // cache expiry in milliseconds
retry?: number // the number of times to retry before it throws an error
}) => ({
data: TResponse | undefined
isLoading: boolean
isRetrying: boolean
isError: boolean
error: Error | null
})
1.4.8
1 year ago
1.4.7
1 year ago
1.4.6
1 year ago
1.4.5
1 year ago
1.4.4
1 year ago
1.4.3
1 year ago
1.4.2
1 year ago
1.4.1
1 year ago
1.4.0
2 years ago
1.3.2
2 years ago
1.3.1
2 years ago
1.3.0
2 years ago
1.2.12
2 years ago
1.2.11
2 years ago
1.2.10
2 years ago
1.2.9
2 years ago
1.2.8
2 years ago
1.2.7
2 years ago
1.2.6
2 years ago
1.2.5
2 years ago
1.2.4
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.2.0
2 years ago
1.1.8
2 years ago
1.1.7
2 years ago
1.1.6
2 years ago
1.1.5
2 years ago
1.1.4
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago