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

Update to new plugin infrastructure #53

Merged
merged 3 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Logs
logs
*.log
Expand Down Expand Up @@ -26,5 +28,3 @@ node_modules

# Users Environment Variables
.lock-wscript

lib/
7 changes: 3 additions & 4 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
verbose: false
instrumentation:
root: src/
root: lib/
excludes:
- lib/
- src/test-fixture.js
- src/test/
- lib/test-fixture.js
- lib/test/
include-all-sources: false
reporting:
print: summary
Expand Down
8 changes: 4 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.editorconfig
.jshintrc
.travis.yml
.istanbul.yml
.babelrc
.idea/
src/
.vscode/
test/
!lib/
.github/
coverage
coverage/
.github/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: node_js
node_js:
- node
- '6'
- '4'
addons:
code_climate:
repo_token: 9231342f0075572264b1eb61e9c4967d9d97534cd4a609a4c1b83caeef380b86
Expand Down
4 changes: 2 additions & 2 deletions src/arguments.js → lib/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function isObjectOrArray (value) {
return typeof value === 'object' && value !== null;
}

export function validateArguments (method, args) {
exports.validateArguments = function validateArguments (method, args) {
// Check if the last argument is a callback which are no longer supported
if (typeof args[args.length - 1] === 'function') {
throw new Error('Callbacks are no longer supported. Use Promises or async/await instead.');
Expand Down Expand Up @@ -53,4 +53,4 @@ export function validateArguments (method, args) {
}

return true;
}
};
5 changes: 5 additions & 0 deletions lib/commons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const utils = require('./utils');
const hooks = require('./hooks');
const args = require('./arguments');

module.exports = Object.assign({}, utils, args, { hooks });
54 changes: 22 additions & 32 deletions src/hooks.js → lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { each } from './utils';
const { each } = require('./utils')._;

function getParams (value) {
return value || {};
Expand All @@ -19,8 +19,7 @@ function convertUpdateOrPatch (args) {
};
}

// Service method argument to hook object converters
export const converters = {
exports.converters = {
find (args) {
return {
params: getParams(args[0])
Expand All @@ -38,10 +37,8 @@ export const converters = {
patch: convertUpdateOrPatch
};

// Creates a new hook object from given method, type and arguments
// `app` can be either the app or an object to extend this hook object with
export function createHookObject (method, args, data = {}) {
const hook = converters[method](args);
exports.createHookObject = function createHookObject (method, args, data = {}) {
const hook = exports.converters[method](args);

return Object.assign(hook, data, {
method,
Expand All @@ -56,11 +53,9 @@ export function createHookObject (method, args, data = {}) {
.find(path => app.services[path] === service);
}
});
}
};

// A fallback for creating arguments
// Probably not used in reality but kept for ambiguous method calls
export function defaultMakeArguments (hook) {
exports.defaultMakeArguments = function defaultMakeArguments (hook) {
const result = [];

if (typeof hook.id !== 'undefined') {
Expand All @@ -74,10 +69,9 @@ export function defaultMakeArguments (hook) {
result.push(hook.params || {});

return result;
}
};

// Turns the hook object back into actual service method arguments
export function makeArguments (hook) {
exports.makeArguments = function makeArguments (hook) {
if (hook.method === 'find') {
return [ hook.params ];
}
Expand All @@ -94,11 +88,10 @@ export function makeArguments (hook) {
return [ hook.data, hook.params ];
}

return defaultMakeArguments(hook);
}
return exports.defaultMakeArguments(hook);
};

// Converts the different ways hooks can be registered into the same format
export function convertHookData (obj) {
exports.convertHookData = function convertHookData (obj) {
var hook = {};

if (Array.isArray(obj)) {
Expand All @@ -112,16 +105,15 @@ export function convertHookData (obj) {
}

return hook;
}
};

// A hook object is anything that has a `method` and `type` string property
export function isHookObject (hookObject) {
exports.isHookObject = function isHookObject (hookObject) {
return typeof hookObject === 'object' &&
typeof hookObject.method === 'string' &&
typeof hookObject.type === 'string';
}
};

export function getHooks (app, service, type, method, appLast = false) {
exports.getHooks = function getHooks (app, service, type, method, appLast = false) {
const appHooks = app.__hooks[type][method] || [];
const serviceHooks = service.__hooks[type][method] || [];

Expand All @@ -131,13 +123,13 @@ export function getHooks (app, service, type, method, appLast = false) {
}

return appHooks.concat(serviceHooks);
}
};

export function processHooks (hooks, initialHookObject) {
exports.processHooks = function processHooks (hooks, initialHookObject) {
let hookObject = initialHookObject;
let updateCurrentHook = current => {
if (current) {
if (!isHookObject(current)) {
if (!exports.isHookObject(current)) {
throw new Error(`${hookObject.type} hook for '${hookObject.method}' method returned invalid hook object`);
}

Expand Down Expand Up @@ -172,11 +164,9 @@ export function processHooks (hooks, initialHookObject) {
error.hook = hookObject;
throw error;
});
}
};

// Adds a `.hooks` method and properties to store hook functions
// to an existing object (used for both, app and services)
export function enableHooks (obj, methods, types) {
exports.enableHooks = function enableHooks (obj, methods, types) {
if (typeof obj.hooks === 'function') {
return obj;
}
Expand All @@ -200,7 +190,7 @@ export function enableHooks (obj, methods, types) {
throw new Error(`'${type}' is not a valid hook type`);
}

const hooks = convertHookData(obj);
const hooks = exports.convertHookData(obj);

each(hooks, (value, method) => {
if (method !== 'all' && methods.indexOf(method) === -1) {
Expand All @@ -225,4 +215,4 @@ export function enableHooks (obj, methods, types) {
return this;
}
});
}
};
2 changes: 1 addition & 1 deletion src/test/client.js → lib/test/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
const assert = require('assert');

export default function (app, name) {
const getService = () => (name && typeof app.service === 'function')
Expand Down
6 changes: 3 additions & 3 deletions src/test/fixture.js → lib/test/fixture.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
const assert = require('assert');

const findAllData = [{
id: 0,
Expand All @@ -8,7 +8,7 @@ const findAllData = [{
description: 'You have to do laundry'
}];

export const Service = {
exports.Service = {
events: [ 'log' ],

find () {
Expand Down Expand Up @@ -72,7 +72,7 @@ export const Service = {
}
};

export const verify = {
exports.verify = {
find (data) {
assert.deepEqual(findAllData, data, 'Data as expected');
},
Expand Down
Loading