Skip to content

@drupal-js-sdk/xhr

Overview

HTTP client abstractions with a unified interface, supporting native fetch or Axios. Used by all SDK packages for requests.

Usage

JavaScript
import { FetchClient } from "@drupal-js-sdk/xhr";

const client = new FetchClient({ baseURL: "https://example.com" }).addDefaultHeaders({
  "X-App": "docs",
});

const response = await client.call("GET", "/jsonapi/node/article");

Public API

class FetchClient implements XhrInterface

Constructor

constructor(config?: XhrRequestConfig)

setClient

setClient(client: typeof fetch): this

getClient

getClient(): typeof fetch

addDefaultHeaders

addDefaultHeaders(headers: Record<string, any>): XhrInterface
Example
JavaScript
client.addDefaultHeaders({ Authorization: "Bearer token" });

call

call(method: XhrMethod, path: string, config?: XhrRequestConfig): Promise<XhrResponse>
Example
JavaScript
const res = await client.call("GET", "/jsonapi/node/article");

class AxiosClient implements XhrInterface

Constructor

constructor(client: { request(config: XhrRequestConfig): Promise<XhrResponse> })

setClient

setClient(client: { request(config: XhrRequestConfig): Promise<XhrResponse> }): this

getClient

getClient(): { request(config: XhrRequestConfig): Promise<XhrResponse> }

addDefaultHeaders

addDefaultHeaders(headers: Record<string, any>): XhrInterface

call

call(method: XhrMethod, path: string, config?: Record<string, any>): Promise<XhrResponse>

Notes

  • Both clients adhere to XhrInterface from @drupal-js-sdk/interfaces.