Code Monkey home page Code Monkey logo

Service Mocker

Travis CI CircleCI Coverage Version License

Build Status

Service Mocker is an API mocking framework for frontend developers. With the power of service workers, we can easily set up mocking services without any real servers. It sets developers free from intricate workflows, complex documentations and endless proxies from server to server.

Q: Is Service Worker ready?

A: No, not yet.

Q: Is Service Mocker ready?

A: Yes! Welcome to the future!

Installation

Since you are likely to run Service Mocker only during development, you will need to add service-mocker as a devDependency:

npm install service-mocker --save-dev

For legacy browsers, you may also need the polyfills:

npm install service-mocker-polyfills --save-dev

Features

  • No server is required.
  • Real HTTP requests and responses that can be inspected in modern browsers.
  • express style routing system.
  • IE10+ compatibility.

Hello new world

A typical mocker includes two parts: client and server. First, let's create a server script named server.js:

// server.js
import { createServer } from 'service-mocker/server';

const { router } = createServer();

router.get('/greet', (req, res) => {
  res.send('Hello new world!');
});

// or you can use the shorthand method
router.get('/greet', 'Hello new world!');

Then, we need to write a client script to connect to the server:

// app.js
import 'service-mocker-polyfills';
import { createClient } from 'service-mocker/client';

const client = createClient('path/to/server.js');

client.ready.then(async () => {
  const response = await fetch('/greet');

  console.log(await response.text());
});

After that, create a .html file and include ONLY the client script:

<script src="app.js"></script>

Now navigate your browser to your local dev server (e.g. http://localhost:3000). Open the console and you will see the following messages:

> [mocker:modern] connection established
>
> Hello new world!

Welcome to the future 👏.

Working with webpack

While using webpack, it's recommended to use sw-loader to create a standalone server script:

import scriptURL from 'sw-loader!path/to/server.js';
import { createClient } from 'service-mocker/client';

const client = createClient(scriptURL);

client.ready.then(...);

Docs & Demos

Team

Dolphin Wood Vincent Bel
Dolphin Wood Vincent Bel

License

MIT

Service Mocker's Projects

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.