Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: added some utils fns #1657

Merged
merged 5 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Emitter = require('component-emitter');
const safeStringify = require('fast-safe-stringify');
const qs = require('qs');
const RequestBase = require('./request-base');
const isObject = require('./is-object');
const { isObject, mixin, hasOwn } = require('./utils');
const ResponseBase = require('./response-base');
const Agent = require('./agent-base');

Expand Down Expand Up @@ -110,7 +110,7 @@ function serialize(object) {
if (!isObject(object)) return object;
const pairs = [];
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key))
if (hasOwn(object, key))
pushEncodedKeyValuePair(pairs, key, object[key]);
}

Expand Down Expand Up @@ -139,7 +139,7 @@ function pushEncodedKeyValuePair(pairs, key, value) {
}
} else if (isObject(value)) {
for (const subkey in value) {
if (Object.prototype.hasOwnProperty.call(value, subkey))
if (hasOwn(value, subkey))
pushEncodedKeyValuePair(pairs, `${key}[${subkey}]`, value[subkey]);
}
} else {
Expand Down Expand Up @@ -361,8 +361,7 @@ function Response(request_) {
}
}

// eslint-disable-next-line new-cap
ResponseBase(Response.prototype);
mixin(Response.prototype, ResponseBase.prototype);

/**
* Parse the given body `str`.
Expand Down Expand Up @@ -492,7 +491,7 @@ function Request(method, url) {
// eslint-disable-next-line new-cap
Emitter(Request.prototype);
// eslint-disable-next-line new-cap
RequestBase(Request.prototype);
mixin(Request.prototype, RequestBase.prototype);

/**
* Set Content-Type to `type`, mapping values from `request.types`.
Expand Down Expand Up @@ -875,7 +874,7 @@ Request.prototype._end = function () {
for (const field in this.header) {
if (this.header[field] === null) continue;

if (Object.prototype.hasOwnProperty.call(this.header, field))
if (hasOwn(this.header, field))
xhr.setRequestHeader(field, this.header[field]);
}

Expand Down
13 changes: 0 additions & 13 deletions src/is-object.js

This file was deleted.

10 changes: 6 additions & 4 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const RequestBase = require('../request-base');
const { unzip } = require('./unzip');
const Response = require('./response');

const { mixin, hasOwn } = utils;

let http2;

if (semver.gte(process.version, 'v10.10.0')) http2 = require('./http2wrapper');
Expand Down Expand Up @@ -174,7 +176,7 @@ function Request(method, url) {
*/
util.inherits(Request, Stream);
// eslint-disable-next-line new-cap
RequestBase(Request.prototype);
mixin(Request.prototype, RequestBase.prototype);

/**
* Enable or Disable http2.
Expand Down Expand Up @@ -825,13 +827,13 @@ Request.prototype.request = function () {
}

for (const key in this.header) {
if (Object.prototype.hasOwnProperty.call(this.header, key))
if (hasOwn(this.header, key))
req.setHeader(key, this.header[key]);
}

// add cookies
if (this.cookies) {
if (Object.prototype.hasOwnProperty.call(this._header, 'cookie')) {
if (hasOwn(this._header, 'cookie')) {
// merge
const temporaryJar = new CookieJar.CookieJar();
temporaryJar.setCookies(this._header.cookie.split(';'));
Expand Down Expand Up @@ -1214,7 +1216,7 @@ Request.prototype._end = function () {
// set headers
const headers = formData.getHeaders();
for (const i in headers) {
if (Object.prototype.hasOwnProperty.call(headers, i)) {
if (hasOwn(headers, i)) {
debug('setting FormData header: "%s: %s"', i, headers[i]);
req.setHeader(i, headers[i]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/node/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const util = require('util');
const Stream = require('stream');
const ResponseBase = require('../response-base');
const { mixin } = require('../utils');

/**
* Expose `Response`.
Expand Down Expand Up @@ -52,8 +53,8 @@ function Response(request) {
*/

