Skip to content

@drupal-js-sdk/core

Overview

Base SDK used by all features. Manages configuration, HTTP client, and exposes shared services for other packages.

Usage

JavaScript
import { Drupal } from "@drupal-js-sdk/core";

const drupal = new Drupal({ baseURL: "https://example.com" });
// Set a header for all subsequent requests
drupal.getClientService().addDefaultHeaders({ "X-App": "docs" });

Public API

class Core

Constructor

Instantiate Core

constructor(config: StorageRecordInterface)
Example
JavaScript
import { Core } from "@drupal-js-sdk/core";

const drupal = new Core({ baseURL: "https://example.com" });
// Set a header for all subsequent requests
drupal.getClientService().addDefaultHeaders({ "X-App": "docs" });

setConfigService

setConfigService(config: StorageInterface): this
Example
JavaScript
import { Core } from "@drupal-js-sdk/core";
import { StorageInMemory } from "@drupal-js-sdk/storage";

const core = new Core({});
core.setConfigService(new StorageInMemory());

getConfigService

getConfigService(): StorageInterface
Example
JavaScript
const cfg = drupal.getConfigService();
cfg.setString("lang", "en");

setClientService

setClientService(client: XhrInterface): this
Example
JavaScript
import { FetchClient } from "@drupal-js-sdk/xhr";

const client = new FetchClient({ baseURL: "https://example.com" });
drupal.setClientService(client);

getClientService

getClientService(): XhrInterface
Example
JavaScript
const client = drupal.getClientService();
const res = await client.call("GET", "/jsonapi/node/article");

setSessionService

setSessionService(session: SessionInterface): this
Example
JavaScript
// Provide your own SessionInterface implementation
drupal.setSessionService(mySession);

getSessionService

getSessionService(): SessionInterface
Example
JavaScript
const session = drupal.getSessionService();
session.setItem("token", { value: "abc" });

class Drupal extends Core

Constructor

constructor(config: {
  baseURL: string;
  auth?: XhrBasicCredentials;
  headers?: XhrRequestHeaders;
  client?: XhrInterface;
})
Example
JavaScript
import { Drupal } from "@drupal-js-sdk/core";

const drupal = new Drupal({
  baseURL: "https://example.com",
  headers: { "X-App": "docs" },
});

initialize

initialize(options): this
Example
JavaScript
drupal.initialize({ baseURL: "https://example.com" });

Notes

  • Provides shared configuration, HTTP client, and session plumbing.