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-client

v0.2.0

Published

Javascript Drupal Services Client

Downloads

12

Readme

drupal-client

A Javascript client for Drupal 7 / Services Module

Build Status

Requirements

  1. An installation of Drupal 7.x and Services Module

  2. 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.

  3. A Javascript project - node.js or Titanium are known to work.

Installation

node.js

npm install drupal-client

Titanium/Alloy

Copy lib/drupal.js and lib/field.js into your app/lib/ folder.

Usage

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".

Get a session

drupal.systemConnect(
	//success
	function(sessionData) {
		var uid = sessionData.user.uid;
		console.log('session found for user '+uid);
	},
	//failure
	function(error) {
		console.log('boo :(');
	}
);

Create an account

var user = {
	name: 'my_new_username',
	pass: 'my_new_password',
	mail: '[email protected]'
};

drupal.createAccount(user,
	//success
	function(userData) {
		console.log('yay!');
	},
	//failure
	function(error) {
		console.log('boo :(');
	},
	headers //optional
);

Login

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.');
	}
);

Modify User Info

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.');
	}
);

Upload A File

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');
  }
);

Create a New Node

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);
  }
);

Make Requests

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.

Tests

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