Drupal
The Drupal API Client can be used within a Drupal site (often called progressive decoupling).
Loading your JavaScript as a module
Given the following JavaScript that loads the JSON:API Client from a CDN:
You will need to load this JavaScript as a module in your Drupal site by adding the following to a libraries.yml
file in your module or theme:
Using a dynamic baseUrl
In the previous example, you’ll note that the baseUrl
argument is being set to window.location.origin
.
In a progressively decoupled Drupal site, this will allow the client to make requests to the same domain as the Drupal site without having to specify different values on non-production environments.
Inheriting authentication
When used inside of your Drupal site, the client will inherit the current user session by automatically including any cookies that are set by Drupal when making requests.
Given the following JavaScript:
In a browser window logged in as a user with the ‘administer actions’ permission, you will see results for the ‘action’ collection. As an unauthenticated in an incognito window you will see no results.