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

[@types/urijs] Fix unnecessarily strict query params #54650

Merged
merged 2 commits into from Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = Record<string, any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely less safe than before. any can take literally anything, including objects, Maps, and a lot of things we shouldn't allow. What's wrong with taking your list (assuming you double checked it is correct): boolean | number | string | null | undefined | Array<boolean | number | string | null | undefined>.

Also to be correct we should make it Partial<Record<....>> because of course it is a partial. (Non-partial Record<string>s almost never make any sense.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryDataMaps can take literally anything including objects, Maps, and a lot of things

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-partial Records almost never make any sense.

These are parameters for a URL

Copy link
Contributor Author

@binury binury Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with taking your list?
<boolean | number | string | null | undefined | Array<boolean | number | string | null | undefined>

The issue that I see is that it would be incomplete, because urijs queries can support any object, even if it seems like it shouldn't. https://github.com/medialize/URI.js/blob/gh-pages/src/URI.js#L782-L811

 map = new Map()
 map.set('a', 'b')
 uri('github.com').addQuery({'map':map}).toString() // => 'github.com?map=%5Bobject+Map%5D'

}

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