A modular and strictly typed Drupal SDK for Node.JS and the browser with an extensive plugin and hook system.
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.
Install the core package:
npm install @drupal-kit/coreInstall additional plugins depending on your use-case. See plugins here
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.
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;
})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:
@drupal-kit/jsonapi@drupal-kit/simple-oauth@drupal-kit/simple-oauth-auth-code@drupal-kit/consumers@drupal-kit/verification@drupal-kit/user-api
This plugin integrates with the built-in jsonapi drupal core module.
Features:
- JSON:API Index
-
GETsingle JSON:API resource -
GETmultiple JSON:API resources -
POSTJSON:API resource -
PATCHJSON:API resource -
DELETEJSON: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.
This plugin integrates with the simple_oauth drupal module.
Features:
-
/oauth/tokenendpoint -
/oauth/userinfoendpoint -
/oauth/authorizeendpoint -
/oauth/jwksendpoint
This plugin integrates with the simple_oauth_auth_code drupal module.
Features:
-
/simple-oauth/auth-codeendpoint
This plugin integrates with the consumers drupal module.
Features:
- Set
X-Consumer-IDheader
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_codedrupal module)
This plugin integrates with the user_api drupal module.
Features:
-
/user-api/registerendpoint -
/user-api/init-account-cancelendpoint -
/user-api/cancel-accountendpoint -
/user-api/reset-passwordendpoint -
/user-api/update-passwordendpoint -
/user-api/passwordless-loginendpoint -
/user-api/verify-emailendpoint -
/user-api/update-emailendpoint