mock-backend

0.0.5 • Public • Published

Mock Backend

A simple framework for intercepting fetch() and XMLHttpRequest requests, and generating responses from within the client.

Usage

  <script src="https://unpkg.com/mock-backend"></script> 
  <script>
    mockBackend([
      {
        claim: (r) => r.url === '/foo', 
        handle: (r) => ({ status: 200, body: { hello: 'world' } }) 
      },
      {
        claim: (r) => r.url === '/bar', 
        handle: (r) => r.urlParts.query.get('secret') === 'letMeIn' ?
            ({ status: 200, body: { secret: 'treasure' } }) :
            ({ status: 403, body: 'You do not have access to /bar' }) 
      },
    ], {
      defaultConfig: {
        // How requests that don't match a handler are treated (default: 'ERROR')
        unclaimedRequests: 'ERROR' /* or 'PASS_THROUGH' */
 
        // Time to wait (ms) before sending a response {default: 5000}
        delayBeforeResponding: 2500,
      }
    })
  </script> 
 
  <!-- ... your app html ... --> 

API

Try it out

Readme

Keywords

none

Package Sidebar

Install

npm i mock-backend

Weekly Downloads

0

Version

0.0.5

License

MIT

Unpacked Size

1.08 MB

Total Files

4

Last publish

Collaborators

  • andrewpmckenzie