api.md
August 17, 2020 ยท View on GitHub
API
redux-json-api provides a simple API for all four CRUD actions.
- Create resource object using createResource
- Read endpoints through readEndpoint
- Update resource with updateResource
- Delete resource using deleteResource
- Add a resource to the store using hydrateStore
- Manage relationships using relationship endpoints
Resource objects
Whenever there's referred to a resource object or entity, it must conform to JSON API specifications.
Resource endpoints
redux-json-api resolves resource objects to endpoints based on their specified type. The following resource will update and delete to /tasks/1:
{
"id": 1,
"type": "tasks",
"attributes": {
"title": "Lorem ipsum"
}
}
While dispatching a create action for the following resource will make a request to /tasks:
{
"type": "tasks",
"attributes": {
"title": "Lorem ipsum"
}
}
API Promises
The redux-json-api's CRUD API methods will all return a single promise. The fulfillment handler will receive one argument with the response body. One exception to this is the fulfillment handler from a deleteResource promise, which will not receive any arguments.
API Methods
Note: Return values noted below are after dispatch, i.e. dispatch(createResource({ ... })).
createResource( resource: JsonApiResource ): Promise<JsonApiDocument>
Use this action creator to trigger a POST request to your API with the given resource.
readEndpoint( endpoint: string ): Promise<JsonApiDocument>
This action creator will trigger a GET request to the specified endpoint.
updateResource( resource: JsonApiResource ): Promise<JsonApiDocument>
Update entities using this action creator. It will make a PATCH request to your API.
deleteResource( resource: JsonApiResource ): Promise<void>
Use this action creator to issue a DELETE request to your API.
More details on deleteResource
hydrateStore( payload: JsonApiDocument ): Action
Use this action to hydrate the store with e.g. bootstrapped data.