8000 [FR] Support reverse application hooks · Issue #1548 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content

[FR] Support reverse application hooks #1548

@FossPrime

Description

@FossPrime

Steps to reproduce

If you need before hooks to run after all services, you'd have to copy paste the hook to every single service, or hack it into an extended adapter.

Solutions:

  1. add applications hooks: afterBefore, beforeAfter, beforeError that can handle the reverse scenario.
  2. make the cli generate apps with a hook filter on the service that can facility support for these cases. Hook middleware in a way.

My current solution:

Usage in service: service.hooks(proxyHooks(hooks))

// Application hooks that run for every service

const proxiedHooks = {
  before: {
    all: [],
    find: [],
    get: [],
    create: [ ],
    update: [ ],
    patch: [ ],
    remove: []
  },

  after: {
    all: [ ],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
}

modules.exports = function proxyHooks (hooks) {
  for (const baeK of Object.keys(hooks)) {
    const cruds = hooks[baeK]
    for (const crudK of Object.keys(cruds)) {
      const hook = cruds[crudK]
      if (Array.isArray(hook)) {
        cruds[crudK] = baeK === 'before'
          ? [...hook, ...proxiedHooks[baeK][crudK]]
          : [...proxiedHooks[baeK][crudK], ...hook]
      }
    }
  }
  return hooks
}

Conversation that inspired this: https://pastebin.com/fQh43HhT

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0