@thorgate/spa-entities
v1.0.0-beta.4
Published
Redux normalized storage & fetch helper
Downloads
37
Readme
@thorgate/spa-entities
Normalized data storage helper.
Basic Usage
import { createFetchAction, createFetchSaga, createSchemaSelector, createDetailSchemaSelector } from '@thorgate/spa-entities';
import { schema } from 'normalizr';
const article = new schema.Entity('articles');
const fetchAction = createFetchAction('fetch/articles');
// Create worker to fetch data
const fetchSaga = createFetchSaga({
resource: new FetchResource('/articles'),
// method: 'fetch', // This is the default value
key: article.key,
listSchema: [article],
});
const listSelector = createSchemaSelector(article);
const detailSelector = createDetailSchemaSelector(article);
// Usage with `@thorgate/spa-view-manager`
const initialFetchWorker = fetchSaga.asInitialWorker((match) => fetchAction());
function* fetchArticleWatcher() {
yield takeLatest(fetchAction.type, fetchSaga);
}
NormalizedFetchOptions
listSchema
: (schema.Entity[]): Required: List schema to be used for serializationkey
: (string|Function): Required: Field name under which key order is stored underresource
: (Resource|SagaResource): Resource instance used to fetch datamethod
: (ResourceMethods): Resource method usedapiFetchHook
: ((action: FetchActionType) => any | Iterator): Custom fetch method. This can be used to customize response or dispatch additional actions.serializeData
: ((result: any, listSchema: schema.Entity[]) => {result: any, entities: any} | Iterator<Effect | {result: any, entities: any}>): Custom serializer method. This can be used to customize response or dispatch additional actions.timeoutMs
: (number): Timeout after which request is cancelled and error is thrown. Default:3000
createFetchAction
Payload object
method
: (ResourceMethods): Optional override to change<resource>
method used.kwargs
: (Object): Optionally URL kwargs to pass to<resource>.<method>
query
: (Object): Optionally URL Query parameters to pass to<resource>.<method>
data
: (Object): Optionally POST like data to pass to<resource>.<method>
callback
: (Function): Function to call when fetch saga finishes