{"version":3,"file":"index.mjs","sources":["../../../../src/core/domain/module/index.ts"],"sourcesContent":["import _ from 'lodash';\nimport { yup } from '@strapi/utils';\nimport type { Strapi, Common, Schema } from '@strapi/types';\n\nimport { removeNamespace } from '../../utils';\nimport { validateModule } from './validation';\n\ninterface LifecyclesState {\n  bootstrap?: boolean;\n  register?: boolean;\n  destroy?: boolean;\n}\n\nexport interface RawModule {\n  config?: Record<string, unknown>;\n  routes?: Common.Module['routes'];\n  controllers?: Common.Module['controllers'];\n  services?: Common.Module['services'];\n  contentTypes?: Common.Module['contentTypes'];\n  policies?: Common.Module['policies'];\n  middlewares?: Common.Module['middlewares'];\n  bootstrap?: (params: { strapi: Strapi }) => Promise<void>;\n  register?: (params: { strapi: Strapi }) => Promise<void>;\n  destroy?: (params: { strapi: Strapi }) => Promise<void>;\n}\n\nexport interface Module {\n  bootstrap: () => Promise<void>;\n  register: () => Promise<void>;\n  destroy: () => Promise<void>;\n  load: () => void;\n  routes: Common.Module['routes'];\n  config: (path: string, defaultValue?: unknown) => unknown;\n  contentType: (ctName: Common.UID.ContentType) => Schema.ContentType;\n  contentTypes: Record<string, Schema.ContentType>;\n  service: (serviceName: Common.UID.Service) => Common.Service;\n  services: Record<string, Common.Service>;\n  policy: (policyName: Common.UID.Policy) => Common.Policy;\n  policies: Record<string, Common.Policy>;\n  middleware: (middlewareName: Common.UID.Middleware) => Common.Middleware;\n  middlewares: Record<string, Common.Middleware>;\n  controller: (controllerName: Common.UID.Controller) => Common.Controller;\n  controllers: Record<string, Common.Controller>;\n}\n\nconst uidToPath = (uid: string) => uid.replace('::', '.');\n\n// Removes the namespace from a map with keys prefixed with a namespace\nconst removeNamespacedKeys = <T extends Record<string, unknown>>(map: T, namespace: string) => {\n  return _.mapKeys(map, (value, key) => removeNamespace(key, namespace));\n};\n\nconst defaultModule = {\n  config: {},\n  routes: [],\n  controllers: {},\n  services: {},\n  contentTypes: {},\n  policies: {},\n  middlewares: {},\n};\n\nexport const createModule = (namespace: string, rawModule: RawModule, strapi: Strapi): Module => {\n  _.defaults(rawModule, defaultModule);\n\n  try {\n    validateModule(rawModule);\n  } catch (e) {\n    if (e instanceof yup.ValidationError) {\n      throw new Error(`strapi-server.js is invalid for '${namespace}'.\\n${e.errors.join('\\n')}`);\n    }\n  }\n\n  const called: LifecyclesState = {};\n  return {\n    async bootstrap() {\n      if (called.bootstrap) {\n        throw new Error(`Bootstrap for ${namespace} has already been called`);\n      }\n      called.bootstrap = true;\n      await (rawModule.bootstrap && rawModule.bootstrap({ strapi }));\n    },\n    async register() {\n      if (called.register) {\n        throw new Error(`Register for ${namespace} has already been called`);\n      }\n      called.register = true;\n      await (rawModule.register && rawModule.register({ strapi }));\n    },\n    async destroy() {\n      if (called.destroy) {\n        throw new Error(`Destroy for ${namespace} has already been called`);\n      }\n      called.destroy = true;\n      await (rawModule.destroy && rawModule.destroy({ strapi }));\n    },\n    load() {\n      strapi.container.get('content-types').add(namespace, rawModule.contentTypes);\n      strapi.container.get('services').add(namespace, rawModule.services);\n      strapi.container.get('policies').add(namespace, rawModule.policies);\n      strapi.container.get('middlewares').add(namespace, rawModule.middlewares);\n      strapi.container.get('controllers').add(namespace, rawModule.controllers);\n      strapi.container.get('config').set(uidToPath(namespace), rawModule.config);\n    },\n    get routes() {\n      return rawModule.routes ?? {};\n    },\n    config(path: string, defaultValue: unknown) {\n      return strapi.container.get('config').get(`${uidToPath(namespace)}.${path}`, defaultValue);\n    },\n    contentType(ctName: Common.UID.ContentType) {\n      return strapi.container.get('content-types').get(`${namespace}.${ctName}`);\n    },\n    get contentTypes() {\n      const contentTypes = strapi.container.get('content-types').getAll(namespace);\n      return removeNamespacedKeys(contentTypes, namespace);\n    },\n    service(serviceName: Common.UID.Service) {\n      return strapi.container.get('services').get(`${namespace}.${serviceName}`);\n    },\n    get services() {\n      const services = strapi.container.get('services').getAll(namespace);\n      return removeNamespacedKeys(services, namespace);\n    },\n    policy(policyName: Common.UID.Policy) {\n      return strapi.container.get('policies').get(`${namespace}.${policyName}`);\n    },\n    get policies() {\n      const policies = strapi.container.get('policies').getAll(namespace);\n      return removeNamespacedKeys(policies, namespace);\n    },\n    middleware(middlewareName: Common.UID.Middleware) {\n      return strapi.container.get('middlewares').get(`${namespace}.${middlewareName}`);\n    },\n    get middlewares() {\n      const middlewares = strapi.container.get('middlewares').getAll(namespace);\n      return removeNamespacedKeys(middlewares, namespace);\n    },\n    controller(controllerName: Common.UID.Controller) {\n      return strapi.container.get('controllers').get(`${namespace}.${controllerName}`);\n    },\n    get controllers() {\n      const controllers = strapi.container.get('controllers').getAll(namespace);\n      return removeNamespacedKeys(controllers, namespace);\n    },\n  };\n};\n"],"names":[],"mappings":";;;;AA6CA,MAAM,YAAY,CAAC,QAAgB,IAAI,QAAQ,MAAM,GAAG;AAGxD,MAAM,uBAAuB,CAAoC,KAAQ,cAAsB;AACtF,SAAA,EAAE,QAAQ,KAAK,CAAC,OAAO,QAAQ,gBAAgB,KAAK,SAAS,CAAC;AACvE;AAEA,MAAM,gBAAgB;AAAA,EACpB,QAAQ,CAAC;AAAA,EACT,QAAQ,CAAC;AAAA,EACT,aAAa,CAAC;AAAA,EACd,UAAU,CAAC;AAAA,EACX,cAAc,CAAC;AAAA,EACf,UAAU,CAAC;AAAA,EACX,aAAa,CAAC;AAChB;AAEO,MAAM,eAAe,CAAC,WAAmB,WAAsB,WAA2B;AAC7F,IAAA,SAAS,WAAW,aAAa;AAE/B,MAAA;AACF,mBAAe,SAAS;AAAA,WACjB,GAAG;AACN,QAAA,aAAa,IAAI,iBAAiB;AAC9B,YAAA,IAAI,MAAM,oCAAoC,SAAS;AAAA,EAAO,EAAE,OAAO,KAAK,IAAI,CAAC,EAAE;AAAA,IAC3F;AAAA,EACF;AAEA,QAAM,SAA0B,CAAA;AACzB,SAAA;AAAA,IACL,MAAM,YAAY;AAChB,UAAI,OAAO,WAAW;AACpB,cAAM,IAAI,MAAM,iBAAiB,SAAS,0BAA0B;AAAA,MACtE;AACA,aAAO,YAAY;A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