Configure Drupal API Client automatically for code components

Created on 15 July 2025, 4 days ago

Overview

The Drupal API Client (@drupal-api-client/json-api-client) β†’ is bundled for use in code components (β€” added in ✨ [spike] Add way to view fetch results inside code editor Active ).

Code component authors can use it the following way:

import { JsonApiClient } from "@drupal-api-client/json-api-client";
const client = new JsonApiClient(baseUrl, options);

There is already wrapper code added which tries to populate baseUrl using the value of window.location.origin, and also configures Jsona as the serializer in options.

We need to address the followings:

  1. Reading the value of window.location.origin for baseUrl doesn't always work.
  2. The default base path the JSON:API module uses can be changed from /jsonapi in a service parameter.

The goal is to allow code component authors to write the following, without passing anything in the class constructor:

import { JsonApiClient } from "@drupal-api-client/json-api-client";
const client = new JsonApiClient();

Proposed resolution

  1. Instead of using the value of window.location.origin for baseUrl, use the data provided by the backend since πŸ“Œ Populate data to drupalSettings to enable Dynamic Code Components Active .
  2. Pass relevant JSON:API module config and service parameters following the mechanism from πŸ“Œ Populate data to drupalSettings to enable Dynamic Code Components Active , make it conditional on the use of @drupal-api-client/json-api-client in the JS code.
  3. Update our bundled wrapper of @drupal-api-client/json-api-client to allow instantiating the JsonApiClient() class without any parameters, and have everything set up properly.
  4. Update our starter component template or the appropriate documentation file if πŸ“Œ Move code component starter template documentation from inline comments to online docs Active lands first.

User interface changes

n/a

✨ Feature request
Status

Active

Version

0.0

Component

Theme builder

Created by

πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024