# @mistenkt/use-rest-api

> A customizable REST Api and data manager hook for your React application

Latest version **1.0.12** (published 2020-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mistenkt/use-rest-api
pnpm add @mistenkt/use-rest-api
yarn add @mistenkt/use-rest-api
bun add @mistenkt/use-rest-api
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2020-07-01 |
| First published | 2020-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mistenkt |
| Maintainers | mistenkt |

## Links

- npm: https://www.npmjs.com/package/@mistenkt/use-rest-api
- Homepage: https://github.com/mistenkt/use-rest-api
- npm.io page: https://npm.io/package/@mistenkt/use-rest-api

## Dependencies (2)

- [redux](https://npm.io/package/redux.md) ^4.0.5
- [react-redux](https://npm.io/package/react-redux.md) ^7.2.0

## Recent versions

- 1.0.12 (latest) — 2020-07-01
- 1.0.11 — 2020-06-22
- 1.0.10 — 2020-06-17
- 1.0.9 — 2020-06-17
- 1.0.8 — 2020-06-10
- 1.0.7 — 2020-06-10
- 1.0.6 — 2020-06-08
- 1.0.5 — 2020-06-08
- 1.0.4 — 2020-06-05
- 1.0.3 — 2020-06-05
- 1.0.2 — 2020-06-05
- 1.0.1 — 2020-06-04
- 1.0.0 — 2020-06-04

## README

# use-rest-api
A customizable REST Api and data manager hook for your React application

## Installation
`yarn add @mistenkt/use-rest-api`

## Usage

1. Initialize the `ApiProvider` in your `App.js` file and provide the required params.

    ````jsx harmony
   import React from 'react';
   import {ApiProvider} from '@mistenkt/use-rest-api';
   
   const apiResources = {
       users: {
           endpoint: '/user'
       },
       posts: {
           endpoint: '/post',
       }
   };
   
   const App = () => {
       return (
           <ApiProvider
               baseUrl="https://locahost/api"
               resources={apiResources}
           >
               {/* The rest of your app*/}
           </ApiProvider>
       )
   };
   
   export default App;
    ````
    
2. Use the `useApi` hook inside your containers/components that want to consume api data

    ````jsx harmony
   import React from 'react';
   import {useApi} from '@mistenkt/use-rest-api';
   
   const UserIndex = () => {
       const [users, loading, update, reset] = useApi('users.list');
       
       if(loading) return (
           <div>Loading...</div>
       )
       
       if(!users && !loading) return (
           <div>No users</div>
       )
   
       return (
           <ul>
               {users.map(user => (
                   <li key={user.id}>{user.name}</li>
               ))}
           </ul>
       )   
   }
   
   export default UserIndex;
   ````
   
3. To send create/update request to the api use the `useProducer` hook.

    ````jsx harmony
   import React from 'react';
   import {useApi, useProducer} from '@mistenkt/use-rest-api';
   
   const UserIndex = () => {
       const [users, loading] = useApi('users.list');
       const [produce, produceLoading, validationErrors] = useProducer({
           onSuccess: data => console.log('Success callback', data),
           onFail: err => console.log('Fail callback', data),
           onValidationError: errors => console.log('Validation error callback', errors)
       });
   
       const createUser = () => {
           produce('users.create', {
               name: 'foo'
           })
       };
       
       if(loading) return (
           <div>Loading...</div>
       )
       
       if(!users && !loading) return (
           <div>No users</div>
       )
   
       return (
           <>
               <ul>
                   {users.map(user => (
                       <li key={user.id}>{user.name}</li>
                   ))}
               </ul>
               <button onClick={createUser}>Create user</button>
           </>
       )   
   }
   
   export default UserIndex;
   ````
   
## Api Documentation

Ful api documentation coming soon.

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/@mistenkt/use-rest-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
