{"version":3,"file":"services.mjs","sources":["../../../src/core/registries/services.ts"],"sourcesContent":["import { pickBy, has } from 'lodash/fp';\nimport type { Strapi, Common } from '@strapi/types';\nimport { addNamespace, hasNamespace } from '../utils';\n\nexport type ServiceFactory = (params: { strapi: Strapi }) => Common.Service | Common.Service;\nexport type ServiceFactoryMap = Record<string, ServiceFactory>;\nexport type ServiceMap = Record<string, Common.Service>;\nexport type ServiceExtendFn = (service: Common.Service) => Common.Service;\n\nconst servicesRegistry = (strapi: Strapi) => {\n  const services: ServiceFactoryMap = {};\n  const instantiatedServices: ServiceMap = {};\n\n  return {\n    /**\n     * Returns this list of registered services uids\n     */\n    keys() {\n      return Object.keys(services);\n    },\n\n    /**\n     * Returns the instance of a service. Instantiate the service if not already done\n     */\n    get(uid: Common.UID.Service) {\n      if (instantiatedServices[uid]) {\n        return instantiatedServices[uid];\n      }\n\n      const service = services[uid];\n      if (service) {\n        instantiatedServices[uid] = typeof service === 'function' ? service({ strapi }) : service;\n        return instantiatedServices[uid];\n      }\n    },\n\n    /**\n     * Returns a map with all the services in a namespace\n     */\n    getAll(namespace: string): ServiceMap {\n      const filteredServices = pickBy((_, uid) => hasNamespace(uid, namespace))(services);\n\n      // create lazy accessor to avoid instantiating the services;\n      const map = {};\n      for (const uid of Object.keys(filteredServices)) {\n        Object.defineProperty(map, uid, {\n          enumerable: true,\n          get: () => {\n            return this.get(uid as Common.UID.Service);\n          },\n        });\n      }\n\n      return map;\n    },\n\n    /**\n     * Registers a service\n     */\n    set(uid: string, service: ServiceFactory) {\n      services[uid] = service;\n      delete instantiatedServices[uid];\n      return this;\n    },\n\n    /**\n     * Registers a map of services for a specific namespace\n     */\n    add(namespace: string, newServices: ServiceFactoryMap) {\n      for (const serviceName of Object.keys(newServices)) {\n        const service = newServices[serviceName];\n        const uid = addNamespace(serviceName, namespace);\n\n        if (has(uid, services)) {\n          throw new Error(`Service ${uid} has already been registered.`);\n        }\n        services[uid] = service;\n      }\n\n      return this;\n    },\n\n    /**\n     * Wraps a service to extend it\n     */\n    extend(uid: Common.UID.Service, extendFn: ServiceExtendFn) {\n      const currentService = this.get(uid);\n\n      if (!currentService) {\n        throw new Error(`Service ${uid} doesn't exist`);\n      }\n\n      const newService = extendFn(currentService);\n      instantiatedServices[uid] = newService;\n\n      return this;\n    },\n  };\n};\n\nexport default servicesRegistry;\n"],"names":[],"mappings":";;AASM,MAAA,mBAAmB,CAAC,WAAmB;AAC3C,QAAM,WAA8B,CAAA;AACpC,QAAM,uBAAmC,CAAA;AAElC,SAAA;AAAA;AAAA;AAAA;AAAA,IAIL,OAAO;AACE,aAAA,OAAO,KAAK,QAAQ;AAAA,IAC7B;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,KAAyB;AACvB,UAAA,qBAAqB,GAAG,GAAG;AAC7B,eAAO,qBAAqB,GAAG;AAAA,MACjC;AAEM,YAAA,UAAU,SAAS,GAAG;AAC5B,UAAI,SAAS;AACU,6BAAA,GAAG,IAAI,OAAO,YAAY,aAAa,QAAQ,EAAE,QAAQ,IAAI;AAClF,eAAO,qBAAqB,GAAG;AAAA,MACjC;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,OAAO,WAA+B;AAC9B,YAAA,mBAAmB,OAAO,CAAC,GAAG,QAAQ,aAAa,KAAK,SAAS,CAAC,EAAE,QAAQ;AAGlF,YAAM,MAAM,CAAA;AACZ,iBAAW,OAAO,OAAO,KAAK,gBAAgB,GAAG;AACxC,eAAA,eAAe,KAAK,KAAK;AAAA,UAC9B,YAAY;AAAA,UACZ,KAAK,MAAM;AACF,mBAAA,KAAK,IAAI,GAAyB;AAAA,UAC3C;AAAA,QAAA,CACD;AAAA,MACH;AAEO,aAAA;AAAA,IACT;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,KAAa,SAAyB;AACxC,eAAS,GAAG,IAAI;AAChB,aAAO,qBAAqB,GAAG;AACxB,aAAA;AAAA,IACT;AAAA;AAAA;AAAA;AAAA,IAKA,IAAI,WAAmB,aAA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