Last active
April 2, 2016 01:37
-
-
Save SamuelMarks/d561575d13499c53ebead97e0a51eb04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var http_1 = require('http'); | |
function httpF(method) { | |
return function (options, body_or_cb, cb) { | |
if (!cb) { | |
cb = body_or_cb; | |
body_or_cb = null; | |
} | |
options['method'] = method; | |
if (body_or_cb) | |
if (!options) | |
options = { 'headers': { 'Content-Length': Buffer.byteLength(body_or_cb) } }; | |
else if (!options.headers) | |
options.headers = { 'Content-Length': Buffer.byteLength(body_or_cb) }; | |
else if (!options.headers['Content-Length']) | |
options.headers['Content-Length'] = Buffer.byteLength(body_or_cb); | |
var req = http_1.request(options, function (res) { | |
if (!res) | |
return cb(res); | |
else if ((res.statusCode / 100 | 0) > 3) | |
return cb(res); | |
return cb(null, res); | |
}); | |
body_or_cb && req.write(body_or_cb); | |
req.end(); | |
return req; | |
}; | |
} | |
var httpPOST = httpF('POST'); | |
httpPOST({ | |
protocol: 'http:', | |
host: 'httpbin.org', | |
path: '/post', | |
headers: { 'Content-Type': 'application/json' } | |
}, JSON.stringify({ hello: 'world' }), function (err, res) { | |
if (err) | |
console.error(err.statusCode, err.statusMessage); | |
else | |
console.info(res.statusCode, res.statusMessage); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {RequestOptions, IncomingMessage, ClientRequest, request as http_request} from 'http'; | |
interface callback { | |
(res: IncomingMessage): void; | |
} | |
interface cb { | |
(err: IncomingMessage, res?: IncomingMessage): void; | |
} | |
function httpF(method: 'POST'|'PUT'|'PATCH'|'HEAD'|'GET'|'DELETE') { | |
return (options: RequestOptions, | |
body_or_cb: string|callback|cb|AsyncResultCallback<{}>, | |
cb?: callback|cb|AsyncResultCallback<{}>): ClientRequest => { | |
if (!cb) { | |
cb = <callback|cb|AsyncResultCallback<{}>>body_or_cb; | |
body_or_cb = null; | |
} | |
options['method'] = method; | |
if (body_or_cb) | |
if (!options) | |
options = {'headers': {'Content-Length': Buffer.byteLength(<string>body_or_cb)}}; | |
else if (!options.headers) | |
options.headers = {'Content-Length': Buffer.byteLength(<string>body_or_cb)}; | |
else if (!options.headers['Content-Length']) | |
options.headers['Content-Length'] = Buffer.byteLength(<string>body_or_cb); | |
const req = http_request(options, (res: IncomingMessage) => { | |
if (!res) return (<cb>cb)(res); | |
else if ((res.statusCode / 100 | 0) > 3) return (<cb>cb)(res); | |
return (<cb>cb)(null, res); | |
}); | |
//body_or_cb ? req.end(<string>body_or_cb, cb) : req.end(); | |
body_or_cb && req.write(body_or_cb); | |
req.end(); | |
return req; | |
} | |
} | |
const httpPOST = httpF('POST'); | |
httpPOST({ | |
protocol: 'http:', | |
host: 'httpbin.org', | |
path: '/post', | |
headers: {'Content-Type': 'application/json'} | |
}, JSON.stringify({hello: 'world'}), (err: IncomingMessage, res: IncomingMessage) => { | |
if (err) console.error(err.statusCode, err.statusMessage); | |
else console.info(res.statusCode, res.statusMessage); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment