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
21 changes: 5 additions & 16 deletions algolia/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const algoliasearchSpy = vi.fn(() => ({ __mockClient: true }));

// algoliasearch v4 uses a default export; v5 uses a named export. The
// production module imports the default with a `type SearchClient`
// type-only named import, so mocking the default is sufficient.
vi.mock("algoliasearch", () => ({
default: (...args: unknown[]) =>
algoliasearch: (...args: unknown[]) =>
algoliasearchSpy(...(args as Parameters<typeof algoliasearchSpy>)),
}));

Expand Down Expand Up @@ -59,7 +56,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.objectContaining({ requester: expect.anything() }));
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP_X", "ADMIN");
expect(client).toEqual({ __mockClient: true });
});

Expand All @@ -77,7 +74,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.anything());
expect(algoliasearchSpy).toHaveBeenNthCalledWith(2, "APP_X", "ADMIN2");
});

it("throws when applicationId is missing", () => {
Expand All @@ -88,11 +85,7 @@ 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.objectContaining({ requester: expect.anything() }),
);
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP", "SEARCH_ONLY");
});

it("throws when both keys are empty", () => {
Expand All @@ -103,11 +96,7 @@ 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.objectContaining({ requester: expect.anything() }),
);
expect(algoliasearchSpy).toHaveBeenCalledExactlyOnceWith("APP", "ADMIN");
});
});

Expand Down
15 changes: 5 additions & 10 deletions algolia/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* Magento, and Algolia has consistent muscle memory.
*/

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

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

Expand Down Expand Up @@ -73,14 +72,10 @@ export function getAlgoliaClient(): SearchClient {
"as a worker env var, or populate searchApiKey on the block.",
);
}
// 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(),
});
// algoliasearch v5 uses the global `fetch` and `crypto` APIs by
// default — works on Cloudflare Workers, Bun, Deno, modern Node.
// v4 (with crypto / node:http imports) does not run on Workers.
cachedClient = algoliasearch(c.applicationId, key);
return cachedClient;
}

Expand Down
41 changes: 17 additions & 24 deletions bun.lock

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

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,13 @@
"access": "public"
},
"peerDependencies": {
"@algolia/requester-fetch": "^4",
"@decocms/start": ">=5.3.0",
"@tanstack/react-query": ">=5",
"algoliasearch": "^4 || ^5",
"algoliasearch": "^5",
"react": ">=18",
"react-dom": ">=18"
},
"peerDependenciesMeta": {
"@algolia/requester-fetch": {
"optional": true
},
"algoliasearch": {
"optional": true
}
Expand All @@ -144,9 +140,8 @@
"@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",
"algoliasearch": "^5.53.0",
"happy-dom": "^20.9.0",
"knip": "^5.86.0",
"react": "^19.0.0",
Expand Down
Loading