Skip to content

Commit ffb6486

Browse files
heyacherryacaothomasheyenbrock
authored
feat: add createLocalStorage to enable custom storage namespace (#3022)
Co-authored-by: Rikki Schulte <[email protected]> Co-authored-by: Thomas Heyenbrock <[email protected]>
1 parent 4d6addb commit ffb6486

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

.changeset/lovely-glasses-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphiql/toolkit': minor
3+
---
4+
5+
Add a new utility function `createLocalStorage` that creates a local storage with support for custom namespaces

examples/monaco-graphql-react-vite/vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import react from '@vitejs/plugin-react';
33
import monacoEditorPlugin from 'vite-plugin-monaco-editor';
44

55
export default defineConfig({
6+
build: {
7+
minify: false,
8+
},
69
plugins: [
710
react(),
811
monacoEditorPlugin({
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* This function enables a custom namespace for localStorage
3+
*/
4+
5+
import { Storage } from './base';
6+
7+
export type CreateLocalStorageOptions = {
8+
/**
9+
* specify a different storage namespace prefix from the default of 'graphiql'
10+
*/
11+
namespace?: string;
12+
};
13+
/**
14+
* generate a custom local storage adapter for GraphiQL `storage` prop.
15+
*/
16+
export function createLocalStorage({
17+
namespace,
18+
}: CreateLocalStorageOptions): Storage {
19+
const storageKeyPrefix = `${namespace}:`;
20+
const getStorageKey = (key: string) => `${storageKeyPrefix}${key}`;
21+
22+
const storage: Storage = {
23+
setItem: (key, value) => localStorage.setItem(getStorageKey(key), value),
24+
getItem: key => localStorage.getItem(getStorageKey(key)),
25+
removeItem: key => localStorage.removeItem(getStorageKey(key)),
26+
get length() {
27+
let keys = 0;
28+
for (const key in window.localStorage) {
29+
if (key.indexOf(storageKeyPrefix) === 0) {
30+
keys += 1;
31+
}
32+
}
33+
return keys;
34+
},
35+
36+
clear() {
37+
// We only want to clear the namespaced items
38+
for (const key in window.localStorage) {
39+
if (key.indexOf(storageKeyPrefix) === 0) {
40+
window.localStorage.removeItem(key);
41+
}
42+
}
43+
},
44+
};
45+
46+
return storage;
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './base';
22
export * from './history';
33
export * from './query';
4+
export * from './custom';

packages/monaco-graphql/test/monaco-editor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('monaco-editor', () => {
1313
expect(lines[0]).toBe('$ vite build');
1414
expect(lines[1]).toMatch(' building for production...');
1515
expect(lines[2]).toBe('transforming...');
16-
expect(lines[3]).toMatch('✓ 1092 modules transformed.');
16+
expect(lines[3]).toMatch('✓ 1093 modules transformed.');
1717
expect(lines[4]).toBe('rendering chunks...');
1818
expect(lines[5]).toBe('computing gzip size...');
1919
expect(lines[6]).toMatch('dist/index.html');

0 commit comments

Comments
 (0)