@drupal-js-sdk/core
Overview
Base SDK used by all features. Manages configuration, HTTP client, and exposes shared services for other packages.
Usage
JavaScriptimport { 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
JavaScriptimport { 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
JavaScriptimport { 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
JavaScriptconst cfg = drupal.getConfigService();
cfg.setString("lang", "en");
setClientService
setClientService(client: XhrInterface): this
Example
JavaScriptimport { FetchClient } from "@drupal-js-sdk/xhr";
const client = new FetchClient({ baseURL: "https://example.com" });
drupal.setClientService(client);
getClientService
getClientService(): XhrInterface
Example
JavaScriptconst 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
JavaScriptconst session = drupal.getSessionService();
session.setItem("token", { value: "abc" });
class Drupal
extends Core
Constructor
constructor(config: {
baseURL: string;
auth?: XhrBasicCredentials;
headers?: XhrRequestHeaders;
client?: XhrInterface;
})
Example
JavaScriptimport { Drupal } from "@drupal-js-sdk/core";
const drupal = new Drupal({
baseURL: "https://example.com",
headers: { "X-App": "docs" },
});
initialize
initialize(options): this
Example
JavaScriptdrupal.initialize({ baseURL: "https://example.com" });
Notes
- Provides shared configuration, HTTP client, and session plumbing.