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

merry-rest

v1.0.1

Published

serve rest apis with merry and json-schema

Downloads

1

Readme

merry-rest

npm version Build Status js-standard-style

serve rest apis with merry and json-schema

Usage

var rest = require('merry-rest')
var merry = require('merry')

var app = merry()
var api = rest(app)

// db is a levelup instance
var model = api.model(db, 'schema.json')

api.resource(model, { route: 'schema' })
api.start()

After running the .start() method, you will have the following routes availaible

GET     /api/v1/schema
GET     /api/v1/schema/:id
POST    /api/v1/schema
PUT     /api/v1/schema/:id
DELETE  /api/v1/schema/:id

API

api = rest(app [, opts])

Create a rest api instance. app is a merry instance and is mandatory. opts is an optional configuration object, availaible options are:

  • version: Defaults to 1, specify the version of your api, it will be used in the route.
  • default: Set a default route in case of a missmatch. Must be a function like function (reques, response, context)

The returned object has two properties: api the merry instance, and prefix the prefix of all the routes. The prefix has the form '/api/v:version'

var model = api.model(db, schema)

Create a rest-parser instance using level-rest-parser as backend. db and schema are mandatory. db is a levelup instance where the data is saved and schema is a string with the path to the json file that contains your schema. The rest-parser instance returned here is used later in the resource method.

api.resource(model , opts)

Generate the rest routes for the given model. You must provide a model and opts argument, where opts can be a string or an object. If opts is a string, then that will be the route of your model, it it is an object, it can have the following properties:

  • route: The only required property. Must be a string indicating your model route.

  • only: Must be an array of strings. If defined, set explicity the methods for which this resource define routes. For example, seting { only: ['GET', 'POST'] } will set only these routes

GET     /api/v1/schema
GET     /api/v1/schema/:id
POST    /api/v1/schema
  • except: Similar to the only option, must be an array of strings and will do the oposite thing, so having { except: ['PUT', 'DELETE'] } as an option will result in these routes:
GET     /api/v1/schema
GET     /api/v1/schema/:id
POST    /api/v1/schema

As you can see, you can achieve the same result with both options, but what you can not do, is define them bot in the same call, { except: ['DELETE'], only: ['GET'] }, that option has no sense, and it will throw.

  • before: Must be a function. If defined, will be called before every route for this resource. It accepts four arguments req, res, ctx and next which are the reques, the response, the merry context and a callback, in this case, the actual route method. So if you don't call next(req, res, ctx) your actual route wont be called, this is useful if your before hook must cancell the request

  • after: As you might guess, this is like before hook, but after. Difference are that you don't provide a next hook, because there is nothing next. Also, you must end the request manually here, this is easily done with merry context object, like ctx.send(200, bodyData, headers)

api.route(method, route, handler)

Define a special route not set by any of the rest routes. Method is a string, route is also a string and handler a function like next in the hooks (with req, res, ctx).

var server = api.start([cb])

Start a regular http server with the rest routes defined. Optionally, you can pass a callback to be run after the server start listening. cb must be a function.

License

MIT