Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.
Darko Kukovec edited this page Feb 23, 2018 · 5 revisions

The library is exposing a config for some networking related options:

baseUrl

The base URL used for the networking. By default, this is "/", but can be changed to something like "https://example.com/api/"

import {config} from 'mobx-jsonapi-store';

config.baseUrl = 'https://example.com/api/';

// Ready for some API calls

fetchReference

If you want to use the fetch API, but need to use a different implementation, like isomorphic-fetch, you can set the new reference here.

import {config} from 'mobx-jsonapi-store';
import fetch from 'isomorphic-fetch';

config.fetchReference = fetch;

// Ready for some API calls

defaultHeaders

If needed, you can set the default headers that will be sent in every API call. The default headers are {'content-type': 'application/vnd.api+json'}

import {config} from 'mobx-jsonapi-store';

config.defaultHeaders = {
  'content-type': 'application/json',
  'user-agent': 'MyAwesomeApp/1.0',
};

// Ready for some API calls

defaultFetchOptions

If needed, you can define default options that are passed to the fetch method. Keep in mind that the body, headers and method properties will be overridden.

import {config} from 'mobx-jsonapi-store';

config.defaultFetchOptions = {
  credentials: 'same-origin',
};

// Ready for some API calls

paramArrayType

Some JSON API param formats are not strictly specified (like filter), while others might not always be implemented according to the spec. To work around this, the lib exposes the paramArrayType property in the config. It can be one of the four values exposed in the ParamArrayType enum:

  • COMMA_SEPARATED (default value) - filter[a]=1,2
  • MULTIPLE_PARAMS - filter[a]=1&filter[a]=2
  • PARAM_ARRAY - filter[a][]=1&filter[a][]=2
  • OBJECT_PATH - filter[a.0]=1&filter[a.1]=2

baseFetch

If you want to use something other than the fetch API (e.g. jQuery, superagent, axios or anything else), you'll need to override the baseFetch method.

baseFetch(
  method: string,
  url: string,
  body?: object,
  requestHeaders?: IHeaders,
): Promise<IRawResponse>

The function will receive a request method, request url, a body object and request headers (key/value object), and it needs to return a promise that resolves to a raw response object - a JSON API valid response with some additional properties:

  • status - HTTP status
  • requestHeaders - the same object that was received by the function
  • headers - A Headers object received from the server

Depending on your baseFetch implementation, you might not need the baseUrl, fetchReference and defaultHeaders properties since they're consumed by the default baseFetch implementation.

transformRequest

transformRequest(options: IStoreFetchOpts): IStoreFetchOpts

Can be used to modify the request before it's made. It is receiving a IStoreFetchOpts object, and it should also return an object with the same interface.

Note: It is recommended not to modify the existing object but instead create a copy.

transformResponse

transformResponse(response: IRawResponse): IRawResponse

Can be used to modify the response before it's parsed. It is receiving a IRawResponse object, and it should also return an object with the same interface.

Note: It is recommended not to modify the existing object but instead create a copy.

storeFetch

NOTE: Deprecated in favor of transformRequest and transformResponse.

If you need something network specific, but you need to rely on something from the store, you should override the storeFetch function

storeFetch({
  url,
  options,
  data,
  method = 'GET',
  store,
}): Promise<LibResponse> {

The function receives an object with options and it returns a Promise that resolves to a Response instance. The basic implementation of the function is just calling the baseFetch function and creating a new Response instance from the resolved value.

Clone this wiki locally