actionsConfig

Map of <actionName:config> which defines the actions you will be able to perform on a resource.

Properties

  1. (method) mandatory: (string) The method of the action. Can be one of: GET, PATCH, PUT, POST, DELETE.

  2. (url) mandatory: (string || () : string) The URL on which the action has to fetch. For dynamic parameters, prefix them with :: for the resource ID (e.g. ::userId), and : for other parameters (e.g. :userType), and they will get replaced with the urlParams you will provide (see actions documentation).

  3. (cacheHint): ((urlParams, query, body, otherArgs) : object) A function which will be invoked to generate a cache hint, appended to the formatted URL for caching purpose. It is expected to return an object of serializable values.

  4. (beforeHook): ((urlParams, query, body, otherArgs, dispatch) : undefined || any) A hook which can be invoked just before performing the request. Will be awaited if async. If it returns a non-falsy value, the return will be used as the body for the principal request.

  5. (normalizer): ((payload, resources, urlParams, query, body, otherArgs) : { entities: normalizedPayload, result: principalResourceId }) A function which will be invoked to normalize the payload of the request. It is expected to return an object with entities and result, respectively containing the normalized payload and the sorted ids, just as normalizr does.

  6. (metadataNormalizer): ((payload, resources, urlParams, query, body, otherArgs) : object) A function which will be invoked to extract metadata from the payload (if any). It is expected to return an object, which will be available via a selector.

  7. (afterHook): ((normalizedPayload, urlParams, query, body, otherArgs, dispatch) : undefined) A hook which can be invoked after performing the request and normalizing the payload. Will be awaited if async.

  8. (networkHelpers): (map<string|func>) A map of handlers used when performing network requests. Override the default ones, and the ones specified using initializeNetworkHelpers. Documentation on the content of the map can be found here.

Example

This example demonstrates a sample actionsConfig.

const actionsConfig = {
  create: {
    // Mandatory
    method: 'GET',
    url: 'https://api.co/users/:userType/::userId/infos',
    // Optional
    cacheHint: (urlParams, query, body, otherArgs) => otherArgs.language,
    beforeHook: (urlParams, query, body, otherArgs, dispatch) =>
      console.log(
        'User infos retrieved with query: ',
        query,
        'and with custom property: ',
        otherArgs.property,
      ),
    normalizer: ({ user }) => ({
      entities: { users: { [user.id]: user } },
      result: user.id,
    }),
    metadataNormalizer: ({ _meta }) => _meta,
    afterHook: (
      normalizedPayload,
      urlParams,
      query,
      body,
      otherArgs,
      dispatch,
    ) => {
      if (Object.values(normalizedPayload.users)[0].isCompleted) {
        return dispatch(someAction());
      } else {
        console.log(otherArgs.otherProperty);
      }
    },
    networkHelpers: {
      getToken: () => 'custom_token',
    },
  },
};

Tips

  • Returning a promise in a hook will make the execution wait for it to complete before continuing

Last updated