-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
createLocalStorage
to enable custom storage namespace (#3022
) Co-authored-by: Rikki Schulte <[email protected]> Co-authored-by: Thomas Heyenbrock <[email protected]>
- Loading branch information
1 parent
4d6addb
commit ffb6486
Showing
5 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@graphiql/toolkit': minor | ||
--- | ||
|
||
Add a new utility function `createLocalStorage` that creates a local storage with support for custom namespaces |
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,47 @@ | ||
/** | ||
* This function enables a custom namespace for localStorage | ||
*/ | ||
|
||
import { Storage } from './base'; | ||
|
||
export type CreateLocalStorageOptions = { | ||
/** | ||
* specify a different storage namespace prefix from the default of 'graphiql' | ||
*/ | ||
namespace?: string; | ||
}; | ||
/** | ||
* generate a custom local storage adapter for GraphiQL `storage` prop. | ||
*/ | ||
export function createLocalStorage({ | ||
namespace, | ||
}: CreateLocalStorageOptions): Storage { | ||
const storageKeyPrefix = `${namespace}:`; | ||
const getStorageKey = (key: string) => `${storageKeyPrefix}${key}`; | ||
|
||
const storage: Storage = { | ||
setItem: (key, value) => localStorage.setItem(getStorageKey(key), value), | ||
getItem: key => localStorage.getItem(getStorageKey(key)), | ||
removeItem: key => localStorage.removeItem(getStorageKey(key)), | ||
get length() { | ||
let keys = 0; | ||
for (const key in window.localStorage) { | ||
if (key.indexOf(storageKeyPrefix) === 0) { | ||
keys += 1; | ||
} | ||
} | ||
return keys; | ||
}, | ||
|
||
clear() { | ||
// We only want to clear the namespaced items | ||
for (const key in window.localStorage) { | ||
if (key.indexOf(storageKeyPrefix) === 0) { | ||
window.localStorage.removeItem(key); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
return storage; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './base'; | ||
export * from './history'; | ||
export * from './query'; | ||
export * from './custom'; |
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