@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
74 lines (73 loc) • 3.29 kB
JavaScript
import { isClient } from "@scalar/api-client/v2/blocks/operation-code-sample";
import { isObject } from "@scalar/helpers/object/is-object";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { clientStorage, authStorage } from "./storage.js";
const loadClientFromStorage = (store) => {
const storedClient = clientStorage().get();
if (isClient(storedClient) && !store.workspace["x-scalar-default-client"]) {
store.update("x-scalar-default-client", storedClient);
}
};
const mergeSecuritySchemas = (current, stored, level = 0) => {
if (!isObject(current) || !isObject(stored)) {
return;
}
for (const [key, storedValue] of Object.entries(stored)) {
if (level === 0 && key === "type") {
continue;
}
if (typeof storedValue === "string" && storedValue !== "" || typeof storedValue === "number" || typeof storedValue === "boolean") {
current[key] = storedValue;
continue;
}
mergeSecuritySchemas(getResolvedRef(current[key]), storedValue, level + 1);
}
};
const restoreAuthSecretsFromStorage = (store) => {
const slug = store.workspace["x-scalar-active-document"];
const activeDocument = store.workspace.activeDocument;
if (!activeDocument || !slug) {
console.warn("Active document not found in workspace, skipping auth secrets loading");
return;
}
const securitySchemes = activeDocument.components?.securitySchemes ?? {};
const storedAuthSchemes = authStorage().getSchemas(slug);
for (const [key, storedScheme] of Object.entries(storedAuthSchemes)) {
const currentScheme = getResolvedRef(securitySchemes[key]);
if (isObject(currentScheme)) {
mergeSecuritySchemas(currentScheme, storedScheme);
}
}
};
const isSchemeValid = (scheme, availableSchemes) => Object.keys(scheme).every((key) => availableSchemes.has(key));
const clampSelectedIndex = (selectedIndex, schemesLength) => selectedIndex >= schemesLength ? schemesLength - 1 : selectedIndex;
const loadAuthSchemesFromStorage = (store) => {
const slug = store.workspace["x-scalar-active-document"];
const activeDocument = store.workspace.activeDocument;
if (!activeDocument || !slug) {
console.warn("Active document not found in workspace, skipping auth schemes loading");
return;
}
const authPersistence = authStorage();
const storedSelectedAuthSchemes = authPersistence.getSelectedSchemes(slug);
restoreAuthSecretsFromStorage(store);
if (activeDocument["x-scalar-selected-security"] !== void 0) {
return;
}
const availableSchemes = new Set(Object.keys(activeDocument.components?.securitySchemes ?? {}));
const selectedSchemes = storedSelectedAuthSchemes["x-scalar-selected-security"]?.selectedSchemes;
const validSchemes = selectedSchemes?.filter((scheme) => isSchemeValid(scheme, availableSchemes));
if (validSchemes && validSchemes.length > 0) {
const selectedIndex = storedSelectedAuthSchemes["x-scalar-selected-security"]?.selectedIndex ?? 0;
const clampedIndex = clampSelectedIndex(selectedIndex, validSchemes.length);
activeDocument["x-scalar-selected-security"] = {
selectedIndex: clampedIndex,
selectedSchemes: validSchemes
};
}
};
export {
loadAuthSchemesFromStorage,
loadClientFromStorage,
mergeSecuritySchemas
};