UNPKG

@scalar/oas-utils

Version:

Open API spec and Yaml handling utilities

35 lines (34 loc) 1.1 kB
import { parse } from 'flatted'; import { DATA_VERSION_LS_LEY } from '../migrations/data-version.js'; /** * Supports pre-flatted local storage */ export const parseLocalStorage = (lsKey) => { const item = localStorage.getItem(lsKey) || '[{}]'; const data = item[0] === '[' ? parse(item) : JSON.parse(item); return data; }; /** Take a best guess of the localStorage version */ export const getLocalStorageVersion = () => { const collectionStr = localStorage.getItem('collection'); const dataVersion = localStorage.getItem(DATA_VERSION_LS_LEY); if (dataVersion) { return dataVersion; } // No flatted means first version if (!collectionStr?.length || collectionStr?.[0] === '{') { return '0.0.0'; } // Flatted + types means > 2.1.0 but we should have a data version try { const [collection] = Object.values(parse(collectionStr) ?? {}); if (collection?.type === 'collection') { return '2.1.0'; } return '0.0.0'; } catch (e) { console.error(e); return '0.0.0'; } };