Utilities for custom fetch
npm install create-fetchUtilities for custom fetch.



``console`
npm install create-fetch
`js
import 'cross-fetch/polyfill' // add universal-fetch polyfill if needed
import {composeFetch, query, headers, bodify} from 'create-fetch'
const myFetch = composeFetch([
query(),
bodify(),
headers({'x-requested-with': 'fetch'}),
])(fetch)
// same as:
// const myFetch = compose(query(), ...)(fetch)
// could also use pipeline operator:
// const myFetch = fetch |> query() |> ...
myFetch('/api', {
method: 'POST',
query: {filter: 'user'},
body: {name: 'JoJo'},
})
// =>
// POST /api?filter=user
// Request Headers:
// content-type: application/json
// x-requested-with: fetch
// Request Payload:
// {"name":"JoJo"}
`
`ts
import {FetchCompose, FetchEnhancer, query, bodify} from 'create-fetch'
import flowRight from 'lodash/flowRight'
// inject extra options
type FooInit = {foo?: boolean}
const foo =
(): FetchEnhancer
(fetch) =>
(url, {foo, ...options}) => {
if (foo) {
// ...
}
return fetch(url, options)
}
const bar = (): FetchEnhancer => (fetch) => (url, options) => {
return fetch(url, options)
}
// same as composeFetch
const myFetch = (flowRight as FetchCompose)([
//
foo(),
bar(),
query(),
bodify(),
])(fetch)
// (url: RequestInfo, options?: RequestInit & QueryInit & FooInit) => Promise
myFetch('/', {
withEncrypt: true,
})
`
Import from module script (1.4K gzip size):
`html`
- defaults(options)
- baseUrl(url)
- headers(options)
- query()
- bodify()
- xsrf(options)
Add default request options.
`js
import {defaults} from 'create-fetch'
const myFetch = defaults({
credentials: 'same-origin',
})(fetch)
myFetch('/')
`
Add request base url.
`js
import {baseUrl} from 'create-fetch'
const ghApi = baseUrl('https://api.github.com')(fetch)
ghApi('/users')
// =>
// GET https://api.github.com/users
`
Add default request headers.
`js
import {headers} from 'create-fetch'
const myFetch = headers({
'x-requested-with': 'fetch',
// null or undefined will be removed
'x-foo': null,
})(fetch)
myFetch('/')
// =>
// GET /
// Request Headers:
// x-requested-with: fetch
`
Stringify query string.
`js
import {query} from 'create-fetch'
const myFetch = query()(fetch)
myFetch('/', {
query: {
filter: 'user'
// null or undefined will be set to empty
foo: null
},
})
// =>
// GET /?filter=user&foo
`
Stringify request body.
`js
import {bodify} from 'create-fetch'
const myFetch = bodify()(fetch)
// stringify json by default
myFetch('/', {
method: 'POST',
body: {name: 'JoJo'},
})
// =>
// POST /
// Request Headers:
// content-type: application/json
// Request Payload:
// {"name":"JoJo"}
// stringify form
myFetch('/', {
method: 'POST',
body: new URLSearchParams({name: 'JoJo'}),
})
// =>
// POST /
// Request Headers:
// content-type: application/x-www-form-urlencoded
// Request Payload:
// name=JoJo
`
Add XSRF token header.
`js
import {xsrf} from 'create-fetch'
const myFetch = xsrf({
cookieName, // defaults to _xsrfx-xsrftoken
headerName, // defaults to
})(fetch)
myFetch('/', {
method: 'POST',
})
// =>
// POST /
// Request Headers:
// x-xsrftoken:
``