0.2.1 • Published 10 years ago
rqst v0.2.1
Minimal XHR request library
Installation
npm install rqstExample
const { get } = require('rqst')
get('https://api.github.com/users/tj')
.then((data) => console.log(data.avatar_url))
.catch(console.error.bind(console))
// -> https://avatars.githubusercontent.com/u/25254?v=3Guide
About the API
Doing basic success requests the promise interface can be used. As soon as you want seperate callback handlers for cancellation, a promise falls short. This is where you can use the traditional callback-based API:
get(API_URL, {
onSucess: () => console.log('successful request'),
onError: () => console.error('something wen\'t wrong'),
onAbort: () => console.info('the request has been aborted')
})POST
const data = {
userId: 1,
title: 'foo',
body: 'bar'
}
post('http://jsonplaceholder.typicode.com/posts', data)
.then((user) => console.log(user))
.catch((err) => console.error(err))Cancellation
Calling any of the http requests (i.E. .get, .put) returns a function to abort a request. This is useful for example if you have a GET request inflight and the user switches pages on a SPA. You want to cancel that request since its no longer neccessary.
const abort = get('http://url.com/data.json', {
onAbort: () => console.log('request has been cancelled.')
})
// aborts the request
abort()Minimize bundle size
rqst is already small as is but if you, for your specific project, only need let's say GET requests you can just require that:
const post = require('rqst/post')
const get = require('rqst/get')File Upload
Todo
Progress
Todo
Tests
npm test