README
sendr
An isomorphic request library with reusable & declarative queries
Install
npm i sendr
<script src="https://wzrd.in/standalone/sendr@1.2.1"></script>
Import
import sendr from 'sendr'
const sendr = require('sendr')
Request
Requests are immutable - every modifier creates a new request, allowing reusability and templating.
const todo = 'https://jsonplaceholder.typicode.com/todos/:id'
const { status, headers, data } = await sendr(todo)
.params({ id: 1 })
.type('json')
.send()
.progress((current, total) => {
console.log(`${((current / total) * 100).toFixed(2)}%`)
})
console.log(data)
Modifiers
path
Append to the request URL.
function path(...paths: sendr.Path[]): sendr.Request
sendr('/users').path('ken')
params
Add or remove URL parameters. Set the value to null
or undefined
to remove them.
function params(params: sendr.Params): sendr.Request
sendr('/users/:id').params({ id: 'ken' })
method
Set the request method.
function method(method: sendr.Method): sendr.Request
sendr('/posts').method('post')
query
Add or remove query parameters. Set the value to null
or undefined
to remove them.
function query(query: sendr.Query): sendr.Request
sendr('/users').query({ order: 'ascending' })
credentials
Include credentials in cross-origin requests.
function credentials(credentials: sendr.Credentials): sendr.Request
sendr('https://example.com/posts').method('post').credentials(true) // true or false
headers
Add or remove headers. Set the value to null
or undefined
to remove them.
function headers(headers: sendr.RequestHeaders): sendr.Request
sendr('/messages')
.method('post')
.headers({ 'content-type': 'application/json' })
body
Set the request body.
function body(body: sendr.Body): sendr.Request
sendr('/messages')
.method('post')
.headers({ 'content-type': 'application/json' })
.body(JSON.stringify({ message: 'Hello from sendr!' }))
type
Set the response type.
function type(type: sendr.ResponseType): sendr.Request
sendr('https://jsonplaceholder.typicode.com/todos/:id')
.params({ id: 1 })
.type('json')
Publishers
send
Send the request. Returns a Promise
containing the response.
function send<Data>(): sendr.FutureResponse<Data>
const { data } = await sendr('/users/ken.png').send()
progress
Listen for the progress of the request.
function progress(progress: sendr.Progress): sendr.Request
function progress(progress: sendr.Progress): sendr.FutureResponse<Data>
const { data } = await sendr('/users/ken.png')
.progress((current, total) => {
// Before send
console.log(`Progress #1: ${((current / total) * 100).toFixed(2)}%`)
})
.send()
.progress((current, total) => {
// After send
console.log(`Progress #2: ${((current / total) * 100).toFixed(2)}%`)
})
abort
Abort the request. Causes the request to fail with code aborted
.
function abort(): void
sendr('/users/ken.png').send().abort()
(timeout) abort
Set a timeout for the request. Set the value to null
or undefined
to remove the timeout. Causes the request to fail with code aborted
after the timeout.
function abort(after: sendr.Timeout): sendr.Request
function abort(after: sendr.Timeout): sendr.FutureResponse<Data>
sendr('/users/ken.png')
.abort(50) // Timeout of 50ms
.send()
.abort(null) // Remove the timeout
.abort(300) // Set a new timeout of 300ms