Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #20 from acquia/prefill-resources
Browse files Browse the repository at this point in the history
Ability to prefill resources, error handling and docs
  • Loading branch information
mattgrill authored Jul 25, 2016
2 parents f7685e2 + 279ede1 commit 4c7f3d7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ const waterwheel = new Waterwheel('http://test.dev', {username: 'admin', 'passwo
// Browser
import '../../path/to/node_modules/waterwheel/dist/waterwheel.js'
const waterwheel = new window.Waterwheel('http://test.dev', {username: 'admin', 'password': '1234'});

// With resources
const waterwheel = new Waterwheel('http://test.dev', {username: 'admin', 'password': '1234'}, require('./resources.json'));
```

Waterwheel when instantiated accepts two arguments
Waterwheel when instantiated accepts three arguments,
- `base`: The base path for your Drupal instance. All request paths will be built from this base
- `credentials`: An object containing the `username` and `password` used to authenticate with Drupal.
- `resources`: A JSON object that represents the resources available to `waterwheel`.

Supplying the `resources` object is equivalent to calling `.populateResources()` but does not incur an HTTP request, and alleviates the need to call `.populateResources()` prior to making any requests. You can fetch this object by calling `waterwheel.fetchResources()`. Additionally if a valid `resources` object is passed, `credentials` become optional when `waterwheel` is instantiated.

### Populate `waterwheel` resources

Expand Down
27 changes: 25 additions & 2 deletions lib/waterwheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,38 @@ module.exports = class Waterwheel extends Request {
* The base path of the Drupal site.
* @param {object} credentials
* The credentials object.
* @param {object} resources
* An object representing all the resources available to waterwheel.
*/
constructor(base, credentials) {
constructor(base, credentials, resources = null) {

if (base === '' || !base) {
throw new Error('Missing base path.');
}

if (arguments.length === 2 && !credentials) {
throw new Error('Missing credentials.');
}

super(base, credentials);

this.base = base;
this.credentials = credentials;
this.credentials = credentials ? credentials : false;
this.api = {
query: entityType => new Query(base, credentials, entityType)
};

if (resources && Object.keys(resources).length) {
Object.keys(resources).forEach(entity => {
this.api[entity] = {};
(resources[entity].bundles ? resources[entity].bundles : [resources[entity].label.toLowerCase().replace(/ /g, '_')]).forEach(bundle => {
this.api[entity][bundle] = new Entity(this.base, this.credentials, resources[entity].methods, entity, bundle, resources[entity].more);
if (entity === bundle) {
this.api[entity] = this.api[entity][bundle];
}
});
});
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waterwheel",
"version": "0.6.2",
"version": "0.6.3",
"description": "A generic JavaScript helper library to query and manipulate Drupal 8 via core REST",
"author": "Preston So <preston.so@acquia.com>",
"contributors": [
Expand Down
14 changes: 14 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ test('Waterwheel Creation', t => {
t.is(true, waterwheel instanceof t.context.Waterwheel, 'Unexpected creation.');
});

test('Waterwheel Creation - Missing informations', t => {
t.plan(3);

t.throws(() => new t.context.Waterwheel(null, null), 'Missing base path.');
t.throws(() => new t.context.Waterwheel(null, null, {}), 'Missing base path.');
t.throws(() => new t.context.Waterwheel('http://foo.dev', null), 'Missing credentials.');
});

test('Waterwheel Creation - Create with resources', t => {
t.plan(1);
const waterwheel = new t.context.Waterwheel('http://foo.dev', null, entityTypes);
t.is(true, waterwheel instanceof t.context.Waterwheel, 'Unexpected creation.');
});

test('Create New Entity Query', t => {
t.plan(1);
const Query = requireSubvert.require('../lib/resources/entityQuery');
Expand Down

0 comments on commit 4c7f3d7

Please sign in to comment.