Skip to content

Commit

Permalink
fix: Update withParams (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertho-zero committed Feb 12, 2020
1 parent c89bccd commit b89d044
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
42 changes: 29 additions & 13 deletions packages/hooks/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,39 @@ export function withParams<T = any> (...params: Array<string | [string, any]>) {
context[name] = args[index] === undefined ? defaultValue : args[index];
});

if (!context.arguments) {
if (params.length > 0) {
Object.defineProperty(context, 'arguments', {
get (this: HookContext<T>) {
const result = params.map(param => {
const name = typeof param === 'string' ? param : param[0];
return this[name];
if (params.length > 0) {
Object.defineProperty(context, 'arguments', {
enumerable: true,
get (this: HookContext<T>) {
const result: any = [];

params.forEach((param, index) => {
const name = typeof param === 'string' ? param : param[0];

Object.defineProperty(result, index, {
enumerable: true,
configurable: true,
get: () => this[name],
set: (value) => {
this[name] = value;
if (result[index] !== this[name]) {
result[index] = value;
}
}
});

return Object.freeze(result);
}
});
} else {
context.arguments = args;
}
this[name] = result[index];
});

return result;
}
});
} else if (!context.arguments) {
context.arguments = args;
}

Object.seal(context.arguments);

if (self) {
context.self = self;
}
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/test/decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('hookDecorator', () => {
assert.deepStrictEqual(ctx, new HookContext({
method: 'sayHi',
self: instance,
arguments: ['David NameFromTopLevel NameFromDummyClass'],
name: expectedName
}));

Expand Down
47 changes: 42 additions & 5 deletions packages/hooks/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ describe('functionHooks', () => {
assert.equal(await fn('Dave'), 'Hello Changed');
});

it('with named context ctx.arguments is frozen', async () => {
it('ctx.arguments is configurable with named params', async () => {
const modifyArgs = async (ctx: HookContext, next: NextFunction) => {
ctx.arguments[0] = 'Test';
ctx.arguments[0] = 'Changed';
ctx.arguments.push('no');

assert.equal(ctx.name, ctx.arguments[0]);

await next();
};
Expand All @@ -217,9 +220,15 @@ describe('functionHooks', () => {
context: withParams('name')
});

await assert.rejects(() => fn('There'), {
message: `Cannot assign to read only property '0' of object '[object Array]'`
});
const customContext = new HookContext({});
const resultContext = await fn('Daffl', {}, customContext);

assert.equal(resultContext, customContext);
assert.deepEqual(resultContext, new HookContext({
arguments: ['Changed'],
name: 'Changed',
result: 'Hello Changed'
}));
});

it('can take and return an existing HookContext', async () => {
Expand All @@ -242,6 +251,34 @@ describe('functionHooks', () => {

assert.equal(resultContext, customContext);
assert.deepEqual(resultContext, new HookContext({
arguments: ['Changed'],
message: 'Custom message',
name: 'Changed',
result: 'Hello Changed'
}));
});

it('takes parameters with multiple withParams', async () => {
const message = 'Custom message';
const fn = hooks(hello, {
middleware: [
async (ctx, next) => {
assert.equal(ctx.name, 'Dave');
assert.equal(ctx.message, message);

ctx.name = 'Changed';
await next();
}
],
context: [withParams(), withParams('name'), withParams()]
});

const customContext = new HookContext({ message });
const resultContext: HookContext = await fn('Dave', {}, customContext);

assert.equal(resultContext, customContext);
assert.deepEqual(resultContext, new HookContext({
arguments: ['Changed'],
message: 'Custom message',
name: 'Changed',
result: 'Hello Changed'
Expand Down
8 changes: 5 additions & 3 deletions packages/hooks/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('objectHooks', () => {
const hookedObj = hooks(obj, {
sayHi: [async (ctx: HookContext, next: NextFunction) => {
assert.deepEqual(ctx, new HookContext({
arguments: [ 'David' ],
self: obj,
method: 'sayHi',
arguments: [ 'David' ]
method: 'sayHi'
}));

await next();
Expand All @@ -59,6 +59,7 @@ describe('objectHooks', () => {
sayHi: {
middleware: [async (ctx: HookContext, next: NextFunction) => {
assert.deepStrictEqual(ctx, new HookContext({
arguments: ['David'],
method: 'sayHi',
name: 'David',
self: obj
Expand Down Expand Up @@ -122,6 +123,7 @@ describe('objectHooks', () => {
sayHi: {
middleware: [async (ctx: HookContext, next: NextFunction) => {
assert.deepStrictEqual(ctx, new HookContext({
arguments: ['David'],
self: instance,
method: 'sayHi',
name: 'David'
Expand Down Expand Up @@ -150,8 +152,8 @@ describe('objectHooks', () => {
hooks(DummyClass, {
sayHi: [async (ctx: HookContext, next: NextFunction) => {
assert.deepStrictEqual(ctx, new HookContext({
method: 'sayHi',
arguments: [ 'David' ],
method: 'sayHi',
self: instance
}));

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ hooks(HelloSayer.prototype, {
With decorators and inheritance

```js
import { hooks, params, HookContext, NextFunction } from '@feathersjs/hooks';
import { hooks, withParams, HookContext, NextFunction } from '@feathersjs/hooks';

@hooks([
async (context: HookContext, next: NextFunction) => {
Expand Down

0 comments on commit b89d044

Please sign in to comment.