@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
32 lines (31 loc) • 1.03 kB
JavaScript
import { createFuseInstance } from "../helpers/create-fuse-instance.js";
import { createSearchIndex } from "../helpers/create-search-index.js";
import { computed, ref, toValue } from "vue";
//#region src/features/Search/hooks/useSearchIndex.ts
var MAX_SEARCH_RESULTS = 25;
/**
* Creates the search index from an OpenAPI document.
*/
function useSearchIndex(document) {
const searchIndex = computed(() => createSearchIndex(toValue(document)));
/** When the document changes we replace the search index */
const fuse = computed(() => {
const instance = createFuseInstance();
instance.setCollection(searchIndex.value);
return instance;
});
const query = ref("");
return {
results: computed(() => {
if (query.value.length !== 0) return fuse.value.search(query.value, { limit: MAX_SEARCH_RESULTS });
return searchIndex.value.slice(0, MAX_SEARCH_RESULTS).map((item, index) => ({
item,
refIndex: index
}));
}),
query
};
}
//#endregion
export { useSearchIndex };
//# sourceMappingURL=useSearchIndex.js.map