fix: allow changing regions safely

This commit is contained in:
Sebastian Rindom committed 2021-10-19 18:56:52 +02:00
commit 06f5fe2670
278 files changed
+14657 -9233

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,8 @@
/src/subscribers
/api
/services
/models
/subscribers
/loaders
/utils
@@ -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,30 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.0.5](https://github.com/medusajs/medusa/compare/medusa-plugin-meilisearch@0.0.4...medusa-plugin-meilisearch@0.0.5) (2021-10-18)
**Note:** Version bump only for package medusa-plugin-meilisearch
## [0.0.4](https://github.com/medusajs/medusa/compare/medusa-plugin-meilisearch@0.0.3...medusa-plugin-meilisearch@0.0.4) (2021-10-18)
**Note:** Version bump only for package medusa-plugin-meilisearch
## 0.0.3 (2021-10-18)
### Bug Fixes
- meiliesearch README.md + remove: searchService from silentResolver ([1444353](https://github.com/medusajs/medusa/commit/1444353b0af4e18a23cebbf46b6d1246aa495bb4))
- move subscriber to core ([700f8c3](https://github.com/medusajs/medusa/commit/700f8c39190469337c74d9bf3f046f293024e521))
- remove package-lock ([08b2d8f](https://github.com/medusajs/medusa/commit/08b2d8fc39e7e04fadbfb012fcbc083febf9c290))
- use type to choose transformer before adding or replacing documents ([24eecd2](https://github.com/medusajs/medusa/commit/24eecd2922e0c3425f2d43549b3227c756820387))
## 0.0.2 (2021-10-18)
### Bug Fixes
- meiliesearch README.md + remove: searchService from silentResolver ([1444353](https://github.com/medusajs/medusa/commit/1444353b0af4e18a23cebbf46b6d1246aa495bb4))
- move subscriber to core ([700f8c3](https://github.com/medusajs/medusa/commit/700f8c39190469337c74d9bf3f046f293024e521))
- remove package-lock ([08b2d8f](https://github.com/medusajs/medusa/commit/08b2d8fc39e7e04fadbfb012fcbc083febf9c290))
- use type to choose transformer before adding or replacing documents ([24eecd2](https://github.com/medusajs/medusa/commit/24eecd2922e0c3425f2d43549b3227c756820387))
@@ -0,0 +1,21 @@
# medusa-plugin-meilisearch
Meilisearch Plugin for Medusa to search for products.
## Plugin Options
```
{
config: {
host: [your meilisearch host],
},
settings: {
[indexName]: [meilisearch settings passed to meilisearch's `updateSettings()` method]
// example
products: {
searchableAttributes: ["title", "description", "variant_sku", "type_value"],
displayedAttributes: ["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-meilisearch",
"version": "0.0.5",
"description": "A starter for Medusa projects.",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-meilisearch"
},
"author": "Zakaria El Asri",
"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": {
"body-parser": "^1.19.0",
"lodash": "^4.17.21",
"medusa-core-utils": "^1.1.26",
"medusa-interfaces": "^1.1.27",
"meilisearch": "^0.20.0"
},
"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,14 @@
export default async (container, options) => {
try {
const meilisearchService = container.resolve("meilisearchService")
await Promise.all(
Object.entries(options.settings).map(([key, value]) =>
meilisearchService.updateSettings(key, value)
)
)
} catch (err) {
// ignore
console.log(err)
}
}
@@ -0,0 +1,69 @@
import { SearchService } from "medusa-interfaces"
import { MeiliSearch } from "meilisearch"
import { indexTypes } from "medusa-core-utils"
import { transformProduct } from "../utils/transform-product"
class MeiliSearchService extends SearchService {
constructor(container, options) {
super()
this.options_ = options
this.client_ = new MeiliSearch(options.config)
}
createIndex(indexName, options) {
return this.client_.createIndex(indexName, options)
}
getIndex(indexName) {
return this.client_.index(indexName)
}
addDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
return this.client_.index(indexName).addDocuments(transformedDocuments)
}
replaceDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
return this.client_.index(indexName).addDocuments(transformedDocuments)
}
deleteDocument(indexName, document_id) {
return this.client_.index(indexName).deleteDocument(document_id)
}
deleteAllDocuments(indexName) {
return this.client_.index(indexName).deleteAllDocuments()
}
search(indexName, query, options) {
const { paginationOptions, filter, additionalOptions } = options
return this.client_
.index(indexName)
.search(query, { filter, ...paginationOptions, ...additionalOptions })
}
updateSettings(indexName, settings) {
return this.client_.index(indexName).updateSettings(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 MeiliSearchService
@@ -0,0 +1,41 @@
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.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