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

restfool

v0.1.2

Published

A framework for building fake endpoints that can be used to test REST calls.

Downloads

1

Readme

Build Status

RESTfool is largely just a set of higher order functions that produce middleware for mocking responses with assertions and fixture data. These middleware also report request and response information to a RESTfool Dashboard instance for debugging and analysis of each made call.

Getting Started

Install via npm:

npm install restfool

Create a server.js file and require restfool:

var restfool = require('restfool')

Starting a server

RESTfool requires an Express server to serve mocked routes and the dashboard. You can choose to add the middleware yourself, but it's recommended to have RESTfool create and configure a server for you.

var server = restfool.create()

server.get('/', function (req, res, next) {
  res.send('OK')
})

server.listen(4050)

Fixture Data

RESTfool makes it easy to generate fake fixture data by using the fixture() method. The fixture() methods accepts either an object literal containing, or a function that returns, the desired output schema. When using the object literal method, you can pass in functions to generate values dynamically.

Note: We're using the faker library below to quickly generate fake data.

var faker = require('faker')

// object literal
var postFixture = restfool.fixture({

  title: faker.lorem.sentence,

  body: faker.lorem.paragraph,

})

// function
var userFixture = restfool.fixture(function () {
  return {

    name: faker.name.firstName() + ' '+ faker.name.lastName(),

    email: faker.internet.email()

  }
})

Generating rows

Now that you've defined the schema for the data, you can output a single row by calling the makeOne() method or a defined number of rows using the make() method.

var one = postFixture.makeOne()
// => { title: 'Lorem ipsum dolor', ... }

var many = postFixture.make(3)
// => [ {...}, {...}, {...} ]

You can override the results of custom values by passing an object or function in as a second argument.

var active = userFixture.make(2, { active: true })
// => [ {name: '...', active: true}, {name: '...', active: true} ]

var noNicks = userFixture.make(10, function (user) {
  // return false to filter out records
  if (user.name.match(/^nic(k|holas) .*/i)) return false

  return user
})

Generating middleware

You can also generate middleware quickly using the send() and sendOne() methods. These methods function the same as their "make" counterparts but will send a 200 JSON response containing the data.

server.get('/posts', postFixture.send(10, { published: true }))

Mocking entire resources

You know what's even cooler than sending completely dynamic fixture data? Mocking data persistence complete with scaffolded routes!

Note: RESTfool simply stores the data in memory. This means that the data will be lost once the server is closed.

var uuid = require('uuid')

var posts = restfool.resource({
  // define the endpoint/resource name
  name: 'posts',

  // provide an initial set seed data
  seed: postFixture.make(10),

  // format the resource data on output
  format: function (data) {
    var req = this
    var output = { data: data }

    if (Array.isArray(data)) {
      data.page = req.query.page || 1
      data.results = data.length
    }

    return data;
  }

  // run this function when a post is made
  onCreate: function (post) {
    // if the primary key doesn't have a value then an auto-incrementing
    // index will be provided
    post.id = uuid.v1()

    return post
  }
})

The following events are supported: create, update, save, delete