base-fetch v2.1.1
Fetch-based wrapper!
Requires NodeJS v17.5.0 or newer.
Installation
npm install base-fetch
Documentation
If you are using typescript, the following libraries should be provided in your tsconfig file: "DOM", "DOM.Iterable", "ES2015"
Core
Addon
Utilities
You can use dev tools to test following example.
Do not forget to include request
function into you environment (browser, node...) and then the rest of the items if needed, such as unwrap
for example
Example requests can be done at https://jsonplaceholder.typicode.com
Use this helper:
const base = new URL('https://jsonplaceholder.typicode.com')
Basic usage example in browser or node runtimes
import { request, unwrap } from 'base-fetch'
const url = new URL('todos', base)
request(url)
.then(unwrap)
.then(console.log)
.catch(reason => console.error(reason))
Providing type information
interface Shape {
id: number
}
request<Shape>(url)
.then(unwrap)
.then(value => value.id)
Core
request
Request from from the given resource.
Learn:
- Request (
RequestInfo
andRequestInit
are typescript definitions)
example
const url = new URL('todos', base)
request('https://jsonplaceholder.typicode.com/todos')
request(url, { method: 'GET' })
request(url, { method: 'POST', body: JSON.stringify({}) })
request(url, { method: 'PUT', body: new FormData() })
request(url, { method: 'DELETE', signal: new AbortController().signal })
request(url, { method: 'PATCH', credentials: 'include' })
Addon
pipe
Will be written...
Utilities
unwrap
Simple functions taht provides a little bit more cleaner syntax when working with request's return value
example
request(url)
.then(unwrap)
.then(value => value)
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago