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

catnapify

v0.1.22

Published

Promise-based, typescript ready, decorated wrapper around restify

Downloads

4

Readme

catnapify

catnapify logo Build Status

Catnapify is a pretty, decorated wrapper around restify. Using brand new, ES6 decorators, It allows developpers to move logic such as data validation, logging and sourcing datas. It is typescript ready and promise based. It also support hooks and header validation.

Try it out

Install with the usual

npm install catnapify --save

Then, create a server and a simple route:

import { Server, catnapify } from 'catnapify';

constructor(){ super() }	

	class MyServer extends Server {
	
		@catnapify('get', '/ping')	
		ping(request: Request) {
		
			return 'pong'	
		
		}
	
	
	}
	
	let serv = new MyServer;
	serv.listen()

That's it! No need to register the route. You can add middleware using the same API as in restify (using use()), and pass a customer restify instance to Server constructor. Notice that you can link a server to a Controller instance using .link(). The example above works because Server is itself inheritating from the Controller class.

Chaining

The decorated function must accept a Request object. This object is a wrapper around the usual req, res, done triplet, and is used to store custom data as well. It must return either a Response (which would follow a pattern {code: XXX, answer: {}}) or a simple object (in this case, the HTTP code 200 would be used). The function can also throw a Response, which would be correctly interpreted by the catnapify instance. The Response can or cannot be wrapped in a Promise (which allow for asynchrone threatement such as grabbing data from MongoDB)

Need and give

A weakness of restify is that it force the coder to check incoming and outgoing variable on everyroute. Catnapify prevent that by providing the give() and need() decorator. Both of them accept a variable name, an array of variable name, or a variable name and a validator, like in this exemple:

	interface Doggo {
    race: string;	
    name: string;
    good_doggo: boolean;
	}

	function isGoodDoggo(doggo: any) : boolean {
    return doggo.good_doggo;	
	}

	class TestController extends Controller {

		constructor(){ super() }	

		@catnapify('post', '/post')	
		@give('doggo', isGoodDoggo)
		post(request: Request) {

			return Promise.resolve({code: 200, give: {
					doggo: {
						race: 'Pit bull',
						name: 'Headeater',
						good_doggo: false
					}}})

		}

	}

If the route is not called with the right request parameters, catnapify will answer a 400 BAD REQUEST HTTP response.

Logger

Logger link to a bunzyan instance to log inbound and outbound stream. It can be linked with a loggerConfig instance to set in which stream the 'input', 'output', 'error' and 'internal' error will be showed.

	let loggerConfig = {
		logger: loggerMock,
		input: 'notice',
		output: 'trace'
	}

	class TestController extends Controller {

		constructor(){ super() }	

		@catnapify('post', '/post')	
		@logger(loggerConfig)
		post(request: Request) {

			return Promise.resolve({code: 200, response: 'ok'})

		}

	} 

Hooks

Catnapify also add a support for Hooks. It can execute function before or after the decorated function, and in case of an error.

		@catnapify('post', '/post')	
		@before(function(request: TestRequest){

			request.foo = 'bar'
			return request;	

		})
		post(request: TestRequest) {

			expect(request.foo).to.be.equal('bar')
			return Promise.resolve({code: 200, give: 'ok'})

		}