1.2.0 • Published 10 years ago

farfetched v1.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

farfetched

This is a simple library for mocking responses (like Mockjax) for window.fetch. I recommend using GitHub's fetch polyfill.

I'm going to start simple and add methods and features as I need them or they're requested.

Build Status npm

Installation

Farfetched is made to work with Browserify. Install it:

$ npm install --save-dev farfetched

Then, require it in your project and attach it to window:

var farfetched = require("farfetched");
farfetched.attach(window);

Example

// Call this once to enable farfetched.
farfetched.attach(window)

//
// A basic mocked response
//
farfetched('/the-answer', { response: '42' })

fetch('/the-answer')
  .then(log) // '42'


//
// Dynamic responses
//
farfetched(/\/food/, { response: url => url.split('?')[1] })

fetch('/food?apples')
  .then(log) // 'apples'


// Helper logging function.
function log (response) {
  response.text().then(res => console.log(res))
}

API

farfetched(url, options)

Creates a handler and returns its ID.

url is the URL (either a String or Regexp) to match and mock.

options is an Object and can have the following properties:

  • response

    The response to return or a function that can be called to obtain it.

farfetched.attach(window)

Attaches farfetched to the global scope, replacing window.fetch. window.fetch is still used when farfetched can't find a handler that matches a route.

farfetched.detach(window)

Detaches farfetched from the global scope and restores the original function.

farfetched.clear(id)

Clears the handler with the given ID.

1.2.0

10 years ago

1.1.10

10 years ago

1.1.9

10 years ago

1.1.8

10 years ago

1.1.7

10 years ago

1.1.6

10 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago