vault/ui/app/lib/local-storage.ts
Jordan Reimer bbcd0e0465
[UI] API Service (#29965)
* adds api service

* adds missing copyright headers

* Update ui/app/services/api.ts

Co-authored-by: claire bontempo <68122737+hellobontempo@users.noreply.github.com>

* removes response cache and comments from api service

* removes hide warnings condition from showWarnings middleware in api service

* splits out setHeaders test

---------

Co-authored-by: claire bontempo <68122737+hellobontempo@users.noreply.github.com>
2025-03-20 16:28:02 -06:00

51 lines
1.2 KiB
TypeScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import type { ApiError } from 'vault/api';
export default {
isLocalStorageSupported() {
try {
const key = `__storage__test`;
window.localStorage.setItem(key, '');
window.localStorage.removeItem(key);
return true;
} catch (e) {
const error = e as ApiError;
// modify the e object so we can customize the error message.
// e.message is readOnly.
error.errors = [`This is likely due to your browser's cookie settings.`];
throw e;
}
},
getItem(key: string) {
const item = window.localStorage.getItem(key);
return item && JSON.parse(item);
},
setItem(key: string, val: unknown) {
window.localStorage.setItem(key, JSON.stringify(val));
},
removeItem(key: string) {
return window.localStorage.removeItem(key);
},
keys() {
return Object.keys(window.localStorage);
},
cleanupStorage(string: string, keyToKeep: string) {
if (!string) return;
const relevantKeys = this.keys().filter((str) => str.startsWith(string));
relevantKeys?.forEach((key) => {
if (key !== keyToKeep) {
localStorage.removeItem(key);
}
});
},
};