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.
## 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
npm install --save @coolaj86/urequest
npm install --save request-lite
```
```js
var request = require('@coolaj86/urequest');
var request = require('request-lite');
request('http://www.google.com', function (error, response, body) {
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
@ -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`:
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).
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).
<!-- TODO

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
'use strict';
//var request = require('urequest');
//var request = require('request-lite');
var request = require('../');
// 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) {
defs = defs || {};
function urequestHelper(opts, cb) {
debug("\n[urequest] processed options:");
function requestLiteHelper(opts, cb) {
debug("\n[request-lite] processed options:");
debug(opts);
function onResponse(resp) {
@ -96,7 +96,7 @@ function setDefaults(defs) {
}
opts.url = resp.headers.location;
opts.uri = url.parse(opts.url);
return urequestHelper(opts, cb);
return requestLiteHelper(opts, cb);
}
}
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());
cb(null, resp, resp.body);
});
@ -169,12 +169,12 @@ function setDefaults(defs) {
// TODO support unix sockets
if ('https:' === finalOpts.protocol) {
// 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);
req = https.request(finalOpts, onResponse);
} else if ('http:' === finalOpts.protocol) {
// 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);
req = http.request(finalOpts, onResponse);
} else {
@ -186,7 +186,7 @@ function setDefaults(defs) {
});
if (_body) {
debug("\n[urequest] body");
debug("\n[request-lite] body");
debug(_body);
// used for chunked encoding
//req.write(_body);
@ -197,8 +197,8 @@ function setDefaults(defs) {
}
}
function urequest(opts, cb) {
debug("\n[urequest] received options:");
function requestLite(opts, cb) {
debug("\n[request-lite] received options:");
debug(opts);
var reqOpts = {};
// 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);
return setDefaults(_defs);
};
[ 'get', 'put', 'post', 'patch', 'delete', 'head', 'options' ].forEach(function (method) {
urequest[method] = function (obj) {
requestLite[method] = function (obj) {
if ('string' === typeof obj) {
obj = { url: obj };
}
obj.method = method.toUpperCase();
urequest(obj);
requestLite(obj);
};
});
urequest.del = urequest.delete;
requestLite.del = requestLite.delete;
return urequest;
return requestLite;
}
var _defaults = {
@ -285,6 +285,6 @@ module.exports._keys = Object.keys(_defaults).concat([
, 'body'
, '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",
"description": "A lightweight drop-in replacement for request",
"main": "index.js",
@ -11,15 +11,16 @@
},
"repository": {
"type": "git",
"url": "https://git.ppl.family/ppl/urequest.js.git"
"url": "https://git.ppl.family/ppl/request-lite.js.git"
},
"keywords": [
"request",
"urequest",
"lightweight",
"alternative",
"http",
"https",
"call"
"client"
],
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)"