Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

faux-fetch

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

faux-fetch

A utility for faking fetch requests

  • 0.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Faux Fetch

A utility that uses local storage to mock servers. Currently, it assumes the same API as fancy-fetch as I intend to use it as a drop-in replacement with that library, though I'd like for it support raw fetch as well in the future.

Usage

import fauxFetch from 'faux-fetch'

// Puts an entry into local storage
fauxFetch({
  url: '/me',
  method: 'put',
  body: { name: 'Smeagol' }
  success: (result) => {
    console.log(result) // { name: 'Smeagol' }
  }
})

// Get an entry from local storage
fauxFetch({
  url: '/me',
  success: (result) => {
    console.log(result) // { name: 'Smeagol' }
  }
})

In the real world, it takes some time for these requests to occur. fauxFetch uses setTimeout with a default delay of 200ms. You can provide your own custom delay, along with many other options, within the object passed to fauxFetch.

Options:

  • url: String
  • body: Object|String|Null (anything JSON.parseable)
  • success: Function
  • error: Function
  • delay: Number
  • custom: Object

If you do not provide a url parameter in these options, fauxFetch will give you a partially applied function, this will let you configure fetch how you please. For example:

const ImmediateFetch = fauxFetch({ delay: 0 })

Will give you a faux fetcher that does not wait the default 200ms. You might want to customize how your server API works, however:

const customFetch = fauxFetch({
  custom: {
    '/me/get_ring': {
      'post': () => {
        immediateFetch({
          url: '/me',
          method: 'post',
          body: { age: 'old', name: 'Gollum' }
        })
      }
    }
  }
})

customFetch({
  url: '/me',
  success: (result) => {
    console.log(result) // { name: 'Smeagol' }
  }
})

customFetch({ url: '/me/get_ring' })

customFetch({
  url: '/me',
  success: (result) => {
    console.log(result) // { name: 'Gollum', age: 'old' }
  }
})

I'm still working on making this cleaner.

FAQs

Package last updated on 07 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc