@nichoth/http
Helper for making http calls
Last updated 2 years ago by nichoth .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @nichoth/http 
SYNC missed versions from official npm registry.

http

A helper for making http calls. Cleanly separate request data from request IO.

install

$ npm install @nichoth/http

example

http calls as data

This is a function that returns arguments for request or xhr, and can be partially applied in helpful ways.

var HttpRequest = require('../')
var assert = require('assert')

// return data that you can pass to xhr/request
var RequestData = HttpRequest({
    url: 'http://localhost:8000',
    method: 'POST',
    headers: { foo: 'bar' },
    body: { a: 'b' },
    json: true
})

// add fields to the request body, and return a new request object
var data = RequestData({ test: 'test' })
console.log(data)
assert.deepEqual(data, {
    url: 'http://localhost:8000',
    method: 'POST',
    headers: { foo: 'bar' },
    body: { a: 'b', test: 'test' },
    json: true
})

// pass in an array of path segments to add to the URL
var FooRequest = RequestData(['foo'])

// return a request with the given object added to the body
var fooData = FooRequest({ hello: 'world' })
console.log(fooData)
assert.deepEqual(fooData, {
    url: 'http://localhost:8000/foo',
    method: 'POST',
    headers: { foo: 'bar' },
    body: { a: 'b', hello: 'world' },
    json: true
})

// pass in nothing to get the request as is
var _data = FooRequest()
console.log('_data', _data)
assert.deepEqual(_data, {
    url: 'http://localhost:8000/foo',
    method: 'POST',
    headers: { foo: 'bar' },
    body: { a: 'b' },
    json: true
})

IO

Make an http request an map the response

var Io = require('../io')

// create a new function that calls the given predicates with the response
var io = Io
    .map(function (err, res, body) {
        if (err) return [err]
        return [null, body.data]
    })
    .map(function (err, res) {
        if (err) return [err]
        return [null, res.foo]
    })

io({
    url: 'example.com',
    method: 'POST',
    body: { hello: 'world' },
    json: true
}, function onResponse (err, res) {
    // the arguments here have been mapped by the predicates above
    // res === body.data.foo
})

Current Tags

  • 0.1.0                                ...           latest (2 years ago)

2 Versions

  • 0.1.0                                ...           2 years ago
  • 0.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |