UNPKG

@medusajs/utils

Version:

Medusa utilities functions shared by Medusa core and Modules

112 lines 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateContainerTypes = generateContainerTypes; const path_1 = require("path"); const definition_1 = require("./definition"); const file_system_1 = require("../common/file-system"); const to_unix_slash_1 = require("../common/to-unix-slash"); const to_camel_case_1 = require("../common/to-camel-case"); const upper_case_first_1 = require("../common/upper-case-first"); /** * For known services that has interfaces, we will set the container * type to the interface than the actual service implementation. * * The idea is to provide more precise types. */ const SERVICES_INTERFACES = { [definition_1.Modules.AUTH]: "IAuthModuleService", [definition_1.Modules.CACHE]: "ICacheService", [definition_1.Modules.CART]: "ICartModuleService", [definition_1.Modules.CUSTOMER]: "ICustomerModuleService", [definition_1.Modules.EVENT_BUS]: "IEventBusModuleService", [definition_1.Modules.INVENTORY]: "IInventoryService", [definition_1.Modules.PAYMENT]: "IPaymentModuleService", [definition_1.Modules.PRICING]: "IPricingModuleService", [definition_1.Modules.PRODUCT]: "IProductModuleService", [definition_1.Modules.PROMOTION]: "IPromotionModuleService", [definition_1.Modules.SALES_CHANNEL]: "ISalesChannelModuleService", [definition_1.Modules.TAX]: "ITaxModuleService", [definition_1.Modules.FULFILLMENT]: "IFulfillmentModuleService", [definition_1.Modules.STOCK_LOCATION]: "IStockLocationService", [definition_1.Modules.USER]: "IUserModuleService", [definition_1.Modules.WORKFLOW_ENGINE]: "IWorkflowEngineService", [definition_1.Modules.REGION]: "IRegionModuleService", [definition_1.Modules.ORDER]: "IOrderModuleService", [definition_1.Modules.API_KEY]: "IApiKeyModuleService", [definition_1.Modules.STORE]: "IStoreModuleService", [definition_1.Modules.CURRENCY]: "ICurrencyModuleService", [definition_1.Modules.FILE]: "IFileModuleService", [definition_1.Modules.NOTIFICATION]: "INotificationModuleService", [definition_1.Modules.LOCKING]: "ILockingModule", }; /** * Modules registered inside the config file points to one * of the following paths. * * - A package name * - A relative application import * - Or an absolute path using `require.resolve` * * In case of a relative import, we mutate the path to resolve properly * when the output file is inside the ".medusa/types" directory. * For example: * * => "./src/modules/brand" will become "../../src/modules/brand" * * Package names and absolute paths are left as it is. */ function normalizeModuleResolvePath(modulePath) { return modulePath.startsWith("./") || modulePath.startsWith("../") ? (0, to_unix_slash_1.toUnixSlash)((0, path_1.join)("../", "../", modulePath)) : modulePath; } /** * Creates the "modules-bindings.d.ts" file with container mappings * for the modules enabled inside a user's project. */ async function generateContainerTypes(modules, { outputDir, interfaceName, }) { const { imports, mappings } = Object.keys(modules).reduce((result, key) => { const services = Array.isArray(modules[key]) ? modules[key] : [modules[key]]; services.forEach((service) => { if (!service.__definition.resolvePath) { return; } /** * Key registered within the container */ const key = service.__definition.key; const interfaceKey = `'${key}'`; if (SERVICES_INTERFACES[key]) { result.imports.push(`import type { ${SERVICES_INTERFACES[key]} } from '@medusajs/framework/types'`); result.mappings.push(`${interfaceKey}: ${SERVICES_INTERFACES[key]}`); return; } /** * @todo. The property should exist on "LoadedModule" */ let servicePath = normalizeModuleResolvePath(service.__definition.resolvePath); /** * We create the service name (aka default import name) from the * service key that is registered inside the container. */ const serviceName = (0, upper_case_first_1.upperCaseFirst)((0, to_camel_case_1.toCamelCase)(key)); result.imports.push(`import type ${serviceName} from '${servicePath}'`); result.mappings.push(`${interfaceKey}: InstanceType<(typeof ${serviceName})['service']>`); }); return result; }, { imports: [], mappings: [], }); const fileSystem = new file_system_1.FileSystem(outputDir); const fileName = "modules-bindings.d.ts"; const fileContents = `${imports.join("\n")}\n\ndeclare module '@medusajs/framework/types' { interface ${interfaceName} { ${mappings.join(",\n ")} } }`; await fileSystem.create(fileName, fileContents); } //# sourceMappingURL=modules-to-container-types.js.map