algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.
https://www.algolia.com/doc/api-client/javascript/getting-started/
MIT License
1.33k stars 222 forks source link

Module types missing from manifest #1540

Closed Zach-Jaensch closed 3 months ago

Zach-Jaensch commented 3 months ago

We have just upgraded from v4 to v5 (specifically v5.1.0) and ran into some issues when using the methods, primarily with the types. We noticed some issues like: Unsafe assignment of an error typed value.eslint[@typescript-eslint/no-unsafe-assignment](https://typescript-eslint.io/rules/no-unsafe-assignment) image

Could not find a declaration file for module '@algolia/client-search' image

After some investigation, I when adding the types to the exports field in the manifest, the issues would be mostly solved The client search package for example.

  "exports": {
    ".": {
      "node": {
        "import": "./dist/client-search.esm.node.js",
        "module": "./dist/client-search.esm.node.js",
        "require": "./dist/client-search.cjs",
-       "default": "./dist/client-search.cjs"
+       "default": "./dist/client-search.cjs",
+       "types": "./dist/builds/node.d.ts"
      },
      "default": {
        "umd": "./dist/client-search.umd.js",
        "module": "./dist/client-search.esm.browser.js",
        "import": "./dist/client-search.esm.browser.js",
-       "default": "./dist/client-search.cjs"
+       "default": "./dist/client-search.cjs",
+       "types": "./dist/builds/browser.d.ts"
      }
    },
    "./src/*": "./src/*.ts",
    "./model": "./model/index.ts"
  },

It looks like all the packages need to be updated in a similar fashion which will allow the, otherwise when using the clients, we have to define the client list so (Note we patched the two packages to allow us to upgrade now)

import { algoliasearch } from "algoliasearch";

// These should not be needed 
import type { SearchClient } from "@algolia/client-search";
import type { RecommendClient } from "@algolia/recommend";

// this return type should be infered
export function getAlgoliaClient(): SearchClient {
  return algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY);
}

// as above
export function getAlgoliaRecommendationClient(): RecommendClient {
  return algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY).initRecommend();
}