Supercharged REST-api wrapper for React.
- react-supermodel
- Demo
- Features
- Installation
- Get started
- Examples
- Using Baobab as application's store
- Using with redux / etc
- Using without React
- Development & test
- Licence
– Gif – https://codesandbox.io/s/04poy3y2kp
– Works out of the box – Cache control – Optimistic/Pessimistic strategies for UI/data updating – Backend-agnostic – Immutable state –
yarn add react-supermodel
npm install react-supermodel --save
The first thing you need to do is to init main config.
Typically, your app's top-level component or main file like index.js
will probably contains this config.
import {setConfig} from 'react-supermodel'
setConfig( options )
Baobab instance. Make sure you have an $api
cursor in your tree.
import Baobab from 'baobab'
import {setConfig} from 'react-supermodel'
const tree = new Baobab({$api: {}})
setConfig({tree})
Accept header for request. Default is json
See – https://visionmedia.github.io/superagent/#setting-accept
Authorization header. Default is empty string. Can be string
or function
.
For example:
{
auth: `Bearer: USER_TOKEN`,
// Or using dynamic token
auth: () => `Bearer: ${window.ComputeUserToken()}`,
}
Base URL prefix. All model's requests will be prefixed with it. If you are going to use custom domain as prefix, make sure you know about CORS and credentials (see below).
setConfig({prefix: '/api'})
// Or custom domain
setConfig({prefix: 'http://customdomain.com/api'})
This option enables the ability to send cookies from the origin, however only when Access-Control-Allow-Origin is not a wildcard ("*"), and Access-Control-Allow-Credentials is "true". See – https://visionmedia.github.io/superagent/#cors
–
–
Main.
import {Model} from 'react-supermodel'
const UserModel = new Model({
name: 'User', // This will be the name in props for connected component
api: {
get: '/users/:id',
list: '/users',
create: 'POST /users',
delete: 'DELETE /users/:id',
update: 'PUT /users/:id',
}
})
–
–
–
–
–
–
import connect from 'react-supermodel'
import UserModel from './models/UserModel'
@connect(UserModel)
import connect from 'react-supermodel'
import UserModel from './models/UserModel'
@connect(UserModel)
class App extends Component {
render(){
return (
<ul>
{this.props.User.list().data.map({data} =>
<li key={data.id}>{data.name}</li>
)}
</ul>
)
}
}
–
–
–
git clone https://github.com/ekorzun/react-supermodel.git
cd react-supermodel
yarn install
yarn test
MIT.