$api

Introduction

The $api instance object provides access to a shared instance of the EJS REST Client to be available across components. This instance is created before any modules are loaded and is configured using the apiPath directive from the environment's configuration file.

The apiPath directive is relative to the current host origin. Cross-origin API requests are currently not allowed and will be rejected, throwing a Http/NetworkError.

The $api object allows you to make API requests without importing or instantiating REST Client. In addition, this enables the environment to fully manage the API authorization process, including the initial seed token.

Enabling the $api instance object

The $api instance object can be enabled across an application by including it in the Plugin namespace and adding the related entry to the environment configuration file.

In this example, the environment will expect to find the plugin class at: Plugin/Vue/InstanceMethod/Api.js.

environment.js
...
plugins: [
    'Vue/InstanceMethod/Api'
]
...

Usage

The $api instance object is an instance of REST Client, so its interface is the same.

MyComponent.vue
this.$api.get('/path/to/endpoint').then(response => {
    let result = response.json();
});

Last updated