
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-supermodel
Advanced tools
Supercharged REST-api wrapper for React.
yarn add react-supermodel baobab
npm install react-supermodel baobab --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 – it's required.
import Baobab from 'baobab'
import { setConfig } from 'react-supermodel'
const tree = new Baobab({
$api: {}, //
whateverYouNeedThere: {},
})
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. Can be string or function. 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' })
// Or custom function
setConfig({ prefix: () => `/api/${window.API_VERSION_CONFIG}` })
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
Once you've setuped supermodel's config, you'll need to create model. Basically model describes how to store/import/export and sync local data with an API provided data.
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', // :id will be replaced for real user is by connector
list: '/users',
create: 'POST /users', // Also you can speciafy request's method with first-word prefix like GET, POST. DELETE, PUT
delete: 'DELETE /users/:id',
update: 'PUT /users/:id',
}
})
name key contains the name of the model. It'll be passed to Component props via connect function.
idKey is the name for unique key of your objects. By default it is equal to id.
For example, if your API has users collection contains an objects like {user_id: 1, user_name: 'admin'}, you should set up idKey as user_id.
Default is data. Name of the key from your API response when requesting a single object.
Default is data. Name of the key from your API response when requesting a list.
The most important option. api is an object that describes how to work with your API. api has several predefined special keys which have a mapped dataflow methods like get, list, create, delete. You can also create your own methods. Working with connectors
Each property can be string contains an url pattern or an object.
If you need to manipulate with response data, you should and object condiguration.
Here is an example how to add an extra property full_name to user object.
{
api: {
get: {
url: '/user/:id', // the same if it was a string
import( user ) {
return {
...user,
full_name: `${user.last_name} ${user.first_name}`
}
}
}
}
}
http://api2.mysite.com/api) or relative (/api), which will be prefixed with modelOptions.prefix prop."POST /api")Examples:
If you call User.get( 1 ) with an url like /users/:id, Supermodel will interpolate it like /users/1.
If url will look like /users, Supermodel will interpolate it like /users?id=1
If url will look like POST /users Supermodel will send send POST request with JSON-payload like {id:1}. But if you need both (Request body and GET param), you should define an url like /POST /users?id=:id.
One of the most advanced feature. Basically it is simple data-processing function which describes how to import API data to storage. It is an entry point to convert or modify incoming data.
import( user ) {
return {
...user,
// Adding fullname property
full_name: `${user.last_name} ${user.first_name}`,
// Adding formatted birthdate string property from backend UNIX timestamp
birthdate: moment(user.birth_timestamp * 1000).format('DD.MM.YYYY'),
}
}
It is simple data-processing function which describes how to export your data to API. For example, user changed his birthdate:
export( user ) {
return {
// birth_timestamp is the name of propery stored on backend
// So we should send it as UNIX timestamp
birth_timestamp: moment(user.birthdate).unix()
}
}
– WIP: UI/data updating strategy
import connect from 'react-supermodel'
import UserModel from './models/UserModel'
@connect(UserModel)
class App extends Component {}
That's it.
Once you've added connector to your component, you a ready to use it.
@connect(UserModel)
class App extends Component {
render(){
const { User } = this.props // Get UserModel's connector from props
// ...
}
}
Model's connector provides some predefined methods like get, list, create, delete, update and all using own dataflow inside.
Designed for using inside Component's render method.
const user = User.get(1)
return (<pre>{JSON.stringify(user)}</pre>)
import connect from 'react-supermodel'
import UserModel from './models/UserModel'
@connect(UserModel)
class App extends Component {
render(){
// Get UserModel's connector from props
const { User } = this.props
// Getting users list from an API
const users = User.list()
// Showing progress indicator while users are loading
if( users.isLoading ) {
return 'Loading ...'
}
return (
<ul>
{users.data.map({data} =>
<li key={data.id}>{data.name}</li>
)}
</ul>
)
}
}
– WIP
– WIP
– WIP
git clone https://github.com/ekorzun/react-supermodel.git
cd react-supermodel
yarn install
yarn test
MIT.
FAQs
Supercharged REST-api wrapper for React.
The npm package react-supermodel receives a total of 1 weekly downloads. As such, react-supermodel popularity was classified as not popular.
We found that react-supermodel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.