GithubHelp home page GithubHelp logo

alex3165 / express-fixturer Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 1.0 36 KB

Express middleware to automatically generate and use fixtures

TypeScript 100.00%
express-middleware fixtures mocking api nodejs

express-fixturer's Introduction

Express-fixturer

Express middleware that save the response payload (fixtures) of each http request against a hash and reply with those fixtures when you turn it into fixture mode.

The name of the file is the hash created from the request so you get the right mocked data for each of your request.

It should help you mocking endpoints so you don't have to bother updating fixtures.

How to get started

Install the middleware:

npm install express-fixturer

Plug it to express:

const app = express();
app.use(expressFixturer({ fixtureBasePath: './fixtures' }));

API

middlewareFactory

Description

Create a unique hash for each http request and use this hash as a filename to save the response payload.

How to import:

import middlewareFactory from 'express-fixturer';

Parameters of the factory function:

  • saveRoutes (optional, default: true) boolean | string[]: If true, it will create fixtures for each of your express endpoints. You can also pass an array of routes and it will only create fixtures for those routes.
  • fixtureRoutes (optional, default: false) boolean | string[]: If true, it will use the fixtures if it find them or will trigger your handler and save the response payload as a fixture.
  • fixtureBasePath (optional, default: ./) string: Define the path to the folder of your fixtures.
  • hashFn (optional) (req: express.Request) => object: Define how to generate your hash used to identify the fixture to the right request. The default hash function will include req.body, req.cookies, req.params, req.query and req.path
  • logging (optional, default: verbose) verbose | quiet: If set to verbose it will log.

Example

This will save fixtures to the fixtures folder.

const app = express();
app.use(expressFixturer({ fixtureBasePath: './fixtures' }));

TODO

  • Implement string[] routes to save fixtures or not
  • Implement string[] for using fixtures or not
  • Handle nested folder structure for fixtureBasePath

express-fixturer's People

Contributors

alex3165 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

elischutze

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.