From b23779dd625eee613cee31f64a97e7247da69d66 Mon Sep 17 00:00:00 2001 From: Boegie19 Date: Sun, 12 Nov 2023 19:53:01 +0100 Subject: [PATCH] move provider registration to bootstrap --- .../server/bootstrap.js | 54 ++++++++++++++++++- .../server/register.js | 51 +----------------- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/packages/strapi-plugin-rest-cache/server/bootstrap.js b/packages/strapi-plugin-rest-cache/server/bootstrap.js index 96fdfd9ef..f37ec4139 100644 --- a/packages/strapi-plugin-rest-cache/server/bootstrap.js +++ b/packages/strapi-plugin-rest-cache/server/bootstrap.js @@ -1,12 +1,52 @@ 'use strict'; +const chalk = require('chalk'); + const permissionsActions = require('./permissions-actions'); +const { CacheProvider } = require('./types'); +const createProvider = async (providerConfig, { strapi }) => { + const providerName = providerConfig.name.toLowerCase(); + let provider; + + let modulePath; + try { + modulePath = require.resolve(`strapi-provider-rest-cache-${providerName}`); + } catch (error) { + if (error.code === 'MODULE_NOT_FOUND') { + modulePath = providerName; + } else { + throw error; + } + } + + try { + // eslint-disable-next-line + provider = require(modulePath); + } catch (err) { + throw new Error( + `Could not load REST Cache provider "${providerName}". You may need to install a provider plugin "yarn add strapi-provider-rest-cache-${providerName}".` + ); + } + + const providerInstance = await provider.init(providerConfig.options, { + strapi, + }); + + if (!(providerInstance instanceof CacheProvider)) { + throw new Error( + `Could not load REST Cache provider "${providerName}". The package "strapi-provider-rest-cache-${providerName}" does not export a CacheProvider instance.` + ); + } + + return Object.freeze(providerInstance); +}; /** * @param {{ strapi: Strapi }} strapi */ async function bootstrap({ strapi }) { - + // resolve user configuration, check for missing or invalid optinos + const pluginOption = strapi.config.get('plugin.rest-cache'); const cacheStore = strapi.plugin('rest-cache').service('cacheStore'); // watch for changes in any roles -> clear all cache // need to be done before lifecycles are registered @@ -22,8 +62,18 @@ async function bootstrap({ strapi }) { await strapi.admin.services.permission.actionProvider.registerMany( permissionsActions.actions ); + + // register cache provider + const provider = await createProvider(pluginOption.provider, { strapi }); + cacheStore.init(provider); + + strapi.log.info( + `Using REST Cache plugin with provider "${chalk.cyan( + pluginOption.provider.name + )}"` + ); } module.exports = { - bootstrap, + bootstrap, }; diff --git a/packages/strapi-plugin-rest-cache/server/register.js b/packages/strapi-plugin-rest-cache/server/register.js index 1a8c55d83..9ca29dba9 100644 --- a/packages/strapi-plugin-rest-cache/server/register.js +++ b/packages/strapi-plugin-rest-cache/server/register.js @@ -3,55 +3,16 @@ /** * @typedef {import('@strapi/strapi').Strapi} Strapi */ -const chalk = require('chalk'); const debug = require('debug'); -const { CacheProvider } = require('./types'); const { resolveUserStrategy } = require('./utils/config/resolveUserStrategy'); const { injectMiddlewares } = require('./utils/middlewares/injectMiddlewares'); -const createProvider = async (providerConfig, { strapi }) => { - const providerName = providerConfig.name.toLowerCase(); - let provider; - - let modulePath; - try { - modulePath = require.resolve(`strapi-provider-rest-cache-${providerName}`); - } catch (error) { - if (error.code === 'MODULE_NOT_FOUND') { - modulePath = providerName; - } else { - throw error; - } - } - - try { - // eslint-disable-next-line - provider = require(modulePath); - } catch (err) { - throw new Error( - `Could not load REST Cache provider "${providerName}". You may need to install a provider plugin "yarn add strapi-provider-rest-cache-${providerName}".` - ); - } - - const providerInstance = await provider.init(providerConfig.options, { - strapi, - }); - - if (!(providerInstance instanceof CacheProvider)) { - throw new Error( - `Could not load REST Cache provider "${providerName}". The package "strapi-provider-rest-cache-${providerName}" does not export a CacheProvider instance.` - ); - } - - return Object.freeze(providerInstance); -}; - /** * @param {{ strapi: Strapi }} strapi */ async function register({ strapi }) { - // resolve user configuration, check for missing or invalid options + // resolve user configuration, check for missing or invalid optinos const pluginOption = strapi.config.get('plugin.rest-cache'); const cacheStore = strapi.plugin('rest-cache').service('cacheStore'); @@ -67,19 +28,9 @@ async function register({ strapi }) { debug('strapi:strapi-plugin-rest-cache')('[STRATEGY]: %O', strategy); - // register cache provider - const provider = await createProvider(pluginOption.provider, { strapi }); - cacheStore.init(provider); - // boostrap cache middlewares injectMiddlewares(strapi, strategy); - strapi.log.info( - `Using REST Cache plugin with provider "${chalk.cyan( - pluginOption.provider.name - )}"` - ); - if (strategy.resetOnStartup) { strapi.log.warn('Reset cache on startup is enabled'); await cacheStore.reset();