UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

67 lines (66 loc) 2.3 kB
import { REFERENCE_LS_KEYS, safeLocalStorage } from "@scalar/helpers/object/local-storage"; import { coerceValue } from "@scalar/workspace-store/schemas/typebox-coerce"; import { XScalarSelectedSecuritySchema, SecuritySchemesSchema } from "@scalar/workspace-store/schemas/v3.1/strict/openapi-document"; const storage = safeLocalStorage(); const clientStorage = () => { const key = REFERENCE_LS_KEYS.SELECTED_CLIENT; return { /** * Gets the stored selected client from local storage. */ get: () => { return storage.getItem(key); }, /** * Stores the selected client value in local storage. * @param value The value to store */ set: (value) => { storage.setItem(key, value); } }; }; const authStorage = () => { const schemasKey = REFERENCE_LS_KEYS.AUTH_SCHEMES; const selectedSchemesKey = REFERENCE_LS_KEYS.SELECTED_AUTH_SCHEMES; const getAuthId = (type, prefix) => { const getKey = (type2) => { return type2 === "schemas" ? schemasKey : selectedSchemesKey; }; return `${prefix}-${getKey(type)}`; }; return { /** * Retrieves and coerces the authentication schemes stored in local storage. */ getSchemas: (slug) => { const parsed = JSON.parse(storage.getItem(getAuthId("schemas", slug)) ?? "{}"); return coerceValue(SecuritySchemesSchema, parsed); }, /** * Stores the authentication schemes in local storage. * @param value The SecuritySchemes object to stringify and store. */ setSchemas: (slug, value) => { storage.setItem(getAuthId("schemas", slug), JSON.stringify(value)); }, /** * Retrieves and coerces the selected authentication schemes stored in local storage. */ getSelectedSchemes: (slug) => { const parsed = JSON.parse(storage.getItem(getAuthId("selectedSchemes", slug)) ?? "{}"); return coerceValue(XScalarSelectedSecuritySchema, parsed); }, /** * Stores the user's selected authentication schemes in local storage. * @param value The XScalarSelectedSecurity object to stringify and store. */ setSelectedSchemes: (slug, value) => { storage.setItem(getAuthId("selectedSchemes", slug), JSON.stringify(value)); } }; }; export { authStorage, clientStorage };