-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: Add support for localStorage for the browser platform. #566
Merged
Merged
Changes from all commits
Commits
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
139 changes: 139 additions & 0 deletions
139
packages/sdk/browser/__tests__/platform/LocalStorage.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,139 @@ | ||
import LocalStorage from '../../src/platform/LocalStorage'; | ||
|
||
it('can set values', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'setItem'); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.set('test-key', 'test-value'); | ||
expect(spy).toHaveBeenCalledWith('test-key', 'test-value'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('can handle an error setting a value', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'setItem'); | ||
spy.mockImplementation(() => { | ||
throw new Error('bad'); | ||
}); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.set('test-key', 'test-value'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
'Error setting key in localStorage: test-key, reason: Error: bad', | ||
); | ||
}); | ||
|
||
it('can get values', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'getItem'); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.get('test-key'); | ||
expect(spy).toHaveBeenCalledWith('test-key'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('can handle an error getting a value', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'getItem'); | ||
spy.mockImplementation(() => { | ||
throw new Error('bad'); | ||
}); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.get('test-key'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
'Error getting key from localStorage: test-key, reason: Error: bad', | ||
); | ||
}); | ||
|
||
it('can clear values', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'removeItem'); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.clear('test-key'); | ||
expect(spy).toHaveBeenCalledWith('test-key'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('can handle an error clearing a value', async () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
// Storage here needs to be the global browser 'Storage' not the interface | ||
// for our platform. | ||
const spy = jest.spyOn(Storage.prototype, 'removeItem'); | ||
spy.mockImplementation(() => { | ||
throw new Error('bad'); | ||
}); | ||
|
||
const storage = new LocalStorage(logger); | ||
storage.clear('test-key'); | ||
|
||
expect(logger.debug).not.toHaveBeenCalled(); | ||
expect(logger.info).not.toHaveBeenCalled(); | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
'Error clearing key from localStorage: test-key, reason: Error: bad', | ||
); | ||
}); |
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,12 @@ | ||
|
||
export default { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jest-environment-jsdom', | ||
transform: { | ||
"^.+\\.tsx?$": "ts-jest" | ||
// process `*.tsx` files with `ts-jest` | ||
}, | ||
moduleNameMapper: { | ||
'\\.(gif|ttf|eot|svg|png)$': '<rootDir>/test/__ mocks __/fileMock.js', | ||
}, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
LDOptions, | ||
Storage, | ||
/* platform */ | ||
} from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import LocalStorage, { isLocalStorageSupported } from './LocalStorage'; | ||
|
||
export default class BrowserPlatform /* implements platform.Platform */ { | ||
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. Once we implement enough to support the platform interface, then we can uncomment some things. |
||
// encoding?: Encoding; | ||
// info: Info; | ||
// fileSystem?: Filesystem; | ||
// crypto: Crypto; | ||
// requests: Requests; | ||
storage?: Storage; | ||
|
||
constructor(options: LDOptions) { | ||
if (isLocalStorageSupported()) { | ||
this.storage = new LocalStorage(options.logger); | ||
} | ||
} | ||
} |
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,42 @@ | ||
import type { LDLogger, Storage } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
export function isLocalStorageSupported() { | ||
// Checking a symbol using typeof is safe, but directly accessing a symbol | ||
// which is not defined would be an error. | ||
return typeof localStorage !== 'undefined'; | ||
} | ||
|
||
/** | ||
* Implementation of Storage using localStorage for the browser. | ||
* | ||
* The Storage API is async, and localStorage is synchronous. This is fine, | ||
* and none of the methods need to internally await their operations. | ||
*/ | ||
export default class PlatformStorage implements Storage { | ||
constructor(private readonly logger?: LDLogger) {} | ||
async clear(key: string): Promise<void> { | ||
try { | ||
localStorage.removeItem(key); | ||
} catch (error) { | ||
this.logger?.error(`Error clearing key from localStorage: ${key}, reason: ${error}`); | ||
} | ||
} | ||
|
||
async get(key: string): Promise<string | null> { | ||
try { | ||
const value = localStorage.getItem(key); | ||
return value ?? null; | ||
} catch (error) { | ||
this.logger?.error(`Error getting key from localStorage: ${key}, reason: ${error}`); | ||
return null; | ||
} | ||
} | ||
|
||
async set(key: string, value: string): Promise<void> { | ||
try { | ||
localStorage.setItem(key, value); | ||
} catch (error) { | ||
this.logger?.error(`Error setting key in localStorage: ${key}, reason: ${error}`); | ||
} | ||
} | ||
} |
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.
Added some missing config and dependencies to allow imports in jest.