sanity-io / sanity-algolia

Utilities for indexing Sanity documents in Algolia
MIT License
67 stars 16 forks source link

indexer is not a function #28

Open antiantivirus opened 2 years ago

antiantivirus commented 2 years ago

getting the error "indexer is not a function" with the following code. Written in javascript and deployed on netlify serverless function

const sanityClient = require('@sanity/client')
const algoliasearch = require('algoliasearch')
const indexer = require('sanity-algolia')

const algolia = algoliasearch(...)
const sanity = sanityClient({...})

exports.handler = async (event, context) => {
if (event.headers['content-type'] !== 'application/json') {
return {
statusCode: 400,
body: "Bad request"
}
}

const algoliaIndex = algolia.initIndex('products')

const sanityAlgolia = indexer(
{
product: {
index: algoliaIndex,
projection: { _type, _rev, "objectID": _id, "slug":slug.current, _createdAt, title, "categories": categories[]->name, "image": mainImage.asset->url description, shortSummary, features, benefits, applications, "description": pt::text(description), }
},
},
)

sanityAlgolia
.webhookSync(sanity, event.body)
.then(() => {
return {
statusCode: 200,
body: "All is good"
}
}
)
}
Mehoff commented 1 year ago

My solution was adding .default. const indexer = require('sanity-algolia').default

kamerat commented 1 year ago

Might be related / fixed by this: https://github.com/sveltejs/kit/issues/6694

https://github.com/sanity-io/sanity-algolia/blob/b93dbde5795ef577c56828f90a7a3686070c570d/package.json#L27

https://publint.dev/sanity-algolia@1.1.0 states: pkg.module is used to output ESM, but pkg.exports is not defined. As NodeJS doesn't read pkg.module, the ESM output may be skipped. Consider adding pkg.exports to export the ESM output. pkg.module can usually be removed alongside too. (This will be a breaking change)

HAS_MODULE_BUT_NO_EXPORTS: If the package has a module field, but has no exports field, suggest to use exports instead. This is because Node.js doesn't recognize the module field, so instead using exports would increase compatibility with it.

If the package isn't meant for Node.js usage, it is safe to ignore this suggestion, but it is still recommended to use exports whenever possible.

kamerat commented 1 year ago

As suggested from https://publint.dev/sanity-algolia@1.1.0 I modified my local node_modules\sanity-algolia\package.json to the following, and that made it work for me.

{
  "version": "1.1.0",
  "license": "MIT",
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  "files": [
    "dist",
    "src"
  ],
  "engines": {
    "node": ">=10"
  },
  "scripts": {
    "start": "tsdx watch",
    "build": "tsdx build",
    "test": "tsdx test",
    "lint": "tsdx lint src test",
    "prepare": "tsdx build"
  },
  "husky": {
    "hooks": {
      "pre-commit": "tsdx lint src test"
    }
  },
  "name": "sanity-algolia",
  "author": "Rune Botten",
+ "type": "module",
  "module": "dist/sanity-algolia.esm.js",
+ "exports": {
+     "import": "./dist/sanity-algolia.esm.js",
+     "require": "./dist/sanity-algolia.cjs.js"
+ },
  "devDependencies": {
    "@types/jest": "^26.0.14",
    "@types/stopword": "^0.3.0",
    "husky": "^4.3.0",
    "prettier": "^2.1.2",
    "tsdx": "^0.14.0",
    "tslib": "^2.0.2",
    "typescript": "^4.0.3"
  },
  "dependencies": {
    "algoliasearch": "^4.5.1",
    "stopword": "^1.0.3"
  }
}

I don't know if this would break something else, but could you take a look at maybe implementing this @runeb? :)

kamerat commented 11 months ago

Any updates on this?