-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js,types): Construct urls based on Organization or User <O…
…rganizationSwitcher/> (#1503) + Introduces dynamicParamParser (+tests) + Add types for functions and string literals in OrganizationSwitcherProps & CreateOrganizationProps + afterSelectPersonalUrl & afterSelectOrganizationUrl
- Loading branch information
1 parent
58a3db6
commit 52ce791
Showing
10 changed files
with
274 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Construct urls based on context in <OrganizationSwitcher/> | ||
- Deprecate `afterSwitchOrganizationUrl` | ||
- Introduce `afterSelectOrganizationUrl` & `afterSelectPersonalUrl` | ||
|
||
`afterSelectOrganizationUrl` accepts | ||
- Full URL -> 'https://clerk.com/' | ||
- relative path -> '/organizations' | ||
- relative path -> with param '/organizations/:id' | ||
- function that returns a string -> (org) => `/org/${org.slug}` | ||
`afterSelectPersonalUrl` accepts | ||
- Full URL -> 'https://clerk.com/' | ||
- relative path -> '/users' | ||
- relative path -> with param '/users/:username' | ||
- function that returns a string -> (user) => `/users/${user.id}` |
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
29 changes: 29 additions & 0 deletions
29
packages/clerk-js/src/utils/__tests__/dynamicParamParser.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,29 @@ | ||
import { describe } from '@jest/globals'; | ||
|
||
import { createDynamicParamParser } from '../dynamicParamParser'; | ||
|
||
const entity = { | ||
foo: 'foo_string', | ||
bar: 'bar_string', | ||
}; | ||
|
||
describe('createDynamicParamParser', () => { | ||
const testCases = [ | ||
[':foo', entity, 'foo_string'], | ||
['/:foo', entity, '/foo_string'], | ||
['/some/:bar/any', entity, '/some/bar_string/any'], | ||
['/:notValid', entity, '/:notValid'], | ||
] as const; | ||
|
||
it.each(testCases)( | ||
'replaces the dynamic param with the value assigned to the key inside the object. Url=(%s), Object=(%s), result=(%s)', | ||
(urlWithParam, obj, result) => { | ||
expect( | ||
createDynamicParamParser({ regex: /:(\w+)/ })({ | ||
urlWithParam, | ||
entity: obj, | ||
}), | ||
).toEqual(result); | ||
}, | ||
); | ||
}); |
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,14 @@ | ||
export const createDynamicParamParser = | ||
({ regex }: { regex: RegExp }) => | ||
<T extends Record<any, any>>({ urlWithParam, entity }: { urlWithParam: string; entity: T }) => { | ||
const match = regex.exec(urlWithParam); | ||
|
||
if (match) { | ||
const key = match[1]; | ||
if (key in entity) { | ||
const value = entity[key] as string; | ||
return urlWithParam.replace(match[0], value); | ||
} | ||
} | ||
return urlWithParam; | ||
}; |
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
Oops, something went wrong.