Skip to content

Repository files navigation

mande Build Statusnpm packagecoveragethanks

Simple, light and easy to use wrapper around fetch

Requires fetch support.

mande has better defaults to communicate with APIs using fetch, so instead of writing:

// creating a new userfetch('/api/users',{method: 'POST',headers: {Accept: 'application/json','Content-Type': 'application/json',},body: JSON.stringify({name: 'Dio',password: 'irejectmyhumanityjojo',}),}).then((response)=>{if(response.status>=200&&response.status<300){returnresponse.json()}// reject if the response is not 2xxthrownewError(response.statusText)}).then((user)=>{// ...})

You can write:

constusers=mande('/api/users')users.post({name: 'Dio',password: 'irejectmyhumanityjojo',}).then((user)=>{// ...})

Installation

npm install mande
yarn add mande

Usage

Creating a small layer to communicate to your API:

// api/usersimport{mande}from'mande'constusers=mande('/api/users',globalOptions)exportfunctiongetUserById(id){returnusers.get(id)}exportfunctioncreateUser(userData){returnusers.post(userData)}

Adding Authorization tokens:

// api/usersimport{mande}from'mande'consttodos=mande('/api/todos',globalOptions)exportfunctionsetToken(token){// todos.options will be used for all requeststodos.options.headers.Authorization='Bearer '+token}exportfunctionclearToken(){deletetodos.options.headers.Authorization}exportfunctioncreateTodo(todoData){returntodo.post(todoData)}
// In a different file, setting the token whenever the login status changes. This depends on your frontend code, for instance, some libraries like Firebase provide this kind of callback but you could use a watcher on Vue.onAuthChange((user)=>{if(user)setToken(user.token)elseclearToken()})

You can also globally add default options to all mande instances:

import{defaults}from'mande'defaults.headers.Authorization='Bearer token'

API

Most of the code can be discovered through the autocompletion but the API documentation is available at https://posva.net/mande/.

Related

  • fetchival: part of the code was borrowed from it and the api is very similar
  • axios:

License

MIT

About

Simple, light and easy to use wrapper around fetch

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages