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

swatchjs-express

v0.2.5

Published

Express adapter for swatchjs

Downloads

3

Readme

swatchjs-express

CircleCI | codecov | Coverage Status | Known Vulnerabilities

An adapter to expose swatchjs via Express.

Quick start

The following exposes the simple API from the swatchjs's README file:

// Application server
const swatch = require('swatchjs');
const swatchExpress = require('swatchjs-express');

const model = swatch({
    "numbers.add": (a, b) => Number(a) + Number(b),
    "numbers.sub": (a, b) => Number(a) - Number(b),
});

const app = express();
swatchExpress(app, model);

That's it! No HTTP headers, status codes, or any other distracting verbiage. Each API method you declared can now be invoked using any of the supported HTTP verbs.

Note: It is strongly advised that you only enable your APIs over HTTPS. Read more about HTTPS and why it's important and how to use HTTPS with your node server. This tutorial also provides a walkthrough of the entire process, including certificate setup and configuration.

Verbs

GET

The GET binding expects all parameters to be passed in the query string.

For instance, using the popular request package, you would invoke the numbers.add method above as follows:

// Client code (ex: Node server)
var request = require('request');

request('https://my-server/numbers.add?a=1&b=2');

Note: Every parameter in the query string is received as, well, a string. So if you plan on supporting the GET verb, it is recommended that either you provide parse functions for each of your arguments, or that you validate and coerce them inside your function (as shown in the example above).

POST

The POST binding expects parameters to be passed in the body property of Express' request object.

Below is an example of calling the API using an XMLHttpRequest object:

// Client code (ex: Browser JS)
function post(url, body) {
    var request = new XMLHttpRequest();

    request.open('POST', url);
    request.setRequestHeader('Content-Type', 'application/json');
    request.send(JSON.stringify(body));

    return request;
}

var request = post(
    'https://my-server/numbers.add',
    {
        a: 1,
        b: 2,
    });

Note

Be mindful that how the body property of Express' request object gets populated depends on which body-parsing middleware you have enabled.

There are popular libraries for Express that enable the service to choose their behavior with regards to body parsing. A popular one is body-parser.

If you have a simple JSON body parser, then it will parse application/json content types, and your parameters could potentially have non-string types.

If, on the other hand, you have application/x-www-form-urlencoded parsing enabled, then all parameters will be strings, and the same considerations of the GET verb apply.

See the body-parser documentation for detailed instructions to use those and other options, or the documentation in the library of your choice.

Security Notice: Remember that you may receive string objects using either approach, which you should validate and coerce before passing to your handler functions.

// Application server
var express = require('express');
var bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

API reference

The swatchExpress function

// Application server
const swatchExpress = require('swatch-express');

swatchExpress(app, model, options);

Loading this library will result in a function (swatchExpress in the example above) which takes the following parameters:

| Parameter | Required | Description | |:--- |:--- |:--- | |app | Yes | The Express app to populate. | |model | Yes | The API model created by swatchjs. | |options | No | Additional options when exposing the API. When present, must be an object. |

The options object has the following properties:

| Property | Example | Default value | Description | |:--- |:--- |:--- |:--- | |verbs |['get'] |['get','post'] | An array with a list of HTTP verbs to use. Each verb must be a string, and must be known. | |prefix |'api' |'' | A URL prefix to be added to every route. For example, if the value of this option is 'product', then the URL of all APIs will start with /product/. |

Developers

Coding Style

This project follows the AirBnB Javascript Coding Guidelines using ESLint settings.