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

fix: merge defaults and options using defu + queryParams can be inherited #210

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class HTTP {
}

create(options) {
const { retry, timeout, prefixUrl, headers } = this._defaults
const { retry, timeout, prefixUrl, headers, searchParams } = this._defaults

return createHttpInstance(defu(options, { retry, timeout, prefixUrl, headers }))
return createHttpInstance(defu(options, { retry, timeout, prefixUrl, headers, searchParams }))
}
}

Expand All @@ -82,7 +82,23 @@ for (let method of ['get', 'head', 'delete', 'post', 'put', 'patch']) {
}
}

const _options = { ...this._defaults, ...options }
const _options = defu(options, this._defaults)

// Merge searchParams (mix strings, objects, and URLSearchParams instances)
if (this._defaults.searchParams && options && options.searchParams) {
const params1 = new URLSearchParams(this._defaults.searchParams);
const params2 = new URLSearchParams(options.searchParams);

for (let [key] of params2.entries()) {
// params2 overrides params1 to let's remove any param with the same key
params1.delete(key)
}

for (let [key, val] of params2.entries()) {
params1.append(key, val);
}
_options.searchParams = params1
}

if (/^https?/.test(url)) {
delete _options.prefixUrl
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"consola": "^2.15.0",
"defu": "^3.2.2",
"destr": "^1.0.1",
"ky": "^0.25.1",
"ky": "^0.31.3",
"node-fetch": "^2.6.1",
"web-streams-polyfill": "^3.0.1"
},
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6973,10 +6973,10 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

ky@^0.25.1:
version "0.25.1"
resolved "https://registry.yarnpkg.com/ky/-/ky-0.25.1.tgz#0df0bd872a9cc57e31acd5dbc1443547c881bfbc"
integrity sha512-PjpCEWlIU7VpiMVrTwssahkYXX1by6NCT0fhTUX34F3DTinARlgMpriuroolugFPcMgpPWrOW4mTb984Qm1RXA==
ky@^0.31.3:
version "0.31.3"
resolved "https://registry.yarnpkg.com/ky/-/ky-0.31.3.tgz#f00e72b9c0467ab19b0b20f15daf7dff09f67dde"
integrity sha512-YDDQKG0Lt4PFSPZGJI8WKAm5y+1ebbeA306am+4nT7riX13wjNVD5sR/QOKtgsaQaARwrdUHlv0M9kJ1qv+Jug==

last-call-webpack-plugin@^3.0.0:
version "3.0.0"
Expand Down