Skip to content

Commit

Permalink
move provider registration to bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Boegie19 committed Nov 12, 2023
1 parent 708a48d commit b23779d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
54 changes: 52 additions & 2 deletions packages/strapi-plugin-rest-cache/server/bootstrap.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
};
51 changes: 1 addition & 50 deletions packages/strapi-plugin-rest-cache/server/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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();
Expand Down

0 comments on commit b23779d

Please sign in to comment.