Skip to content

Modern, modular and composable Drupal SDK for Node.js, Edge and Browser with an extensive plugin system and first-class TypeScript support.

Notifications You must be signed in to change notification settings

wunderwerkio/drupal-kit

Repository files navigation

Drupal Kit

A modular and strictly typed Drupal SDK for Node.JS and the browser with an extensive plugin and hook system.

Main Goal

The main goal of this project is to provide you with the building blocks (or a "kit" of tools) to build your very own specialized Drupal API client. Hence the name "Drupal Kit".

Drupal Kit consists of a core package that implements the bare minimum.
Plugin packages then implement specific functionality.

This concept makes Drupal Kit suitable for any project that needs to access the Drupal API, wheter it is a Drupal site itself that is progressively decoupling or a full fledged decoupled drupal site.

Installation

Install the core package:

npm install @drupal-kit/core

Install additional plugins depending on your use-case. See plugins here

Hooks

A core feature of Drupal Kit is it's hook system, thanks to before-after-hook. This hook system makes it possible to register custom hooks before and after the target function is executed.

request hook

The request hook is used to register custom hooks before and after the request function is executed.

const drupalkit = createDrupalKitSomehow().

// Add custom header to each request. 
drupalkit.hook.before('request', async (requestOptions) => {
  requestOptions.headers['X-Custom-Header'] = 'Value';
});

// Log successful response status codes.
drupalkit.hook.after('request', async (response, requestOptions) => {
  console.log("Successful response: " + response.status);
});

// Alternative way to register custom hooks.
drupalkit.hook.wrap('request', async (requestFunc, requestOptions) => {
  requestOptions.headers['X-Custom-Header'] = 'Value';

  const result = await requestFunc(requestOptions);

  console.log("Successful response: " + response.status);

  return result;
});

// Wrap enables us to re-run the request.
drupalkit.hook.wrap('request', async (requestFunc, requestOptions) => {
  try {
    return await requestFunc(requestOptions);
  } catch (error) {
    // Do something to alter request, e.g. renew access token.

    // Re-run the request here.
    return await requestFunc(requestOptions);
  }
});

// When the request errors.
drupalkit.hook.error('request', async (error) => {
  // Throw custom error here.

  throw error;
})

Plugins

The Drupal Kit class can be extended by plugins.

import { Drupalkit } from "@drupal-kit/core"
import { DrupalkitJsonApi } from "@drupal-kit/jsonapi";
import { DrupalkitUserApi } from "@drupal-kit/user-api";

// Create your very own Drupal Kit class that is
// extended by all of the registered plugins.
const EnhancedDrupalkit = Drupalkit.plugin(
  DrupalkitJsonApi,
  DrupalkitUserApi,
);

// Create a Drupal Kit instance.
const drupalkit = new EnhancedDrupalkit();

A plugin is just a function that takes the core Drupalkit and the DrupalkitOptions as arguments. It may return an object that is than merged with the core Drupalkit class to extend it.

Drupal Kit is meant to be extended by you!

List of built-in plugins:

JSON:API

This plugin integrates with the built-in jsonapi drupal core module.

Features:

  • JSON:API Index
  • GET single JSON:API resource
  • GET multiple JSON:API resources
  • POST JSON:API resource
  • PATCH JSON:API resource
  • DELETE JSON:API resource
  • Localization
  • Query Parameters

Typescript:

The JSON:API resources are strongly typed via the JsonApiResources interface. This interface MUST be augmented in your code in order for TypeScript to infer the correct types for the Drupalkit.jsonapi.resource() method.

Simple OAuth

This plugin integrates with the simple_oauth drupal module.

Features:

  • /oauth/token endpoint
  • /oauth/userinfo endpoint
  • /oauth/authorize endpoint
  • /oauth/jwks endpoint

Simple OAuth Auth Code

This plugin integrates with the simple_oauth_auth_code drupal module.

Features:

  • /simple-oauth/auth-code endpoint

Consumers

This plugin integrates with the consumers drupal module.

Features:

  • Set X-Consumer-ID header

Verification

This plugin integrates with the verification drupal module.

It is meant to be used by endpoints which require verification.

Features:

  • Hash based verification
  • Magic-Code based verification (via the magic_code drupal module)

User-Api

This plugin integrates with the user_api drupal module.

Features:

  • /user-api/register endpoint
  • /user-api/init-account-cancel endpoint
  • /user-api/cancel-account endpoint
  • /user-api/reset-password endpoint
  • /user-api/update-password endpoint
  • /user-api/passwordless-login endpoint
  • /user-api/verify-email endpoint
  • /user-api/update-email endpoint

About

Modern, modular and composable Drupal SDK for Node.js, Edge and Browser with an extensive plugin system and first-class TypeScript support.

Resources

Stars

Watchers

Forks