| | |
| | | const apiKey = "{{$.Site.Data.config.operandApiKey}}" |
| | | import { |
| | | operandClient, |
| | | indexIDHeaderKey, |
| | | } from "https://unpkg.com/@operandinc/sdk@4.1.3/dist/esm/index.js" |
| | | |
| | | const apiKey = "{{$.Site.Data.config.search.operandApiKey}}" |
| | | const indexId = "{{$.Site.Data.config.search.operandIndexId}}" |
| | | const operand = operandClient( |
| | | ObjectService, |
| | | apiKey, |
| | | "https://api.operand.ai", |
| | | { |
| | | [indexIDHeaderKey]: indexId, |
| | | } |
| | | ); |
| | | |
| | | async function searchContents(query) { |
| | | const response = await fetch('https://prod.operand.ai/v3/search/objects', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | Authorization: apiKey, |
| | | }, |
| | | body: JSON.stringify({ |
| | | query, |
| | | max: 10 |
| | | }), |
| | | }); |
| | | return (await response.json()); |
| | | const results = await operand.searchWithin({ |
| | | query, |
| | | limit: 10, |
| | | }) |
| | | console.log(results.matches) |
| | | return results.matches.flat() |
| | | } |
| | | |
| | | registerHandlers((e) => { |
| | | function debounce(func, timeout = 200) { |
| | | let timer; |
| | | return (...args) => { |
| | | clearTimeout(timer) |
| | | timer = setTimeout(() => { func.apply(this, args); }, timeout) |
| | | }; |
| | | } |
| | | |
| | | registerHandlers(debounce((e) => { |
| | | term = e.target.value |
| | | if (term !== "") { |
| | | searchContents(term) |
| | |
| | | )) |
| | | .then(results => displayResults(results)) |
| | | } |
| | | }) |
| | | })) |