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

fast-api

v0.4.0

Published

Build RESTful APIs in seconds.

Downloads

73

Readme

Fast API

Create RESTful API in seconds.

Build Status npm version Dependency Status

With Fast, you don't have to define routes for each service in your API. Just put your service in your API folder, and the path to the service will be his access point. You can also nest folders, Fast will handle it.

Each module should exports 'routes' object which defines the services in this module.

In the example below the module 'myservice.js' expose 2 GET services:

  • http://YOUR-APP/api/myservice/
  • http://YOUR-APP/api/myservice/:id

Inside your service, 'Core' object is available.

If you want to use one of your services in other module, just expose the service with the name 'service' like the second service in myservice.js file. Then, call it from other modules like this:

var result = Core.api.myservice();

Beyond the scene, Fast is wraping eash API call will "bluebird" promise. So you can make DB calls, read files, etc.. and Just return the promise. Fast will handle it. "bluebird" module is available at

Core.promise

You have access to the lodash utility module from:

Core.utils

You can change the default '/api' path for your API folder. Just add this to the createServer options:

apiName : "MY_CUSTOM_PATH"

For each service you need to define his params. Only this params with this settings will be valid for this service.

By default, Fast will expose for you API documentation in JSON format in this path:

/YOUR_API_FOLDER/docs

You can change it by adding this to the createServer options:

apiDocsPath : "MY_CUSTOM_DOCS_PATH"

and the path will be:

/YOUR_API_FOLDER/MY_CUSTOM_DOCS_PATH

To disable the documentation feature, add this to createServer options:

exposeDocs : false

Fast has built on top of Express so you are more then welcome to fork on github and start hacking.

Install

npm install fast-api

Example:

app.js

var Fast    = require( 'fast-api' ),
    Path    = require('path' );

var app = Fast.createServer({
        apiRoot : Path.join( __dirname, "api" )
});

app.listen( "4000" );

Then, in the api folder, you can have this file:

myservice.js

module.exports.routes = {
    "/"  	: {
        summery 	: "Get list",
        httpMethod 	: "get",
        parameters 	:[
            { name : "username", description : "User Name", required : true, dataType : "string", allowMultiple : true, paramType : "query" }
        ],

        service 	: "service"
    },

    "/:id" 	: {
        summery 	: "Get list by ID",
        httpMethod 	: "get",
        parameters	: [
            { name : "id", description : "User ID", required : true, dataType : "string", allowMultiple : true, paramType : "path" }
        ],

        service 	: "getByID"
    }
};

module.exports.subscribers = {
	"set_private"  	: {
    		summery 	: "Set Private Message",
    		parameters 	: [
    			{ name 		: "receiver_id",	description : "Receiver ID", 	required : true, dataType : "string", allowMultiple : false },
    			{ name 		: "sender_id",	 	description : "Sender ID", 		required : true, dataType : "string", allowMultiple : false },
    			{ name 		: "media_type",	 	description : "Media Type", 	required : true, dataType : "number", allowMultiple : false },
    			{ name 		: "content",	 	description : "Content", 		required : true, dataType : "string", allowMultiple : false },
    			{ name 		: "balloon_color",	description : "Balloon Color", 	required : false, dataType : "string", allowMultiple : false },
    			{ name 		: "text_color",		description : "Text Color", 	required : false, dataType : "string", allowMultiple : false }
    		],
    
    		service 	: "setPrivate"
    	}
};

module.exports.privileges = {
    "service" : 0,
    "getByID" : 1
};

module.exports.getByID = function( req ){
    return req.params.id;
};

module.exports.setPrivate = function( req ){
	req.body.timestamp 	= Core.date.unix();
	req.body.sent 		= 1;
	req.body.received 	= 0;
	req.body.likes 		= [];

	Core.models.message.setPrivate( req.body ).then( res.success, res.error );

	if( req.isSocket ){
		var sender 		= Core.socket.getSocket( req.body.sender_id ),
			receiver 	= Core.socket.getSocket( req.body.receiver_id );

		sender.broadcast.to( receiver ).emit( "message", req.body );
	}
};

module.exports.service = function( req ){
    return "done";
};

Go to http://YOUR-APP/api/myservice and........ Voila!

Available options for createServer method and defaults

{
    apiName	                    : "api",
    apiDocsPath		            : "docs",
    exposeDocs		            : true,
    enableWebSocket	            : false,
    webSocketConnectionCallback	: false,
    
}

The listen method accept 2 parameters, both are optional:

app.listen( port, callback );

The default port is 3000. The callback gets no params and invoked when Fast finisg the init phase and ready for requests..

Extra Modules

You can inject to the Core object one property of you own. For example, if you want to have 'models' module for the DB layer, add this to the options:

extraModules : "models"

Then create 'models' directory right under your project root. Lets say we have 'message' model. ( meaning 'message.js' file inside this directory ) We can call it like this:

Core.models.message.setPrivate()

Socket.io:

To enable socket.io support, add this to the createServer options:

enableWebSocket	: true,
socketKey       : "KEY-IN-HANDSHAKE"

The socketKey property should hold the property name in the handshake phase of the connection. The value of this propery should hold the identifier for this socket. ( userID for example ). When true, Fast will look for 'subscribers' object that exported from each API end point. This object is the same as the 'routes' object, except the httpMethod property. Also, the paramType for socket params is always 'body'. You can get any socket object using:

Core.socket.getSocket( SOCKET-ID )

SSL:

To enable SSL, add this to the createServer options:

useSSL : true
useSSL : {
    key   : YOUR_KEY,
    cert  : YOUR_CERT
}