From 2c55dd84c689cdc2374a3573e7fab8194364f020 Mon Sep 17 00:00:00 2001 From: Boegie19 Date: Thu, 12 Oct 2023 22:23:41 +0200 Subject: [PATCH] fixed harding api and removed not used stuff --- .../utils/config/resolveUserStrategy.js | 8 ++--- .../server/utils/config/routeExists.js | 30 ------------------- .../server/utils/middlewares/flattenRoutes.js | 6 +++- 3 files changed, 8 insertions(+), 36 deletions(-) delete mode 100644 packages/strapi-plugin-rest-cache/server/utils/config/routeExists.js diff --git a/packages/strapi-plugin-rest-cache/server/utils/config/resolveUserStrategy.js b/packages/strapi-plugin-rest-cache/server/utils/config/resolveUserStrategy.js index 207d869b3..444101f1a 100644 --- a/packages/strapi-plugin-rest-cache/server/utils/config/resolveUserStrategy.js +++ b/packages/strapi-plugin-rest-cache/server/utils/config/resolveUserStrategy.js @@ -5,10 +5,8 @@ * @typedef {import('@strapi/strapi').Strapi} Strapi */ -const debug = require('debug')('strapi:strapi-plugin-rest-cache'); const { getRelatedModelsUid } = require('./getRelatedModelsUid'); const { deepFreeze } = require('./deepFreeze'); -const { routeExists } = require('./routeExists'); const { CachePluginStrategy, CacheRouteConfig, @@ -144,7 +142,7 @@ function resolveUserStrategy(strapi, userOptions) { ) { cacheConfig.routes.push( new CacheRouteConfig({ - path: `/api${route.path}`, + path: `/${apiPrefix}${route.path}`, paramNames: route.path.match(routeParams) ?? [], method: route.method, keys: new CacheKeysConfig(cacheConfig.keys), @@ -162,7 +160,7 @@ function resolveUserStrategy(strapi, userOptions) { ) { cacheConfig.routes.push( new CacheRouteConfig({ - path: `/api${route.path}`, + path: `/${apiPrefix}${route.path}`, paramNames: route.path.match(routeParams) ?? [], method: route.method, keys: new CacheKeysConfig(cacheConfig.keys), @@ -177,7 +175,7 @@ function resolveUserStrategy(strapi, userOptions) { ) { cacheConfig.routes.push( new CacheRouteConfig({ - path: `/api${route.path}`, + path: `/${apiPrefix}${route.path}`, paramNames: route.path.match(routeParams) ?? [], method: route.method, keys: new CacheKeysConfig(cacheConfig.keys), diff --git a/packages/strapi-plugin-rest-cache/server/utils/config/routeExists.js b/packages/strapi-plugin-rest-cache/server/utils/config/routeExists.js deleted file mode 100644 index 6dd8f631a..000000000 --- a/packages/strapi-plugin-rest-cache/server/utils/config/routeExists.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -/** - * @typedef {import('../../types').CacheRouteConfig} CacheRouteConfig - */ - -/** - * Check if a custom route is registered in strapi - * - * @param {Strapi} strapi - * @param {CacheRouteConfig} route - * @return {boolean} - */ -function routeExists(strapi, route) { - // check api routes - const allRoutes = [ - ...strapi.server.listRoutes(), - ...strapi.server.api('content-api').listRoutes(), - ...strapi.server.api('admin').listRoutes(), - ]; - const match = allRoutes.find( - (routeLayer) => - routeLayer.methods.includes(route.method) && - routeLayer.path.match(new RegExp(`^${route.path}/?`)) // match with optional leading slash - ); - - return Boolean(match); -} - -module.exports = { routeExists }; diff --git a/packages/strapi-plugin-rest-cache/server/utils/middlewares/flattenRoutes.js b/packages/strapi-plugin-rest-cache/server/utils/middlewares/flattenRoutes.js index 25fcc48bf..30426a71e 100644 --- a/packages/strapi-plugin-rest-cache/server/utils/middlewares/flattenRoutes.js +++ b/packages/strapi-plugin-rest-cache/server/utils/middlewares/flattenRoutes.js @@ -4,14 +4,18 @@ * @param {Strapi} strapi * @return {void} */ + function flattenRoutes(strapi) { let routes = []; for (const contentTypes of Object.values(strapi.api)) { routes = routes.concat(flatten(contentTypes)); } // @TODO add prefix support before doing this + + const apiPrefix = strapi.config.get('api.rest.prefix'); + for (const route of routes) { - route.globalPath = `/api${route.path}`; + route.globalPath = `/${apiPrefix}${route.path}`; } return routes; }