|
1 | | -import { hasWindow } from '../has.js'; |
2 | 1 | import type { StorageProvider, UrlProviderOptions } from './types.js'; |
3 | 2 |
|
| 3 | +function getGlobalStorage(name: 'localStorage' | 'sessionStorage'): Storage | undefined { |
| 4 | + return typeof globalThis !== 'undefined' && name in globalThis |
| 5 | + ? globalThis[name] |
| 6 | + : undefined; |
| 7 | +} |
| 8 | + |
| 9 | +function getStorageKeys(storage: Storage | undefined): string[] { |
| 10 | + if (!storage) { |
| 11 | + return []; |
| 12 | + } |
| 13 | + |
| 14 | + return Array.from({ length: storage.length }, (_, index) => storage.key(index)).filter( |
| 15 | + (key): key is string => key !== null, |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +function createNoopProvider(): StorageProvider { |
| 20 | + return { |
| 21 | + get: () => null, |
| 22 | + set: () => {}, |
| 23 | + remove: () => {}, |
| 24 | + has: () => false, |
| 25 | + keys: () => [], |
| 26 | + clear: () => {}, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function getBrowserContext(): Pick<Window, 'location' | 'history'> | undefined { |
| 31 | + if (typeof globalThis === 'undefined' || !('window' in globalThis)) { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + |
| 35 | + const { window } = globalThis; |
| 36 | + |
| 37 | + if (!window || !window.location || !window.history) { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + |
| 41 | + return { |
| 42 | + location: window.location, |
| 43 | + history: window.history, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
4 | 47 | export const localStorageProvider: StorageProvider = { |
5 | 48 | syncEvent: 'storage', |
6 | | - storageArea: hasWindow() ? localStorage : undefined, |
| 49 | + get storageArea() { |
| 50 | + return getGlobalStorage('localStorage'); |
| 51 | + }, |
7 | 52 | get(key: string): string | null { |
8 | | - if (!hasWindow()) return null; |
9 | | - return localStorage.getItem(key); |
| 53 | + return getGlobalStorage('localStorage')?.getItem(key) ?? null; |
10 | 54 | }, |
11 | 55 | set(key: string, value: string): void { |
12 | | - if (!hasWindow()) return; |
13 | | - localStorage.setItem(key, value); |
| 56 | + getGlobalStorage('localStorage')?.setItem(key, value); |
14 | 57 | }, |
15 | 58 | remove(key: string): void { |
16 | | - if (!hasWindow()) return; |
17 | | - localStorage.removeItem(key); |
| 59 | + getGlobalStorage('localStorage')?.removeItem(key); |
18 | 60 | }, |
19 | 61 | has(key: string): boolean { |
20 | | - if (!hasWindow()) return false; |
21 | | - return localStorage.getItem(key) !== null; |
| 62 | + return getGlobalStorage('localStorage')?.getItem(key) !== null; |
22 | 63 | }, |
23 | 64 | keys(): string[] { |
24 | | - if (!hasWindow()) return []; |
25 | | - return Object.keys(localStorage); |
| 65 | + return getStorageKeys(getGlobalStorage('localStorage')); |
26 | 66 | }, |
27 | 67 | clear(): void { |
28 | | - if (!hasWindow()) return; |
29 | | - localStorage.clear(); |
| 68 | + getGlobalStorage('localStorage')?.clear(); |
30 | 69 | }, |
31 | 70 | }; |
32 | 71 |
|
33 | 72 | export const sessionStorageProvider: StorageProvider = { |
| 73 | + get storageArea() { |
| 74 | + return getGlobalStorage('sessionStorage'); |
| 75 | + }, |
34 | 76 | get(key: string): string | null { |
35 | | - if (!hasWindow()) return null; |
36 | | - return sessionStorage.getItem(key); |
| 77 | + return getGlobalStorage('sessionStorage')?.getItem(key) ?? null; |
37 | 78 | }, |
38 | 79 | set(key: string, value: string): void { |
39 | | - if (!hasWindow()) return; |
40 | | - sessionStorage.setItem(key, value); |
| 80 | + getGlobalStorage('sessionStorage')?.setItem(key, value); |
41 | 81 | }, |
42 | 82 | remove(key: string): void { |
43 | | - if (!hasWindow()) return; |
44 | | - sessionStorage.removeItem(key); |
| 83 | + getGlobalStorage('sessionStorage')?.removeItem(key); |
45 | 84 | }, |
46 | 85 | has(key: string): boolean { |
47 | | - if (!hasWindow()) return false; |
48 | | - return sessionStorage.getItem(key) !== null; |
| 86 | + return getGlobalStorage('sessionStorage')?.getItem(key) !== null; |
49 | 87 | }, |
50 | 88 | keys(): string[] { |
51 | | - if (!hasWindow()) return []; |
52 | | - return Object.keys(sessionStorage); |
| 89 | + return getStorageKeys(getGlobalStorage('sessionStorage')); |
53 | 90 | }, |
54 | 91 | clear(): void { |
55 | | - if (!hasWindow()) return; |
56 | | - sessionStorage.clear(); |
| 92 | + getGlobalStorage('sessionStorage')?.clear(); |
57 | 93 | }, |
58 | 94 | }; |
59 | 95 |
|
60 | 96 | export function createUrlSearchParamsProvider( |
61 | 97 | providerOptions: UrlProviderOptions = {}, |
62 | 98 | ): StorageProvider { |
| 99 | + const browserContext = getBrowserContext(); |
| 100 | + |
| 101 | + if (!browserContext) { |
| 102 | + return createNoopProvider(); |
| 103 | + } |
| 104 | + |
63 | 105 | const { push = false } = providerOptions; |
64 | 106 | const method = push ? 'pushState' : 'replaceState'; |
| 107 | + const { location, history } = browserContext; |
65 | 108 |
|
66 | 109 | return { |
67 | 110 | syncEvent: 'popstate', |
68 | 111 | get(key: string): string | null { |
69 | | - if (!hasWindow()) return null; |
70 | | - return new URLSearchParams(window.location.search).get(key); |
| 112 | + return new URLSearchParams(location.search).get(key); |
71 | 113 | }, |
72 | 114 | set(key: string, value: string): void { |
73 | | - if (!hasWindow()) return; |
74 | | - const params = new URLSearchParams(window.location.search); |
| 115 | + const params = new URLSearchParams(location.search); |
75 | 116 | params.set(key, value); |
76 | | - const newUrl = `${window.location.pathname}?${params.toString()}${window.location.hash}`; |
77 | | - window.history[method]({}, '', newUrl); |
| 117 | + const newUrl = `${location.pathname}?${params.toString()}${location.hash}`; |
| 118 | + history[method]({}, '', newUrl); |
78 | 119 | }, |
79 | 120 | remove(key: string): void { |
80 | | - if (!hasWindow()) return; |
81 | | - const params = new URLSearchParams(window.location.search); |
| 121 | + const params = new URLSearchParams(location.search); |
82 | 122 | params.delete(key); |
83 | 123 | const search = params.toString(); |
84 | | - const newUrl = `${window.location.pathname}${search ? `?${search}` : ''}${window.location.hash}`; |
85 | | - window.history[method]({}, '', newUrl); |
| 124 | + const newUrl = `${location.pathname}${search ? `?${search}` : ''}${location.hash}`; |
| 125 | + history[method]({}, '', newUrl); |
86 | 126 | }, |
87 | 127 | has(key: string): boolean { |
88 | | - if (!hasWindow()) return false; |
89 | | - return new URLSearchParams(window.location.search).has(key); |
| 128 | + return new URLSearchParams(location.search).has(key); |
90 | 129 | }, |
91 | 130 | keys(): string[] { |
92 | | - if (!hasWindow()) return []; |
93 | | - return [...new URLSearchParams(window.location.search).keys()]; |
| 131 | + return [...new URLSearchParams(location.search).keys()]; |
94 | 132 | }, |
95 | 133 | clear(): void { |
96 | | - if (!hasWindow()) return; |
97 | | - const newUrl = `${window.location.pathname}${window.location.hash}`; |
98 | | - window.history[method]({}, '', newUrl); |
| 134 | + history[method]({}, '', `${location.pathname}${location.hash}`); |
99 | 135 | }, |
100 | 136 | }; |
101 | 137 | } |
102 | 138 |
|
103 | 139 | export const urlSearchParamsProvider: StorageProvider = createUrlSearchParamsProvider(); |
104 | 140 |
|
105 | | -function getParamsFromHash(): URLSearchParams | null { |
106 | | - if (!hasWindow()) return null; |
107 | | - return new URLSearchParams(window.location.hash.slice(1)); |
108 | | -} |
109 | | - |
110 | 141 | export function createUrlSearchParamsInHashProvider( |
111 | 142 | providerOptions: UrlProviderOptions = {}, |
112 | 143 | ): StorageProvider { |
| 144 | + const browserContext = getBrowserContext(); |
| 145 | + |
| 146 | + if (!browserContext) { |
| 147 | + return createNoopProvider(); |
| 148 | + } |
| 149 | + |
113 | 150 | const { push = false } = providerOptions; |
| 151 | + const { location, history } = browserContext; |
| 152 | + |
| 153 | + function getParamsFromHash(): URLSearchParams { |
| 154 | + return new URLSearchParams(location.hash.slice(1)); |
| 155 | + } |
114 | 156 |
|
115 | 157 | return { |
116 | 158 | syncEvent: 'hashchange', |
117 | 159 | get(key: string): string | null { |
118 | | - return getParamsFromHash()?.get(key) ?? null; |
| 160 | + return getParamsFromHash().get(key); |
119 | 161 | }, |
120 | 162 | set(key: string, value: string): void { |
121 | 163 | const params = getParamsFromHash(); |
122 | | - if (!params) return; |
123 | 164 | params.set(key, value); |
124 | 165 | const newHash = params.toString(); |
| 166 | + |
125 | 167 | if (push) { |
126 | | - window.location.hash = newHash; |
| 168 | + location.hash = newHash; |
127 | 169 | } else { |
128 | | - window.history.replaceState( |
129 | | - {}, |
130 | | - '', |
131 | | - `${window.location.pathname}${window.location.search}#${newHash}`, |
132 | | - ); |
| 170 | + history.replaceState({}, '', `${location.pathname}${location.search}#${newHash}`); |
133 | 171 | } |
134 | 172 | }, |
135 | 173 | remove(key: string): void { |
136 | 174 | const params = getParamsFromHash(); |
137 | | - if (!params) return; |
138 | 175 | params.delete(key); |
139 | 176 | const newHash = params.toString(); |
| 177 | + |
140 | 178 | if (push) { |
141 | | - window.location.hash = newHash; |
| 179 | + location.hash = newHash; |
142 | 180 | } else { |
143 | 181 | const url = newHash |
144 | | - ? `${window.location.pathname}${window.location.search}#${newHash}` |
145 | | - : `${window.location.pathname}${window.location.search}`; |
146 | | - window.history.replaceState({}, '', url); |
| 182 | + ? `${location.pathname}${location.search}#${newHash}` |
| 183 | + : `${location.pathname}${location.search}`; |
| 184 | + history.replaceState({}, '', url); |
147 | 185 | } |
148 | 186 | }, |
149 | 187 | has(key: string): boolean { |
150 | | - return getParamsFromHash()?.has(key) ?? false; |
| 188 | + return getParamsFromHash().has(key); |
151 | 189 | }, |
152 | 190 | keys(): string[] { |
153 | | - const params = getParamsFromHash(); |
154 | | - return params ? [...params.keys()] : []; |
| 191 | + return [...getParamsFromHash().keys()]; |
155 | 192 | }, |
156 | 193 | clear(): void { |
157 | | - if (!hasWindow()) return; |
158 | 194 | if (push) { |
159 | | - window.location.hash = ''; |
| 195 | + location.hash = ''; |
160 | 196 | } else { |
161 | | - window.history.replaceState({}, '', `${window.location.pathname}${window.location.search}`); |
| 197 | + history.replaceState({}, '', `${location.pathname}${location.search}`); |
162 | 198 | } |
163 | 199 | }, |
164 | 200 | }; |
|
0 commit comments