Code Monkey home page Code Monkey logo

fixture-middleware's Introduction

fixture-middleware — Use fixture data in your express app!

Motivation

Sometimes I want to mock certain paths of my API made with Express. Therefore, I created fixture-middleware which will use fixture data (mock) for any URL that matches a file inside the configured fixture directory.

Installation

npm install fixture-middleware --save-dev
yarn add fixture-middleware --dev

Usage

const path = require('path')
const express = require('express')
const fixtureMiddleware = require('fixture-middleware')

const app = express()
const fixtureDirectory = path.resolve(__dirname, './fixtures')

app.use(fixtureMiddleware(fixtureDirectory))

Matching URLs with fixture files

All URLs are normalized (all / will be replaced by --) and if a Accept headers is present it will be used to determine the file extension, otherwise the first matching file will be returned.

Example

Suppose we have the following files in the configured fixture directory:

`-- fixtures
    |-- users.json
    |-- users.html
    `-- users--active.json

The following request will match those files as follows

Request Response
URL: /users
Headers: (no accept header)
File: users.html
URL: /users
Headers: accept=application.json
File: users.json
URL: /users
Headers: accept=application.html
File: users.html
URL: /users/active
Headers: (no accept header)
File: users--active.json
URL: /users/active
Headers: accept=application.json
File: users--active.json

Important: Any unmatched request will be delegated to the next middleware.

Development

  1. Clone and fork this repo.
  2. Install dependencies: yarn or npm install.
  3. Run tests.
  4. Prepare a pull request.

Test

  • yarn test – to run all tests.
  • yarn test -- --watch – to run all tests in watch mode.

Publish

  1. Bump package version: yarn version --new-version x.x.x -m 'Version %s.'.
  2. Publish to NPM registry: npm publish.
  3. Push new tag: git push origin --tags.

Made with ❤️ by Rubens Mariuzzo.

MIT license

fixture-middleware's People

Contributors

rmariuzzo avatar

Stargazers

 avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.