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

selfapi

v1.0.0

Published

Simple, self-documenting and self-testing API helper for Node.js.

Downloads

77

Readme

Self API

Travis status NPM version NPM dependencies Renovate

Simple, self-documenting and self-testing API helper for Node.js.

Works with express, restify and scout camp.

Note: This is not a routing engine. You need one of the above to build a working API. (Self API just provides an easier way for you to develop, document and test web APIs.)

Try Self API

Install it:

npm install selfapi

Use it:

var selfapi = require('selfapi');

// API root resource, mounted at '/api' on your server app:
var api = selfapi(app, '/api', 'My API');

api.get({
  title: 'Show API version',
  description: 'Show the latest API version currently supported.',

  handler: function (request, response) {
    response.end('v1.0');
  },

  examples: [{
    response: {
      body: 'v1.0'
    }
  }]
});

// API sub-resource, mounted at '/api/items' on your server app:
var items = api.api('/items', 'Items');

items.post({
  title: 'Add a new item',
  description: 'Create a new item and add it to our collection.',

  handler: function (request, response) {
    var json = '';
    request.on('data', function (chunk) {
      json += String(chunk);
    });
    request.on('end', function () {
      var item = JSON.parse(json);
      response.statusCode = 201; // Created
      response.end(JSON.stringify({ status: 'Created', item: item }));
    });
  },

  examples: [{
    request: {
      body: '{"name":"My Item"}'
    },
    response: {
      status: 201,
      body: '{"status":"Created","item":{"name":"My Item"}}'
    }
  }]
});

Your API can self-document:

> console.log(api.toMarkdown());
# My API

## Show API version

`GET /api`

Show the latest API version currently supported.

### Example request:

    GET /api

### Example response:

    Status: 200 OK

    v1.0

# Items

## Add a new item

`POST /api/items`

Create a new item and add it to our collection.

### Example request:

    POST /api/items

    {"name":"My Item"}

### Example response:

    Status: 201 Created

    {"status":"Created","item":{"name":"My Item"}}
…

… and self-test, using its own examples:

> api.test('http://localhost:8080');
Results: 2/2 tests passed.

Note: When testing the API, all your routes should have examples, which might be empty. This is to ensure all routes are documented and you are well aware some endpoints are lacking tests.

Note: You can also document and test each API resource individually, but remember to provide the correct base path like so:

> console.log(items.toMarkdown('/api'))
## Add a new item

`POST /api/items`

Create a new item and add it to our collection
…

> items.test('http://localhost:8080/api')
Results: 1/1 test passed.

Getting started

Create your API using express:

var express = require('express');
var selfapi = require('selfapi');

// Create your server app:
var app = express();

// Create your API, mounted at '/api' on your server app:
var api = selfapi(app, '/api', 'My API');

// This does exactly the same thing:
var api = selfapi({
  parent: app, // can be omitted, but eventually required for your API to work
  path: '/api', // optional, defaults to no path prefix
  title: 'My API', // optional
  description: '' // optional
});

// This too:
app.api = selfapi;
var api = app.api('/api', 'My API');

// This too:
var router = express.Router();
app.use('/api', router);
var api = selfapi(router, '/', 'My API');

Write request handlers the same way you would in express, just with a bit more info:

// This will register some metadata, and export the handler function to express.
api.get({
  title: 'Show API version',
  description: 'Show the latest API version currently supported.',

  handler: function (request, response) {
    // Here you can do anything you would in `app.get('/api/items', …)`, e.g.
    response.json({ version: 'v1.0' });
  },

  examples: [{
    response: {
      body: '{"version":"v1.0"}'
    }
  }]
});

Create API sub-resources when it seems useful.

Note: They're basically just a common prefix for similar request handlers (a bit like a very lightweight express Router), but they'll create dedicated documentation sections, and can be tested individually.

// Create an API sub-resource, mounted at '/api/items' on your server app:
var items = api.api('/items', 'Items');

// This does the same thing:
var items = selfapi(api, '/items', 'Items');

// This too:
var items = selfapi({
  parent: api,
  path: '/items',
  title: 'Items'
});

// … you get the idea.

For more examples of how to use Self API, please have a look at the tests.

License

MIT