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 againsdefaultOptions
(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 tooptions
(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')
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