Skip to content

Commit

Permalink
prefer localStorage over window.localStorage (#3646)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina authored Jul 24, 2024
1 parent 32ea9f1 commit 56c6f45
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-geckos-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphiql/toolkit": patch
---

prefer `localStorage` over `window.localStorage`
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ module.exports = {
'no-octal-escape': 'error',
'no-param-reassign': 'error',
'no-proto': 'error',
'no-restricted-properties': 'off',
'no-restricted-properties': [
'error',
{
object: 'window',
property: 'localStorage',
message: 'Use `localStorage` instead',
},
],
'no-return-assign': 'error',
'no-return-await': 'error',
'no-script-url': 'error',
Expand Down
12 changes: 6 additions & 6 deletions packages/graphiql-toolkit/src/storage/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export class StorageAPI {
this.storage = null;
} else {
this.storage = {
getItem: window.localStorage.getItem.bind(window.localStorage),
setItem: window.localStorage.setItem.bind(window.localStorage),
removeItem: window.localStorage.removeItem.bind(window.localStorage),
getItem: localStorage.getItem.bind(localStorage),
setItem: localStorage.setItem.bind(localStorage),
removeItem: localStorage.removeItem.bind(localStorage),

get length() {
let keys = 0;
for (const key in window.localStorage) {
for (const key in localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
keys += 1;
}
Expand All @@ -80,9 +80,9 @@ export class StorageAPI {

clear() {
// We only want to clear the namespaced items
for (const key in window.localStorage) {
for (const key in localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
window.localStorage.removeItem(key);
localStorage.removeItem(key);
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions packages/graphiql-toolkit/src/storage/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createLocalStorage({
removeItem: key => localStorage.removeItem(getStorageKey(key)),
get length() {
let keys = 0;
for (const key in window.localStorage) {
for (const key in localStorage) {
if (key.indexOf(storageKeyPrefix) === 0) {
keys += 1;
}
Expand All @@ -35,9 +35,9 @@ export function createLocalStorage({

clear() {
// We only want to clear the namespaced items
for (const key in window.localStorage) {
for (const key in localStorage) {
if (key.indexOf(storageKeyPrefix) === 0) {
window.localStorage.removeItem(key);
localStorage.removeItem(key);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const simpleIntrospection = {
};

beforeEach(() => {
window.localStorage.clear();
localStorage.clear();
});

describe('GraphiQL', () => {
Expand Down

0 comments on commit 56c6f45

Please sign in to comment.