app-search-javascript icon indicating copy to clipboard operation
app-search-javascript copied to clipboard

Add option to specify fetch function per request

Open half2me opened this issue 3 years ago • 0 comments

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>

half2me avatar Jul 13 '21 12:07 half2me