Skip to content

Latest commit

 

History

History
94 lines (52 loc) · 6.48 KB

README.md

File metadata and controls

94 lines (52 loc) · 6.48 KB

REST Middleware

API

middleware

Creates a middleware to handle REST requests.

Parameters

  • options object The configuration object { actions: RequestActions, idPath: IdPath, baseRoute: string, immutable?: boolean}. actions: and object where each key is a REST verb containing REST actions (request, succes and fail action creators) or contains REST reducer action creators. IdPath: the path to the REST entities identifiers (string, string array of function). baseRoute: the base route of the REST api (e.g. /api/users). immutable: use immutable objects as actions payload (true by default). E.g. with idPath='slug' and baseRoute='/users', a call to /users/:slug will be made to handle the findOne verb.

Returns Middleware The REST middleware to add to your store.

normalize

Normalizes data if it is an array.

Parameters

  • data (object | [object]) the data to normalize.
  • idPath IdPath The path to the value to identify the REST entities (string, string array, function or string with dot seperaing keys).

Returns object The normalized data as an immutable object with the following keys: result, the list of object keys (see idPath) and entities, an object with each key containing the related entity.

getEntities

Gets the entities from the state.

Parameters

  • reducerSubState object the part of the state handled by the REST reducer.
  • options object?= getEntitiesOptionsDefault the options may contain: immutable (boolean, defaults to true) to define if JS (requires a toJS conversion) or immutable objects (better for performance) should be returned; reverse (boolean, defaults to false) to return the objects in reverse order.

Returns oject The entities from the state (immutable list or JS array depending on the configuration).

getEntity

Gets an entity from the state based on its key.

Parameters

  • reducerSubState object the part of the state handled by the REST reducer.
  • key string the key of the entity to retrieve (the value returned by the specified idPath).
  • options object?= getEntityOptionsDefault the options to use. immutable: boolean to specify if the returned object should be immutable (defaults to true).

Returns object The entity (immutable map or JS array depending on the configuration).

getStatus

Gets the status of the request by status type.

Parameters

  • reducerSubState object the part of the state handled by the REST reducer.
  • type string one of finding, findingOne, creating, updating or deleting.
  • key string (optional) - the key of the entity (the value returned by the specified idPath).

Returns boolean true if the request is pending, else false.

get

Gets the value within an immutable or JS object.

Parameters

  • obj (Iterable | Object | undefined) the immutable or JS object. undefined is returned if it is not defined.
  • path (string | [string]) the key(s) to follow. It can be a string , e.g. 'slug', a path in dot notation, e.g. 'profile.slug', or an array of strings, e.g. ['profile', 'slug'].

Returns any The value within the object following path, or undefined if it does not exist.

handlerCreator

Creates the handler function to handle the REST request actions (request, success and fail).

Parameters

  • verb string The verb to handle (find, findOne, create, update or delete).
  • requestActions RequestActions An object containing the request, success and fail action creators to handle.
  • idPath IdPath The path to the value to identify the REST entities (string, string array, function or string with dot seperaing keys).

Returns object An object containing the functions to handle request, success and fail state change in the reducer.

restReducer

Creates a REST reducer to handle the verbs of the REST action creators.

Parameters

  • config object The REST reducer config with the following keys: idPath, the path to the identifier of the requested objects (string, string array, function or string with dot seperaing keys); actions: each key of the object is a verb (find, findOne, create, update or delete) and contains the related REST action creators (request, success and fail); extraHandlers: an object where each key is the action type to handle and each key contains a function to handle the state change for these actions.

Returns Reducer A reducer to handle REST request actions and the extra handlers actions.