Skip to main content

Custom Strategies in Plugins

When building Vendure plugins, you often need to provide extensible, pluggable implementations for specific features. The strategy pattern is the perfect tool for this, allowing plugin users to customize behavior by providing their own implementations.

This guide shows you how to implement custom strategies in your plugins, following Vendure's established patterns and best practices.

Overview

A strategy in Vendure is a way to provide a pluggable implementation of a particular feature. Custom strategies in plugins allow users to:

  • Override default behavior with their own implementations
  • Inject dependencies and services through the init() lifecycle method
  • Clean up resources using the destroy() lifecycle method
  • Configure the strategy through the plugin's init options

Creating a Strategy Interface

First, define the interface that your strategy must implement. All strategy interfaces should extend InjectableStrategy to support dependency injection and lifecycle methods.

src/strategies/my-custom-strategy.ts

Implementing a Default Strategy

Create a default implementation that users can extend or replace:

src/strategies/default-my-custom-strategy.ts

Adding Strategy to Plugin Options

Define your plugin's initialization options to include the strategy:

src/types.ts

Configuring the Plugin

In your plugin definition, provide the default strategy and allow users to override it:

src/my-plugin.ts

Using the Strategy in Services

Access the strategy through dependency injection in your services:

src/services/my-plugin.service.ts

User Implementation Example

Plugin users can now provide their own strategy implementations:

src/my-custom-implementation.ts

Plugin Configuration by Users

Users configure the plugin with their custom strategy:

vendure-config.ts

Strategy with Options

You can also create strategies that accept configuration options:

src/strategies/configurable-strategy.ts

Usage:

vendure-config.ts

Multiple Strategies in One Plugin

For complex plugins, you might need multiple strategies:

src/types.ts
src/complex-plugin.ts

Best Practices

1. Always Extend InjectableStrategy

Ts

2. Provide Sensible Defaults

Always provide a default implementation so users can use your plugin out-of-the-box:

Ts

3. Handle Lifecycle Properly

Always implement proper init/destroy handling in your plugin:

Ts

4. Use TypeScript for Better DX

Provide strong typing for better developer experience:

Ts

5. Document Your Strategy Interface

Provide comprehensive JSDoc comments:

Ts

Summary

Custom strategies in plugins provide a powerful way to make your plugins extensible and configurable. By following the patterns outlined in this guide, you can:

  • Define clear strategy interfaces that extend InjectableStrategy
  • Provide default implementations that work out-of-the-box
  • Allow users to inject dependencies through the init() method
  • Properly manage strategy lifecycle with init() and destroy() methods
  • Enable users to provide their own implementations
  • Support configuration options for strategies

This approach ensures your plugins are flexible, maintainable, and follow Vendure's established conventions.

Was this chapter helpful?
Report Issue
Edited Feb 23, 2026ยทEdit this page