createStorage.tsx 824 B

12345678910111213141516171819202122232425262728293031
  1. // our storage wrapper is a subset of the full API
  2. type Storage = Pick<globalThis.Storage, 'getItem' | 'setItem' | 'removeItem'>;
  3. export default function createStorage(getStorage: () => globalThis.Storage): Storage {
  4. try {
  5. const storage = getStorage();
  6. const mod = 'sentry';
  7. storage.setItem(mod, mod);
  8. storage.removeItem(mod);
  9. return {
  10. setItem: storage.setItem.bind(storage),
  11. getItem: storage.getItem.bind(storage),
  12. removeItem: storage.removeItem.bind(storage),
  13. } as Storage;
  14. } catch (e) {
  15. return {
  16. setItem() {
  17. return;
  18. },
  19. // Returns null if key doesn't exist:
  20. // https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem
  21. getItem() {
  22. return null;
  23. },
  24. removeItem() {
  25. return null;
  26. },
  27. };
  28. }
  29. }