How to use the feathers-hooks-common.replaceItems 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.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

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
// 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 / test-expands / ts-cumulative-2-hooks.test-expected / src1 / hooks / hook.nedb12.ts View on Github external
// Throw if the hook is being called from an unexpected location.
    checkContext(context, null, ['find', 'get', 'create', 'update', 'patch', 'remove']);

    // Get the authenticated user.
    // tslint:disable-next-line:no-unused-variable
    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
// 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 feathersjs-ecosystem / feathers-hooks-common / types / tests.ts View on Github external
populate({
    schema: {
        include: {
            service: 'roles',
            nameAs: 'role',
            parentField: 'roleId',
            childField: '_id'
        }
    }
});

// $ExpectType Hook>
preventChanges(true, 'security.badge', 'abc');

// $ExpectType void
replaceItems(context1, [{}, {}]);

// $ExpectType Hook>
required('field1', 'field2');

// $ExpectType Promise
runHook()(keep('abc'))([]);
// $ExpectType Promise
runHook(context1)(keep('abc'))([]);

// $ExpectType Hook>
runParallel(hook1, x => x);
// $ExpectType Hook>
runParallel(hook1, x => x, 7);

// $ExpectType Hook>
serialize({
github kalisio / krawler / src / utils.js View on Github external
// Retrieve the items from the hook
    const items = getItems(hookObject)
    const isArray = Array.isArray(items)
    if (isArray) {
      for (let i = 0; i < items.length; i++) {
        // Handle error hooks as usual
        if (hook.type === 'error') items[i].error = _.omit(hook.error, ['hook']) // Avoid circular reference
        await f(items[i], hookObject)
      }
    } else {
      // Handle error hooks as usual
      if (hook.type === 'error') items.error = _.omit(hook.error, ['hook']) // Avoid circular reference
      await f(items, hookObject)
    }
    // Replace the items within the hook
    replaceItems(hookObject, items)
    return hookObject
  }
}
github Human-Connection / API / server / hooks / xss.js View on Github external
// items is an array so fall this function for all items
    items.forEach((item, key) => {
      items[key] = cleanAllFields(items[key], fields);
    });
  } else if (intersection(Object.keys(items), fields).length) {
    // clean value for all fields on the single given item
    fields.forEach((field) => {
      // get item by dot notation
      const value = getByDot(items, field);
      // set cleaned item by dot notation
      setByDot(items, field, clean(value));
    });
  }

  if (hook && items) {
    replaceItems(hook, items);
  }

  return items;
}
github feathers-plus / feathers-authentication-management / src / hooks / remove-verification.js View on Github external
}

      if (hook.params.provider && user) { // noop if initiated by server
        delete user.verifyExpires;
        delete user.resetExpires;
        delete user.verifyChanges;
        if (!ifReturnTokens) {
          delete user.verifyToken;
          delete user.verifyShortToken;
          delete user.resetToken;
          delete user.resetShortToken;
        }
      }
    });
    // Replace the items within the hook
    replaceItems(hook, isArray ? users : users[0]);
  };
}
github Human-Connection / API / server / helper / alter-items.js View on Github external
module.exports = func => hook => {
  if (!func || typeof func !== 'function') {
    return hook;
  }
  let items = getItems(hook);
  if (Array.isArray(items)) {
    items = items.map(item => func(item, hook));
  } else if (items) {
    items = func(items, hook);
  }
  replaceItems(hook, items);
  return hook;
};