服务端 API:配置(Configuration)
页面摘要: 服务端 API 暴露一个带有
default属性和validator函数的config对象。Strapi 将默认值与用户的config/plugins文件进行深合并,然后在插件加载前运行验证。在运行时使用strapi.plugin('my-plugin').config('key')读取配置。
一个插件可以从其 服务端入口文件 暴露一个 config 对象。该对象定义默认配置值,并验证从应用的 config/plugins.js|ts 文件加载的任何用户覆盖值。
在深入阅读本页概念之前,请确保你已经:
- 创建了一个 Strapi 插件,
- 阅读并理解了 Server API 的基础知识。
配置形态
config 对象接受 2 个属性:
| Property | Type | Description |
|---|---|---|
default | Object,或返回 Object 的 Function | 插件的默认配置值。使用深合并与用户配置合并(用户值优先)。 |
validator | Function | 接收合并后的配置对象,如果结果无效则必须抛出错误。 |
配置加载
当 Strapi 加载一个插件时,它会应用以下顺序:
| Step | What Strapi does | Notes |
|---|---|---|
| 1 | 计算默认配置 | 如果 default 是一个函数,则使用 { env } 调用它。否则直接使用该对象。 |
| 2 | 与用户配置深合并 | 来自 config/plugins.js\|ts 的值优先于默认值。 |
| 3 | 运行 validator(mergedConfig) | 如果验证失败则抛出带有上下文的错误,停止启动。 |
| 4 | 存储最终配置 | 可通过 strapi.plugin('my-plugin').config('key') 在插件实例上访问。 |
传递给 default 函数的 { env } 参数与 Strapi 配置文件中使用的 env 工具相同。它读取 process.env 值并支持类型转换:env('MY_VAR')、env.int('PORT', 3000)、env.bool('ENABLED', true) 等。
配置示例
JavaScript
'use strict';
module.exports = {
default: ({ env }) => ({
enabled: true,
maxItems: env.int('MY_PLUGIN_MAX_ITEMS', 10),
endpoint: env('MY_PLUGIN_ENDPOINT', 'https://api.example.com'),
}),
validator: (config) => {
if (typeof config.enabled !== 'boolean') {
throw new Error('"enabled" must be a boolean');
}
if (typeof config.maxItems !== 'number' || config.maxItems < 1) {
throw new Error('"maxItems" must be a positive number');
}
},
};
TypeScript
export default {
default: ({ env }: { env: any }) => ({
enabled: true,
maxItems: env.int('MY_PLUGIN_MAX_ITEMS', 10),
endpoint: env('MY_PLUGIN_ENDPOINT', 'https://api.example.com'),
}),
validator: (config: { enabled: unknown; maxItems: unknown }) => {
if (typeof config.enabled !== 'boolean') {
throw new Error('"enabled" must be a boolean');
}
if (typeof config.maxItems !== 'number' || config.maxItems < 1) {
throw new Error('"maxItems" must be a positive number');
}
},
};
用户可以在应用的插件配置文件中覆盖这些值:
JavaScript
module.exports = {
'my-plugin': {
enabled: true,
config: {
maxItems: 25,
endpoint: 'https://api.production.example.com',
},
},
};
TypeScript
export default {
'my-plugin': {
enabled: true,
config: {
maxItems: 25,
endpoint: 'https://api.production.example.com',
},
},
};
在将默认值与用户覆盖进行深合并之后,最终配置为 { enabled: true, maxItems: 25, endpoint: 'https://api.production.example.com' }。
运行时访问
插件加载后,只要可以访问 strapi 对象,其配置就随处可用:
// Read one key
const maxItems = strapi.plugin('my-plugin').config('maxItems');
// Read the entire plugin config object
const pluginConfig = strapi.config.get('plugin::my-plugin');
strapi.plugin().config() 和 strapi.config.get() 通常在生命周期函数、控制器或服务中使用。
使用 yarn strapi console 或 npm run strapi console 来检查运行中的 Strapi 实例的实时配置。
最佳实践
-
始终提供
default。 没有默认值的插件会迫使每个用户都提供所有配置值,这会造成困扰。让每个选项都是可选的,并提供一个合理的默认值。 -
对环境感知的配置使用
default的函数形式。({ env }) => ({...})形式让用户可以仅通过环境变量来驱动配置,而无需任何额外设置。纯对象形式适用于真正静态的默认值。 -
保持验证简单且明确。
validator在启动时、在任何请求被处理之前运行。抛出描述性错误,以便运维人员确切知道哪里出了问题。例如,'"maxItems" must be a positive number'比'Invalid config'更有用。 -
不要在插件配置中存储密钥。 插件配置可通过
strapi.config在服务端访问,如果处理不当,可能会通过日志、调试工具或自定义端点无意中暴露。应在服务中直接使用环境变量,或在default中通过env辅助函数读取这些值,而不是将原始凭据嵌入配置对象中。 -
在 services 中读取配置,而非内联读取。 在服务方法内部(而不是在模块加载时)访问
strapi.plugin('my-plugin').config('key'),可确保该值始终是最终合并后的值,而不是在用户覆盖应用之前拍下的快照。