import type { Hook } from '@oclif/core'; import type { B2CInstance } from '../instance/index.js'; import type { Logger } from '../logging/index.js'; /** * Types of B2C operations that support lifecycle hooks. */ export type B2COperationType = 'job:run' | 'job:import' | 'job:export' | 'code:deploy' | 'code:download' | 'code:activate' | 'site-archive:import' | 'site-archive:export' | 'cap:install' | 'cap:uninstall'; /** * Context provided to lifecycle hooks for a B2C operation. * * Includes the B2CInstance so plugins can access API clients and configuration * without needing to construct their own instance. */ export interface B2COperationContext { /** Type of operation being executed */ operationType: B2COperationType; /** Unique ID for this operation invocation */ operationId: string; /** B2C instance with configured API clients */ instance: B2CInstance; /** Start timestamp */ startTime: number; /** Operation-specific metadata (jobId, codeVersion, parameters, etc.) */ metadata: Record; } /** * Result returned by a beforeOperation hook. */ export interface BeforeB2COperationResult { /** Set to true to skip the operation */ skip?: boolean; /** Reason for skipping (logged to user) */ skipReason?: string; /** Modified context to pass through to afterOperation */ context?: Partial; } /** * Result of a B2C operation execution. */ export interface B2COperationResult { /** Whether the operation succeeded */ success: boolean; /** Error if operation failed */ error?: Error; /** Duration in milliseconds */ duration: number; /** Operation-specific result data */ data?: unknown; } /** * Result returned by an afterOperation hook. */ export interface AfterB2COperationResult { /** Additional metadata to include */ metadata?: Record; } /** * Provider interface for B2C operation lifecycle hooks. * * Plugins implement this interface to observe and control B2C operation execution. * The context includes the B2CInstance, giving plugins access to: * - `context.instance.ocapi` - OCAPI client for API calls * - `context.instance.webdav` - WebDAV client for file operations * - `context.instance.config` - Resolved configuration (hostname, credentials, etc.) */ export interface B2COperationLifecycleProvider { /** Human-readable name for the provider (used in logging/debugging) */ readonly name: string; /** * Called before an operation executes. * * Can return `{ skip: true }` to prevent the operation from executing. * * @param context - Operation context with B2CInstance and metadata * @returns Optional result to skip or modify the operation */ beforeOperation?(context: B2COperationContext): Promise; /** * Called after an operation completes (success or failure). * * @param context - Operation context with B2CInstance and metadata * @param result - Operation result with success/failure info * @returns Optional result with additional metadata */ afterOperation?(context: B2COperationContext, result: B2COperationResult): Promise; } /** * Options passed to the `b2c:operation-lifecycle` hook. */ export interface B2COperationLifecycleHookOptions { /** * All parsed CLI flags from the current command. */ flags?: Record; /** Index signature for oclif hook compatibility */ [key: string]: unknown; } /** * Result returned by the `b2c:operation-lifecycle` hook. */ export interface B2COperationLifecycleHookResult { /** Lifecycle providers to register */ providers: B2COperationLifecycleProvider[]; } /** * Hook type for `b2c:operation-lifecycle`. * * Implement this hook in your oclif plugin to receive B2C operation lifecycle events * for jobs, deployments, and other B2C Commerce operations. * * ## Plugin Registration * * Register the hook in your plugin's package.json: * * ```json * { * "oclif": { * "hooks": { * "b2c:operation-lifecycle": "./dist/hooks/operation-lifecycle.js" * } * } * } * ``` * * @example * ```typescript * import type { B2COperationLifecycleHook } from '@salesforce/b2c-tooling-sdk/cli'; * * const hook: B2COperationLifecycleHook = async function(options) { * return { * providers: [{ * name: 'my-audit-provider', * async beforeOperation(context) { * // Access context.instance for API calls * // Log or check policies before operation * }, * async afterOperation(context, result) { * // Log results, send notifications, etc. * }, * }], * }; * }; * * export default hook; * ``` */ export type B2COperationLifecycleHook = Hook<'b2c:operation-lifecycle'>; /** * Creates a new B2C operation context for lifecycle hooks. * * @param operationType - Type of B2C operation * @param metadata - Operation-specific metadata * @param instance - B2C instance with configured clients * @returns New operation context */ export declare function createB2COperationContext(operationType: B2COperationType, metadata: Record, instance: B2CInstance): B2COperationContext; /** * Helper class for running B2C lifecycle hooks in CLI commands. * * This class is used internally by CLI commands to collect and invoke * lifecycle providers from plugins. */ export declare class B2CLifecycleRunner { private providers; private logger?; constructor(logger?: Logger); /** * Adds providers to this runner. */ addProviders(providers: B2COperationLifecycleProvider[]): void; /** * Runs beforeOperation hooks for all providers. * * @param context - Operation context * @returns Aggregated result (skip if any provider requests skip) */ runBefore(context: B2COperationContext): Promise; /** * Runs afterOperation hooks for all providers. * * @param context - Operation context * @param result - Operation result */ runAfter(context: B2COperationContext, result: B2COperationResult): Promise; /** * Returns the number of registered providers. */ get size(): number; }