-
Notifications
You must be signed in to change notification settings - Fork 7
Add vue router guard #12
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
Open
michaelrodriguezGL
wants to merge
2
commits into
launchdarkly:main
Choose a base branch
from
michaelrodriguezGL:feature/vue-router-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,75 @@ | ||
import { launchDarklyGuard } from './guards' | ||
|
||
const existingFlagKey = 'random-flag-key' | ||
const existingFlagValue = 'random-flag-value' | ||
|
||
jest.mock('./utils', () => ({ | ||
watchEffectOnceAsync: () => Promise.resolve(), | ||
})) | ||
|
||
jest.mock('.', () => ({ | ||
launchDarklyClient: { | ||
value: { | ||
variation: (flagKey: string, defaultValue?: unknown) => { | ||
if (existingFlagKey === flagKey) { | ||
return existingFlagValue | ||
} | ||
|
||
return defaultValue | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
describe('guards', () => { | ||
const genericFlag = 'myFlag' | ||
const genericDefaultValue = 'default-value' | ||
const genericArgs: unknown = [] | ||
const genericCallback = () => genericDefaultValue | ||
|
||
const warnSpy = jest.spyOn(global.console, 'warn').mockImplementation(() => undefined) | ||
|
||
test('should return a function', () => { | ||
expect(launchDarklyGuard(existingFlagKey)).toMatchInlineSnapshot(`[Function]`) | ||
}) | ||
|
||
test('should return a promise after invocation', () => { | ||
expect(launchDarklyGuard(existingFlagKey)(genericArgs)).toMatchInlineSnapshot(`Promise {}`) | ||
}) | ||
|
||
test('should return undefined when the flag does not exist', async () => { | ||
const result = await launchDarklyGuard(genericFlag)(genericArgs) | ||
expect(result).toMatchInlineSnapshot(`undefined`) | ||
}) | ||
|
||
test('should return the default value', async () => { | ||
const result = await launchDarklyGuard(genericFlag, null, genericDefaultValue)(genericArgs) | ||
expect(result).toBe(genericDefaultValue) | ||
}) | ||
|
||
test('should return the callback value', async () => { | ||
const result = await launchDarklyGuard(genericFlag, genericCallback)(genericArgs) | ||
expect(result).toBe(genericDefaultValue) | ||
}) | ||
|
||
test('should return the callback value (true)', async () => { | ||
const callbackValue = true | ||
const result = await launchDarklyGuard(genericFlag, () => callbackValue)(genericArgs) | ||
expect(result).toBe(callbackValue) | ||
}) | ||
|
||
test('should return the callback value (false)', async () => { | ||
const callbackValue = false | ||
const result = await launchDarklyGuard(genericFlag, () => callbackValue)(genericArgs) | ||
expect(result).toBe(callbackValue) | ||
}) | ||
|
||
test('should return the existing value (simulates a Launch Darkly real value)', async () => { | ||
const result = await launchDarklyGuard(existingFlagKey)(genericArgs) | ||
expect(result).toBe(existingFlagValue) | ||
}) | ||
|
||
test('should have called console.warn)', async () => { | ||
expect(warnSpy).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains hidden or 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,38 @@ | ||
import { unref } from 'vue' | ||
import { launchDarklyClient, launchDarklyReady } from '.' | ||
import { watchEffectOnceAsync } from './utils' | ||
|
||
/** | ||
* Vue Router Guard. | ||
* | ||
* Provides a way to access flag values in the vue-router guards. | ||
* [per-route-guard](https://router.vuejs.org/guide/advanced/navigation-guards.html#per-route-guard) | ||
* Usage: `beforeEnter: launchDarklyGuard(flagKey, callback, defaultValue),` | ||
* | ||
* Custom logic can be apply using the callback parameter, when this parameter is passed it will be | ||
* in charge of the guard logic. The 'callback' function will hold the resulted flag and the injected | ||
* Vue Router params, check: [NavigationGuard](https://router.vuejs.org/api/interfaces/NavigationGuard.html) | ||
*/ | ||
export function launchDarklyGuard<T>(flagKey: string, callback?: ((arg0: boolean, args: unknown) => void) | null, defaultValue?: T): (args: unknown) => Promise<boolean> { | ||
return async (...args) => { | ||
const fn = () => { | ||
const flag = launchDarklyClient.value.variation(flagKey, defaultValue) | ||
|
||
if (typeof flag !== 'boolean' && typeof callback !== 'function') { | ||
console.warn(`LaunchDarkly guard warning: Be careful, flag key '${flagKey}' does not returns boolean value (${flag}), therefore it can lead to unexpected results.`) | ||
} | ||
|
||
if (typeof callback === 'function') { | ||
return callback(flag, ...args) | ||
} | ||
|
||
return flag | ||
} | ||
|
||
if (!unref(launchDarklyReady)) { | ||
await watchEffectOnceAsync(() => unref(launchDarklyReady)) | ||
} | ||
|
||
return fn() | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,36 @@ | ||
import { ref, unref, nextTick } from 'vue' | ||
import { watchEffectOnceAsync, watchEffectOnce } from './utils' | ||
|
||
describe('utils', () => { | ||
const genericReactiveValue = ref(false) | ||
let genericCallback = jest.fn() | ||
let genericWatcher = () => unref(genericReactiveValue) | ||
|
||
function reset() { | ||
genericCallback.mockReset() | ||
genericReactiveValue.value = false | ||
genericCallback = jest.fn() | ||
genericWatcher = () => unref(genericReactiveValue) | ||
} | ||
|
||
test('watchEffectOnce', async () => { | ||
reset() | ||
watchEffectOnce(genericWatcher, genericCallback) | ||
genericReactiveValue.value = true | ||
await nextTick() | ||
|
||
expect(genericCallback).toBeCalled() | ||
}) | ||
|
||
test('watchEffectOnceAsync', async () => { | ||
reset() | ||
const fulfilledStatus = 'fulfilled' | ||
const resultPromise = watchEffectOnceAsync(genericWatcher) | ||
genericReactiveValue.value = true | ||
await nextTick() | ||
const resultPromiseSettlement = await Promise.allSettled([resultPromise]) | ||
const resultPromiseStatus = (resultPromiseSettlement.pop() || {}).status | ||
|
||
expect(resultPromiseStatus).toBe(fulfilledStatus) | ||
}) | ||
}) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this breaks scoping/encapsulation. As this ref would be the same even with two apps both using the LDPlugin.
Where the provide/inject for the built-in ready and client theoretically would be scoped.
I think that route guards support inject as of vue 3.3. https://router.vuejs.org/guide/advanced/navigation-guards.html#Global-injections-within-guards
I need to look into what the implications of us upgrading would be.
I think, in that case, useLDClient and useLDReady could be used without duplicating these refs and without causing a scope problem.