npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

drupal-sdk

v1.13.7

Published

Javascript SDK for Drupal JSON API

Downloads

331

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

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

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

readbyPath()

Arguments

  • path
  • injectableProps

Example

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

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:

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)
await Drupal.get('node').read('12f930ee-8a7b-11eb-8dcd-0242ac130003');

create a Single Entity -

Arguments

  • body
  • options (optional)
Drupal.get('node', 'article').create({ attributes: { title: 'Lorem' } }, { filter: { lorem: 'ipsum' } });

update a Single Entity

Arguments

  • uuid
  • body
  • options (optional)
Drupal.get('node', 'article').update('74518cfa-77ad-11eb-9439-0242ac130002', { attributes: { title: 'Lorem' } });

delete a Single Entity

Arguments

  • uuid
  • options (optional)
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
Drupal.login('username', 'password');

logout()

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
Drupal.loginByEmail('[email protected]', 'password');

requestPassword()

Arguments

  • mail
Drupal.requestPassword('[email protected]');

requestPasswordByUsername()

Arguments

  • username
Drupal.requestPassword('username');