How to use the feathers-hooks-common.getItems function in feathers-hooks-common

To help you get started, we’ve selected a few feathers-hooks-common examples, based on popular ways it is used in public projects.

github feathers-plus / generator-feathers-plus / test-expands / a-gens / js / test-hook-integ.test-expected / src1 / hooks / hook.app1.js View on Github external
return async (context) => {
    // Throw if the hook is being called from an unexpected location.
    checkContext(context, null, ['find', 'get', 'create', 'update', 'patch', 'remove']);

    // Get the authenticated user.
    // eslint-disable-next-line no-unused-vars
    const { user } = context.params;
    // 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);

    /*
    Modify records and/or context.
     */

    // Place the modified records back in the context.
    replaceItems(context, records);
    // Best practice: hooks should always return the context.
    return context;
  };
};
github feathers-plus / generator-feathers-plus / examples / js / 12-test-examples / feathers-app / src / services / users / hooks / verify-email.js View on Github external
return async (context) => {
    // Throw if the hook is being called from an unexpected location.
    checkContext(context, 'after', ['find', 'get', 'create', 'update', 'patch', 'remove']);

    // Get the authenticated user.
    // eslint-disable-next-line no-unused-vars
    const { user } = context.params;
    // 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);

    /*
    Modify records and/or context.
     */

    // Place the modified records back in the context.
    replaceItems(context, records);
    // Best practice: hooks should always return the context.
    return context;
  };
};
github feathers-plus / feathers-authentication-management / src / hooks / remove-verification.js View on Github external
return hook => {
    checkContext(hook, 'after');
    // Retrieve the items from the hook
    let users = getItems(hook);
    if (!users) return;
    const isArray = Array.isArray(users);
    users = (isArray ? users : [users]);

    users.forEach(user => {
      if (!('isVerified' in user) && hook.method === 'create') {
        /* eslint-disable no-console */
        console.warn('Property isVerified not found in user properties. (removeVerification)');
        console.warn('Have you added authManagement\'s properties to your model? (Refer to README.md)');
        console.warn('Have you added the addVerification hook on users::create?');
        /* eslint-enable */
      }

      if (hook.params.provider && user) { // noop if initiated by server
        delete user.verifyExpires;
        delete user.resetExpires;
github kalisio / krawler / src / hooks / hooks.utils.js View on Github external
return async function (hook) {
    let hookObject = hook
    // Handle error hooks as usual
    if (hook.type === 'error') hookObject = hook.original
    // Retrieve the items from the hook and send it to healthcheck function
    options.function(getItems(hookObject), Healthcheck)
    debug('Applied function on Healthcheck', Healthcheck)
  }
}
github codingfriend1 / Feathers-Vue / server / hooks / associate-current-user.js View on Github external
return function (hook) {

  	if(!_.get(hook, `params.user[${settings.idField}]`)) {
  		throw new errors.NotAuthenticated(`The current user is missing. You must not be authenticated.`);
  	}

  	( _.castArray( getItems(hook) ) )
  	  .forEach(item => { item[settings.fieldToSet] = _.get(hook, `params.user[${settings.idField}]`); });

    // Hooks can either return nothing or a promise
    // that resolves with the `hook` object for asynchronous operations
    return Promise.resolve(hook);
  };
};
github codingfriend1 / Feathers-Vue / server / hooks / set-first-user-to-role.js View on Github external
hook.app.service('/users').find({query: {}}).then(function(found) {
      console.log("Checking if first user");
      if(!Array.isArray(found) && found.data) {
        found = found.data
      }

      if(found.length === 0) {
        var firstUser = _.castArray(getItems(hook))[0];
        
        firstUser.role = options.role || 'admin'
        console.log('set role to admin');
      }

      resolve(hook)
    }, function(err) {
      reject(err)
github codingfriend1 / Feathers-Vue / server / hooks / set-default-role.js View on Github external
return new Promise(async (resolve, reject) => {
      if(hook.data) {

        let [ err, defaultRole ] = await to( hook.app.service('settings').find({ name: 'defaultRole' }) );

        if(!err) {
          defaultRole = _.get(defaultRole, '0');
          const role = _.get(defaultRole, 'value.role') || 'basic';

          ( _.castArray( getItems(hook) ) )
            .forEach(item => { item.role = role; });

        } else {
          console.log('Error setting default role', err);
        }

        resolve(hook);
      }
    })
  }
github kalisio / krawler / src / hooks / hooks.xml.js View on Github external
return async function (hook) {
    const item = getItems(hook)

    const store = await getStoreFromHook(hook, 'readXML', options)
    if (!store.path && !store.buffers) {
      throw new Error('The \'readXML\' hook only work with the fs or memory blob store.')
    }

    let xml
    const xmlName = item.id
    if (store.path) {
      const filePath = path.join(store.path, xmlName)
      debug('Reading XML file ' + filePath)
      xml = await fs.readFile(filePath)
    } else {
      debug('Parsing XML for ' + xmlName)
      xml = store.buffers[xmlName]
    }
github kalisio / krawler / src / hooks / hooks.json.js View on Github external
return async function (hook) {
    const item = getItems(hook)

    const store = await getStoreFromHook(hook, 'readJson', options)
    if (!store.path && !store.buffers) {
      throw new Error('The \'readJson\' hook only work with the fs or memory blob store.')
    }

    let json = {}
    const jsonName = template(item, options.key || item.id)

    if (store.path) {
      const filePath = path.join(store.path, jsonName)
      debug('Reading JSON file ' + filePath)
      json = await fs.readJson(filePath)
    } else {
      debug('Parsing JSON for ' + jsonName)
      const data = store.buffers[jsonName].toString()
github kalisio / krawler / src / hooks / hooks.yaml.js View on Github external
return async function (hook) {
    const item = getItems(hook)

    const store = await getStoreFromHook(hook, 'readYAML', options)
    if (!store.path && !store.buffers) {
      throw new Error('The \'readYAML\' hook only work with the fs or memory blob store.')
    }

    let yaml
    const yamlName = template(item, options.key || item.id)
    if (store.path) {
      const filePath = path.join(store.path, yamlName)
      debug('Reading YAML file ' + filePath)
      yaml = await fs.readFile(filePath)
    } else {
      debug('Parsing YAML for ' + yamlName)
      yaml = store.buffers[yamlName]
    }