Browse Source

docs: Update custom strategy in plugins guide

David Höck 7 months ago
parent
commit
e3f4d2e66c

+ 13 - 12
docs/docs/guides/developer-guide/custom-strategies-in-plugins/index.mdx

@@ -42,24 +42,23 @@ Create a default implementation that users can extend or replace:
 ```ts title="src/strategies/default-my-custom-strategy.ts"
 ```ts title="src/strategies/default-my-custom-strategy.ts"
 import { Injector, RequestContext, Logger } from '@vendure/core';
 import { Injector, RequestContext, Logger } from '@vendure/core';
 import { MyCustomStrategy } from './my-custom-strategy';
 import { MyCustomStrategy } from './my-custom-strategy';
-import { MyPluginService } from '../services/my-plugin.service';
+import { SomeOtherService } from '../services/some-other.service';
+import { loggerCtx } from '../constants';
 
 
 export class DefaultMyCustomStrategy implements MyCustomStrategy {
 export class DefaultMyCustomStrategy implements MyCustomStrategy {
-    private myPluginService: MyPluginService;
-    private logger: Logger;
+    private someOtherService: SomeOtherService;
 
 
     async init(injector: Injector): Promise<void> {
     async init(injector: Injector): Promise<void> {
         // Inject dependencies during the init phase
         // Inject dependencies during the init phase
-        this.myPluginService = injector.get(MyPluginService);
-        this.logger = injector.get(Logger);
+        this.someOtherService = injector.get(SomeOtherService);
 
 
         // Perform any setup logic
         // Perform any setup logic
-        this.logger.info('DefaultMyCustomStrategy initialized');
+        Logger.info('DefaultMyCustomStrategy initialized', loggerCtx);
     }
     }
 
 
     async destroy(): Promise<void> {
     async destroy(): Promise<void> {
         // Clean up resources if needed
         // Clean up resources if needed
-        this.logger.info('DefaultMyCustomStrategy destroyed');
+        Logger.info('DefaultMyCustomStrategy destroyed', loggerCtx);
     }
     }
 
 
     async processData(ctx: RequestContext, data: any): Promise<string> {
     async processData(ctx: RequestContext, data: any): Promise<string> {
@@ -69,7 +68,8 @@ export class DefaultMyCustomStrategy implements MyCustomStrategy {
         }
         }
 
 
         // Use injected service to process data
         // Use injected service to process data
-        const result = await this.myPluginService.processData(ctx, data);
+        const result = await this.someOtherService.doSomething(ctx, data);
+        // ... do something with the result
         return result;
         return result;
     }
     }
 
 
@@ -113,11 +113,13 @@ import { MY_PLUGIN_OPTIONS } from './constants';
 import { MyPluginInitOptions } from './types';
 import { MyPluginInitOptions } from './types';
 import { DefaultMyCustomStrategy } from './strategies/default-my-custom-strategy';
 import { DefaultMyCustomStrategy } from './strategies/default-my-custom-strategy';
 import { MyPluginService } from './services/my-plugin.service';
 import { MyPluginService } from './services/my-plugin.service';
+import { SomeOtherService } from './services/some-other.service';
 
 
 @VendurePlugin({
 @VendurePlugin({
     imports: [PluginCommonModule],
     imports: [PluginCommonModule],
     providers: [
     providers: [
         MyPluginService,
         MyPluginService,
+        SomeOtherService,
         {
         {
             provide: MY_PLUGIN_OPTIONS,
             provide: MY_PLUGIN_OPTIONS,
             useFactory: () => MyPlugin.options,
             useFactory: () => MyPlugin.options,
@@ -202,18 +204,17 @@ Plugin users can now provide their own strategy implementations:
 import { Injector, RequestContext, Logger } from '@vendure/core';
 import { Injector, RequestContext, Logger } from '@vendure/core';
 import { MyCustomStrategy } from '@my-org/my-plugin';
 import { MyCustomStrategy } from '@my-org/my-plugin';
 import { ExternalApiService } from './external-api.service';
 import { ExternalApiService } from './external-api.service';
+import { loggerCtx } from '../constants';
 
 
 export class CustomProcessingStrategy implements MyCustomStrategy {
 export class CustomProcessingStrategy implements MyCustomStrategy {
     private externalApi: ExternalApiService;
     private externalApi: ExternalApiService;
-    private logger: Logger;
 
 
     async init(injector: Injector): Promise<void> {
     async init(injector: Injector): Promise<void> {
         this.externalApi = injector.get(ExternalApiService);
         this.externalApi = injector.get(ExternalApiService);
-        this.logger = injector.get(Logger);
 
 
         // Initialize external API connection
         // Initialize external API connection
         await this.externalApi.connect();
         await this.externalApi.connect();
-        this.logger.info('Custom processing strategy initialized');
+        Logger.info('Custom processing strategy initialized', loggerCtx);
     }
     }
 
 
     async destroy(): Promise<void> {
     async destroy(): Promise<void> {
@@ -221,7 +222,7 @@ export class CustomProcessingStrategy implements MyCustomStrategy {
         if (this.externalApi) {
         if (this.externalApi) {
             await this.externalApi.disconnect();
             await this.externalApi.disconnect();
         }
         }
-        this.logger.info('Custom processing strategy destroyed');
+        Logger.info('Custom processing strategy destroyed', loggerCtx);
     }
     }
 
 
     async processData(ctx: RequestContext, data: any): Promise<string> {
     async processData(ctx: RequestContext, data: any): Promise<string> {