-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[App Search] DRY helper for encoding/decoding routes that can have sp…
…ecial characters in params (#89811) * Add encodePathParam helper + update components that need it - Primarily document URLs & analytics queries (which uses generateEnginePath) * Add useDecodedParams helper + update components that need it - Documents titles & Analytics queries * [Misc] Change popout icon to eye - Feedback from Davey - the pages don't open in a new window, so shouldn't use the popout icon - Not strictly related but since we're touching these links anyway, I'm shoving it in (sorry) * Remove document detail decode test - now handled/tested by useDecodedParams helper * Add new generateEncodedPath helper - Should be used in place of generatePath * Update all instances of generatePath to generateEncodedPath for consistency across the App Search codebase * Fix failing tests due to extra encodeURI() done by generatePath * Add missing branch test for analytics query titles Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
4940a1c
commit 977fc6c
Showing
12 changed files
with
125 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...s/enterprise_search/public/applications/app_search/utils/encode_path_params/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import '../../../__mocks__/react_router_history.mock'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
import { encodePathParams, generateEncodedPath, useDecodedParams } from './'; | ||
|
||
describe('encodePathParams', () => { | ||
it('encodeURIComponent()s all object values', () => { | ||
const params = { | ||
someValue: 'hello world???', | ||
anotherValue: 'test!@#$%^&*[]/|;:"<>~`', | ||
}; | ||
expect(encodePathParams(params)).toEqual({ | ||
someValue: 'hello%20world%3F%3F%3F', | ||
anotherValue: 'test!%40%23%24%25%5E%26*%5B%5D%2F%7C%3B%3A%22%3C%3E~%60', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('generateEncodedPath', () => { | ||
it('generates a react router path with encoded path parameters', () => { | ||
expect( | ||
generateEncodedPath('/values/:someValue/:anotherValue/new', { | ||
someValue: 'hello world???', | ||
anotherValue: 'test!@#$%^&*[]/|;:"<>~`', | ||
}) | ||
).toEqual( | ||
'/values/hello%20world%3F%3F%3F/test!%40%23%24%25%5E%26*%5B%5D%2F%7C%3B%3A%22%3C%3E~%60/new' | ||
); | ||
}); | ||
}); | ||
|
||
describe('useDecodedParams', () => { | ||
it('decodeURIComponent()s all object values from useParams()', () => { | ||
(useParams as jest.Mock).mockReturnValue({ | ||
someValue: 'hello%20world%3F%3F%3F', | ||
anotherValue: 'test!%40%23%24%25%5E%26*%5B%5D%2F%7C%3B%3A%22%3C%3E~%60', | ||
}); | ||
expect(useDecodedParams()).toEqual({ | ||
someValue: 'hello world???', | ||
anotherValue: 'test!@#$%^&*[]/|;:"<>~`', | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
...lugins/enterprise_search/public/applications/app_search/utils/encode_path_params/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { generatePath, useParams } from 'react-router-dom'; | ||
|
||
type PathParams = Record<string, string>; | ||
|
||
export const encodePathParams = (pathParams: PathParams) => { | ||
const encodedParams: PathParams = {}; | ||
|
||
Object.entries(pathParams).map(([key, value]) => { | ||
encodedParams[key] = encodeURIComponent(value); | ||
}); | ||
|
||
return encodedParams; | ||
}; | ||
|
||
export const generateEncodedPath = (path: string, pathParams: PathParams) => { | ||
return generatePath(path, encodePathParams(pathParams)); | ||
}; | ||
|
||
export const useDecodedParams = () => { | ||
const decodedParams: PathParams = {}; | ||
|
||
const params = useParams(); | ||
Object.entries(params).map(([key, value]) => { | ||
decodedParams[key] = decodeURIComponent(value as string); | ||
}); | ||
|
||
return decodedParams; | ||
}; |