util.inherits(Response, Stream);
// eslint-disable-next-line new-cap
ResponseBase(Response.prototype);

mixin(Response.prototype, ResponseBase.prototype);

/**
* Implements methods of a `ReadableStream`
Expand Down
33 changes: 7 additions & 26 deletions src/request-base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Module of mixed-in functions shared between node and client code
*/
const isObject = require('./is-object');
const { isObject, hasOwn } = require('./utils');

/**
* Expose `RequestBase`.
Expand All @@ -15,26 +15,7 @@ module.exports = RequestBase;
* @api public
*/

function RequestBase(object) {
if (object) return mixin(object);
}

/**
* Mixin the prototype properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/

function mixin(object) {
for (const key in RequestBase.prototype) {
if (Object.prototype.hasOwnProperty.call(RequestBase.prototype, key))
object[key] = RequestBase.prototype[key];
}

return object;
}
function RequestBase() {}

/**
* Clear previous timeout.
Expand Down Expand Up @@ -127,7 +108,7 @@ RequestBase.prototype.timeout = function (options) {
}

for (const option in options) {
if (Object.prototype.hasOwnProperty.call(options, option)) {
if (hasOwn(options, option)) {
switch (option) {
case 'deadline':
this._timeout = options.deadline;
Expand Down Expand Up @@ -391,7 +372,7 @@ RequestBase.prototype.getHeader = RequestBase.prototype.get;
RequestBase.prototype.set = function (field, value) {
if (isObject(field)) {
for (const key in field) {
if (Object.prototype.hasOwnProperty.call(field, key))
if (hasOwn(field, key))
this.set(key, field[key]);
}

Expand Down Expand Up @@ -454,7 +435,7 @@ RequestBase.prototype.field = function (name, value) {

if (isObject(name)) {
for (const key in name) {
if (Object.prototype.hasOwnProperty.call(name, key))
if (hasOwn(name, key))
this.field(key, name[key]);
}

Expand All @@ -463,7 +444,7 @@ RequestBase.prototype.field = function (name, value) {

if (Array.isArray(value)) {
for (const i in value) {
if (Object.prototype.hasOwnProperty.call(value, i))
if (hasOwn(value, i))
this.field(name, value[i]);
}

Expand Down Expand Up @@ -652,7 +633,7 @@ RequestBase.prototype.send = function (data) {
// merge
if (isObject_ && isObject(this._data)) {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key))
if (hasOwn(data, key))
this._data[key] = data[key];
}
} else if (typeof data === 'string') {
Expand Down
21 changes: 1 addition & 20 deletions src/response-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,7 @@ module.exports = ResponseBase;
* @api public
*/

function ResponseBase(object) {
if (object) return mixin(object);
}

/**
* Mixin the prototype properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/

function mixin(object) {
for (const key in ResponseBase.prototype) {
if (Object.prototype.hasOwnProperty.call(ResponseBase.prototype, key))
object[key] = ResponseBase.prototype[key];
}

return object;
}
function ResponseBase() {}

/**
* Get case-insensitive `field` value.
Expand Down
32 changes: 32 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,35 @@ exports.cleanHeader = (header, changesOrigin) => {

return header;
};

/**
* Check if `obj` is an object.
*
* @param {Object} object
* @return {Boolean}
* @api private
*/
exports.isObject = (object) => {
return object !== null && typeof object === 'object';
}

/**
* Object.hasOwn fallback/polyfill.
*
* @type {(object: object, property: string) => boolean} object
* @api private
*/
exports.hasOwn = Object.hasOwn || function (object, property) {
if (object == null) {
throw new TypeError("Cannot convert undefined or null to object")
}
return Object.prototype.hasOwnProperty.call(Object(object), property)
}

exports.mixin = (target, source) => {
for (const key in source) {
if (exports.hasOwn(source, key)) {
target[key] = source[key];
}
}
}