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

snuffles

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snuffles

A wrapper around the native fetch function, returning the response body as a camelCased object

  • 1.0.0
  • Source
  • npm
  • Socket score

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

A wrapper around the native fetch function, providing a more convenient way to use it

At its core, Snuffles is just a very slim wrapper around the native fetch function. It allows for setting a base url and default options for your request, provides some wrappers around some of the more frequently used HTTP methods and takes care of all casing. You send camelCased objects in, you get camelCased objects out.

Installation

npm install --save snuffles

Usage

import Snuffles from 'snuffles'

export default function myApiWrapper() {
  cosnt defaultOptions = {
    headers: {
      'X-AUTH-TOKEN': 'my-secret-token'
    }
  }

  const api = new Snuffles('http://base-url.tld', defaultOptions)
  
  const user = api.get('/user')
}

To create a new instance of Snuffles:

const api = new Snuffles(baseUrl[, defaultOptions])
  • baseUrl: The base url of the API you want to make requests agains
  • defaultOptions (optional): An Object, containing a set of default options you want to sent in every request, e.g. headers for authentication

As of now, Snuffles has wrappers for 5 request methods:

  • get(path[, options])
  • post(path[, options])
  • put(path[, options])
  • patch(path[, options])
  • delete(path[, options])

Where

  • path: the path you want that specific request to go to
  • options (optional): An Object containing a set of options you want to merge with the base options on this specific request. Options passed to the wrapper functions are deep-merged, but will override identical keys.

Options

Snuffles accepts all options that fetch accepts as its init parameter (docs). In fact, snuffles does not validate the options that are passed at all.

Using querystrings

Snuffles does support the setting of querystrings via its options parameter. You can pass in a query object with the desired key-value-pairs.
For example:

const api = new Snuffles('http://base-url.tld')

const options = {
  query: {
    'name': 'sirius',
    'animal': 'dog'
  }
}

const user = api.get('/user')

// => fetch('http://base-url.tld/user?name=sirius&animal=dog')

Casing

Snuffles will take care of transforming the casing of response and request bodies, so that you can pass in a camelCased object as a request body (passed via options.body) and get out the response body as a camelCased object as well.

License

MIT © railslove

Dog Illustrastion from Pixabay under CC0-License.

Made with 💚 in Cologne

FAQs

Package last updated on 11 Oct 2018

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