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

trailpack-annotations

v2.0.0

Published

Add routes, policies and custom annotations support for Tails.js applications

Downloads

96

Readme

trailpack-annotations

Gitter NPM version NPM downloads Build status Test status Dependency Status Code Climate Beerpay Beerpay

:package: Add Route, Policy and custom annotations support for Tails.js applications

Intallation

With yo :

npm install -g yo generator-trails
yo trails:trailpack trailpack-annotations

With npm (you will have to create config file manually) :

npm install --save trailpack-annotations

Configuration

First you need to add this trailpack to your main configuration :

// config/main.js

module.exports = {
   ...

   packs: [
      ...
      require('trailpack-annotations'),
      ...
   ]
   ...
}

Then :

// config/annotations.js
module.exports = {
  policy: true,//enable policy annotations
  route: true,//enable route annotations
  pathToScan: './api/controllers',//or ./api for hmvc
  customAnnotations: null, //Add your custom annotations here, require('./annotations') for example

}

Usage

Route

A route added with annotation will replace any previous route set under config/routes.js (for a same path).

module.exports = class DefaultController extends Controller {

  /**
   * Return some info about this application
   * @Route("GET /default/info") or @Route({method: ["GET"], path: "/default/info"})
   */
  info (request, reply) {
    reply.json(this.app.services.DefaultService.getApplicationInfo())
  }
}

You can also use @METHOD for defining new routes.

module.exports = class DefaultController extends Controller {

  /**
   * Return some info about this application
   * @GET('/default/info')
   * @HEAD('/default/info')
   * @OPTIONS('/default/info')
   * @POST('/default/info')
   * @PUT('/default/info')
   * @PATCH('/default/info')
   * @DELETE('/default/info')
   */
  info (request, reply) {
    reply.json(this.app.services.DefaultService.getApplicationInfo())
  }
}

A more complex sample with validation.

module.exports = class DefaultController extends Controller {

  /**
   * Return some info about this application
   * @GET(path:{'/default/info'}, config: { validate: {
   * query: { infos: Joi.sring().required() }
   * }})
   */
  info (request, reply) {
    reply.json(this.app.services.DefaultService.getApplicationInfo())
  }
}

See hapijs tutorial on validation and joi schema validation for more complex with validate object.

Policy

A policy added with annotation will be added to policies set under config/policies.js.

module.exports = class DefaultController extends Controller {

  /**
   * Return some info about this application
   * @Policy("Default.auth") or @Policy(["Default.auth", "Default.acl"])
   */
  info (request, reply) {
    reply.json(this.app.services.Defaultervice.getApplicationInfo())
  }
}

Custom

Create your own annotation like this :

'use strict'
const Annotation = require('ecmas-annotations').Annotation

module.exports = class MyCustomAnnotation extends Annotation{

    /**
     * The possible targets
     *
     * (Annotation.CONSTRUCTOR, Annotation.PROPERTY, Annotation.METHOD)
     *
     * @type {Array}
     */
    static get targets() {
      return [Annotation.METHOD]
    }

    /**
     * The function to call when annotations are find
     *
     * @type {Function}
     */
    handler(app, annotation) {
      //Do whatever you want when annotation is found
    }

    /**
     * File path
     *
     * @type {String}
     * @required
     */
    static get path() {
      return __filename
    }

}

Now I can add @MyCustomAnnotation("It's works") on methods.

License

MIT

Support on Beerpay

Hey dude! Help me out for a couple of :beers:!

Beerpay Beerpay