Skip to content

Instantly share code, notes, and snippets.

@orodio
Created January 15, 2016 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orodio/a70b65b6583e3744f3f0 to your computer and use it in GitHub Desktop.
Save orodio/a70b65b6583e3744f3f0 to your computer and use it in GitHub Desktop.
import xhr from 'superagent'
import { Observable } from 'rx'
/** body
* @description returns the body of a supplied response
* @param {Object} response the response from the xhr request
* @return {Any} what ever the body is
*/
function body (response) {
return response.body
}
/** respond
* @description deals with the response of an xhr request
* @param {Function} resolve promise resolve function
* @param {Function} reject promise reject function
* @param {Function} fn transform response function
* @returns {Function} function that can be used in the xhr callback
*/
function respond (resolve, reject, fn = body) {
return function (err, res) {
if (res.ok) return resolve(fn(res))
return reject(err)
}
}
/** http
* @description standardized http requests
* @param {String} method the http method to be used
* @param {Strung} url the url to make the request to
* @param {Object} data any data that needs to be sent along with the reqeust
* @param {Object} opts any additional options ie: headers and stuff
* @return {Promise} then(res) or catch(err)
*/
export function http (method, url, data = {}, opts = {}) {
return new Promise((resolve, reject) =>
xhr(method, url)
.send(data)
.set(opts)
.end(respond( resolve, reject, body ))
)
}
/** get post del
* @description returns a response from the supplied arguments
* @param {String} url the url to request a response from
* @param {Object} data the data to be passed along with the request
* @param {Object} opts any additional options that need to be passed with the request
* @return {Promise} then(res) or catch(err)
*/
export const get = http.bind(null, `GET`)
export const post = http.bind(null, `POST`)
export const del = http.bind(null, `DELETE`)
/** get$
* @description returns an observable stream of results from the given options
* @param {String} url the url to request
* @param {Object} data the data to be passed along with the request
* @param {Object} opts any options that need to be passed with the resquest
* @return {Observable} an observable stream of responses
*/
export function get$ (...args) {
return Observable.fromPromise(get(...args))
}
/** post$
* @description returns an observable stream of results from the given options
* @param {String} url the url to request
* @param {Object} data the data to be passed along with the request
* @param {Object} opts any options that need to be passed with the resquest
* @return {Observable} an observable stream of responses
*/
export function post$ (...args) {
return Observable.fromPromise(post(...args))
}
/** del$
* @description returns an observable stream of results from the given options
* @param {String} url the url to request
* @param {Object} data the data to be passed along with the request
* @param {Object} opts any options that need to be passed with the resquest
* @return {Observable} an observable stream of responses
*/
export function del$ (...args) {
return Observable.fromPromise(del(...args))
}
/** poll$
* @description returns an observable stream of results from the given options
* @param {Number} rate the number in ms of how often to make the request
* @param {String} url the url to request
* @param {Object} data the data to be passed along with the request
* @param {Object} opts any options that need to be passed with the resquest
* @return {Observable} an observable stream of responses
*/
export function poll$ (rate, ...args) {
return Observable
.interval(rate)
.flatMap(() => get$(...args))
.merge(get$(...args))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment