Skip to content

Commit

Permalink
🤖 Merge PR #54650 [@types/urijs] Fix unnecessarily strict query param…
Browse files Browse the repository at this point in the history
…s by @binury

* Fix: unnecessarily strict urijs query params

* Clean: QueryDataMap record as Partial

Suggested per #54650

Co-authored-by: Bin Ury <binury@users.noreply.github.com>
  • Loading branch information
binury and binury committed Sep 7, 2021
1 parent a61d1d2 commit b0f6a9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
26 changes: 12 additions & 14 deletions types/urijs/index.d.ts
Expand Up @@ -31,13 +31,13 @@ declare const URI: {

new(value?: string | URI.URIOptions | HTMLElement): URI;

addQuery(data: object, prop: string, value: string): object;
addQuery(data: object, qryObj: object): object;
addQuery(data: URI.QueryDataMap, prop: string, value: string): object;
addQuery(data: URI.QueryDataMap, qryObj: object): object;

build(parts: URI.URIOptions): string;
buildAuthority(parts: { username?: string | undefined; password?: string | undefined; hostname?: string | undefined; port?: string | undefined }): string;
buildHost(parts: { hostname?: string | undefined; port?: string | undefined }): string;
buildQuery(data: object, duplicateQueryParameters?: boolean, escapeQuerySpace?: boolean): string;
buildQuery(data: URI.QueryDataMap, duplicateQueryParameters?: boolean, escapeQuerySpace?: boolean): string;
buildUserinfo(parts: { username?: string | undefined; password?: string | undefined }): string;

commonPath(path1: string, path2: string): string;
Expand Down Expand Up @@ -118,17 +118,15 @@ declare namespace URI {
preventInvalidHostname: boolean;
}

interface QueryDataMap {
[key: string]: string | null | Array<string | null>;
}
type QueryDataMap = Partial<Record<string, any>>;
}

interface URI {
absoluteTo(path: string | URI): URI;
addFragment(fragment: string): URI;
addQuery(qry: string | object): URI;
addQuery(qry: string | URI.QueryDataMap): URI;
addQuery(qry: string, value: any): URI;
addSearch(qry: string | object): URI;
addSearch(qry: string | URI.QueryDataMap): URI;
addSearch(key: string, value: any): URI;
authority(): string;
authority(authority: string): URI;
Expand Down Expand Up @@ -216,9 +214,9 @@ interface URI {

readable(): string;
relativeTo(path: string): URI;
removeQuery(qry: string | object): URI;
removeQuery(qry: string | URI.QueryDataMap): URI;
removeQuery(name: string, value: string): URI;
removeSearch(qry: string | object): URI;
removeSearch(qry: string | URI.QueryDataMap): URI;
removeSearch(name: string, value: string): URI;
resource(): string;
resource(resource: string): URI;
Expand All @@ -237,10 +235,10 @@ interface URI {
segmentCoded(segments: string[] | string): URI;
segmentCoded(position: number): string;
segmentCoded(position: number, level: string): URI;
setQuery(key: string, value: string): URI;
setQuery(qry: object): URI;
setSearch(key: string, value: string): URI;
setSearch(qry: object): URI;
setQuery(key: string, value: any): URI;
setQuery(qry: URI.QueryDataMap): URI;
setSearch(key: string, value: any): URI;
setSearch(qry: URI.QueryDataMap): URI;
hasQuery(
name: /*string | */ any,
value?: string | number | boolean | string[] | number[] | boolean[] | RegExp | ((...args: any[]) => any),
Expand Down
11 changes: 10 additions & 1 deletion types/urijs/test/urijs-dom.test.ts
Expand Up @@ -217,17 +217,26 @@ declare var $: (arg?: any) => JQuery;
Tests for URI.buildQuery()
From: https://medialize.github.io/URI.js/docs.html#static-buildQuery
*/
const buildQueryData = {
const buildQueryData: URI.QueryDataMap = {
foo: 'bar',
hello: ['world', 'mars', 'mars'],
bam: '',
yup: null,
removed: undefined,
removedList: [undefined, undefined, undefined]
};
test(URI.buildQuery(buildQueryData), 'foo=bar&hello=world&hello=mars&bam=&yup');
test(URI.buildQuery(buildQueryData, true), 'foo=bar&hello=world&hello=mars&hello=mars&bam=&yup');
test(URI.buildQuery({ space: 'hello space' }, false), 'space=hello+space');
test(URI.buildQuery({ space: 'hello space' }, false, false), 'space=hello%20space');
test(URI.buildQuery({ habitable: false }), 'habitable=false');
test(URI.buildQuery({ orbit: 687 }), 'orbit=687');
test(URI.buildQuery({ gas: [96, 1.9, 1.8, 0.146, 0.0] }), 'gas=96&gas=1.9&gas=1.8&gas=0.146&gas=0');
test(URI.buildQuery({ prediction: [true, false, true] }), 'prediction=true&prediction=false');
test(
URI.buildQuery({ silly: [Infinity, NaN, { a: 1 }, new RegExp('')] }),
'silly=Infinity&silly=NaN&silly=%5Bobject+Object%5D&silly=%2F%28%3F%3A%29%2F'
);

/*
Tests for URI.parseQuery()
Expand Down
11 changes: 10 additions & 1 deletion types/urijs/test/urijs-nodejs.test.ts
Expand Up @@ -222,17 +222,26 @@ declare var $: (arg?: any) => JQuery;
Tests for URI.buildQuery()
From: https://medialize.github.io/URI.js/docs.html#static-buildQuery
*/
const buildQueryData = {
const buildQueryData: URI.QueryDataMap = {
foo: 'bar',
hello: ['world', 'mars', 'mars'],
bam: '',
yup: null,
removed: undefined,
removedList: [undefined, undefined, undefined]
};
test(URI.buildQuery(buildQueryData), 'foo=bar&hello=world&hello=mars&bam=&yup');
test(URI.buildQuery(buildQueryData, true), 'foo=bar&hello=world&hello=mars&hello=mars&bam=&yup');
test(URI.buildQuery({ space: 'hello space' }, false), 'space=hello+space');
test(URI.buildQuery({ space: 'hello space' }, false, false), 'space=hello%20space');
test(URI.buildQuery({ habitable: false }), 'habitable=false');
test(URI.buildQuery({ orbit: 687 }), 'orbit=687');
test(URI.buildQuery({ gas: [96, 1.9, 1.8, 0.146, 0.0] }), 'gas=96&gas=1.9&gas=1.8&gas=0.146&gas=0');
test(URI.buildQuery({ prediction: [true, false, true] }), 'prediction=true&prediction=false');
test(
URI.buildQuery({ silly: [Infinity, NaN, { a: 1 }, new RegExp('')] }),
'silly=Infinity&silly=NaN&silly=%5Bobject+Object%5D&silly=%2F%28%3F%3A%29%2F'
);

/*
Tests for URI.parseQuery()
Expand Down

0 comments on commit b0f6a9d

Please sign in to comment.