
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html

<%- tplTsOnly([`import { Hook } from '@feathersjs/feathers'${sc}${EOL}`]) -%>
<%- tplImports('{ checkContext, getItems, replaceItems }', 'feathers-hooks-common') %>

// <%- lintDisableNextLineUnused %>
<%- tplExport('function (options = {}) {', 'function (options: any = {}): Hook {') %>

  // Return the actual hook.
  return async (context) => {
    // Throw if the hook is being called from an unexpected location.
    checkContext(context, null, ['find', 'get', 'create', 'update', 'patch', 'remove'])<%- sc %>

    // Get the authenticated user.
    // <%- lintDisableNextLineUnused %>
    const { user } = context.params<%- tplTsOnly(['!']) -%><%- sc %>
    // Get the record(s) from context.data (before), context.result.data or context.result (after).
    // getItems always returns an array to simplify your processing.
    const records = getItems(context)<%- sc %>

    /*
    Modify records and/or context.
     */

    // Place the modified records back in the context.
    replaceItems(context, records)<%- sc %>
    // Best practice: hooks should always return the context.
    return context<%- sc %>
  }<%- sc %>
}<%- tplJsOnly(sc) %>

// Throw to reject the service call, or on an unrecoverable error.
// <%- lintDisableNextLineUnused %>
function error(msg<%- tplTsOnly([': string']) -%>) {
  throw new Error(msg)<%- sc %>
}
