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

koa-resteasy

v1.0.0-alpha.11

Published

REST endpoint middleware for Koa & Knex, currently only supports Koa 1 and Postgres.

Downloads

3

Readme

koa-resteasy

REST endpoint middleware for Koa & Knex, currently only supports Koa 1 and Postgres.

Usage

npm install --save koa-resteasy

TODO:

  • has-and-belongs-to-many/join table support (e.g. /api/playlists/5/tracks)

Example:

var app = require('koa')();
var body = require('koa-better-body');
var mount = require('koa-mount');
var helmet = require('koa-helmet');
var pg = require('koa-pg');
var _ = require('lodash');

var Router = require('koa-router');
var router = new Router();
var preRouter = new Router();

var knex = require('knex')({ client: 'pg',
                             connection: {
                               host: 'postgres',
                               user: process.env.POSTGRES_ENV_POSTGRES_USER,
                               password: process.env.POSTGRES_ENV_POSTGRES_PASSWORD,
                               database: 'koa-rest-development',
                               port: '5432',
                             },
                           });

var Resteasy = require('koa-resteasy')(knex);

var pgUser = process.env.POSTGRES_ENV_POSTGRES_USER;
var pgPass = process.env.POSTGRES_ENV_POSTGRES_PASSWORD;
var dbName = process.env.DATABASE_NAME + '-' + app.env;
var postgresUrl = (process.env.POSTGRES_URL || 'postgres://' + pgUser + ':' + pgPass + '@postgres:5432/' + dbName);

// omitting table option makes it a generic instance:

// adding the table thing, collapses the RESTEasy instance to just one
// table:
var reviewsRest = Resteasy({ table: 'reviews' });

// and you can add routes taht leverage the REST middleware: or not,
// where you register the router in the middleware pipeline determines
// whether or not the RESTEasy code will be used:
preRouter.get('/api/v0/reviews/moderate', function *(next) {
  this.body = { result: 'NOT IMPLEMENTED' };
});

router.get('/api/v0/reviews/', function*(next) {
  this.body = { result: 'OK', sql: this.resteasy.query.toSQL().sql };
});

app.use(function *(next) {
  var start = new Date();
  yield next;
  var end = new Date();

  if (typeof this.body == 'object') {
    this.body.timing = { duration: end - start };
  }
});

app.use(require('koa-error')());
app.use(helmet()).use(body());

// preRouter doesn't use middleware:
app.use(preRouter.routes());

// router does:
app.use(mount('/api', Resteasy()));
app.use(router.routes());

console.log('Listenting on ' + (process.env.PORT || 3008));
app.listen(process.env.PORT || 3008);