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 🙏

© 2025 – Pkg Stats / Ryan Hefner

declarative-api

v0.0.6

Published

A declarative REST API generator built on Express.js

Readme

Declarative API

var declarativeApi = require('declarative-api');

var server = declarativeApi( {
	'port': '3000',
	'host': '127.0.0.1'
} );

var endpoint = {
	'Hello World' : {
		'description': 'Hello World!',
		'url' : '/hw',
		'requiredFields' : {},
		'optionalFields': {},
		'function' : function() { return 'Hello World!'; }
	}
};

server.start( endpoint );

Declarative API is a library for quickly setting up a self-documenting REST endpoint declaratively. It is built on top of Express. Please note the project is still in alpha - expect more updates to come soon, some potentially backwards-incompatible. This library is designed to be fast and lightweight, but not terribly flexible - the idea is to quickly and easily expose any node library or API via a REST endpoint or set of endpoints.

Installation

npm install declarative-api

Quick Start

Once you've installed the library, open up main.js and require it like so:

var declarativeApi = require('declarative-api');

From there, initialize a web server with the port and host specified as options:

var server = declarativeApi( {
	host: '127.0.0.1',
	port: '3000'
} );

After that, just call server.start() with the endpoints specified


server.start( {
	'Square' : {
		'description': 'Square a number.',
		'url' : '/square',
		'requiredFields' : {
			'num' :
			{
				'type': 'Number',
				'description': 'The number to be squared.'
			}
		},
		'optionalFields': {},
		'function' : function( payload ) { return payload.num * payload.num; }
	} );

This will create server with two paths. The path "/square" that takes a single argument, "num" (via GET headers, POST payload, or query string parameter) and returns the same number squared. The base path provides a documentation for the API taken from the endpoint specification.

Required Endpoint Properties

When you start the server, you feed it an object consisting of a series of named endpoints. There are five (case-sensitive) properties that must be part of this object.

description

This is a short description of the endpoint's functionality. It will appear in the auto-generated documentation.

url

This is the route that the server will serve the endpoint from.

requiredFields

This is a list of required fields. If a request is made to the endpoint without any of these, the server will return an error statemtent. This can be empty.

optionalFields

This is a list of optional fields. It is best to define default values for these in the function property. Like the required fields, this can be empty.

function

This is the function that is run when the endpoint is hit. It takes all of the input (required and optional), performs a computation on the server, and returns the result as a JSON object.

Field Properties

Each field listed, whether required or optional, must have the following properties:

type

This is the type of the field. In future releases, this will be used to validate request inputs

description

This is a description of the field. It will appear in the auto-generated documentation.

Caveats

  • Two routes are reserved, the base route ('/') and a route for serving up the endpoint information ('/endpoints'). If you attempt to use either of these routes as an endpoint, the app will fail.
  • This is still very much a work in progress, so expect bugs and gaps in functionality. Any comments, issues, or pull requests are welcome!

TODOS

  • Validate request input by type
  • Provide more configuration options
  • Write tests
  • Provide examples