chore: local workflow proxying methods to pass context (#6263)
What: - When calling a module's method inside a Local Workflow the MedusaContext is passed as the last argument to the method if not provided - Add `requestId` to req - A couple of fixes on Remote Joiner and the data fetcher for internal services Why: - The context used to initialize the workflow has to be shared with all modules. properties like transactionId will be used to emit events and requestId to trace logs for example.
This commit is contained in:
committed by
GitHub
parent
a2bf6756ac
commit
45134e4d11
@@ -14,7 +14,7 @@ import {
|
||||
shouldForceTransaction,
|
||||
upperCaseFirst,
|
||||
} from "../common"
|
||||
import { MedusaContext } from "../decorators"
|
||||
import { MedusaContext } from "../modules-sdk"
|
||||
import { buildQuery } from "./build-query"
|
||||
import { InjectManager, InjectTransactionManager } from "./decorators"
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
export function MedusaContext() {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
parameterIndex: number
|
||||
) {
|
||||
target.MedusaContextIndex_ ??= {}
|
||||
target.MedusaContextIndex_[propertyKey] = parameterIndex
|
||||
}
|
||||
}
|
||||
|
||||
MedusaContext.getIndex = function (
|
||||
target: any,
|
||||
propertyKey: string
|
||||
): number | undefined {
|
||||
return target.MedusaContextIndex_?.[propertyKey]
|
||||
}
|
||||
|
||||
export const MedusaContextType = "MedusaContext"
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./context-parameter"
|
||||
export * from "./inject-manager"
|
||||
export * from "./inject-shared-context"
|
||||
export * from "./inject-transaction-manager"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Context, SharedContext } from "@medusajs/types"
|
||||
import { Context } from "@medusajs/types"
|
||||
|
||||
export function InjectManager(managerProperty?: string): MethodDecorator {
|
||||
return function (
|
||||
@@ -16,13 +16,31 @@ export function InjectManager(managerProperty?: string): MethodDecorator {
|
||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||
|
||||
descriptor.value = function (...args: any[]) {
|
||||
const context: SharedContext | Context = { ...(args[argIndex] ?? {}) }
|
||||
const originalContext = args[argIndex] ?? {}
|
||||
const copiedContext = {} as Context
|
||||
for (const key in originalContext) {
|
||||
if (key === "manager" || key === "transactionManager") {
|
||||
continue
|
||||
}
|
||||
|
||||
Object.defineProperty(copiedContext, key, {
|
||||
get: function () {
|
||||
return originalContext[key]
|
||||
},
|
||||
set: function (value) {
|
||||
originalContext[key] = value
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const resourceWithManager = !managerProperty
|
||||
? this
|
||||
: this[managerProperty]
|
||||
|
||||
context.manager = context.manager ?? resourceWithManager.getFreshManager()
|
||||
args[argIndex] = context
|
||||
copiedContext.manager ??= resourceWithManager.getFreshManager()
|
||||
copiedContext.transactionManager ??= originalContext?.transactionManager
|
||||
|
||||
args[argIndex] = copiedContext
|
||||
|
||||
return originalMethod.apply(this, args)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Context, SharedContext } from "@medusajs/types"
|
||||
import { MedusaContextType } from "./context-parameter"
|
||||
|
||||
export function InjectSharedContext(): MethodDecorator {
|
||||
return function (
|
||||
@@ -16,7 +17,9 @@ export function InjectSharedContext(): MethodDecorator {
|
||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||
|
||||
descriptor.value = function (...args: any[]) {
|
||||
const context: SharedContext | Context = { ...(args[argIndex] ?? {}) }
|
||||
const context: SharedContext | Context = {
|
||||
...(args[argIndex] ?? { __type: MedusaContextType }),
|
||||
}
|
||||
args[argIndex] = context
|
||||
|
||||
return originalMethod.apply(this, args)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Context, SharedContext } from "@medusajs/types"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { isString } from "../../common"
|
||||
import { MedusaContextType } from "./context-parameter"
|
||||
|
||||
export function InjectTransactionManager(
|
||||
shouldForceTransactionOrManagerProperty:
|
||||
@@ -31,7 +32,8 @@ export function InjectTransactionManager(
|
||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const shouldForceTransactionRes = shouldForceTransaction(target)
|
||||
const context: SharedContext | Context = args[argIndex] ?? {}
|
||||
const context: Context = args[argIndex] ?? {}
|
||||
const originalContext = args[argIndex] ?? {}
|
||||
|
||||
if (!shouldForceTransactionRes && context?.transactionManager) {
|
||||
return await originalMethod.apply(this, args)
|
||||
@@ -42,8 +44,27 @@ export function InjectTransactionManager(
|
||||
: this[managerProperty]
|
||||
).transaction(
|
||||
async (transactionManager) => {
|
||||
args[argIndex] = { ...(args[argIndex] ?? {}) }
|
||||
args[argIndex].transactionManager = transactionManager
|
||||
const copiedContext = {} as Context
|
||||
for (const key in originalContext) {
|
||||
if (key === "manager" || key === "transactionManager") {
|
||||
continue
|
||||
}
|
||||
|
||||
Object.defineProperty(copiedContext, key, {
|
||||
get: function () {
|
||||
return originalContext[key]
|
||||
},
|
||||
set: function (value) {
|
||||
originalContext[key] = value
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
copiedContext.transactionManager ??= transactionManager
|
||||
copiedContext.manager ??= originalContext?.manager
|
||||
copiedContext.__type ??= MedusaContextType
|
||||
|
||||
args[argIndex] = copiedContext
|
||||
|
||||
return await originalMethod.apply(this, args)
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user