Skip to content
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

Remove side-effect in StorageAPI that overrides localStorage.clear #2923

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-cups-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/toolkit': patch
---

Remove side-effect in StorageAPI that overrides localStorage.clear
6 changes: 5 additions & 1 deletion packages/graphiql-toolkit/src/storage/__tests__/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { StorageAPI } from '../base';

describe('StorageAPI', () => {
const storage = new StorageAPI();
let storage = new StorageAPI();

beforeEach(() => {
storage = new StorageAPI();
});
Comment on lines +4 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes are not required but I thought it could be nice to have a clean slate in every test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!


it('returns nothing if no value set', () => {
const result = storage.get('key1');
Expand Down
29 changes: 22 additions & 7 deletions packages/graphiql-toolkit/src/storage/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,29 @@ export class StorageAPI {
// Passing `null` creates a noop storage
this.storage = null;
} else if (typeof window !== 'undefined') {
this.storage = window.localStorage;
// We only want to clear the namespaced items
this.storage.clear = () => {
for (const key in window.localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
window.localStorage.removeItem(key);
this.storage = {
getItem: window.localStorage.getItem.bind(window.localStorage),
setItem: window.localStorage.setItem.bind(window.localStorage),
removeItem: window.localStorage.removeItem.bind(window.localStorage),

get length() {
let keys = 0;
for (const key in window.localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
keys += 1;
}
}
}
return keys;
},

clear: () => {
// We only want to clear the namespaced items
for (const key in window.localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
window.localStorage.removeItem(key);
}
}
},
};
} else {
this.storage = null;
Expand Down