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

synth-api

v0.3.0

Published

Scans through the specified directory and builts endpoints that are then added to an Express app.

Downloads

21

Readme

SYNTH-API

Scans through the specified directory and builds endpoints that are then added to an Express app.

Each API endpoint is crafted by merely by giving it the name of the HTTP method it handles.

Synth-api is one of the major features provided by the Synth framework but is made available here for people who just want a stripped down module, and not the rest of the Synth framework (which includes support for asset compilation and more fun things).

Within your request handlers, you can either return data that will be JSONified and sent back to the client (useful for stubbing during development), a promise that will then return such data, or call the methods on the Express response object directly. See the examples below.

Build Status Code Climate Test Coverage

Example Usage

app.js:

var express = require('express');
var synthApi = require('synth-api');

var app = express();
synthApi.generateHandlers({
  resourceDir: __dirname + '/resources', // This is the default, not required
  prefix: '/api', // This is the default, not required
  app: app,
  timeout: 300
});

app.listen(80);

resources/tweets/tweets.js

// Return data directly, useful for stubbing during development
exports.getIndex = function (req, res) {
  return {
    tweets: [
      {
        message: "Fake tweet!",
        createdAt: new Date()
      }
    ]
  };
};

// Return a promise!
exports.get = function (req, res) {
  var id = req.params.id;
  return req.db.collection('tweets').findOne({
    id: id
  }).then(function (data) {
    return {
      tweet: data
    };
  });
};

// Or talk directly to Express response object
exports.post = function (req, res) {
  req.db.collection('tweets').insert({
    message: req.body.message,
    createdAt: new Date()
  }, function (err, data) {
    if (err) {
      res.status(500).send("Something went wrong: " + err.message);
    } else {
      res.send(data);
    }
  });
};

The above will create request handlers for the following routes:

  • GET /api/tweets
  • GET /api/tweets/:id
  • POST /api/tweets

You can also create nested resources, handlers for PUT and DELETE, as well as custom actions. Learn more at (synthjs.com)[http://www.synthjs.com/docs/#creating-api-endpoints]

generateHandlers(options)

Options

An object with the following keys (all are optional).

| option | Type | Default | What it does | |--------|------|---------|--------------| | prefix | String | '/api' | Specifies what should precede the resource name for the generated routes. | | resourceDir | String | process.cwd() + '/resources' | The directory to look into for generating the API endpoints. | | app | ExpressApp | null | If given an Express app, it will have the API and view endpoints automatically attached. | | timeout| Number | 5000 | Time (in milliseconds) before an error response is returned to the client instead of the expected result. | | catchAll | Function | null | An optional Express style request handler to handle any requests to the api path that are not handled (regardless of HTTP method). Can be used to return a custom 404 error. Note: This function should not return data or a promise. It should use the Express response object directly. |

Returns

generateHandlers() returns an object with a 'handers' key, which is an array of all the API handlers generated.

Each handler object contains the following keys:

  • file - String - Path to the js file that the API handler was found in.
  • method - String - The HTTP method that this handler respnds to. e.g. 'get', 'post', 'put', or 'delete'.
  • path - String - The URL path that the endpoint responds to. e.g. '/api/tweets'
  • isCustom - Boolean - Whether this handler is a custom method. This is good to know so that you register it before the non-custom methods. For example, you want '/api/tweets/favorites' to be registered with your Express apps before '/api/tweets/:id'.
  • funcName - String - The name of function. e.g. 'getIndex'
  • resources - Array[String] - The list of resources that this handler is a child of. e.g. the handler at '/api/tweets/1234/comments' would have a resources array of ['tweets', 'comments'].

Defining API endpoints

For this, just check out the existing Synth Documentation.

License

MIT

Credit