From 9bff960bda42fd8af7b8569f121ca35c7f4cfae4 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 26 Jun 2020 21:26:46 +0200 Subject: [PATCH] fix: regression from #272 --- lib/client.js | 5 +++-- lib/helpers/client.js | 4 ++-- lib/helpers/merge.js | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 lib/helpers/merge.js diff --git a/lib/client.js b/lib/client.js index 0024c573..7c10ba61 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1594,10 +1594,11 @@ BaseClient.prototype.resource = deprecate( /* istanbul ignore next */ async function resource(resourceUrl, accessToken, options) { let token = accessToken; - const opts = defaultsDeep({}, options, { + const opts = { verb: 'GET', via: 'header', - }); + ...options, + }; if (token instanceof TokenSet) { if (!token.access_token) { diff --git a/lib/helpers/client.js b/lib/helpers/client.js index 3dc98644..b438b540 100644 --- a/lib/helpers/client.js +++ b/lib/helpers/client.js @@ -5,7 +5,7 @@ const { random } = require('./generators'); const now = require('./unix_timestamp'); const request = require('./request'); const instance = require('./weak_cache'); -const { deep: defaultsDeep } = require('./defaults'); +const merge = require('./merge'); const formUrlEncode = (value) => encodeURIComponent(value).replace(/%20/g, '+'); @@ -125,7 +125,7 @@ async function authenticatedPost(endpoint, opts, { clientAssertionPayload, endpointAuthMethod = endpoint, } = {}) { const auth = await authFor.call(this, endpointAuthMethod, { clientAssertionPayload }); - const requestOpts = defaultsDeep({ form: true }, auth, opts); + const requestOpts = merge(opts, auth, { form: true }); const mTLS = this[`${endpointAuthMethod}_endpoint_auth_method`].includes('tls_client_auth') || (endpoint === 'token' && this.tls_client_certificate_bound_access_tokens); diff --git a/lib/helpers/merge.js b/lib/helpers/merge.js new file mode 100644 index 00000000..c6410052 --- /dev/null +++ b/lib/helpers/merge.js @@ -0,0 +1,23 @@ +/* eslint-disable no-restricted-syntax */ + +const isPlainObject = require('./is_plain_object'); + +function merge(...sources) { + const target = {}; + for (const source of sources) { + if (!isPlainObject(source)) { + continue; // eslint-disable-line no-continue + } + for (const [key, value] of Object.entries(source)) { + if (isPlainObject(target[key]) && isPlainObject(value)) { + target[key] = merge(target[key], value); + } else if (typeof value !== 'undefined') { + target[key] = value; + } + } + } + + return target; +} + +module.exports = merge.bind(undefined, false);