Skip to content

Commit

Permalink
Hide the feature behind an argument
Browse files Browse the repository at this point in the history
preserves backwards compatibility
  • Loading branch information
elbywan committed Jun 1, 2024
1 parent 6c7fcc7 commit 3ff477e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/addons/queryString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function stringify(value?: string | null): string | null {
return typeof value !== "undefined" ? value : ""
}

const appendQueryParams = (url: string, qp: object | string, replace: boolean, config: Config) => {
const appendQueryParams = (url: string, qp: object | string, replace: boolean, omitUndefinedOrNullValues: boolean, config: Config) => {
let queryString: string

if (typeof qp === "string") {
Expand All @@ -13,7 +13,7 @@ const appendQueryParams = (url: string, qp: object | string, replace: boolean, c
const usp = config.polyfill("URLSearchParams", true, true)
for (const key in qp) {
const value = qp[key]
if (value === null || value === undefined) continue
if (omitUndefinedOrNullValues && (value === null || value === undefined)) continue
if (qp[key] instanceof Array) {
for (const val of value)
usp.append(key, stringify(val))
Expand Down Expand Up @@ -41,6 +41,7 @@ export interface QueryStringAddon {
* to the current url. String values are used as the query string verbatim.
*
* Pass `true` as the second argument to replace existing query parameters.
* Pass `true` as the third argument to completely omit the key=value pair for undefined or null values.
*
* ```
* import QueryAddon from "wretch/addons/queryString"
Expand Down Expand Up @@ -85,7 +86,7 @@ export interface QueryStringAddon {
*
* @param qp - An object which will be converted, or a string which will be used verbatim.
*/
query<T extends QueryStringAddon, C, R>(this: T & Wretch<T, C, R>, qp: object | string, replace?: boolean): this
query<T extends QueryStringAddon, C, R>(this: T & Wretch<T, C, R>, qp: object | string, replace?: boolean, omitUndefinedOrNullValues?: boolean): this
}

/**
Expand All @@ -99,8 +100,8 @@ export interface QueryStringAddon {
*/
const queryString: WretchAddon<QueryStringAddon> = {
wretch: {
query(qp, replace = false) {
return { ...this, _url: appendQueryParams(this._url, qp, replace, this._config) }
query(qp, replace = false, omitUndefinedOrNullValues = false) {
return { ...this, _url: appendQueryParams(this._url, qp, replace, omitUndefinedOrNullValues, this._config) }
}
}
}
Expand Down

0 comments on commit 3ff477e

Please sign in to comment.