chore(medusa,orchestration,link-modules,modules-sdk): internal services as modules (#4925)
This commit is contained in:
@@ -11,8 +11,9 @@ export * from "./is-object"
|
||||
export * from "./is-string"
|
||||
export * from "./omit-deep"
|
||||
export * from "./product-category"
|
||||
export * from "./remote-query-fetch-data"
|
||||
export * from "./remove-undefined-properties"
|
||||
export * from "./set-metadata"
|
||||
export * from "./validate-id"
|
||||
export * from "./validators/is-type"
|
||||
export { registerOverriddenValidators, validator } from "./validator"
|
||||
export * from "./validators/is-type"
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { MedusaModule, RemoteQuery } from "@medusajs/modules-sdk"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
|
||||
export function remoteQueryFetchData(container: MedusaContainer) {
|
||||
return async (expand, keyField, ids, relationship) => {
|
||||
const serviceConfig = expand.serviceConfig
|
||||
const service = container.resolve(serviceConfig.serviceName, {
|
||||
allowUnregistered: true,
|
||||
})
|
||||
|
||||
if (MedusaModule.isInstalled(serviceConfig.serviceName)) {
|
||||
return
|
||||
}
|
||||
|
||||
const filters = {}
|
||||
const options = {
|
||||
...RemoteQuery.getAllFieldsAndRelations(expand),
|
||||
}
|
||||
|
||||
if (ids) {
|
||||
filters[keyField] = ids
|
||||
}
|
||||
|
||||
const hasPagination = Object.keys(options).some((key) =>
|
||||
["skip"].includes(key)
|
||||
)
|
||||
|
||||
let methodName = hasPagination ? "listAndCount" : "list"
|
||||
|
||||
if (relationship?.args?.methodSuffix) {
|
||||
methodName += relationship.args.methodSuffix
|
||||
} else if (serviceConfig?.args?.methodSuffix) {
|
||||
methodName += serviceConfig.args.methodSuffix
|
||||
}
|
||||
|
||||
const result = await service[methodName](filters, options)
|
||||
|
||||
if (hasPagination) {
|
||||
const [data, count] = result
|
||||
return {
|
||||
data: {
|
||||
rows: data,
|
||||
metadata: {},
|
||||
},
|
||||
path: "rows",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
data: result,
|
||||
} as any
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user