
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
drupal-client
Advanced tools
A Javascript client for Drupal 7 / Services Module
An installation of Drupal 7.x and Services Module
REST Server module enabled, an endpoint defined and appropriate permissions (system, user, node, etc.). In server settings, enable only response formatter json
the request parsing mime types application/json
, application/x-www-form-urlencoded
and multipart/form-data
.
A Javascript project - node.js or Titanium are known to work.
npm install drupal-client
Copy lib/drupal.js and lib/field.js into your app/lib/ folder.
Configure the client for your installation of Drupal+Services. Note that the URL includes the trailing slash.
var Drupal = require('drupal');
var drupal = new Drupal();
drupal.setRestPath("http://mywebsite.com/", "rest_endpoint");
Create a Service and enable (at least) the Resources called "system" and "user".
drupal.systemConnect(
//success
function(sessionData) {
var uid = sessionData.user.uid;
console.log('session found for user '+uid);
},
//failure
function(error) {
console.log('boo :(');
}
);
var user = {
name: 'my_new_username',
pass: 'my_new_password',
mail: 'my_email@drupal.js'
};
drupal.createAccount(user,
//success
function(userData) {
console.log('yay!');
},
//failure
function(error) {
console.log('boo :(');
},
headers //optional
);
var my_username = "<DRUPAL USERNAME>";
var my_password = "<DRUPAL PASSWORD>";
var userObject;
drupal.login(my_username, my_password,
function(userData) {
console.log('User ' + userData.uid + ' has logged in.');
userObject = userData;
},
function(err){
console.log('login failed.');
}
);
This updates an account profile on the server. userObject
is a user object that may have been received from a login request (see above).
drupal.putResource("user/"+userObject.uid, userObject,
function(userData) {
console.log('user has been updated.');
},
function(err){
console.log('user update failed.');
}
);
var filename = "uploaded_file.png";
var data = require('fs').readFileSync("path/to/file/file.png");
var base64data = data.toString('base64');
var filesize = data.length;
drupal.uploadFile(base64data, filename, filesize,
function (response) {
fid = response.fid;
},
function (err) {
console.log(err);
},
function (progress_event) {
console.log(progress_event.loaded + '/' + filesize + ' uploaded');
}
);
var node = {
type: "my_content_type",
title: "My New Node",
body: drupal.field.structureField("Check out this great new node!"),
field_bool: drupal.field.structureField(1),
field_decimal: drupal.field.structureField(.1),
field_float: drupal.field.structureField(2.3),
field_integer: drupal.field.structureField(4),
field_multiple: drupal.field.structureField(["one", "two", "three"]),
field_file: drupal.field.structureField(fid, "fid"),
field_date: field.structureField(new Date())
};
drupal.createNode(node,
function (resp) {
console.log(resp);
},
function (err) {
console.log(err);
}
);
The workhorse function of the interface is makeAuthenticatedRequest(config, success, failure, headers)
. There are a few helper functions included for posting/getting nodes, getting views, uploading files, etc. They all construct a call to makeAuthenticatedRequest()
. This function should facilitate most things that people want to do with Drupal in a mobile environment. It's also easy to use `makeAuthenticatedRequest' to make requests against custom Services. The short-term roadmap includes calls to the services supporting entities.
To run the tests, rename test/config.js.example
to test/config.js
and replace strings with the url of your Drupal install and your service endpoint.
npm install
npm test
FAQs
Javascript Drupal Services Client
The npm package drupal-client receives a total of 5 weekly downloads. As such, drupal-client popularity was classified as not popular.
We found that drupal-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.