Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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;
};
};
// 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;
};
}
// 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;
};
};
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({
// 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
}
}
// 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;
}
}
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]);
};
}
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;
};