-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inject middleware instead of using router #75
Merged
derrickmehaffy
merged 7 commits into
dev/strapi5
from
inject-middleware-instead-of-using-router
Nov 4, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
808efd3
inject middleware instead of using router
Boegie19 7b32656
add warning if route is not found
Boegie19 2c55dd8
fixed harding api and removed not used stuff
Boegie19 d0729e2
removed the need to eslint for e2e to run
Boegie19 708a48d
fix: set proper contentType reference name
derrickmehaffy b23779d
move provider registration to bootstrap
Boegie19 5218653
Merge branch 'dev/strapi5' into inject-middleware-instead-of-using-ro…
derrickmehaffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @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 | ||
const pluginOption = strapi.config.get('plugin.rest-cache'); | ||
const cacheStore = strapi.plugin('rest-cache').service('cacheStore'); | ||
|
||
if (pluginOption.strategy.debug === true) { | ||
debug.enable('strapi:strapi-plugin-rest-cache'); | ||
} | ||
|
||
const strategy = resolveUserStrategy(strapi, pluginOption.strategy); | ||
strapi.config.set('plugin.rest-cache', { | ||
...pluginOption, | ||
strategy, | ||
}); | ||
|
||
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(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
register, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be moved back to bootstrap currently it will try to run this before the provider exists:
We can still register the middlewares but we can't create the provider until we get to the bootstrap.