Skip to content

Commit

Permalink
bump query-string version to remove manual type definitions (#78143)
Browse files Browse the repository at this point in the history
* bump query-string version to remove manual type definitions

* remove manual type declaration

* fix cypress tests

* add )
  • Loading branch information
mshustov authored Sep 24, 2020
1 parent 3f2e9f7 commit 8ba60a4
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 221 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"p-map": "^4.0.0",
"pegjs": "0.10.0",
"proxy-from-env": "1.0.0",
"query-string": "5.1.1",
"query-string": "^6.13.2",
"re2": "^1.15.4",
"react": "^16.12.0",
"react-color": "^2.13.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-std/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export { mapToObject } from './map_to_object';
export { merge } from './merge';
export { pick } from './pick';
export { withTimeout } from './promise';
export { isRelativeUrl, modifyUrl, URLMeaningfulParts, ParsedQuery } from './url';
export { isRelativeUrl, modifyUrl, URLMeaningfulParts } from './url';
export { unset } from './unset';
export { getFlattenedObject } from './get_flattened_object';
6 changes: 1 addition & 5 deletions packages/kbn-std/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
*/

import { format as formatUrl, parse as parseUrl, UrlObject } from 'url';

// duplicate type from 'query-string' to avoid adding the d.ts file to all packages depending on kbn-std
export interface ParsedQuery<T = string> {
[key: string]: T | T[] | null | undefined;
}
import type { ParsedQuery } from 'query-string';

/**
* We define our own typings because the current version of @types/node
Expand Down
28 changes: 0 additions & 28 deletions src/core/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,6 @@
* under the License.
*/

declare module 'query-string' {
type ArrayFormat = 'bracket' | 'index' | 'none';

export interface ParseOptions {
arrayFormat?: ArrayFormat;
sort: ((itemLeft: string, itemRight: string) => number) | false;
}

export interface ParsedQuery<T = string> {
[key: string]: T | T[] | null | undefined;
}

export function parse(str: string, options?: ParseOptions): ParsedQuery;

export function parseUrl(str: string, options?: ParseOptions): { url: string; query: any };

export interface StringifyOptions {
strict?: boolean;
encode?: boolean;
arrayFormat?: ArrayFormat;
sort: ((itemLeft: string, itemRight: string) => number) | false;
}

export function stringify(obj: object, options?: StringifyOptions): string;

export function extract(str: string): string;
}

