urequest => request-lite

This commit is contained in:
AJ ONeal 2018-06-19 01:55:40 -06:00
parent bdf5262f1b
commit f3b45b59f8
9 changed files with 36 additions and 35 deletions

View File

@ -1,19 +1,19 @@
# µRequest - Minimalist HTTP client # request-lite - A lightweight drop-in replacement for request
A lightweight alternative to (and drop-in replacement for) request. A zero-dependency alternative to request for 90% of the use cases with only 10% of the code.
Written from scratch. Written from scratch.
## Super simple to use ## Super simple to use
µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default. request-lite is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default.
```bash ```bash
npm install --save @coolaj86/urequest npm install --save request-lite
``` ```
```js ```js
var request = require('@coolaj86/urequest'); var request = require('request-lite');
request('http://www.google.com', function (error, response, body) { request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
@ -121,10 +121,10 @@ These HTTP method convenience functions act just like `request()` but with a def
There are at least <!--three--> two ways to debug the operation of `request`: There are at least <!--three--> two ways to debug the operation of `request`:
1. Launch the node process like `NODE_DEBUG=urequest node script.js` 1. Launch the node process like `NODE_DEBUG=request-lite node script.js`
(`lib,request,otherlib` works too). (`lib,request,otherlib` works too).
2. Set `require('@coolaj86/urequest').debug = true` at any time (this does the same thing 2. Set `require('request-lite').debug = true` at any time (this does the same thing
as #1). as #1).
<!-- TODO <!-- TODO

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// if NODE_DEBUG contains 'urequest' then this should print out 'DEBUG ON for urequest' // if NODE_DEBUG contains 'request-lite' then this should print out 'DEBUG ON for request-lite'
// NODE_DEBUG="urequest" node examples/debug.js // NODE_DEBUG="request-lite" node examples/debug.js
// //
//require('urequest'); //require('request-lite');
require('../'); require('../');

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
//var request = require('urequest'); //var request = require('request-lite');
var request = require('../'); var request = require('../');
// will redirect to https://www.github.com and then https://github.com // will redirect to https://www.github.com and then https://github.com

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
//var request = require('urequest'); //var request = require('request-lite');
var request = require('../'); var request = require('../');
request('https://www.google.com', function (error, response, body) { request('https://www.google.com', function (error, response, body) {
if (error) { if (error) {

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
//var request = require('urequest'); //var request = require('request-lite');
var request = require('../'); var request = require('../');
request('http://www.google.com', function (error, response, body) { request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred console.log('error:', error); // Print the error if one occurred

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
//var request = require('urequest'); //var request = require('request-lite');
var request = require('../'); var request = require('../');
request('https://www.google.com', function (error, response, body) { request('https://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred console.log('error:', error); // Print the error if one occurred

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
//var request = require('urequest'); //var request = require('request-lite');
var request = require('../'); var request = require('../');
// would normally redirect to https://www.github.com and then https://github.com // would normally redirect to https://www.github.com and then https://github.com

View File

@ -50,8 +50,8 @@ function toJSONifier(keys) {
function setDefaults(defs) { function setDefaults(defs) {
defs = defs || {}; defs = defs || {};
function urequestHelper(opts, cb) { function requestLiteHelper(opts, cb) {
debug("\n[urequest] processed options:"); debug("\n[request-lite] processed options:");
debug(opts); debug(opts);
function onResponse(resp) { function onResponse(resp) {
@ -96,7 +96,7 @@ function setDefaults(defs) {
} }
opts.url = resp.headers.location; opts.url = resp.headers.location;
opts.uri = url.parse(opts.url); opts.uri = url.parse(opts.url);
return urequestHelper(opts, cb); return requestLiteHelper(opts, cb);
} }
} }
if (null === opts.encoding) { if (null === opts.encoding) {
@ -132,7 +132,7 @@ function setDefaults(defs) {
} }
} }
debug("\n[urequest] resp.toJSON():"); debug("\n[request-lite] resp.toJSON():");
debug(resp.toJSON()); debug(resp.toJSON());
cb(null, resp, resp.body); cb(null, resp, resp.body);
}); });
@ -169,12 +169,12 @@ function setDefaults(defs) {
// TODO support unix sockets // TODO support unix sockets
if ('https:' === finalOpts.protocol) { if ('https:' === finalOpts.protocol) {
// https://nodejs.org/api/https.html#https_https_request_options_callback // https://nodejs.org/api/https.html#https_https_request_options_callback
debug("\n[urequest] https.request(opts):"); debug("\n[request-lite] https.request(opts):");
debug(finalOpts); debug(finalOpts);
req = https.request(finalOpts, onResponse); req = https.request(finalOpts, onResponse);
} else if ('http:' === finalOpts.protocol) { } else if ('http:' === finalOpts.protocol) {
// https://nodejs.org/api/http.html#http_http_request_options_callback // https://nodejs.org/api/http.html#http_http_request_options_callback
debug("\n[urequest] http.request(opts):"); debug("\n[request-lite] http.request(opts):");
debug(finalOpts); debug(finalOpts);
req = http.request(finalOpts, onResponse); req = http.request(finalOpts, onResponse);
} else { } else {
@ -186,7 +186,7 @@ function setDefaults(defs) {
}); });
if (_body) { if (_body) {
debug("\n[urequest] body"); debug("\n[request-lite] body");
debug(_body); debug(_body);
// used for chunked encoding // used for chunked encoding
//req.write(_body); //req.write(_body);
@ -197,8 +197,8 @@ function setDefaults(defs) {
} }
} }
function urequest(opts, cb) { function requestLite(opts, cb) {
debug("\n[urequest] received options:"); debug("\n[request-lite] received options:");
debug(opts); debug(opts);
var reqOpts = {}; var reqOpts = {};
// request.js behavior: // request.js behavior:
@ -242,25 +242,25 @@ function setDefaults(defs) {
} }
}); });
return urequestHelper(reqOpts, cb); return requestLiteHelper(reqOpts, cb);
} }
urequest.defaults = function (_defs) { requestLite.defaults = function (_defs) {
_defs = mergeOrDelete(defs, _defs); _defs = mergeOrDelete(defs, _defs);
return setDefaults(_defs); return setDefaults(_defs);
}; };
[ 'get', 'put', 'post', 'patch', 'delete', 'head', 'options' ].forEach(function (method) { [ 'get', 'put', 'post', 'patch', 'delete', 'head', 'options' ].forEach(function (method) {
urequest[method] = function (obj) { requestLite[method] = function (obj) {
if ('string' === typeof obj) { if ('string' === typeof obj) {
obj = { url: obj }; obj = { url: obj };
} }
obj.method = method.toUpperCase(); obj.method = method.toUpperCase();
urequest(obj); requestLite(obj);
}; };
}); });
urequest.del = urequest.delete; requestLite.del = requestLite.delete;
return urequest; return requestLite;
} }
var _defaults = { var _defaults = {
@ -285,6 +285,6 @@ module.exports._keys = Object.keys(_defaults).concat([
, 'body' , 'body'
, 'json' , 'json'
]); ]);
module.exports.debug = (-1 !== (process.env.NODE_DEBUG||'').split(/\s+/g).indexOf('urequest')); module.exports.debug = (-1 !== (process.env.NODE_DEBUG||'').split(/\s+/g).indexOf('request-lite'));
debug("DEBUG ON for urequest"); debug("DEBUG ON for request-lite");

View File

@ -1,5 +1,5 @@
{ {
"name": "@coolaj86/urequest", "name": "request-lite",
"version": "1.1.1", "version": "1.1.1",
"description": "A lightweight drop-in replacement for request", "description": "A lightweight drop-in replacement for request",
"main": "index.js", "main": "index.js",
@ -11,15 +11,16 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://git.ppl.family/ppl/urequest.js.git" "url": "https://git.ppl.family/ppl/request-lite.js.git"
}, },
"keywords": [ "keywords": [
"request", "request",
"urequest",
"lightweight", "lightweight",
"alternative", "alternative",
"http", "http",
"https", "https",
"call" "client"
], ],
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)", "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)" "license": "(MIT OR Apache-2.0)"