Ajax requests

Many functions in the framework make requests. For example, some components get their template from a file. The framework uses its own service for the requests.

import request, { Request } from 'akili/src/services/request';
console.log(request === Akili.services.request); // true
console.log(request instanceof Request); // true

Request.prototype.constructor(baseUrl, defaultOptions)

  • [baseUrl=""] string - initial part of the URL for any request
  • [defaultOptions] object - default options for all requests

Create a new request instance.

Request.prototype.query(url, options) Promise

  • url string - URL
  • [options] Object - request options

The base method to make request.

request.query({
  method: 'GET',
  url: '/templates/cat.html'
}).then((res) => {
  console.log(res.data) // response data
  console.log(res.status) // status code
}).catch((err) => {
  throw err;
});

Full request URL is request.baseUrl + options.url => /templates/cat.html. The default method is GET.

As previously stated, request object is an instance of Request but has some own methods.

request.addInstance(name, instance)

  • name string - name of the instance
  • instance Request - the instance

Add a new instance to request for managing.

request.removeInstance(name)

  • name string - name of the instance

Remove the request instance.

request.addInstance('api', new Request('/api/', {
  responseType: 'json'
}));

request.use.api.query({
  url: '/users/'
});

Full URL is /api/templates/cat.html, request options include responseType = 'json'. So you can create and use any request instances you need to make the requests.

request.addInstance('api', new Request('/api/v1/'));
request.addInstance('apiV2', new Request('/api/v2/'));

request.use.api.query({ url: '/users/'});
request.use.apiV2.query({ url: '/users/'});
request.query({ url: '/dog.html'});

Cache

To enable the cache use cache option.

request.addInstance('api', new Request('api/', { cache: 1000 }));

Of course, you can change this option for a specific request.

request.use.api.get('user/1', { 
  cache: 1200 
});

request.use.api.get('user/1', { 
  cache: false 
});

To enable the cache for the main request instance use request.defaults.

request.defaults.cache = options => options.url.match(/\.html$/i)? 5000: false;

Query options

  • url - URL (xhr)
  • method - method (xhr)
  • body - body (xhr)
  • withCredentials - control CORS (xhr)
  • responseType - response type (xhr)
  • timeout - timeout (xhr)
  • onStart - function is called before the request
  • onProgress - function is called on xhr.onprocess
  • headers - object with http headers
  • json - object to send JSON
  • form - object to send files
  • params - object with query params
  • cache - time in ms to cache the requests
  • statusErrorsPattern - pattern to throw an error by response status
request.query({
  onStart: (xhr) => {
    // do something
  }
});
request.query({
  onProgress: (event, xhr) => {
    // do something
  }
});
request.query({
  headers: {
    'content-type': 'application/json'
  }
});
request.query({
  json: {
    name: 'Hugo',
    age: '27'
  }
});
request.query({
  form: {
    name: 'Hugo',
    avatar: new File(...)
  }
});
request.query({
  url: '/find/'
  params: {
    lon: 27,
    lat: 27
  }
});

// => '/find/?lon=27&lat=27';
request.query({
  statusErrorPattern: /^[^23]/
}).catch(() => {
  // if /^[^23]/.test(err.response.status) === true
});

Request.prototype.get(url, options) Promise

  • url string - URL
  • [options] Object - request options

GET request.

Request.prototype.post(url, options) Promise

  • url string - URL
  • [options] Object - request options

POST request.

Request.prototype.put(url, options) Promise

  • url string - URL
  • [options] Object - request options

PUT request.

Request.prototype.delete(url, options) Promise

  • url string - URL
  • [options] Object - request options

DELETE request.

Request.prototype.patch(url, options) Promise

  • url string - URL
  • [options] Object - request options

PATCH request.

Request.prototype.head(url, options) Promise

  • url string - URL
  • [options] Object - request options

HEAD request.

Request.prototype.transformBefore(xhr, options) { xhr, options }

  • xhr XMLHttpRequest - request instance
  • options Object - request options

Transform the xhr and the options before the request.

Request.prototype.transformAfter(result) object

  • result object - response result

Transform the response result.