-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[App Search] DRY helper for encoding/decoding routes that can have special characters in params #89811
[App Search] DRY helper for encoding/decoding routes that can have special characters in params #89811
Changes from all commits
8a644db
5a2d9a6
c0ad74e
12e4db9
68a3057
a72aef2
9dd3fdf
b4b4b6a
a64d53a
8f3568f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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!@#$%^&*[]/|;:"<>~`', | ||
}); | ||
}); | ||
}); |
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; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just want to note - on some reflection, it's maybe a little overkill to have a helper for 2 instances in our app, but I think it'll be worth it (from the perspective of future-proofing) that we have one documented and canonical place where we ensure our URLs are correctly encoded, vs. doing it manually and maybe slightly differently in multiple places in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change has nothing to do with encoding/decoding, but Davey requested it recently (since the popout icon typically indicates a new tab/window, and that's not the case here) and I figure I'd shove it in this PR since it touches the 2 concerned views 😬 If you super hate that lmk and I can stop being lazy & pull it out to a separate PR haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine.........😒