Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions algolia/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("getAlgoliaClient", () => {
it("constructs the SDK with applicationId + adminApiKey", () => {
mod.configureAlgolia({ applicationId: "APP_X", searchApiKey: "S", adminApiKey: "ADMIN" });
const client = mod.getAlgoliaClient();
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP_X", "ADMIN");
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP_X", "ADMIN", expect.objectContaining({ requester: expect.anything() }));
expect(client).toEqual({ __mockClient: true });
});

Expand All @@ -77,7 +77,7 @@ describe("getAlgoliaClient", () => {
mod.configureAlgolia({ applicationId: "APP_X", searchApiKey: "S", adminApiKey: "ADMIN2" });
mod.getAlgoliaClient();
expect(algoliasearchSpy).toHaveBeenCalledTimes(2);
expect(algoliasearchSpy).toHaveBeenNthCalledWith(2, "APP_X", "ADMIN2");
expect(algoliasearchSpy).toHaveBeenNthCalledWith(2, "APP_X", "ADMIN2", expect.anything());
});

it("throws when applicationId is missing", () => {
Expand All @@ -88,7 +88,11 @@ describe("getAlgoliaClient", () => {
it("falls back to searchApiKey when adminApiKey is empty", () => {
mod.configureAlgolia({ applicationId: "APP", searchApiKey: "SEARCH_ONLY", adminApiKey: "" });
mod.getAlgoliaClient();
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP", "SEARCH_ONLY");
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith(
"APP",
"SEARCH_ONLY",
expect.objectContaining({ requester: expect.anything() }),
);
});

it("throws when both keys are empty", () => {
Expand All @@ -99,7 +103,11 @@ describe("getAlgoliaClient", () => {
it("prefers adminApiKey over searchApiKey when both present", () => {
mod.configureAlgolia({ applicationId: "APP", searchApiKey: "S", adminApiKey: "ADMIN" });
mod.getAlgoliaClient();
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP", "ADMIN");
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith(
"APP",
"ADMIN",
expect.objectContaining({ requester: expect.anything() }),
);
});
});

Expand Down
10 changes: 9 additions & 1 deletion algolia/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import algoliasearch, { type SearchClient } from "algoliasearch";
import { createFetchRequester } from "@algolia/requester-fetch";

import type { AlgoliaConfig } from "./types";

Expand Down Expand Up @@ -72,7 +73,14 @@ export function getAlgoliaClient(): SearchClient {
"as a worker env var, or populate searchApiKey on the block.",
);
}
cachedClient = algoliasearch(c.applicationId, key);
// algoliasearch v4's default Node http requester relies on
// `node:http` which isn't available in Cloudflare Workers, so the
// first request hangs / crashes. `@algolia/requester-fetch` uses
// the global `fetch`, which is what every Deco target runtime
// provides — Workers, Bun, modern Node.
cachedClient = algoliasearch(c.applicationId, key, {
requester: createFetchRequester(),
});
return cachedClient;
}

Expand Down
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@
"access": "public"
},
"peerDependencies": {
"@algolia/requester-fetch": "^4",
"@decocms/start": ">=5.3.0",
"@tanstack/react-query": ">=5",
"algoliasearch": "^4 || ^5",
"react": ">=18",
"react-dom": ">=18"
},
"peerDependenciesMeta": {
"@algolia/requester-fetch": {
"optional": true
},
"algoliasearch": {
"optional": true
}
Expand All @@ -140,6 +144,7 @@
"@semantic-release/exec": "^7.1.0",
"@tanstack/react-query": "^5.90.21",
"@types/react": "^19.0.0",
"@algolia/requester-fetch": "^4.27.0",
"@vitest/coverage-v8": "^4.1.0",
"algoliasearch": "^4.27.0",
"happy-dom": "^20.9.0",
Expand Down
Loading