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

Open hook workflow to custom methods #72

Merged
merged 11 commits into from
May 29, 2018

Conversation

bertho-zero
Copy link
Contributor

@bertho-zero bertho-zero commented May 1, 2018

This PR allows you to add converters to your custom methods, which allows to create the hook corresponding to your arguments and finally to fully use the hook workflow.

I would have liked to use a Symbol but feathersjs/feathers#863.

Example:

const app = feathers().use('/dummy', {
  methods: {
    custom: ['id', 'data', 'params']
  },
  get() {},
  // ...
  custom(id, data, params) {
    return Promise.resolve([id, data, params]);
  }
});

app.service('dummy').hooks({
  before: {
    all: [/**/],
    custom: [/**/]
  },
  after: {
    all: [/**/],
    custom: [/**/]
  }
});

app.service('dummy').custom(28, { bar: 'foo' }, { provider: 'rest' });

@daffl
Copy link
Member

daffl commented May 4, 2018

This is great, thank you! I was actually thinking about doing the same thing for #853. I'm not sure if the converters are needed. I was going to put the arguments array for unknown methods into hook.arguments.

@bertho-zero
Copy link
Contributor Author

I chose the simplest solution, the one used internally but it's true that converters can easily be translated from ['id', 'data', 'params'].

I do not know where exactly you would like to declare hook.arguments?

@bertho-zero
Copy link
Contributor Author

Do you think of something like this?

const app = feathers().use('/dummy', {
  methods: {
    custom: ['id', 'data', 'params']
  },
  get() {},
  // ...
  custom(id, data, params) {
    return Promise.resolve([id, data, params]);
  }
});

app.service('dummy').hooks({
  before: {
    all: [/**/],
    custom: [/**/]
  },
  after: {
    all: [/**/],
    custom: [/**/]
  }
});

return app.service('dummy').custom(28, { bar: 'foo' }, { provider: 'rest' });

or

const app = feathers().use('/dummy', {
  methods: ['custom'],
  get() {},
  // ...
  custom(id, data, params) {
    return Promise.resolve([id, data, params]);
  }
});

app.service('dummy').hooks({
  arguments: {
    custom: ['id', 'data', 'params']
  }
  before: {
    all: [/**/],
    custom: [/**/]
  },
  after: {
    all: [/**/],
    custom: [/**/]
  }
});

return app.service('dummy').custom(28, { bar: 'foo' }, { provider: 'rest' });

@bertho-zero
Copy link
Contributor Author

I have updated the first example.

@daffl
Copy link
Member

daffl commented May 14, 2018

What I mean is that context.arguments should contain the actual method arguments because I don't think we need a separate mechanism to convert non-standard method arguments since that can be done already in a hook if needed:

app.hooks({
  before: {
    myMethod(context) {
      const [ id, data, something ] = context.arguments;

      return Object.assign(context, {
        id, data, something
      });
    }
  }
});

In fact, I just realized that we might not need converters at all since the same can totally be done in the already existing argument validation hook (https://github.com/feathersjs/feathers/blob/master/lib/hooks.js#L47) for the normal service methods.

@bertho-zero
Copy link
Contributor Author

I do not see exactly what changes I should make based on your comment, can you point me?

@daffl
Copy link
Member

daffl commented May 23, 2018

I'll merge this one and then will update it accordingly.

@bertho-zero
Copy link
Contributor Author

I have updated this and feathersjs/feathers#864.

@bertho-zero
Copy link
Contributor Author

I deleted converters, it's a hook that is just after validateHook that adds arguments for each method.
And I also modified validateArguments to validate all methods.

@daffl
Copy link
Member

daffl commented May 29, 2018

This looks great, thank you! I'll get this one in and then verify it against feathersjs/feathers#864

@daffl daffl merged commit 78d780d into feathersjs-ecosystem:master May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants