Skip to content

Commit

Permalink
Add tests, rest of public API
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoz committed Nov 19, 2023
1 parent 264b469 commit 4f748c3
Show file tree
Hide file tree
Showing 3 changed files with 467 additions and 29 deletions.
117 changes: 89 additions & 28 deletions lib/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,61 @@
const request = require("@implydata/request");
const cookies = require("@implydata/request/lib/cookies");
const helpers = require("@implydata/request/lib/helpers");
const extend = require("extend");

const errors = require("./errors");

const defaultTransformations = {
HEAD: function (_body, response, resolveWithFullResponse) {
return resolveWithFullResponse ? response : response.headers;
},
};

class RequestPromise extends request.Request {
static defaultTransformations = {
HEAD: function (_body, response, resolveWithFullResponse) {
return resolveWithFullResponse ? response : response.headers;
},
};

init(requestOptions) {
this._rp_promise = new Promise((resolve, reject) => {
this._rp_resolve = resolve;
this._rp_reject = reject;
});
if (
typeof requestOptions === "object" &&
!!requestOptions &&
!this._callback &&
!this._rp_promise
) {
this._rp_promise = new Promise((resolve, reject) => {
this._rp_resolve = resolve;
this._rp_reject = reject;
});

this._rp_callbackOrig = requestOptions.callback;
requestOptions.callback = (err, response, body) =>
this.callback(err, response, body);
this._rp_callbackOrig = requestOptions.callback;
this.callback = (err, response, body) => {
this._doCallback(err, response, body);
};
requestOptions.callback = this.callback;

if (typeof requestOptions.method === "string") {
requestOptions.method = requestOptions.method.toUpperCase();
}
if (typeof requestOptions.method === "string") {
requestOptions.method = requestOptions.method.toUpperCase();
}

requestOptions.transform =
requestOptions.transform ||
defaultTransformations[requestOptions.method];
requestOptions.transform =
requestOptions.transform ||
RequestPromise.defaultTransformations[requestOptions.method];

this._rp_options = requestOptions;
this._rp_options.simple = requestOptions.simple !== false;
this._rp_options.resolveWithFullResponse =
requestOptions.resolveWithFullResponse === true;
this._rp_options.transform2xxOnly =
requestOptions.transform2xxOnly === true;
this._rp_options = requestOptions;
this._rp_options.simple = requestOptions.simple !== false;
this._rp_options.resolveWithFullResponse =
requestOptions.resolveWithFullResponse === true;
this._rp_options.transform2xxOnly =
requestOptions.transform2xxOnly === true;
}

super.init(requestOptions);
}

callback(err, response, body) {
_doCallback(err, response, body) {
var origCallbackThrewException = false,
thrownException = null;

if (typeof this._rp_callbackOrig === "function") {
try {
this._rp_callbackOrig.apply(this, arguments); // TODO: Apply to self mimics behavior of request@2. Is that also right for request@next?
this._rp_callbackOrig.call(this, err, response, body); // TODO: Apply to self mimics behavior of request@2. Is that also right for request@next?
} catch (e) {
origCallbackThrewException = true;
thrownException = e;
Expand Down Expand Up @@ -171,7 +181,7 @@ function requestPromiseNative(uri, options, callback) {
function verbFunc(verb) {
var method = verb.toUpperCase();
return function (uri, options, callback) {
var params = initParams(uri, options, callback);
var params = request.initParams(uri, options, callback);
params.method = method;
return requestPromiseNative(params, params.callback);
};
Expand All @@ -195,4 +205,55 @@ requestPromiseNative.cookie = function (str) {
return cookies.parse(str);
};

function wrapRequestMethod(method, options, requester, verb) {
return function (uri, opts, callback) {
var params = request.initParams(uri, opts, callback);

var target = {};
extend(true, target, options, params);

target.pool = params.pool || options.pool;

if (verb) {
target.method = verb.toUpperCase();
}

if (typeof requester === "function") {
method = requester;
}

return method(target, target.callback);
};
}

requestPromiseNative.defaults = function (options, requester) {
var self = this;

options = options || {};

if (typeof options === "function") {
requester = options;
options = {};
}

var defaults = wrapRequestMethod(self, options, requester);

var verbs = ["get", "head", "post", "put", "patch", "del", "delete"];
verbs.forEach(function (verb) {
defaults[verb] = wrapRequestMethod(
self[verb],
options,
requester,
verb
);
});

defaults.cookie = wrapRequestMethod(self.cookie, options, requester);
defaults.jar = self.jar;
defaults.defaults = self.defaults;
return defaults;
};

requestPromiseNative.Request = RequestPromise;

module.exports = requestPromiseNative;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"native"
],
"main": "./lib/rp.js",
"typings": "./index.d.ts",
"scripts": {
"test": "mocha test/spec/**/*.js"
},
Expand All @@ -29,7 +30,9 @@
"engines": {
"node": ">=14.0.0"
},
"dependencies": {},
"dependencies": {
"extend": "^3.0.2"
},
"peerDependencies": {
"@implydata/request": "^2.88.3"
},
Expand Down
Loading

0 comments on commit 4f748c3

Please sign in to comment.