add: abstract search functionality to core + adjust meilisearch-plugin
add SearchService interface to medusa-interfaces add DefaultSearchService skeleton implementation to core add search-index.js loader to core for indexing db documents add SearchSubscriber to core add loadToSearchEngine method in ProductService switch order of loaders in core to load subscriptions AFTER plugins adjust service and loader for medusa-plugin-meilisearch
This commit is contained in:
@@ -15,6 +15,7 @@ import returnReasonRoutes from "./return-reasons"
|
||||
import swapRoutes from "./swaps"
|
||||
import variantRoutes from "./variants"
|
||||
import giftCardRoutes from "./gift-cards"
|
||||
import searchRoutes from "./search"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -43,6 +44,7 @@ export default (app, container, config) => {
|
||||
returnRoutes(route)
|
||||
giftCardRoutes(route)
|
||||
returnReasonRoutes(route)
|
||||
searchRoutes(route)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Router } from "express"
|
||||
import middlewares from "../../../middlewares"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default app => {
|
||||
app.use("/search", route)
|
||||
|
||||
route.post("/", middlewares.wrap(require("./search").default))
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { INDEX_NS } from "../../../../utils/index-ns"
|
||||
|
||||
export default async (req, res) => {
|
||||
const schema = Validator.object()
|
||||
.keys({
|
||||
q: Validator.string().required(),
|
||||
indexName: Validator.string().required(),
|
||||
})
|
||||
.options({ allowUnknown: true })
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
try {
|
||||
const { q, indexName, ...options } = value
|
||||
|
||||
const searchService = req.scope.resolve("searchService")
|
||||
|
||||
const results = await searchService.search(
|
||||
`${INDEX_NS}_${indexName}`,
|
||||
q,
|
||||
options
|
||||
)
|
||||
|
||||
res.status(200).send(results)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user