# drupal-sdk

> Javascript SDK for Drupal JSON API

Latest version **1.13.7** (published 2023-12-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install drupal-sdk
pnpm add drupal-sdk
yarn add drupal-sdk
bun add drupal-sdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.13.7 |
| Published | 2023-12-11 |
| First published | 2021-01-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 815.3 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | voide |
| Keywords | api, json:api, drupal, headless, javascript, node |

## Links

- npm: https://www.npmjs.com/package/drupal-sdk
- Homepage: https://gitlab.com/VoidE/drupal-sdk
- Issues: https://gitlab.com/VoidE/drupal-sdk/-/issues
- npm.io page: https://npm.io/package/drupal-sdk

## Dependencies (5)

- [axios](https://npm.io/package/axios.md) ^0.21.0
- [luxon](https://npm.io/package/luxon.md) ^2.3.0
- [jsonpath](https://npm.io/package/jsonpath.md) ^1.1.1
- [date-format](https://npm.io/package/date-format.md) ^3.0.0
- [p-iteration](https://npm.io/package/p-iteration.md) ^1.1.8

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.13.7 (latest) — 2023-12-11
- 2.0.0-rc.18 (rc) — 2025-07-02
- 2.0.0-rc.17 — 2025-06-18
- 2.0.0-rc.16 — 2025-06-11
- 2.0.0-rc.15 — 2025-06-11
- 2.0.0-rc.14 — 2025-06-11
- 2.0.0-rc.13 — 2025-06-11
- 2.0.0-rc.12 — 2025-06-11
- 2.0.0-rc.11 — 2024-10-23
- 2.0.0-rc.10 — 2024-10-23
- 2.0.0-rc.9 — 2024-10-23
- 2.0.0-rc.8 — 2024-09-25
- 2.0.0-rc.7 — 2024-09-24
- 2.0.0-rc.5 — 2024-09-24
- 2.0.0-rc.2 — 2024-09-24
- … 45 more at https://npm.io/package/drupal-sdk/versions

## README

# Drupal SDK

## Introduction

The Drupal SDK is a helper package for calling Drupal endpoints, like the JSON:API, in a more efficient and easy way.

## Installation

```
npm install drupal-sdk
```


## Development & Bugtracking
Development and bugtracking take place on gitlab.com. See https://gitlab.com/VoidE/drupal-sdk/-/issues for the current issue queue and https://gitlab.com/VoidE/drupal-sdk for the current repository.



## Usage

```JS
const Drupal = new DrupalSDK({
  url: 'https://drupal-sdk.prod.voide.dev',
});

async fetchData() {
  const items = await Drupal.get('node', 'article').read('12f930ee-8a7b-11eb-8dcd-0242ac130003');

  await Drupal.login('username', password);

  Drupal.get('node').create({
    title: 'Lorem ipsum',
  });
}
```


## Configuration

| Name                | Description                                                  | Type    | Required | Default    |
| ------------------- | ------------------------------------------------------------ | ------- | -------- | ---------- |
| url                 | The URL of the Drupal environment                            | string  | true     |            |
| authMode            | The authentication mode to use. For now, only "cookie" is available, but "jwt" will be implemented later on. | string  | false    | cookie     |
| storage             | The storage object for storing CSRF tokens and jwt tokens. This object must contain a "setItem" and "getItem" method. Only required when using authentication. | object  | false    |            |
| storageKey          | The key to use in your storage object.                       | string  | false    | drupal-sdk |
| apiBasePath         | The api base path to use in the JSON:API related methods     | string  | false    | /jsonapi   |
| useDecoupledRouter  | Wheter to use a decoupled router method or not. If true, make sure the https://www.drupal.org/project/decoupled_router module is installed in your drupal website | boolean | false    | false      |
| token               | The authentication token for usage without login             | string  | false    |            |
| tokenExpirationTime | The expiration time of the login token                       | number  | false    | 30000      |
| methods             | An object with methods to override. See the list below.      | object  | false    | {}         |


## Overridable Methods

For the following methods, overrides are supported.

- getRoute
- readByPath
- loginByEmail

All methods will come with an `injectableProps` argument. This is an object of helper classes in the DrupalSDK. Available classes are:

- `config` - The configuration object passed to the main class.
- `api` - The API class to perform API calls to.
- `auth` - The Authentication helper
- `storage` - The storage helper to store data in.


### getRoute()

**Arguments**

- path
- injectableProps

**Example**

```JS
const Drupal = new DrupalSDK({
  url: 'https://drupal-sdk.voide.dev',
  methods: {
    getRoute(path, { api }) {
      return api.get('/route', { path });
    },
  },
});
```

### readbyPath()

**Arguments**

- path
- injectableProps

**Example**

```JS
const Drupal = new DrupalSDK({
  url: 'https://drupal-sdk.voide.dev',
  methods: {
    readByPath(path, inputParams, { api }) {
      return Drupal.getRoute(path).then((json) => api.get(json.path));
    },
  },
});
```


### loginByEmail()

**Arguments**

- mail
- Password
- injectableProps

**Example**

```JS
const Drupal = new DrupalSDK({
  url: 'https://drupal-sdk.voide.dev',
  methods: {
    readByPath(path, inputParams, { api, storage }) {
      const body = {
        mail,
        pass: password,
      };

      return api
        .post('/user/email-login', body, { _format: 'json' })
        .then((json) => {
          api.setCSRF(json.csrf_token);
          storage.setItem('logout_token', json.logout_token);
          return json;
        });
    },
  },
});
```

## Entities and Storages

In the Drupal SDK a proxy layer between the SDK object and the entity is used. In the context of the SDK and Drupal, we call this layer the EntityStorage.

To get the storage of an entity, please use the following code:

```JS
const nodeStorage = Drupal.get('node');
```

The argument of this `get`-method is the machine name of the entity.

### `read` a Single Entity

**Arguments**

 - uuid
 - options (optional)

```JS
await Drupal.get('node').read('12f930ee-8a7b-11eb-8dcd-0242ac130003');
```

### `create` a Single Entity -

**Arguments**

 - body
 - options (optional)

```JS
Drupal.get('node', 'article').create({ attributes: { title: 'Lorem' } }, { filter: { lorem: 'ipsum' } });
```

### `update` a Single Entity

**Arguments**

 - uuid
 - body
 - options (optional)

```JS
Drupal.get('node', 'article').update('74518cfa-77ad-11eb-9439-0242ac130002', { attributes: { title: 'Lorem' } });
```

### `delete` a Single Entity

**Arguments**

 - uuid
 - options (optional)

```JS
Drupal.get('node', 'article').delete('74518cfa-77ad-11eb-9439-0242ac130002');
```

## Authentication

Accessing protected endpoints will be easy using the Drupal SDK. Just login or provide an access token to the configuration object.

For now, only cookie authentication is available, so let's take a look into that.



### login()

**Arguments**

 - username
 - Password

```JS
Drupal.login('username', 'password');
```



### logout()

```JS
Drupal.logout();
```



### loginByEmail()

This method is not available by default, but can be created in the `methods` section of the main configuration object.

**Arguments**

 - username
 - password

```JS
Drupal.loginByEmail('admin@example.com', 'password');
```



### requestPassword()

**Arguments**

 - mail

```JS
Drupal.requestPassword('admin@example.com');
```



### requestPasswordByUsername()

**Arguments**

 - username

```JS
Drupal.requestPassword('username');
```

---
_Source: https://npm.io/package/drupal-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
