Aurelia-api has been built around the concept of multiple endpoints. This means that you can have more than one API to talk to. This is useful when working with micro-services, or external APIs (such as weather APIs).

Following defaults are applied to an endpoint if not specified otherwise:

defaults = {
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'

Ways to register your endpoints in main.js:

Configuration with a function

  /* Your other plugins and init code */
  .plugin('aurelia-api', config => {

    // 1: Current host

    // 2: Specific host
    config.registerEndpoint('api', '');

    // 3: With different endpoint defaults
    config.registerEndpoint('weather', '', {headers: {x: 'foo'}});
    // or
    config.registerEndpoint('weather', configure => {
        .withDefaults({headers: {x: 'foo'}});

    // 4: Without endpoint defaults. Use this for FormData
    config.registerEndpoint('weather', '', null);

    // 5: Own configuration
    config.registerEndpoint('twitter', configure => {
        //.withDefaults({headers: {x: 'foo'}}); // uses own defaults
        //.withDefaults(); // no defaults. same as omitting withDefaults()

    // 6: Set default

    // 7: Chain
      .registerEndpoint('auth', '')

  // 8: Set Default BaseUrl

Configuration with an object

  /* Your other plugins and init code */
  .plugin('aurelia-api', {
    endpoints: [
      // 1: Current host
      {name: 'api'},
      // 2: Specific host
      {name: 'api', endpoint: ''},
      // 3: With different endpoint defaults
      {name: 'weather', endpoint: '', config: {headers: {x: 'foo'}}},
      // 4: Without endpoint defaults
      {name: 'weather', endpoint: '', config: null};
    // 6: Set default endppoint. alternatively, add default: true above
    defaultEndpoint: 'api',
    // 8: Set Default BaseUrl
    defaultBaseUrl: ''

Here's a more detailed explanation for every method of registering used:

1: Default host name

When registering an endpoint with aurelia-api, you have the option to supply the URL for the endpoint. If you decide not to, it will default to the URL where the application is currently running.

2: Specific host

Most of the time, your API resides somewhere that's not the current URL. Or, you're adding multiple endpoints, which all reside at a different URL. This method of registering a new endpoint allows you to specify where it is.

3: With different defaults

This method allows you to specify different defaults to use for your endpoint, in this case headers. Every request made via this endpoint will now include the configured header x with value "foo".

4: With no defaults

This method allows you to remove the defaults to use for your endpoint. This is needed for "multipart/form-data" requests as the content type will be set automatically if the transmitted body is of the type FormData.

5: Own configuration

As mentioned in the introduction, aurelia-api uses aurelia-fetch-client. If you wish to bypass the aurelia-api layer of configuration, simply supply a callback. The callback will receive the original configure instance.

You can read more about that in the aurelia docs.

6: Set default endpoint

This method allows you to set the default endpoint to use. This means, that when getting an endpoint from aurelia-api, without supplying a name, we'll get this endpoint.

7: Chain

All methods return this, allowing you to chain the calls.

8: Set default base URL

All endpoints registered after this call will use this default base URL, rather than the current host URL.

results matching ""

    No results matching ""