type DeeplyMockedKeys<T> = {
[P in keyof T]: T[P] extends (...args: any[]) => any
? jest.MockInstance<ReturnType<T[P]>, Parameters<T[P]>>
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/dashboard/public/url_utils/url_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ describe('', () => {
const id = '123eb456cd';
const url =
"/pep/app/dashboards#/create?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!())";
expect(addEmbeddableToDashboardUrl(url, id, 'visualization')).toEqual(
`/pep/app/dashboards#/create?_a=%28description%3A%27%27%2Cfilters%3A%21%28%29%29&_g=%28refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2Ctime%3A%28from%3Anow-15m%2Cto%3Anow%29%29&addEmbeddableId=${id}&addEmbeddableType=visualization`

expect(addEmbeddableToDashboardUrl(url, id, 'visualization')).toBe(
'/pep/app/dashboards?addEmbeddableId=123eb456cd&addEmbeddableType=visualization#%2Fcreate%3F_g%3D%28refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2Ctime%3A%28from%3Anow-15m%2Cto%3Anow%29%29%26_a%3D%28description%3A%27%27%2Cfilters%3A%21%28%29%29'
);
});
it('addEmbeddableToDashboardUrl when dashboard is saved', () => {
const id = '123eb456cd';
const url =
"/pep/app/dashboards#/view/9b780cd0-3dd3-11e8-b2b9-5d5dc1715159?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(description:'',filters:!())";
expect(addEmbeddableToDashboardUrl(url, id, 'visualization')).toEqual(
`/pep/app/dashboards#/view/9b780cd0-3dd3-11e8-b2b9-5d5dc1715159?_a=%28description%3A%27%27%2Cfilters%3A%21%28%29%29&_g=%28refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2Ctime%3A%28from%3Anow-15m%2Cto%3Anow%29%29&addEmbeddableId=${id}&addEmbeddableType=visualization`
expect(addEmbeddableToDashboardUrl(url, id, 'visualization')).toBe(
'/pep/app/dashboards?addEmbeddableId=123eb456cd&addEmbeddableType=visualization#%2Fview%2F9b780cd0-3dd3-11e8-b2b9-5d5dc1715159%3F_g%3D%28refreshInterval%3A%28pause%3A%21t%2Cvalue%3A0%29%2Ctime%3A%28from%3Anow-15m%2Cto%3Anow%29%29%26_a%3D%28description%3A%27%27%2Cfilters%3A%21%28%29%29'
);
});
});
8 changes: 5 additions & 3 deletions src/plugins/dashboard/public/url_utils/url_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { parseUrl, stringify } from 'query-string';
import { parseUrl, stringifyUrl } from 'query-string';
import { DashboardConstants } from '../index';

/** *
Expand All @@ -34,12 +34,14 @@ export function addEmbeddableToDashboardUrl(
embeddableId: string,
embeddableType: string
) {
const { url, query } = parseUrl(dashboardUrl);
const { url, query, fragmentIdentifier } = parseUrl(dashboardUrl, {
parseFragmentIdentifier: true,
});

if (embeddableId) {
query[DashboardConstants.ADD_EMBEDDABLE_TYPE] = embeddableType;
query[DashboardConstants.ADD_EMBEDDABLE_ID] = embeddableId;
}

return `${url}?${stringify(query)}`;
return stringifyUrl({ url, query, fragmentIdentifier });
}
46 changes: 0 additions & 46 deletions test/typings/query_string.d.ts

This file was deleted.

46 changes: 0 additions & 46 deletions typings/query_string.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
"proper-lockfile": "^3.2.0",
"puid": "1.0.7",
"puppeteer-core": "^1.19.0",
"query-string": "5.1.1",
"query-string": "^6.13.2",
"raw-loader": "3.1.0",
"react": "^16.12.0",
"react-datetime": "^2.14.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('RedirectToLogs component', () => {

expect(component).toMatchInlineSnapshot(`
<Redirect
to="/stream?logPosition=(end:'2019-02-20T14:58:09.404Z',position:(tiebreaker:0,time:1550671089404),start:'2019-02-20T12:58:09.404Z',streamLive:!f)&sourceId=default&logFilter=(expression:'',kind:kuery)"
to="/stream?sourceId=default&logPosition=(end:'2019-02-20T14:58:09.404Z',position:(tiebreaker:0,time:1550671089404),start:'2019-02-20T12:58:09.404Z',streamLive:!f)&logFilter=(expression:'',kind:kuery)"
/>
`);
});
Expand All @@ -33,7 +33,7 @@ describe('RedirectToLogs component', () => {

expect(component).toMatchInlineSnapshot(`
<Redirect
to="/stream?logPosition=(end:'2019-02-20T14:58:09.404Z',position:(tiebreaker:0,time:1550671089404),start:'2019-02-20T12:58:09.404Z',streamLive:!f)&sourceId=default&logFilter=(expression:'FILTER_FIELD:FILTER_VALUE',kind:kuery)"
to="/stream?sourceId=default&logPosition=(end:'2019-02-20T14:58:09.404Z',position:(tiebreaker:0,time:1550671089404),start:'2019-02-20T12:58:09.404Z',streamLive:!f)&logFilter=(expression:'FILTER_FIELD:FILTER_VALUE',kind:kuery)"
/>
`);
});
Expand Down
18 changes: 8 additions & 10 deletions x-pack/plugins/infra/public/utils/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,14 @@ export const replaceStateKeyInQueryString = <UrlState extends any>(
urlState: UrlState | undefined
) => (queryString: string) => {
const previousQueryValues = parse(queryString, { sort: false });
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
url.encodeQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
}),
{ sort: false, encode: false }
);
const newValue =
typeof urlState === 'undefined'
? previousQueryValues
: {
...previousQueryValues,
[stateKey]: encodeRisonUrlState(urlState),
};
return stringify(url.encodeQuery(newValue), { sort: false, encode: false });
};

const replaceQueryStringInLocation = (location: Location, queryString: string): Location => {
Expand Down
19 changes: 9 additions & 10 deletions x-pack/plugins/infra/public/utils/use_url_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,15 @@ export const replaceStateKeyInQueryString = <UrlState extends any>(
urlState: UrlState | undefined
) => (queryString: string) => {
const previousQueryValues = parse(queryString, { sort: false });
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
url.encodeQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
}),
{ sort: false, encode: false }
);
const newValue =
typeof urlState === 'undefined'
? previousQueryValues
: {
...previousQueryValues,
[stateKey]: encodeRisonUrlState(urlState),
};

return stringify(url.encodeQuery(newValue), { sort: false, encode: false });
};

const replaceQueryStringInLocation = (location: Location, queryString: string): Location => {
Expand Down
Loading

0 comments on commit 8ba60a4

Please sign in to comment.