Skip to content

feat(fetch-requester): add @algolia/requester-fetch - #1411

Merged
Haroenv merged 2 commits into
algolia:masterfrom
ykzts:feat/fetch-requester
Jul 6, 2022
Merged

feat(fetch-requester): add @algolia/requester-fetch#1411
Haroenv merged 2 commits into
algolia:masterfrom
ykzts:feat/fetch-requester

Conversation

@ykzts

Copy link
Copy Markdown

Edge Computing such as Cloudflare Worker and Edge Functions (Vercel) do not support XHR or Node.js HTTP modules. add a requester that uses Fetch and Edge Computing. Algolia's JS client can be used with Edge Computing by adding a requester that uses Fetch.

@codesandbox-ci

codesandbox-ciBot commented Jun 24, 2022

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit cbebf59:

SandboxSource
javascript-client-appConfiguration

@Haroenv
Haroenv requested review from Haroenv and shortcutsJune 24, 2022 14:59
@Haroenv

Copy link
Copy Markdown
Contributor

First of all, thanks for this thorough pull request. It looks good on first glance, but I'll make sure to test it completely next week.

It makes sense for the future (even across major) to keep xhr as the default (as you've done here), as there's some other environments that don't have fetch (ie, react native)

@HaroenvHaroenv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great and works as expected (sandbox that uses it: https://codesandbox.io/s/javascript-client-app-forked-iwbbje?file=/src/app.js). Some small nitpicks are all I have to add

Comment threadpackages/requester-fetch/src/createFetchRequester.ts Outdated
Haroenv
Haroenv previously approved these changes Jul 6, 2022
Comment threadpackages/requester-fetch/src/createFetchRequester.ts
shortcuts
shortcuts previously approved these changes Jul 6, 2022

@shortcutsshortcuts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to add, thanks a lot for the contribution!

Comment threadpackage.json Outdated
"@wdio/static-server-service": "5.16.10",
"barrelsby": "2.2.0",
"bundlesize": "0.18.0",
"cross-fetch": "^3.1.5",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"cross-fetch": "^3.1.5",
"cross-fetch": "3.1.5",

We can let renovate do the things

Comment threadpackages/requester-fetch/src/createFetchRequester.ts
Comment threadyarn.lock Outdated
Comment threadpackage.json Outdated
@Haroenv
Haroenv dismissed stale reviews from shortcuts and themself via cbebf59July 6, 2022 14:02
@Haroenv
Haroenv merged commit 7b62403 into algolia:masterJul 6, 2022
@ykzts
ykzts deleted the feat/fetch-requester branch July 6, 2022 15:01
@my2ter

Copy link
Copy Markdown

Thanks for this!

Not sure if I'm missing something but unfortunately CloudFlare does not implement all parameters of the fetch api making it fails when I tried using it. I get a RetryError with the following:

The 'mode' field on 'RequestInitializerDict' is not implemented

If I comment out https://github.com/algolia/algoliasearch-client-javascript/blob/4.14.3/packages/requester-fetch/src/createFetchRequester.ts#L47 then it works.

Npm deps:

  • "@algolia/requester-fetch": "^4.14.3",
  • "algoliasearch": "^4.14.3"

Run locally: npx wrangler dev src/index.js
Code index.js:

import algoliasearch from 'algoliasearch';
import { createFetchRequester } from '@algolia/requester-fetch';
export default {
async fetch(request) {
return handleRequest(request)
},
};
async function handleRequest(request) {
const client = algoliasearch('APP_ID', 'SEARCH_KEY', {
requester: createFetchRequester(),
});
const index = client.initIndex('index');
let results = await index.search("a-search");
return new Response(JSON.stringify(results), {
headers: { 'content-type': 'application/json' },
});
}

@shortcuts

shortcuts commented Jan 19, 2023

Copy link
Copy Markdown
Member

Hey @my2ter! we've change the order of the parameters in v5 (https://github.com/algolia/algoliasearch-client-javascript/blob/next/packages/requester-fetch/src/createFetchRequester.ts#L39-L49), which allows the user to override the value for the cors parameter, but it has not been reflected on v4.

If you wish to open the PR with the changes, please feel free! Otherwise I'll try to do it tomorrow

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ykzts@Haroenv@my2ter@shortcuts