UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

73 lines (72 loc) 3.14 kB
import ApiReference_default from "../../components/ApiReference.vue.js"; import { createApp, createSSRApp, h, reactive } from "vue"; import "@scalar/types/api-reference"; import { createHead } from "@unhead/vue/client"; //#region src/standalone/lib/html-api.ts /** * Create (and mount) a new Scalar API Reference * * @example createApiReference({ url: '/scalar.json' }).mount('#app') * @example createApiReference('#app', { url: '/scalar.json' }) * @example createApiReference(document.getElementById('app'), { url: '/scalar.json' }) */ var createApiReference = (elementOrSelectorOrConfig, optionalConfiguration) => { const idPrefix = "scalar-refs"; const props = reactive({ configuration: optionalConfiguration ?? elementOrSelectorOrConfig ?? {} }); const createReferenceApp = (isSsr = false) => { const referenceApp = isSsr ? createSSRApp(() => h(ApiReference_default, props)) : createApp(() => h(ApiReference_default, props)); referenceApp.use(createHead()); referenceApp.config.idPrefix = idPrefix; return referenceApp; }; const mountElement = optionalConfiguration ? typeof elementOrSelectorOrConfig === "string" ? document.querySelector(elementOrSelectorOrConfig) : elementOrSelectorOrConfig : null; let app = createReferenceApp(!!optionalConfiguration && !!mountElement && mountElement.children.length > 0); if (optionalConfiguration) if (mountElement) app.mount(mountElement); else console.error("Could not find a mount point for API References:", elementOrSelectorOrConfig); /** * Reload the API Reference * @deprecated */ document.addEventListener("scalar:reload-references", () => { console.warn("scalar:reload-references event has been deprecated, please use the scalarInstance.app.mount method instead."); if (!props.configuration) return; const currentElement = typeof elementOrSelectorOrConfig === "string" ? document.querySelector(elementOrSelectorOrConfig) : elementOrSelectorOrConfig; if (!currentElement) return; if (currentElement && !document.body.contains(currentElement)) document.body.appendChild(currentElement); app.unmount(); app = createReferenceApp(); app.mount(currentElement); }, false); /** Destroy the current API Reference instance */ const destroy = () => { props.configuration = {}; app.unmount(); }; /** * Allow user to destroy the API Reference * @deprecated */ document.addEventListener("scalar:destroy-references", () => { console.warn("scalar:destroy-references event has been deprecated, please use scalarInstance.destroy instead."); destroy(); }, false); /** * Allow user to update configuration * @deprecated */ document.addEventListener("scalar:update-references-config", (ev) => { console.warn("scalar:update-references-config event has been deprecated, please use scalarInstance.updateConfiguration instead."); if ("detail" in ev) Object.assign(props, ev.detail); }, false); return { app, getConfiguration: () => props.configuration ?? {}, updateConfiguration: (newConfig) => { props.configuration = newConfig; }, destroy }; }; //#endregion export { createApiReference }; //# sourceMappingURL=html-api.js.map