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-api

v0.1.0

Published

Express-api is an API framework based on Swagger.

Readme

express-api

Express-api is an API framework based on Swagger.

Express-api uses CSON, JSON or YAML files to expose your apis using the eapiger specs. Express-eapi reloads schemas in development facilitating schema creation.

DO NOT USE, WIP! Documentation is inaccurate.

Using

Install the library

npm install express-api

Create resource listings schema and resource schema files in a common directory, eg public/api/docs. Here are the official examples

Implement your resource API. NOTE, the name of your resoure methods must match the nickname property in the schema operations

//// pet.json
apis: [{
  path: "/pet/{petId}",
  operations: [{
    summary: "Find pet by ID",
    nickname: "getPetById",
   }]
}]
//// resource/pet.js
exports.getPetById = function(req, res) {
}

Wire it all up

var express = require('express');
var app = express();
var Swag = require('express-eapi');

// create routers for API and docs
var apiRouter = express.Router();
var docsRouter = epxress.Router();
app.use('/api', apiRouter);
app.use('/api/api-docs', docsRouter);

var eapi = new Swag({
  apiRouter: apiRouter,
  docsRouter: docsRouter,
  docsDir: 'public/api/docs',
  extname: '.json'
});

eapi
  .addApi('pet.json', require('./resources/pet.js'))
  .addApi('user.json', require('./resources/user.js'))
  .configureDocs('index.json', 'http://localhost:8000/api');

app.listen(3000);

Browser your json schemas at http://localhost:3000/api/api-docs or through the eapiger UI.

Middleware

The primary reason for creating express-api is the lack of support for plain middleware in other frameworks.

The implementation method can either be a function or array of functions.

exports.getPetById = function(req, res, next) { ... };

exports.getPetById = [
  validationMiddleware({ body: {id: joi.integer()}}),
  function(req, res, next) { ... };
];

That can be a bit tedious. Express-api can use middleware as plugins. Let's create one.

function validate(req, res, next) {
  // get the spec for the current operation
  var spec = req.__eapiger.operation;
  var params = spec.parameters;
  params.forEach(function(param) {
    if (param.required && param.paramType === 'path')  {
      if (!req.params[param.name]) res.send(400, 'Require argument missing' + param.name);
    }
  });

  next();
}

To use it

eapi
  .use(validate)
  .addApi('pet.json', require('./resources/pet.js'))

The position matters. All middleware used before addApi are applied before a operation method in the pipeline. Post filters look like this.

eapi
  .addApi('pet.json', require('./resources/pet.js'))
  .use(normalizeResult);