xstream-store-resource

A module for removing the repetitive work in configuring asynchronous requests in xstream-store.
Installation
xstream-store-resource
has xstream
and xstream-store
as peer dependencies.
$ npm i xstream xstream-store xstream-store-resource
Example
import createResource from 'xstream-store-resource';
const config = {
effects: ['create', 'get'],
name: 'users',
url: '/api/users',
};
const {actions, actionTypes, streamCreator, effectCreators} = createResource(config);
const actions = {...xs, ...ats};
export {actions, effectCreators, streamCreator};
import createStore from 'xstream-store';
import {user$Creator, userEffectCreators} from './streams/user';
const streamCreators = {
users: user$Creator,
};
const effectCreators = [
...userEffectCreators,
];
const store = createStore(streamCreators, effectCreators);
export default store;
import store from './store';
import {actions as userActions} from './user-resource';
const subs = store.state$.subscribe({
next({users}) {
console.log(users)
},
});
store.dispatch(userActions.get('1')
store.dispatch(userActions.create({username: 'someone@me.com', name: 'Jane Doe'})
Usage
Config
createResource
takes a single config object, and returns generated actions, action types, and a stream and effects creator that must be passed to an xstream-store
.
import createResource from 'xstream-store-resource';
const config = {
name: 'my-resource-name',
url: '/my-resource-endpoint/:with/:params',
baseUrl: '',
customEffectCreators: [],
effects: [
'create',
'find',
'get',
'patch',
'remove',
'update'
],
provider: (requestUrl, data, config) => {...},
requestConfig: (effect) => {},
};
const {
actions,
actionTypes,
streamCreator,
effectCreators,
} = createResource(config);
Actions and Types of Actions
Similarly to Redux, xstream-store
relies on dispatched actions to update the state stream. xstream-store-resource
generates all the actions and action types necessary to make HTTP requests:
import {actions, actionTypes} from './user-resource'
The above actions are the actions that are important as an end user, but a full list can be found in src/action-creators.ts.
Action Parameters
id | appended to url e.g. for the endpoint /users userActions.get(1) will make a request for /users/1 |
data | data sent as the payload of the request |
params | url parameters to replace. e.g. for the endpoint /articles/:articleId/comments articleComments.get(1, {articleId: 2}) will make a request for /articles/2/comments/1 |
extra | additional data to send with the request. Currently supports adding query parameters via the query property e.g. for the endpoint /users userActions.get(1, {}, {query: {name: true; age: true}) will make a request for /users/1?name=true&age=true |
Effects
Effects map to HTTP functions to make it easy to define what to do with a resource. The config allows only specific effects to be specified for a resource. Each effect requires its own subscription, it's best to specify in the config which effects to create subscriptions for.
Each effect's subscription updates the resource's state stream when a related action for that effect is dispatched.
When the create
, find
, get
, patch
, remove
, and update
action creators are dispatched, the resource will immediately be in a state indicating that a response is pending:
{
response: {...userDetails},
requestState: 'REQUESTING',
requestEffect: 'PATCHING',
lastError: {},
}
Once the request is resolved or rejected, the resource's state will be updated:
{
response: {...userDetails},
requestState: 'SUCCESS',
requestEffect: 'IDLE',
lastError: {},
}
{
response: {...userDetails},
requestState: 'FAILURE',
requestEffect: 'IDLE',
lastError: {message: 'request failed'},
}
Todo