app-search-javascript
app-search-javascript copied to clipboard
Add option to specify fetch function per request
This adds the option to specify what function to use for fetch
requests.
Itt adds compatibility for using AppSearch in SvelteKit load functions
Backwards compatible, but allows to specify a custom fetch function per request.
const fetchFn = require('node-fetch')
const result = await client.search(query.get('q'), options, fetchFn)
Or uses the globally available fetch if not specified:
const result = await client.search(query.get('q'), options)
Example usage in SvelteKit load function:
// index.svelte
<script context="module">
import { createClient } from '@elastic/app-search-javascript'
const client = createClient({})
export const load = async ({ fetch }) => {
const result = await client.search('foo', {}, fetch)
return { props: { result } }
}
</script>
<script>
export let result
</script>
<pre>
{JSON.stringify(result, null, 2)}
</pre>