new PolicyExecutor(options)
The PolicyExecutor is in charge of executing policies. Policies can be provided when constructing or when constructed by using the
PolicyExecutor#add
method, policies can be executed using the PolicyExecutor#execute
method. For information on the req object passed into policies, please refer to the documentation of the Request
.
Parameters:
Name | Type | Description |
---|---|---|
options |
Object | Object with the properties listed below |
- See:
Properties:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
policies |
Object.<function()> | Hashmap with policies | ||
reqFactory |
function |
<optional> |
Request | The Request factory used to created the req object passed to policies.
The Request factory is called with two argument, the params/data of this 'request' and an object the request should be extended with, see Request for more information. Your implementation should meet the specifications defined there |
Example
const policyExecutor = PolicyExecutor({ policies: { isLoggedIn(req) { return new Promise((resolve, reject) => { // determine whether the user is logged in or not // resolve if he / she is, // reject if he / she isn't }); } } });
Methods
-
add(name, predicate)
-
Adds one or more policies.
Parameters:
Name Type Description name
String | Object Name of the policy, or a hashmap of policies predicate
function | undefined Function that returns a promise Example
policyExecutor.add('isLoggedIn', function (req) {...}); // or policyExecutor.add({ 'isLoggedIn': function (req) {...} });
-
execute(policies, data) → {Promise}
-
Executes one or more policies with data. If no policies are passed it returns a resolving Promise
Parameters:
Name Type Argument Default Description policies
String | Array.<String> The policy/policies to execute data
Object <optional>
{} Parameters/data for this policy 'request' Returns:
- Type
- Promise
Example
policyExecutor.execute('isLoggedIn', {someParam: 'someValue'}); policyExecutor.execute(['isLoggedIn'], {someParam: 'someValue'});