UNPKG

@medusajs/utils

Version:

Medusa utilities functions shared by Medusa core and Modules

49 lines 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InjectManager = InjectManager; const common_1 = require("../../common"); const context_parameter_1 = require("./context-parameter"); function InjectManager(managerProperty) { return function (target, propertyKey, descriptor) { if (!target.MedusaContextIndex_) { throw new Error(`To apply @InjectManager you have to flag a parameter using @MedusaContext`); } managerProperty ??= "baseRepository_"; const originalMethod = descriptor.value; const argIndex = target.MedusaContextIndex_[propertyKey]; descriptor.value = function (...args) { const originalContext = args[argIndex] ?? {}; const copiedContext = {}; for (const key in originalContext) { if (key === "manager" || key === "transactionManager") { continue; } Object.defineProperty(copiedContext, key, { enumerable: true, get: function () { return originalContext[key]; }, set: function (value) { originalContext[key] = value; }, }); } const resourceWithManager = !managerProperty ? this : this[managerProperty]; if (!(0, common_1.isPresent)(resourceWithManager) && !(0, common_1.isPresent)(originalContext.manager)) { throw new Error(`Could not find a manager in the context. Ensure that ${this.managerProperty} is set on your service that points to a repository.`); } copiedContext.manager = originalContext.manager ?? resourceWithManager.getFreshManager(); if (originalContext?.transactionManager) { copiedContext.transactionManager = originalContext?.transactionManager; } copiedContext.__type = context_parameter_1.MedusaContextType; args[argIndex] = copiedContext; return originalMethod.apply(this, args); }; }; } //# sourceMappingURL=inject-manager.js.map