feat: Algolia plugin for medusa (#718)
Co-authored-by: Rolwin Monterio <rolwin@yellow.ai> Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
12 files changed
+5835
-1
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"@babel/plugin-proposal-class-properties",
|
||||||
|
"@babel/plugin-transform-instanceof",
|
||||||
|
"@babel/plugin-transform-classes"
|
||||||
|
],
|
||||||
|
"presets": ["@babel/preset-env"],
|
||||||
|
"env": {
|
||||||
|
"test": {
|
||||||
|
"plugins": ["@babel/plugin-transform-runtime"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
/dist
|
||||||
|
.env
|
||||||
|
.DS_Store
|
||||||
|
/uploads
|
||||||
|
/node_modules
|
||||||
|
yarn-error.log
|
||||||
|
|
||||||
|
/dist
|
||||||
|
|
||||||
|
/api
|
||||||
|
/services
|
||||||
|
/models
|
||||||
|
/subscribers
|
||||||
|
/loaders
|
||||||
|
/utils
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/lib
|
||||||
|
node_modules
|
||||||
|
.DS_store
|
||||||
|
.env*
|
||||||
|
/*.js
|
||||||
|
!index.js
|
||||||
|
yarn.lock
|
||||||
|
src
|
||||||
|
.gitignore
|
||||||
|
.eslintrc
|
||||||
|
.babelrc
|
||||||
|
.prettierrc
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# medusa-plugin-algolia
|
||||||
|
|
||||||
|
algolia Plugin for Medusa to search for products.
|
||||||
|
|
||||||
|
## Plugin Options
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
application_id: "someId",
|
||||||
|
admin_api_key: "someApiKey",
|
||||||
|
settings: {
|
||||||
|
[indexName]: [algolia settings passed to algolia's `updateSettings()` method]
|
||||||
|
// example
|
||||||
|
products: {
|
||||||
|
searchableAttributes: ["title", "description", "variant_sku", "type_value"],
|
||||||
|
attributesToRetrieve: ["title", "description", "variant_sku", "type_value"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
//noop
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
testEnvironment: "node",
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"name": "medusa-plugin-algolia",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "Search support for algolia",
|
||||||
|
"main": "index.js",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/medusajs/medusa",
|
||||||
|
"directory": "packages/medusa-plugin-algolia"
|
||||||
|
},
|
||||||
|
"author": "rolwin100",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "babel src -d .",
|
||||||
|
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||||
|
"watch": "babel -w src --out-dir . --ignore **/__tests__",
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"medusa-interfaces": "1.x",
|
||||||
|
"typeorm": "0.x"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"algoliasearch": "^4.10.5",
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"medusa-core-utils": "^1.1.20",
|
||||||
|
"medusa-interfaces": "1.x"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.7.5",
|
||||||
|
"@babel/core": "^7.7.5",
|
||||||
|
"@babel/node": "^7.7.4",
|
||||||
|
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||||
|
"@babel/plugin-transform-instanceof": "^7.8.3",
|
||||||
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
|
"@babel/preset-env": "^7.7.5",
|
||||||
|
"@babel/register": "^7.7.4",
|
||||||
|
"@babel/runtime": "^7.9.6",
|
||||||
|
"client-sessions": "^0.8.0",
|
||||||
|
"cross-env": "^5.2.1",
|
||||||
|
"eslint": "^6.8.0",
|
||||||
|
"jest": "^25.5.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
export default async (container, options) => {
|
||||||
|
try {
|
||||||
|
const algoliaService = container.resolve("algoliaService")
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
Object.entries(options.settings).map(([key, value]) =>
|
||||||
|
algoliaService.updateSettings(key, value)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
import algoliasearch from "algoliasearch"
|
||||||
|
import { indexTypes } from "medusa-core-utils"
|
||||||
|
import { SearchService } from "medusa-interfaces"
|
||||||
|
import { transformProduct } from "../utils/transform-product"
|
||||||
|
|
||||||
|
class AlgoliaService extends SearchService {
|
||||||
|
constructor(container, options) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
this.options_ = options
|
||||||
|
const { application_id, admin_api_key } = this.options_
|
||||||
|
|
||||||
|
if (!application_id) {
|
||||||
|
throw new Error("Please provide a valid Application ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!admin_api_key) {
|
||||||
|
throw new Error("Please provide a valid Admin Api Key")
|
||||||
|
}
|
||||||
|
|
||||||
|
this.client_ = algoliasearch(application_id, admin_api_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add two numbers.
|
||||||
|
* @param {string} indexName - The name of the index
|
||||||
|
* @param {*} options - not required just to match the schema we are used it
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
|
createIndex(indexName, options = {}) {
|
||||||
|
return this.client_.initIndex(indexName)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to get an index
|
||||||
|
* @param {string} indexName - the index name.
|
||||||
|
* @return {Promise<{object}>} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
getIndex(indexName) {
|
||||||
|
let hits = []
|
||||||
|
return this.client_
|
||||||
|
.initIndex(indexName)
|
||||||
|
.browseObjects({
|
||||||
|
query: indexName,
|
||||||
|
batch: (batch) => {
|
||||||
|
hits = hits.concat(batch)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(() => hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} indexName
|
||||||
|
* @param {Array} documents - products list array
|
||||||
|
* @param {*} type
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
|
addDocuments(indexName, documents, type) {
|
||||||
|
const transformedDocuments = this.getTransformedDocuments(type, documents)
|
||||||
|
return this.client_.initIndex(indexName).saveObjects(transformedDocuments)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to replace documents
|
||||||
|
* @param {string} indexName - the index name.
|
||||||
|
* @param {Object} documents - array of document objects that will replace existing documents
|
||||||
|
* @param {Array.<Object>} type - type of documents to be replaced (e.g: products, regions, orders, etc)
|
||||||
|
* @return {Promise<{object}>} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
replaceDocuments(indexName, documents, type) {
|
||||||
|
const transformedDocuments = this.getTransformedDocuments(type, documents)
|
||||||
|
return this.client_
|
||||||
|
.initIndex(indexName)
|
||||||
|
.replaceAllObjects(transformedDocuments)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to delete document
|
||||||
|
* @param {string} indexName - the index name
|
||||||
|
* @param {string} document_id - the id of the document
|
||||||
|
* @return {Promise<{object}>} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
deleteDocument(indexName, document_id) {
|
||||||
|
return this.client_.initIndex(indexName).deleteObject(document_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to delete all documents
|
||||||
|
* @param {string} indexName - the index name
|
||||||
|
* @return {Promise<{object}>} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
deleteAllDocuments(indexName) {
|
||||||
|
return this.client_.initIndex(indexName).delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to search for a document in an index
|
||||||
|
* @param {string} indexName - the index name
|
||||||
|
* @param {string} query - the search query
|
||||||
|
* @param {*} options
|
||||||
|
* - any options passed to the request object other than the query and indexName
|
||||||
|
* - additionalOptions contain any provider specific options
|
||||||
|
* @return {*} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
search(indexName, query, options) {
|
||||||
|
const { paginationOptions, filter, additionalOptions } = options
|
||||||
|
if ("limit" in paginationOptions) {
|
||||||
|
paginationOptions["length"] = paginationOptions.limit
|
||||||
|
delete paginationOptions.limit
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.client_.initIndex(indexName).search(query, {
|
||||||
|
filters: filter,
|
||||||
|
...paginationOptions,
|
||||||
|
...additionalOptions
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to update the settings of an index
|
||||||
|
* @param {string} indexName - the index name
|
||||||
|
* @param {object} settings - settings object
|
||||||
|
* @return {Promise<{object}>} - returns response from search engine provider
|
||||||
|
*/
|
||||||
|
updateSettings(indexName, settings) {
|
||||||
|
return this.client_.initIndex(indexName).setSettings(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
getTransformedDocuments(type, documents) {
|
||||||
|
switch (type) {
|
||||||
|
case indexTypes.products:
|
||||||
|
return this.transformProducts(documents)
|
||||||
|
default:
|
||||||
|
return documents
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transformProducts(products) {
|
||||||
|
if (!products) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
return products.map(transformProduct)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default AlgoliaService
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
const variantKeys = [
|
||||||
|
"sku",
|
||||||
|
"title",
|
||||||
|
"upc",
|
||||||
|
"ean",
|
||||||
|
"mid_code",
|
||||||
|
"hs_code",
|
||||||
|
"options",
|
||||||
|
]
|
||||||
|
const prefix = `variant`
|
||||||
|
|
||||||
|
export const transformProduct = (product) => {
|
||||||
|
const initialObj = variantKeys.reduce((obj, key) => {
|
||||||
|
obj[`${prefix}_${key}`] = []
|
||||||
|
return obj
|
||||||
|
}, {})
|
||||||
|
initialObj[`${prefix}_options_value`] = []
|
||||||
|
|
||||||
|
const flattenedVariantFields = product.variants.reduce((obj, variant) => {
|
||||||
|
variantKeys.forEach((k) => {
|
||||||
|
if (k === "options" && variant[k]) {
|
||||||
|
const values = variant[k].map((option) => option.value)
|
||||||
|
obj[`${prefix}_options_value`] =
|
||||||
|
obj[`${prefix}_options_value`].concat(values)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return variant[k] && obj[`${prefix}_${k}`].push(variant[k])
|
||||||
|
})
|
||||||
|
return obj
|
||||||
|
}, initialObj)
|
||||||
|
|
||||||
|
product.objectID = product.id
|
||||||
|
product.type_value = product.type && product.type.value
|
||||||
|
product.collection_title = product.collection && product.collection.title
|
||||||
|
product.collection_handle = product.collection && product.collection.handle
|
||||||
|
product.tags_value = product.tags ? product.tags.map((t) => t.value) : []
|
||||||
|
|
||||||
|
return {
|
||||||
|
...product,
|
||||||
|
...flattenedVariantFields,
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,4 @@
|
|||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { MedusaError, Validator } from "medusa-core-utils"
|
||||||
import ProductService from "../../../../services/product"
|
import ProductService from "../../../../services/product"
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
|
|||||||
Reference in new issue
Block a user