The Config File

Where we discuss the config file, the place where all the magic happens!

Anatomy of a config file

Redux-optimum takes full advantage of the Redux philosophy. It is an add-on. Thus, you will proceed with your reducers any normal way you would. The config file just allows to define what gets call as actions during an API call and to set up some API specific patterns of behavior. First, let's divide up in three parts our config object.

First are the generic options for the config file. All of these come with default values and are at the root of the object.

needsToBeLoggedIn: boolean

@default is false.

This indicates whether or not the user needs to be logged in to call the endpoints.

sendsAccessToken: string

@default is "none".

options:

header, query or, in the body

This indicates whether or not to send an access token and how.

mode: string

@default is "latest".

This indicates whether every time the call is made, yet some of the same API calls are in the queue, we take the latest and abandon the others, or we queue them up or process them in order. A data fetch only needs the last request, a data push often needs all requests to be processed in order.

HTTPCodesRefreshToken: <int>array

@default is [].

This will force to call sendingRefreshToken when the HTTP code answer is one within this array. Needs to also accept patterns like single or double-digits code so they can be expanded like 41 for all 41* codes. -1 For browser failure

HTTPCodeFailures: <int>array

@default is [].

This will force to call the queue manager to abandon the API call if any of these status codes are received. Needs to also accept patterns like single or double-digits code so they can be expanded like 41 for all 41* codes. -1 For browser failure

retriesDelays: <int>array or int

@default is [].

This either takes an array of integers where each integer represents the waiting delay between the first call and the next retry call. A fixed integer means retry at a fixed interval.

clearAfterAllRetriesFailed: boolean

@default is false.

This property indicates whether we should abandon the call and deem it a failure after we have exhausted all the retries or if the call should stay in the retry queue no matter what to be reactivated at a later point. This option is meaningless if the retriesDelays is set on a continuous interval.

Second are the credential management parameters. They sit under the property credentialManagement at the root of the object.

loggedInSelector: (store) => boolean

@default returns false.

This method will always have the state of the store provided as argument and needs to return true or false. Being not logged in when the property needToBeLoggedIn is set to true means that the API will not even be called but queued until logged in.

getAccessToken: (store) => ({key: value})

@default is void.

Gets the store as argument, returns the access token in the form of an object. The property name will be used in whichever method chosen to pass token as its name, and the value as its value. Remember the token can be passed as a query parameter, as a json in the body, or as a header.

getRefreshToken: (store) => ({key: value})

@default is void.

Behaves exactly the same way as the getAccessToken property.

refreshingTokenCallDetails: {
      endpoint: (store) => string,
      method: string,
      HTTPCodeFailures: <int>Array,
      retriesDelays: <int>Array or int
      sendRefreshToken: string
      requestParameters: object,
    })

@default is void.

This property takes an object with many sub-properties.

endpoint: string

The url of the endpoint.

method: string

The HTTP verb as string.

HTTPCodeFailures: <int>Array

Behaves in the same manner as the HTTPCodeFailures property in the generic options but only applies to refreshingTokenEndpoint.

retriesDelays: <int>Array

Behaves in the same manner as the retriesDelays property in the generic options but only applies to refreshingTokenEndpoint.

sendRefreshToken: string

Behaves in the same manner as the retriesDelays property in the generic options but only applies to refreshingTokenEndpoint. header, query or, in the body,

requestParameters: object

Those are the options that will be passed to the Fetch API as options.

uponReceivingFreshToken: (answerbody) => {}

@default is void.

Called when the new token is received.

Third are the actual operations. They sit under the property operations at the root of the object.

operations: <operation>array

Every operation needs to have a few parameters defined. Any of these operations can override the generic options for a specific property.

actionType: string

Takes action type for Redux. This will be the action type passed to the reducers.

APICallSettings: {
        endpoint: (originalActionPayload, store) => string,
        method: string,
        requestParameters: object,
        payload: (originalActionPayload, store) => (object),
      },

Requires an object. Let us discuss the property and methods.

endpoint: (originalActionPayload, store) => string,

Has access to the payload passed to the Redux action (so you can pass query parameters), and must return the URL targeted.

method: string

Provides the HTTP verb for the API endpoint

requestParameters: object

Those are the options that will be passed to the Fetch API as options.

payload: (originalActionPayload, store) => (object)

Has access to the payload passed to the Redux action, and returns the body (JSON stringified) that will be appended to the request. (Should allow body that are not json)

stages: {
          begin: {
            actionType: string,
            payload: (payload,
                      storeWhenDispatching,
                      operation) => (object)
          },
          success: {
            actionType: string,
            payload: (response,
                      originalActionPayload,
                      storeWhenDispatching,
                      operation) => (object)
          },
          failure: {
            actionType: string,
            payload:  (error,
                       originalActionPayload,
                       storeWhenDispatching,
                       operation) => (object),
          },
        },

XXXX

Last updated

Was this helpful?