Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Simple, light and extensible wrapper around fetch with smart defaults
Requires fetch
support.
mande has better defaults to communicate with APIs using fetch
, so instead of writing:
// creating a new user
fetch('/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) {
return response.json()
}
// reject if the response is not 2xx
throw new Error(response.statusText)
})
.then((user) => {
// ...
})
You can write:
const users = mande('/api/users')
users
.post({
name: 'Dio',
password: 'irejectmyhumanityjojo',
})
.then((user) => {
// ...
})
npm install mande
yarn add mande
Creating a small layer to communicate to your API:
// api/users
import { mande } from 'mande'
const users = mande('/api/users', globalOptions)
export function getUserById(id) {
return users.get(id)
}
export function createUser(userData) {
return users.post(userData)
}
Adding Authorization tokens:
// api/users
import { mande } from 'mande'
const todos = mande('/api/todos', globalOptions)
export function setToken(token) {
// todos.options will be used for all requests
todos.options.headers.Authorization = 'Bearer ' + token
}
export function clearToken() {
delete todos.options.headers.Authorization
}
export function createTodo(todoData) {
return todo.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)
else clearToken()
})
You can also globally add default options to all mande instances:
import { defaults } from 'mande'
defaults.headers.Authorization = 'Bearer token'
All methods defined on a mande
instance accept a type generic to type their return:
const todos = mande('/api/todos', globalOptions)
todos.get<{ text: string, id: number, isFinished: boolean }[]>().then(todos => {
// todos is correctly typed
})
To make Mande work on Server, make sure to provide a fetch
polyfill and to use full URLs and not absolute URLs starting with /
. For example, using node-fetch
, you can do:
export const BASE_URL = process.server
? process.env.NODE_ENV !== 'production'
? 'http://localhost:3000'
: 'https://example.com'
: // on client, do not add the domain, so urls end up like `/api/something`
''
const fetchPolyfill = process.server ? require('node-fetch') : fetch
const contents = mande(BASE_URL + '/api', {}, fetchPolyfill)
When using with Nuxt and SSR, you must wrap exported functions so they automatically proxy cookies and headers on the server:
import { mande, nuxtWrap } from 'mande'
const fetchPolyfill = process.server ? require('node-fetch') : fetch
const users = mande(BASE_URL + '/api/users', {}, fetchPolyfill)
export const getUserById = nuxtWrap(users, (api, id: string) => api.get(id))
Make sure to add it as a buildModule as well:
// nuxt.config.js
module.exports = {
buildModules: ['mande/nuxt'],
}
This prevents requests from accidentally sharing headers or bearer tokens.
Make sure to include mande/nuxt
in your tsconfig.json
:
{
"types": ["@types/node", "@nuxt/types", "mande/nuxt"]
}
Most of the code can be discovered through the autocompletion but the API documentation is available at https://posva.net/mande/.
FAQs
800 bytes modern wrapper around fetch with smart defaults
The npm package mande receives a total of 4,562 weekly downloads. As such, mande popularity was classified as popular.
We found that mande demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.