-
Notifications
You must be signed in to change notification settings - Fork 4
UserManager abstraction added #158
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
Reversean
wants to merge
2
commits into
refactor/hawk-storage
Choose a base branch
from
refactor/user-manager
base: refactor/hawk-storage
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
Some comments aren't visible on the classic Files Changed page.
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
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
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export type { HawkStorage } from './storages/hawk-storage'; | ||
| export { HawkUserManager } from './users/hawk-user-manager'; |
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,71 @@ | ||
| import type { AffectedUser } from '@hawk.so/types'; | ||
| import type { HawkStorage } from '../storages/hawk-storage'; | ||
|
|
||
| /** | ||
| * Storage key used to persist the auto-generated user ID. | ||
| */ | ||
| export const HAWK_USER_ID_KEY = 'hawk-user-id'; | ||
|
|
||
| /** | ||
| * Manages the affected user identity. | ||
| * | ||
| * Manually provided users are kept in memory only (they don't change restarts). | ||
| * {@link HawkStorage} is used solely to persist the auto-generated ID | ||
| * so it survives across sessions. | ||
| */ | ||
| export class HawkUserManager { | ||
| /** | ||
| * In-memory user set explicitly via {@link setUser}. | ||
| */ | ||
| private user: AffectedUser | null = null; | ||
|
|
||
| /** | ||
| * Underlying storage used to persist auto-generated user ID. | ||
| */ | ||
| private readonly storage: HawkStorage; | ||
|
|
||
| /** | ||
| * @param storage - Storage backend to use for persistence. | ||
| */ | ||
| constructor(storage: HawkStorage) { | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current affected user, or `null` if none is available. | ||
| * | ||
| * Priority: in-memory user > persisted user ID. | ||
| */ | ||
| public getUser(): AffectedUser | null { | ||
| if (this.user) { | ||
| return this.user; | ||
| } | ||
| const storedId = this.storage.getItem(HAWK_USER_ID_KEY); | ||
| return storedId ? { id: storedId } : null; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the user explicitly (in memory only). | ||
| * | ||
| * @param user - The affected user provided by the application. | ||
| */ | ||
| public setUser(user: AffectedUser): void { | ||
| this.user = user; | ||
| } | ||
|
|
||
| /** | ||
| * Persists an auto-generated user ID to storage. | ||
| * | ||
| * @param id - The generated ID to persist. | ||
| */ | ||
| public persistGeneratedId(id: string): void { | ||
| this.storage.setItem(HAWK_USER_ID_KEY, id); | ||
| } | ||
|
|
||
| /** | ||
| * Clears the explicitly set user, falling back to the persisted user ID. | ||
| */ | ||
| public clear(): void { | ||
| this.user = null; | ||
| } | ||
| } | ||
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,72 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { HawkUserManager } from '../../src'; | ||
| import type { HawkStorage } from '../../src'; | ||
|
|
||
| describe('HawkUserManager', () => { | ||
| let storage: HawkStorage; | ||
| let manager: HawkUserManager; | ||
|
|
||
| beforeEach(() => { | ||
| storage = { | ||
| getItem: vi.fn().mockReturnValue(null), | ||
| setItem: vi.fn(), | ||
| removeItem: vi.fn(), | ||
| }; | ||
| manager = new HawkUserManager(storage); | ||
| }); | ||
|
|
||
| it('should return null when no user is set and storage is empty', () => { | ||
| expect(manager.getUser()).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return in-memory user set via setUser()', () => { | ||
| const user = { id: 'user-1', name: 'Ryan Gosling', url: 'https://example.com', photo: 'https://example.com/photo.png' }; | ||
|
|
||
| manager.setUser(user); | ||
|
|
||
| expect(manager.getUser()).toEqual(user); | ||
| expect(storage.setItem).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not touch storage when setUser() is called', () => { | ||
| manager.setUser({ id: 'user-1' }); | ||
|
|
||
| expect(storage.setItem).not.toHaveBeenCalled(); | ||
| expect(storage.removeItem).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return anonymous user from storage when no in-memory user is set', () => { | ||
| vi.mocked(storage.getItem).mockReturnValue('anon-123'); | ||
|
|
||
| expect(manager.getUser()).toEqual({ id: 'anon-123' }); | ||
| expect(storage.getItem).toHaveBeenCalledWith('hawk-user-id'); | ||
| }); | ||
|
|
||
| it('should prefer in-memory user over persisted anonymous ID', () => { | ||
| vi.mocked(storage.getItem).mockReturnValue('anon-123'); | ||
| manager.setUser({ id: 'explicit-user' }); | ||
|
|
||
| expect(manager.getUser()).toEqual({ id: 'explicit-user' }); | ||
| }); | ||
|
|
||
| it('should persist anonymous ID via persistGeneratedId()', () => { | ||
| manager.persistGeneratedId('anon-456'); | ||
|
|
||
| expect(storage.setItem).toHaveBeenCalledWith('hawk-user-id', 'anon-456'); | ||
| }); | ||
|
|
||
| it('should clear in-memory user and fall back to persisted anonymous ID', () => { | ||
| vi.mocked(storage.getItem).mockReturnValue('anon-123'); | ||
| manager.setUser({ id: 'user-1' }); | ||
| manager.clear(); | ||
|
|
||
| expect(manager.getUser()).toEqual({ id: 'anon-123' }); | ||
| }); | ||
|
|
||
| it('should return null after clear() when no anonymous ID is persisted', () => { | ||
| manager.setUser({ id: 'user-1' }); | ||
| manager.clear(); | ||
|
|
||
| expect(manager.getUser()).toBeNull(); | ||
| }); | ||
| }); |
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,13 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": null, | ||
| "declaration": false, | ||
| "types": ["vitest/globals"] | ||
| }, | ||
| "include": [ | ||
| "src/**/*", | ||
| "tests/**/*", | ||
| "vitest.config.ts" | ||
| ] | ||
| } |
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,15 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| globals: true, | ||
| include: ['tests/**/*.test.ts'], | ||
| typecheck: { | ||
| tsconfig: './tsconfig.test.json', | ||
| }, | ||
| coverage: { | ||
| provider: 'v8', | ||
| include: ['src/**/*.ts'], | ||
| }, | ||
| }, | ||
| }); |
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 |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| import StackParser from './modules/stackParser'; | ||
| import type { CatcherMessage, HawkInitialSettings, BreadcrumbsAPI, Transport } from './types'; | ||
| import { VueIntegration } from './integrations/vue'; | ||
| import { id } from './utils/id'; | ||
| import type { | ||
| AffectedUser, | ||
| EventContext, | ||
|
|
@@ -19,6 +18,9 @@ | |
| import { ConsoleCatcher } from './addons/consoleCatcher'; | ||
| import { BreadcrumbManager } from './addons/breadcrumbs'; | ||
| import { validateUser, validateContext, isValidEventPayload } from './utils/validation'; | ||
| import { HawkUserManager } from '@hawk.so/core'; | ||
| import { HawkLocalStorage } from './storages/hawk-local-storage'; | ||
| import { id } from './utils/id'; | ||
|
|
||
| /** | ||
| * Allow to use global VERSION, that will be overwritten by Webpack | ||
|
|
@@ -62,11 +64,6 @@ | |
| */ | ||
| private readonly release: string | undefined; | ||
|
|
||
| /** | ||
| * Current authenticated user | ||
| */ | ||
| private user: AffectedUser; | ||
|
|
||
| /** | ||
| * Any additional data passed by user for sending with all messages | ||
| */ | ||
|
|
@@ -111,6 +108,11 @@ | |
| */ | ||
| private readonly breadcrumbManager: BreadcrumbManager | null; | ||
|
|
||
| /** | ||
| * Current authenticated user manager instance | ||
| */ | ||
| private readonly userManager: HawkUserManager = new HawkUserManager(new HawkLocalStorage()); | ||
|
|
||
| /** | ||
| * Catcher constructor | ||
| * | ||
|
|
@@ -126,7 +128,9 @@ | |
| this.token = settings.token; | ||
| this.debug = settings.debug || false; | ||
| this.release = settings.release !== undefined ? String(settings.release) : undefined; | ||
| this.setUser(settings.user || Catcher.getGeneratedUser()); | ||
| if (settings.user) { | ||
| this.setUser(settings.user); | ||
| } | ||
| this.setContext(settings.context || undefined); | ||
| this.beforeSend = settings.beforeSend; | ||
| this.disableVueErrorHandler = | ||
|
|
@@ -189,27 +193,6 @@ | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates user if no one provided via HawkCatcher settings | ||
| * After generating, stores user for feature requests | ||
| */ | ||
| private static getGeneratedUser(): AffectedUser { | ||
| let userId: string; | ||
| const LOCAL_STORAGE_KEY = 'hawk-user-id'; | ||
| const storedId = localStorage.getItem(LOCAL_STORAGE_KEY); | ||
|
|
||
| if (storedId) { | ||
| userId = storedId; | ||
| } else { | ||
| userId = id(); | ||
|
Member
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. where we create the default id now?
Member
Author
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. In |
||
| localStorage.setItem(LOCAL_STORAGE_KEY, userId); | ||
| } | ||
|
|
||
| return { | ||
| id: userId, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Send test event from client | ||
| */ | ||
|
|
@@ -272,14 +255,14 @@ | |
| return; | ||
| } | ||
|
|
||
| this.user = user; | ||
| this.userManager.setUser(user); | ||
| } | ||
|
|
||
| /** | ||
| * Clear current user information (revert to generated user) | ||
| * Clear current user information | ||
| */ | ||
| public clearUser(): void { | ||
| this.user = Catcher.getGeneratedUser(); | ||
| this.userManager.clear(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -565,10 +548,16 @@ | |
| } | ||
|
|
||
| /** | ||
| * Current authenticated user | ||
| * Returns the current user if set, otherwise generates and persists an anonymous ID. | ||
| */ | ||
| private getUser(): HawkJavaScriptEvent['user'] { | ||
| return this.user || null; | ||
| private getUser(): AffectedUser { | ||
| const user = this.userManager.getUser(); | ||
| if (user) { | ||
| return user; | ||
| } | ||
| const generatedId = id(); | ||
| this.userManager.persistGeneratedId(generatedId); | ||
| return { id: generatedId }; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
The @hawk.so/core package imports AffectedUser from '@hawk.so/types' but this dependency is not listed in the package.json. This will cause build and runtime errors. Add '@hawk.so/types': 'npm:0.5.8' to the dependencies or peerDependencies section of packages/core/package.json.
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.
Fixed.