{"version":3,"file":"index.mjs","sources":["../../../src/services/content-api/index.ts"],"sourcesContent":["import _ from 'lodash';\nimport { sanitize, validate } from '@strapi/utils';\n\nimport type { Core, UID } from '@strapi/types';\n\nimport instantiatePermissionsUtilities from './permissions';\n\nconst transformRoutePrefixFor = (pluginName: string) => (route: Core.Route) => {\n  const prefix = route.config && route.config.prefix;\n  const path = prefix !== undefined ? `${prefix}${route.path}` : `/${pluginName}${route.path}`;\n\n  return {\n    ...route,\n    path,\n  };\n};\n\nconst filterContentAPI = (route: Core.Route) => route.info.type === 'content-api';\n\n/**\n * Create a content API container that holds logic, tools and utils. (eg: permissions, ...)\n */\nconst createContentAPI = (strapi: Core.Strapi) => {\n  const getRoutesMap = async () => {\n    const routesMap: Record<string, Core.Route[]> = {};\n\n    _.forEach(strapi.apis, (api, apiName) => {\n      const routes = _.flatMap(api.routes, (route) => {\n        if ('routes' in route) {\n          return route.routes;\n        }\n\n        return route;\n      }).filter(filterContentAPI);\n\n      if (routes.length === 0) {\n        return;\n      }\n\n      const apiPrefix = strapi.config.get('api.rest.prefix');\n      routesMap[`api::${apiName}`] = routes.map((route) => ({\n        ...route,\n        path: `${apiPrefix}${route.path}`,\n      }));\n    });\n\n    _.forEach(strapi.plugins, (plugin, pluginName) => {\n      const transformPrefix = transformRoutePrefixFor(pluginName);\n\n      if (Array.isArray(plugin.routes)) {\n        return plugin.routes.map(transformPrefix).filter(filterContentAPI);\n      }\n\n      const routes = _.flatMap(plugin.routes, (route) => route.routes.map(transformPrefix)).filter(\n        filterContentAPI\n      );\n\n      if (routes.length === 0) {\n        return;\n      }\n\n      const apiPrefix = strapi.config.get('api.rest.prefix');\n      routesMap[`plugin::${pluginName}`] = routes.map((route) => ({\n        ...route,\n        path: `${apiPrefix}${route.path}`,\n      }));\n    });\n\n    return routesMap;\n  };\n\n  const sanitizer = sanitize.createAPISanitizers({\n    getModel(uid: string) {\n      return strapi.getModel(uid as UID.Schema);\n    },\n    // NOTE: use lazy access to allow registration of sanitizers after the creation of the container\n    get sanitizers() {\n      return {\n        input: strapi.sanitizers.get('content-api.input'),\n        output: strapi.sanitizers.get('content-api.output'),\n      };\n    },\n  });\n\n  const validator = validate.createAPIValidators({\n    getModel(uid: string) {\n      return strapi.getModel(uid as UID.Schema);\n    },\n    // NOTE: use lazy access to allow registration of validators after the creation of the container\n    get validators() {\n      return {\n        input: strapi.validators.get('content-api.input'),\n      };\n    },\n  });\n\n  return {\n    permissions: instantiatePermissionsUtilities(strapi),\n    getRoutesMap,\n    sanitize: sanitizer,\n    validate: validator,\n  };\n};\n\nexport default createContentAPI;\n"],"names":[],"mappings":";;;AAOA,MAAM,0BAA0B,CAAC,eAAuB,CAAC,UAAsB;AAC7E,QAAM,SAAS,MAAM,UAAU,MAAM,OAAO;AAC5C,QAAM,OAAO,WAAW,SAAY,GAAG,MAAM,GAAG,MAAM,IAAI,KAAK,IAAI,UAAU,GAAG,MAAM,IAAI;AAEnF,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;AAEA,MAAM,mBAAmB,CAAC,UAAsB,MAAM,KAAK,SAAS;AAK9D,MAAA,mBAAmB,CAAC,WAAwB;AAChD,QAAM,eAAe,YAAY;AAC/B,UAAM,YAA0C,CAAA;AAEhD,MAAE,QAAQ,OAAO,MAAM,CAAC,KAAK,YAAY;AACvC,YAAM,SAAS,EAAE,QAAQ,IAAI,QAAQ,CAAC,UAAU;AAC9C,YAAI,YAAY,OAAO;AACrB,iBAAO,MAAM;AAAA,QACf;AAEO,eAAA;AAAA,MAAA,CACR,EAAE,OAAO,gBAAgB;AAEtB,UAAA,OAAO,WAAW,GAAG;AACvB;AAAA,MACF;AAEA,YAAM,YAAY,OAAO,OAAO,IAAI,iBAAiB;AACrD,gBAAU,QAAQ,OAAO,EAAE,IAAI,OAAO,IAAI,CAAC,WAAW;AAAA,QACpD,GAAG;AAAA,QACH,MAAM,GAAG,SAAS,GAAG,MAAM,IAAI;AAAA,MAC/B,EAAA;AAAA,I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