@onlineapps/conn-orch-cookbook
Version:
Complete cookbook toolkit for all services - unified wrapper including core, executor, transformer, and router functionality
230 lines (204 loc) • 6.23 kB
JavaScript
;
/**
* @module @onlineapps/conn-orch-cookbook
* @description Unified cookbook connector for service-wrapper integration.
* This is a THIN WRAPPER around cookbook-* modules, providing a single
* unified interface for all cookbook functionality.
*
* NO DUPLICATION: All implementation is in cookbook-* modules.
* This connector only re-exports and provides compatibility layer.
*
* @see {@link https://github.com/onlineapps/oa-drive/tree/main/shared/connector/conn-orch-cookbook|GitHub Repository}
* @author OA Drive Team
* @license MIT
* @since 2.0.0
*/
// Import from shared cookbook modules - NO LOCAL IMPLEMENTATIONS!
const cookbookCore = require('@onlineapps/cookbook-core');
const cookbookExecutor = require('@onlineapps/cookbook-executor');
const cookbookTransformer = require('@onlineapps/cookbook-transformer');
const cookbookRouter = require('@onlineapps/cookbook-router');
// Re-export all from cookbook-core (parser and validator)
const {
parseCookbookFromFile,
parseCookbookFromFileSync,
parseCookbookFromObject,
validateCookbook,
validateStep,
CookbookValidationError,
validateAllReferences,
validateDependsOnReferences,
validateVariableReferences,
validateErrorHandlerReferences,
CookbookSchema,
loadSchema
} = cookbookCore;
// Re-export all from cookbook-executor
const {
CookbookExecutor,
ContextManager,
StepProcessor,
VariableResolver
} = cookbookExecutor;
// Re-export all from cookbook-transformer
const {
CookbookGenerator,
ApiSpecAnalyzer,
StepTranslator,
ResponseMapper,
createTransformer
} = cookbookTransformer;
// Re-export all from cookbook-router
const {
CookbookRouter,
ServiceDiscovery,
QueueManager,
RetryHandler,
createRouter
} = cookbookRouter;
/**
* Create a cookbook processor with all capabilities
* Factory function that creates a processor with parser, validator, executor, and router
*
* @function createProcessor
* @param {Object} [config={}] - Processor configuration
* @param {Object} [config.executor] - Executor configuration
* @param {Object} [config.router] - Router configuration
* @param {Object} [config.transformer] - Transformer configuration
* @param {Object} [config.mqClient] - Message queue client for router
* @param {Object} [config.registryClient] - Registry client for router
* @returns {Object} Processor instance with process method
*
* @example
* const processor = createProcessor({
* executor: { timeout: 30000 },
* router: { baseUrl: "http://api.example.com" },
* mqClient: mqConnector,
* registryClient: registryConnector
* });
*
* const result = await processor.process(cookbook);
*/
function createProcessor(config = {}) {
const executor = new CookbookExecutor(config.executor);
const router = config.mqClient && config.registryClient
? createRouter(config.mqClient, config.registryClient, config.router)
: null;
const transformer = createTransformer(config.transformer);
const mapper = new ResponseMapper(config.transformer);
return {
/**
* Process a cookbook through the full pipeline
* @param {Object|string} cookbook - Cookbook object or file path
* @param {Object} [context={}] - Execution context
* @returns {Promise<Object>} Execution result
*/
async process(cookbook, context = {}) {
// Parse if string (file path)
if (typeof cookbook === 'string') {
cookbook = await parseCookbookFromFile(cookbook);
}
// Validate
validateCookbook(cookbook);
// Validate all references
validateAllReferences(cookbook);
// Execute with routing and transformation
const executionContext = {
...context,
router,
transformer,
mapper
};
return executor.execute(cookbook, executionContext);
},
// Expose components for direct access
executor,
router,
transformer,
mapper
};
}
/**
* Helper function to execute a single step
* Convenience wrapper around CookbookExecutor
*
* @function executeStep
* @param {Object} step - Step to execute
* @param {Object} [context={}] - Execution context
* @returns {Promise<Object>} Step execution result
*
* @example
* const result = await executeStep({
* id: "step1",
* type: "task",
* service: "api",
* action: "GET",
* endpoint: "/users"
* });
*/
async function executeStep(step, context = {}) {
const executor = new CookbookExecutor();
return executor.executeStep(step, context);
}
/**
* Helper function to execute a complete workflow
* Convenience wrapper around CookbookExecutor
*
* @function executeWorkflow
* @param {Object} cookbook - Cookbook to execute
* @param {Object} [context={}] - Execution context
* @returns {Promise<Object>} Workflow execution result
*
* @example
* const result = await executeWorkflow(cookbook, {
* variables: { userId: 123 }
* });
*/
async function executeWorkflow(cookbook, context = {}) {
const executor = new CookbookExecutor();
return executor.execute(cookbook, context);
}
// Export everything as unified interface
module.exports = {
// Parser functions (from cookbook-core)
parseCookbookFromFile,
parseCookbookFromFileSync,
parseCookbookFromObject,
// Validator functions (from cookbook-core)
validateCookbook,
validateStep,
CookbookValidationError,
// Reference validators (from cookbook-core)
validateAllReferences,
validateDependsOnReferences,
validateVariableReferences,
validateErrorHandlerReferences,
// Schema (from cookbook-core)
CookbookSchema,
loadSchema,
// Executor classes (from cookbook-executor)
CookbookExecutor,
ContextManager,
StepProcessor,
VariableResolver,
// Transformer classes (from cookbook-transformer)
CookbookGenerator,
ApiSpecAnalyzer,
StepTranslator,
ResponseMapper,
createTransformer,
// Router classes (from cookbook-router)
CookbookRouter,
ServiceDiscovery,
QueueManager,
RetryHandler,
createRouter,
// Factory and helper functions
createProcessor,
executeStep,
executeWorkflow,
// Version info
VERSION: '2.0.0',
COMPATIBLE_CORE_VERSION: cookbookCore.VERSION,
COMPATIBLE_SCHEMA_VERSION: cookbookCore.SCHEMA_VERSION
};