algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.

Home Page:https://www.algolia.com/doc/api-client/javascript/getting-started/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using this client in Deno results in XMLHttpRequest is not defined

alex-galey opened this issue · comments

Trying to use algoliasearch client from a supabase edge function (using Deno) but getting this error :

ReferenceError: XMLHttpRequest is not defined
    at file:///src/main.ts:1630:39
    at new Promise (<anonymous>)
    at Object.send (file:///src/main.ts:1629:32)
    at p9 (file:///src/main.ts:462:36)
    at file:///src/main.ts:475:24
    at async Server.<anonymous> (file:///src/main.ts:5778:32)
    at async Server.#respond (file:///src/main.ts:1994:24)

Do you have any suggestion to make this working in a back-end environment ?
Isn't XMLHttpRequest an oldish browser way to make requests ?

I was able to polyfill XMLHttpRequest with https://github.com/kitsonk/xhr and make it work.
I don't close this thread in case refactoring requests out of XMLHttpRequest is a subject

You can use the fetch requester:

import algoliasearch from 'algoliasearch';
import { createFetchRequester } from '@algolia/requester-fetch';

const client = algoliasearch('', '', { requester: createFetchRequester() })

We haven't changed the default, as the out-of-the-box package is already compatible with both node and browsers through conditional imports, deno and worker-style setups are harder to detect

Great, it's working with this requester :)
Thank you for pointing this out and for super fast answer !

I have made a small pull request to the documentation (here) to mention fetch too, if there's another place you would have searched for a solution for this problem, I'm happy to add it there too.

This issue will automatically close when that PR is merged, but feel free to comment further