From b0f6a9d3f034732a21c00c434b83fc3c96a5bf55 Mon Sep 17 00:00:00 2001 From: Robin Ury <1146921+binury@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:30:00 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#54650=20[@types/ur?= =?UTF-8?q?ijs]=20Fix=20unnecessarily=20strict=20query=20params=20by=20@bi?= =?UTF-8?q?nury?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: unnecessarily strict urijs query params * Clean: QueryDataMap record as Partial Suggested per #54650 Co-authored-by: Bin Ury --- types/urijs/index.d.ts | 26 ++++++++++++-------------- types/urijs/test/urijs-dom.test.ts | 11 ++++++++++- types/urijs/test/urijs-nodejs.test.ts | 11 ++++++++++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/types/urijs/index.d.ts b/types/urijs/index.d.ts index fd70f152600e9a..f3195f429af88f 100644 --- a/types/urijs/index.d.ts +++ b/types/urijs/index.d.ts @@ -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; @@ -118,17 +118,15 @@ declare namespace URI { preventInvalidHostname: boolean; } - interface QueryDataMap { - [key: string]: string | null | Array; - } + type QueryDataMap = Partial>; } 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; @@ -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; @@ -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), diff --git a/types/urijs/test/urijs-dom.test.ts b/types/urijs/test/urijs-dom.test.ts index 73d40bd935f223..e21178c0909690 100644 --- a/types/urijs/test/urijs-dom.test.ts +++ b/types/urijs/test/urijs-dom.test.ts @@ -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() diff --git a/types/urijs/test/urijs-nodejs.test.ts b/types/urijs/test/urijs-nodejs.test.ts index d7b5b2322e072f..383ad5b4913614 100644 --- a/types/urijs/test/urijs-nodejs.test.ts +++ b/types/urijs/test/urijs-nodejs.test.ts @@ -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()