-
Notifications
You must be signed in to change notification settings - Fork 1
/
search-params-url.ts
33 lines (30 loc) · 1013 Bytes
/
search-params-url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// deno-lint-ignore no-explicit-any
export type SearchParamsInit = [string, any][] | Record<string, any> | string | URLSearchParams;
// This could be it's own module...
/**
* Like `URL`, but accepts a `params` argument that is added to the search parameters/query string.
*/
export class SearchParamsURL extends URL {
constructor(
url: string | URL,
params?: SearchParamsInit | null,
base?: string | URL
) {
super(url as string, base);
const iterable = Array.isArray(params) || params instanceof URLSearchParams
? params
: typeof params === 'string'
? new URLSearchParams(params)
: Object.entries(params ?? {})
for (const [k, v] of iterable)
this.searchParams.append(k, '' + v);
}
}
export {
SearchParamsURL as SearchURL,
SearchParamsURL as ParamsURL,
}
/** @deprecated Use SearchParamsURL instead */
export const urlWithParams = (...args: ConstructorParameters<typeof SearchParamsURL>) => {
return new SearchParamsURL(...args).href;
}