npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mycro-util-policies

v0.2.0

Published

common policies for mycro apps

Downloads

3

Readme

mycro-util-policies

a hook that common policies for mycro apps

Install

Install the package

npm install --save mycro-util-policies

Include it in your /config/hooks.js file after the default policies hook

// in config/hooks/js

module.exports = [
    // ..
    'policies',
    'mycro-util-policies'
    // ..
];

Policies

#if

Executes a policy, and based on whether or not the policy fails, executes a pass or fail policy.

Arguments
  1. testPolicy (string|function) - the policy to test
  2. passPolicy (string|function) - the policy to execute if the test policy passes
  3. failPolicy (string|function) - the policy to execute if the test policy fails
// in app/routes.js
module.exports = function(mycro) {
    return {
        'v1.0.0': {
            '/posts': {
                policies: [
                    mycro.policies.if(
                        mycro.policies.memberOf('editors'),
                        mycro.policies.filter({status: ['published', 'unpublished']}),
                        mycro.policies.filter({status: ['published']})
                    )
                ]
            }
        }
    }
};

#not

Inverts the outcome of a policy.

Arguments
  1. policy (string|function) - the policy to invert
  2. [options] (object) - options
  3. [options.error] (object) - custom error options
  4. [options.error.status] (number) - custom error response status (defaults to 400)
  5. [options.error.error] (string|object) - custom error response error
// in app/routes.js
module.exports = function(mycro) {
    return {
        'v1.0.0': {
            '/posts': {
                policies: [
                    mycro.policies.if(
                        mycro.policies.not('authenticated'),
                        mycro.policies.filter({scope: 'public', status: 'published'}),
                        mycro.policies.filter({scope: '*', status: 'published'})
                    )
                ]
            }
        }
    }
};

#or

Tests multiple policies, until one passes, in which case the policy passes. Otherwise, the policy fails.

Arguments
  1. policies (...string|function) - one or more policies to test
  2. [options] (object) - options
  3. [options.handleError] (function) - error handler policy (req, res, next)
  4. [options.error] (object) - custom error options
  5. [options.error.status] (number) - custom error response status (defaults to 403)
  6. [options.error.error] (string|object) - custom error response error (defaults to 'Forbidden')
// in app/routes.js
module.exports = function(mycro) {
    return {
        'v1.0.0': {
            '/posts': {
                policies: [
                    mycro.policies.if(
                        mycro.policies.not('authenticated'),
                        mycro.policies.filter({scope: 'public', status: 'published'}),
                        mycro.policies.filter({scope: '*', status: 'published'})
                    )
                ]
            }
        }
    }
};

#validate

Validate the request using joi, returns 401 if validation fails.

Arguments
  1. [container] (string) - the part of the request to validate (body, cookies, headers, query). if no container is specified, the entire request is validated. The validated attributes are then merged into the request object.
  2. factoryFn (function) - a function that receives a joi instance and returns a valid joi schema.
  3. [options] (object) - joi validation options
  4. [options.error] (object) - custom error options
  5. [options.error.status] (number) - custom error response status (defaults to 400)
  6. [options.error.error] (string|object) - custom error response error
// in app/routes.js
module.exports = function(mycro) {
    return {
        'v1.0.0': {
            '/posts': {
                policies: [
                    // limit query params to id, title, and status
                    mycro.policies.validate('query', function(joi) {
                        return joi.object({
                            id: joi.number().integer()
                            title: joi.string(),
                            status: joi.string().valid('published', 'unpublished').default('published')
                        })
                    }, {
                        allowUnknown: true,
                        convert: true,
                        stripUnknown: true
                    })
                ]
            }
        }
    }
};

Testing

run all tests

npm test

run coverage

grunt coverage

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2016 Kutler Skaggs, Inc. Licensed under the MIT license.