Skip to content

Commit

Permalink
feat: improves plugin writability
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Jul 29, 2021
1 parent dbc4115 commit a002b71
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/plugins/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export default config;

Payload Plugins are executed _after_ the incoming config is validated, sanitized, and default options are merged in.

However, after all plugins are executed, the full config with all plugins will be re-sanitized.

## Simple example

Here is an example for how to automatically add a `lastModifiedBy` field to all Payload collections using a Plugin written in TypeScript.
Expand Down
3 changes: 2 additions & 1 deletion src/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export function buildConfig(config: Config): SanitizedConfig {
const sanitized = sanitize(config);

if (Array.isArray(config.plugins)) {
return sanitized.plugins.reduce((configWithPlugins, plugin) => plugin(configWithPlugins), sanitized);
const configWithPlugins = sanitized.plugins.reduce((updatedConfig, plugin) => plugin(updatedConfig), sanitized);
return sanitize(configWithPlugins);
}

return sanitized;
Expand Down
2 changes: 1 addition & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Email = {
}

// eslint-disable-next-line no-use-before-define
type Plugin = (config: SanitizedConfig) => SanitizedConfig;
type Plugin = (config: Config) => Config;

type GeneratePreviewURLOptions = {
locale: string
Expand Down

0 comments on commit a002b71

Please sign in to comment.