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

express-list-endpoints-descriptor

v1.0.13

Published

list all Endpoints registered into an express app with description user defined to build documentation

Downloads

231

Readme

Simple, lightweight, fast and customizable

Extends express routes adding descriptor function. In descriptor you can add the object representing the parameters that you need.

app.get('/books', function (req, res) {
    res.send("My fantastic book");
}).descriptor({
    name : 'Retrieve a fantastic book',
    params : []
});

Basic example

var express = require('express');
var api = require('express-list-endpoints-descriptor')(express);
var app = express();

app.get('/user', function (req, res) {
    res.json({user:true})
}).descriptor({
    name : 'Get list of users'
});

app.get('/api', function (req, res) {    
    res.send(api.listAllEndpoints(app));
}).descriptor({
    name : 'My first API documentation'
});

app.listen(8080, function () {
    console.log('Example app listening on port 8080!');
});

Browsing http://localhost:8080/api will return

[
    {
        "path": "/user",
        "methods": ["GET"],
        "descriptor": [{"name": "Get list of users"}]
    },
    {
        "path": "/api",
        "methods": ["GET"],
        "descriptor": [{"name": "My first API documentation"}]
    }
]

Inside descriptor object you will find the object that you defined inside the descriptor function after route definition.

Keys definition

  • path: [STRING]
  • methods : [ARRAY of strings] Methods for the same endpoint e.g. ['GET','POST']
  • descriptor: [ARRAY of objects] Object user defined inside descriptor function (in the same order of methods array) [{name:'description of /users with method get'}, {name:'description of /users with method post']

Mehods

  • api.listAllEndpoints(server) retrieve all the mapped Express APIs even if the ones that were not extended by the descriptor function. The object server is an instance of express.
  • api.listEndpoints(server) : retrieve only the mapped Express routes, that extend with the descriptor function. The object server is an instance of express.

Retrieve all endpoints list

app.get('/apis', function (req, res) {
    res.send(api.listAllEndpoints(app));
}).descriptor({
    name : 'Retrieve APIs documentation'
});

Retrieve endpoints list that has been extended

app.get('/apis', function (req, res) {
    res.send(api.listEndpoints(app));
}).descriptor({
    routedescr : 'Retrieve APIs list only from routes that have it'
});

Usage

The express-list-endpoints-descriptor module allows to create documentation for Express APIs. Works with Your router instance (router) or your app instance (app).

Example - APP instance

var express = require('express');
var api = require('express-list-endpoints-descriptor')(express);
var app = express();

app.get('/user/:ID', function (req, res) {
    res.json({user:true})
}).descriptor({
    name : 'Get details about user',
    params: {ID:'STRING'}
});

//will not appear on endpoints = api.listEndpoints(app);
app.post('/user/:ID', function (req, res) {
    res.json({user:true})
})

app.get('/api', function (req, res) {    
    res.send(api.listAllEndpoints(app));
}).descriptor({
    name : 'My first API documentation'
});

app.listen(8080, function () {
    console.log('Example app listening on port 8080!');
});

Example - Router instance with multiple files

server.js

vconst express = require('express');
const router1 = require('./modules/router1');
const router2 = require('./modules/router2');
const descriptor = require('express-list-endpoints-descriptor')(express);

const server = express();

//map routes
server.use('/app',router1);
server.use('/sys',router2);

//REMEMBER to invoke the listAllEndpoints or listEndpoints 
//after every route has been defined in the server object
//E.G. in the list inside endpoints /allendpoints and //endpoints are missing
var endpoints = descriptor.listEndpoints(server);

server.get('/allendpoints',(req,res)=>{
    //return all endpoints defined inside routes
    res.send(descriptor.listAllEndpoints(server));
})

server.get('/endpoints',(req,res)=>{
    //return all endpoints defined inside routes
    res.send(descriptor.listEndpoints(server));
})

//create server
var http = server.listen(8080, function () {
    console.log('TEST descriptor server listening on http://localhost:8080');            
});

modules/router1.js

const express = require('express');
var api = require('express-list-endpoints-descriptor')(express);

let router = express.Router();

router.post('/login', (req,res)=>{
    res.json({ok:true})
})
.descriptor({
    descr : 'Perform login, please look at key params for mandatory fields',
    params:[
        {username:'STRING',isMandatory:true},
        {password:'STRING',isMandatory:true}],
    isSecure:false
});

router.get('/verify',  (req,res)=>{
    res.json({ok:true});
})

router.delete('/verify',  (req,res)=>{
    res.json({ok:true});
})

router.post('/verify',  (req,res)=>{
    res.json({ok:true});
})
.descriptor({
    descr : 'decode token and gives back the session, header bearer token is needed',
    params:[
        {header:'STRING',isMandatory:true}
       ],
    isSecure:true
});

module.exports = router;

modules/router2.js

const express = require('express');
var api = require('express-list-endpoints-descriptor')(express);

let router = express.Router();


router.get('/version', (req,res)=>{
    res.json({ok:true})
})
.descriptor({
    descr : 'Get back the version of app',
    needsToken:false
});


router.get('/state',  (req,res)=>{
    res.json({ok:true});
})
.descriptor({
    descr : 'Check state of application',
    params:[
        {header:'STRING',isMandatory:true}
       ],
    needsToken:true
});

module.exports = router;

Installation

npm istall express-list-endpoints-descriptor

License

ISC