universal-agent-memory 0.7.3 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/cli/memory.js +27 -0
  2. package/dist/cli/memory.js.map +1 -1
  3. package/dist/cli/update.d.ts.map +1 -1
  4. package/dist/cli/update.js +2 -2
  5. package/dist/cli/update.js.map +1 -1
  6. package/dist/utils/calculate-average.d.ts +15 -0
  7. package/dist/utils/calculate-average.d.ts.map +1 -0
  8. package/dist/utils/calculate-average.js +21 -0
  9. package/dist/utils/calculate-average.js.map +1 -0
  10. package/dist/utils/config-manager.d.ts +30 -0
  11. package/dist/utils/config-manager.d.ts.map +1 -0
  12. package/dist/utils/config-manager.js +41 -0
  13. package/dist/utils/config-manager.js.map +1 -0
  14. package/dist/utils/fetch-with-retry.d.ts +5 -0
  15. package/dist/utils/fetch-with-retry.d.ts.map +1 -0
  16. package/dist/utils/fetch-with-retry.js +61 -0
  17. package/dist/utils/fetch-with-retry.js.map +1 -0
  18. package/dist/utils/order-processor-refactored.d.ts +126 -0
  19. package/dist/utils/order-processor-refactored.d.ts.map +1 -0
  20. package/dist/utils/order-processor-refactored.js +165 -0
  21. package/dist/utils/order-processor-refactored.js.map +1 -0
  22. package/dist/utils/order-processor-strategy.d.ts +72 -0
  23. package/dist/utils/order-processor-strategy.d.ts.map +1 -0
  24. package/dist/utils/order-processor-strategy.js +158 -0
  25. package/dist/utils/order-processor-strategy.js.map +1 -0
  26. package/dist/utils/order-processor.d.ts +242 -0
  27. package/dist/utils/order-processor.d.ts.map +1 -0
  28. package/dist/utils/order-processor.js +370 -0
  29. package/dist/utils/order-processor.js.map +1 -0
  30. package/dist/utils/rate-limiter-simple.d.ts +58 -0
  31. package/dist/utils/rate-limiter-simple.d.ts.map +1 -0
  32. package/dist/utils/rate-limiter-simple.js +100 -0
  33. package/dist/utils/rate-limiter-simple.js.map +1 -0
  34. package/package.json +1 -1
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Order Processing - Refactored using SOLID Principles
3
+ *
4
+ * This implementation follows:
5
+ * - Single Responsibility Principle: Each strategy handles one order type
6
+ * - Open/Closed Principle: New order types can be added without modifying existing code
7
+ * - Liskov Substitution Principle: All strategies are interchangeable
8
+ * - Interface Segregation Principle: Focused interfaces for specific behaviors
9
+ * - Dependency Inversion Principle: High-level module depends on abstractions
10
+ */
11
+ // ============================================================================
12
+ // Strategy Implementations (Single Responsibility Principle)
13
+ // ============================================================================
14
+ /**
15
+ * Strategy for processing digital orders
16
+ */
17
+ export class DigitalOrderStrategy {
18
+ canHandle(orderType) {
19
+ return orderType === 'digital';
20
+ }
21
+ process(order, logger) {
22
+ logger.log('Sending email with download link');
23
+ const processedOrder = {
24
+ ...order,
25
+ status: 'delivered',
26
+ };
27
+ return {
28
+ success: true,
29
+ order: processedOrder,
30
+ message: 'Digital order delivered via email',
31
+ };
32
+ }
33
+ }
34
+ /**
35
+ * Strategy for processing physical orders
36
+ */
37
+ export class PhysicalOrderStrategy {
38
+ canHandle(orderType) {
39
+ return orderType === 'physical';
40
+ }
41
+ process(order, logger) {
42
+ logger.log('Creating shipping label');
43
+ const processedOrder = {
44
+ ...order,
45
+ status: 'shipped',
46
+ };
47
+ return {
48
+ success: true,
49
+ order: processedOrder,
50
+ message: 'Physical order shipped',
51
+ };
52
+ }
53
+ }
54
+ /**
55
+ * Strategy for processing subscription orders
56
+ */
57
+ export class SubscriptionOrderStrategy {
58
+ canHandle(orderType) {
59
+ return orderType === 'subscription';
60
+ }
61
+ process(order, logger) {
62
+ logger.log('Activating subscription');
63
+ const processedOrder = {
64
+ ...order,
65
+ status: 'active',
66
+ };
67
+ return {
68
+ success: true,
69
+ order: processedOrder,
70
+ message: 'Subscription activated',
71
+ };
72
+ }
73
+ }
74
+ // ============================================================================
75
+ // Default Logger Implementation
76
+ // ============================================================================
77
+ /**
78
+ * Console-based logger implementation
79
+ */
80
+ export class ConsoleLogger {
81
+ log(message) {
82
+ console.log(message);
83
+ }
84
+ }
85
+ // ============================================================================
86
+ // Order Processor (Dependency Inversion Principle)
87
+ // ============================================================================
88
+ /**
89
+ * Main order processor that uses strategies to handle different order types
90
+ */
91
+ export class OrderProcessor {
92
+ strategies;
93
+ logger;
94
+ constructor(strategies, logger) {
95
+ this.strategies = strategies;
96
+ this.logger = logger;
97
+ }
98
+ /**
99
+ * Process an order using the appropriate strategy
100
+ */
101
+ processOrder(order) {
102
+ const strategy = this.strategies.find((s) => s.canHandle(order.type));
103
+ if (!strategy) {
104
+ return {
105
+ success: false,
106
+ order,
107
+ message: `No strategy found for order type: ${order.type}`,
108
+ };
109
+ }
110
+ const result = strategy.process(order, this.logger);
111
+ this.logger.log(`Order processed: ${order.id}`);
112
+ return result;
113
+ }
114
+ }
115
+ // ============================================================================
116
+ // Factory for Easy Setup
117
+ // ============================================================================
118
+ /**
119
+ * Factory function to create a fully configured OrderProcessor
120
+ */
121
+ export function createOrderProcessor(logger) {
122
+ const strategies = [
123
+ new DigitalOrderStrategy(),
124
+ new PhysicalOrderStrategy(),
125
+ new SubscriptionOrderStrategy(),
126
+ ];
127
+ return new OrderProcessor(strategies, logger ?? new ConsoleLogger());
128
+ }
129
+ // ============================================================================
130
+ // Usage Example
131
+ // ============================================================================
132
+ /**
133
+ * Example usage demonstrating the refactored code
134
+ */
135
+ export function exampleUsage() {
136
+ const processor = createOrderProcessor();
137
+ const digitalOrder = {
138
+ id: 'order-001',
139
+ type: 'digital',
140
+ status: 'pending',
141
+ downloadUrl: 'https://example.com/download/abc',
142
+ };
143
+ const physicalOrder = {
144
+ id: 'order-002',
145
+ type: 'physical',
146
+ status: 'pending',
147
+ shippingAddress: '123 Main St, City, Country',
148
+ };
149
+ const subscriptionOrder = {
150
+ id: 'order-003',
151
+ type: 'subscription',
152
+ status: 'pending',
153
+ subscriptionPlan: 'premium-monthly',
154
+ };
155
+ console.log('Processing digital order:');
156
+ const digitalResult = processor.processOrder(digitalOrder);
157
+ console.log('Result:', digitalResult);
158
+ console.log('\nProcessing physical order:');
159
+ const physicalResult = processor.processOrder(physicalOrder);
160
+ console.log('Result:', physicalResult);
161
+ console.log('\nProcessing subscription order:');
162
+ const subscriptionResult = processor.processOrder(subscriptionOrder);
163
+ console.log('Result:', subscriptionResult);
164
+ }
165
+ //# sourceMappingURL=order-processor-refactored.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-processor-refactored.js","sourceRoot":"","sources":["../../src/utils/order-processor-refactored.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA0FH,+EAA+E;AAC/E,6DAA6D;AAC7D,+EAA+E;AAE/E;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAC/B,SAAS,CAAC,SAAoB;QAC5B,OAAO,SAAS,KAAK,SAAS,CAAC;IACjC,CAAC;IAED,OAAO,CAAC,KAAe,EAAE,MAAc;QACrC,MAAM,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;QAC/C,MAAM,cAAc,GAAiB;YACnC,GAAI,KAAsB;YAC1B,MAAM,EAAE,WAAW;SACpB,CAAC;QACF,OAAO;YACL,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,cAAc;YACrB,OAAO,EAAE,mCAAmC;SAC7C,CAAC;IACJ,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAChC,SAAS,CAAC,SAAoB;QAC5B,OAAO,SAAS,KAAK,UAAU,CAAC;IAClC,CAAC;IAED,OAAO,CAAC,KAAe,EAAE,MAAc;QACrC,MAAM,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;QACtC,MAAM,cAAc,GAAkB;YACpC,GAAI,KAAuB;YAC3B,MAAM,EAAE,SAAS;SAClB,CAAC;QACF,OAAO;YACL,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,cAAc;YACrB,OAAO,EAAE,wBAAwB;SAClC,CAAC;IACJ,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,yBAAyB;IACpC,SAAS,CAAC,SAAoB;Q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7D,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;IAEvC,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;IAChD,MAAM,kBAAkB,GAAG,SAAS,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;IACrE,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,kBAAkB,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Strategy Pattern Implementation for Order Processing
3
+ *
4
+ * The Strategy pattern allows defining a family of algorithms, encapsulating each one,
5
+ * and making them interchangeable. This refactoring replaces conditional logic with
6
+ * polymorphic behavior.
7
+ */
8
+ type OrderType = 'digital' | 'physical' | 'subscription';
9
+ interface Order {
10
+ type: OrderType;
11
+ status?: string;
12
+ [key: string]: any;
13
+ }
14
+ /**
15
+ * OrderProcessingStrategy defines the contract for all order processing strategies.
16
+ * Each concrete strategy implements the process method according to its specific logic.
17
+ */
18
+ interface OrderProcessingStrategy {
19
+ process(order: Order): Order;
20
+ }
21
+ /**
22
+ * DigitalOrderStrategy handles digital product orders.
23
+ * Digital orders are immediately delivered upon processing.
24
+ */
25
+ declare class DigitalOrderStrategy implements OrderProcessingStrategy {
26
+ process(order: Order): Order;
27
+ }
28
+ /**
29
+ * PhysicalOrderStrategy handles physical product orders.
30
+ * Physical orders require shipping and physical delivery.
31
+ */
32
+ declare class PhysicalOrderStrategy implements OrderProcessingStrategy {
33
+ process(order: Order): Order;
34
+ private estimateDelivery;
35
+ }
36
+ /**
37
+ * SubscriptionOrderStrategy handles subscription orders.
38
+ * Subscriptions activate immediately and require ongoing billing.
39
+ */
40
+ declare class SubscriptionOrderStrategy implements OrderProcessingStrategy {
41
+ process(order: Order): Order;
42
+ private calculateNextBilling;
43
+ }
44
+ /**
45
+ * OrderProcessor acts as the context that uses strategies to process orders.
46
+ * It maintains a strategy registry and delegates order processing to the appropriate strategy.
47
+ */
48
+ declare class OrderProcessor {
49
+ private strategies;
50
+ constructor();
51
+ /**
52
+ * Process an order using the appropriate strategy based on its type.
53
+ * @param order The order to process
54
+ * @returns The processed order with status and additional metadata
55
+ * @throws Error if no strategy is found for the order type
56
+ */
57
+ processOrder(order: Order): Order;
58
+ /**
59
+ * Register a custom strategy for a specific order type.
60
+ * This allows extending the processor with new order types at runtime.
61
+ * @param orderType The order type this strategy handles
62
+ * @param strategy The strategy to register
63
+ */
64
+ registerStrategy(orderType: string, strategy: OrderProcessingStrategy): void;
65
+ /**
66
+ * Get all registered order types.
67
+ * @returns Array of supported order types
68
+ */
69
+ getSupportedOrderTypes(): OrderType[];
70
+ }
71
+ export { Order, OrderType, OrderProcessingStrategy, DigitalOrderStrategy, PhysicalOrderStrategy, SubscriptionOrderStrategy, OrderProcessor };
72
+ //# sourceMappingURL=order-processor-strategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-processor-strategy.d.ts","sourceRoot":"","sources":["../../src/utils/order-processor-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,KAAK,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,cAAc,CAAC;AAGzD,UAAU,KAAK;IACb,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAMD;;;GAGG;AACH,UAAU,uBAAuB;IAC/B,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC;CAC9B;AAMD;;;GAGG;AACH,cAAM,oBAAqB,YAAW,uBAAuB;IAC3D,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;CAQ7B;AAED;;;GAGG;AACH,cAAM,qBAAsB,YAAW,uBAAuB;IAC5D,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAU5B,OAAO,CAAC,gBAAgB;CAKzB;AAED;;;GAGG;AACH,cAAM,yBAA0B,YAAW,uBAAuB;IAChE,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAW5B,OAAO,CAAC,oBAAoB;CAK7B;AAMD;;;GAGG;AACH,cAAM,cAAc;IAClB,OAAO,CAAC,UAAU,CAA0C;;IAW5D;;;;;OAKG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAUjC;;;;;OAKG;IACH,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,uBAAuB,GAAG,IAAI;IAI5E;;;OAGG;IACH,sBAAsB,IAAI,SAAS,EAAE;CAGtC;AAgDD,OAAO,EACL,KAAK,EACL,SAAS,EACT,uBAAuB,EACvB,oBAAoB,EACpB,qBAAqB,EACrB,yBAAyB,EACzB,cAAc,EACf,CAAC"}
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Strategy Pattern Implementation for Order Processing
3
+ *
4
+ * The Strategy pattern allows defining a family of algorithms, encapsulating each one,
5
+ * and making them interchangeable. This refactoring replaces conditional logic with
6
+ * polymorphic behavior.
7
+ */
8
+ // ============================================================================
9
+ // CONCRETE STRATEGIES
10
+ // ============================================================================
11
+ /**
12
+ * DigitalOrderStrategy handles digital product orders.
13
+ * Digital orders are immediately delivered upon processing.
14
+ */
15
+ class DigitalOrderStrategy {
16
+ process(order) {
17
+ return {
18
+ ...order,
19
+ status: 'delivered',
20
+ deliveryMethod: 'digital',
21
+ deliveryTimestamp: new Date().toISOString()
22
+ };
23
+ }
24
+ }
25
+ /**
26
+ * PhysicalOrderStrategy handles physical product orders.
27
+ * Physical orders require shipping and physical delivery.
28
+ */
29
+ class PhysicalOrderStrategy {
30
+ process(order) {
31
+ return {
32
+ ...order,
33
+ status: 'shipped',
34
+ deliveryMethod: 'physical',
35
+ shippingDate: new Date().toISOString(),
36
+ estimatedDelivery: this.estimateDelivery()
37
+ };
38
+ }
39
+ estimateDelivery() {
40
+ const date = new Date();
41
+ date.setDate(date.getDate() + 5); // 5 business days
42
+ return date.toISOString();
43
+ }
44
+ }
45
+ /**
46
+ * SubscriptionOrderStrategy handles subscription orders.
47
+ * Subscriptions activate immediately and require ongoing billing.
48
+ */
49
+ class SubscriptionOrderStrategy {
50
+ process(order) {
51
+ return {
52
+ ...order,
53
+ status: 'active',
54
+ subscriptionType: 'recurring',
55
+ billingCycle: 'monthly',
56
+ activationDate: new Date().toISOString(),
57
+ nextBilling: this.calculateNextBilling()
58
+ };
59
+ }
60
+ calculateNextBilling() {
61
+ const date = new Date();
62
+ date.setMonth(date.getMonth() + 1);
63
+ return date.toISOString();
64
+ }
65
+ }
66
+ // ============================================================================
67
+ // CONTEXT (ORDER PROCESSOR)
68
+ // ============================================================================
69
+ /**
70
+ * OrderProcessor acts as the context that uses strategies to process orders.
71
+ * It maintains a strategy registry and delegates order processing to the appropriate strategy.
72
+ */
73
+ class OrderProcessor {
74
+ strategies;
75
+ constructor() {
76
+ // Initialize the strategy registry
77
+ this.strategies = new Map([
78
+ ['digital', new DigitalOrderStrategy()],
79
+ ['physical', new PhysicalOrderStrategy()],
80
+ ['subscription', new SubscriptionOrderStrategy()]
81
+ ]);
82
+ }
83
+ /**
84
+ * Process an order using the appropriate strategy based on its type.
85
+ * @param order The order to process
86
+ * @returns The processed order with status and additional metadata
87
+ * @throws Error if no strategy is found for the order type
88
+ */
89
+ processOrder(order) {
90
+ const strategy = this.strategies.get(order.type);
91
+ if (!strategy) {
92
+ throw new Error(`No processing strategy found for order type: ${order.type}`);
93
+ }
94
+ return strategy.process(order);
95
+ }
96
+ /**
97
+ * Register a custom strategy for a specific order type.
98
+ * This allows extending the processor with new order types at runtime.
99
+ * @param orderType The order type this strategy handles
100
+ * @param strategy The strategy to register
101
+ */
102
+ registerStrategy(orderType, strategy) {
103
+ this.strategies.set(orderType, strategy);
104
+ }
105
+ /**
106
+ * Get all registered order types.
107
+ * @returns Array of supported order types
108
+ */
109
+ getSupportedOrderTypes() {
110
+ return Array.from(this.strategies.keys());
111
+ }
112
+ }
113
+ // ============================================================================
114
+ // USAGE EXAMPLE
115
+ // ============================================================================
116
+ /**
117
+ * Example usage demonstrating the Strategy pattern in action.
118
+ */
119
+ function demonstrateOrderProcessing() {
120
+ const processor = new OrderProcessor();
121
+ // Example orders
122
+ const digitalOrder = {
123
+ type: 'digital',
124
+ id: 'DIG-001',
125
+ product: 'Premium Software License',
126
+ price: 99.99
127
+ };
128
+ const physicalOrder = {
129
+ type: 'physical',
130
+ id: 'PHY-001',
131
+ product: 'Office Chair',
132
+ price: 249.99,
133
+ shippingAddress: '123 Main St, City, State'
134
+ };
135
+ const subscriptionOrder = {
136
+ type: 'subscription',
137
+ id: 'SUB-001',
138
+ product: 'Cloud Storage Pro',
139
+ price: 19.99
140
+ };
141
+ // Process orders
142
+ console.log('Processing Digital Order:');
143
+ console.log(JSON.stringify(processor.processOrder(digitalOrder), null, 2));
144
+ console.log('\nProcessing Physical Order:');
145
+ console.log(JSON.stringify(processor.processOrder(physicalOrder), null, 2));
146
+ console.log('\nProcessing Subscription Order:');
147
+ console.log(JSON.stringify(processor.processOrder(subscriptionOrder), null, 2));
148
+ // Show supported order types
149
+ console.log('\nSupported Order Types:', processor.getSupportedOrderTypes());
150
+ }
151
+ // Export for use in other modules
152
+ export { DigitalOrderStrategy, PhysicalOrderStrategy, SubscriptionOrderStrategy, OrderProcessor };
153
+ // Run demo if executed directly
154
+ const isMainModule = import.meta.url === `file://${process.argv[1]}`;
155
+ if (isMainModule) {
156
+ demonstrateOrderProcessing();
157
+ }
158
+ //# sourceMappingURL=order-processor-strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-processor-strategy.js","sourceRoot":"","sources":["../../src/utils/order-processor-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAwBH,+EAA+E;AAC/E,sBAAsB;AACtB,+EAA+E;AAE/E;;;GAGG;AACH,MAAM,oBAAoB;IACxB,OAAO,CAAC,KAAY;QAClB,OAAO;YACL,GAAG,KAAK;YACR,MAAM,EAAE,WAAW;YACnB,cAAc,EAAE,SAAS;YACzB,iBAA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,EAAE,EAAE,SAAS;QACb,OAAO,EAAE,mBAAmB;QAC5B,KAAK,EAAE,KAAK;KACb,CAAC;IAEF,iBAAiB;IACjB,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IACzC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3E,OAAO,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;IAC5C,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,YAAY,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5E,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;IAChD,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,YAAY,CAAC,iBAAiB,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAEhF,6BAA6B;IAC7B,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,SAAS,CAAC,sBAAsB,EAAE,CAAC,CAAC;AAC9E,CAAC;AAED,kCAAkC;AAClC,OAAO,EAIL,oBAAoB,EACpB,qBAAqB,EACrB,yBAAyB,EACzB,cAAc,EACf,CAAC;AAEF,gCAAgC;AAChC,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,UAAU,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;AACrE,IAAI,YAAY,EAAE,CAAC;IACjB,0BAA0B,EAAE,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Order Processing System - SOLID Principles Implementation
3
+ *
4
+ * This module demonstrates a refactored order processing system using:
5
+ * - Single Responsibility Principle: Each strategy handles one order type
6
+ * - Open/Closed Principle: New order types can be added without modifying existing code
7
+ * - Liskov Substitution Principle: All strategies can be used interchangeably
8
+ * - Interface Segregation Principle: Focused interfaces for specific needs
9
+ * - Dependency Inversion Principle: Depends on abstractions (interfaces) not concretions
10
+ */
11
+ import { z } from 'zod';
12
+ /**
13
+ * Order type enum
14
+ */
15
+ export declare enum OrderType {
16
+ DIGITAL = "digital",
17
+ PHYSICAL = "physical",
18
+ SUBSCRIPTION = "subscription"
19
+ }
20
+ /**
21
+ * Order status enum
22
+ */
23
+ export declare enum OrderStatus {
24
+ PENDING = "pending",
25
+ DELIVERED = "delivered",
26
+ SHIPPED = "shipped",
27
+ ACTIVE = "active"
28
+ }
29
+ /**
30
+ * Order interface
31
+ */
32
+ export interface Order {
33
+ id: string;
34
+ type: OrderType;
35
+ status: OrderStatus;
36
+ customerId: string;
37
+ items: Array<{
38
+ productId: string;
39
+ quantity: number;
40
+ price: number;
41
+ }>;
42
+ createdAt: Date;
43
+ updatedAt: Date;
44
+ }
45
+ /**
46
+ * Order processing result interface
47
+ */
48
+ export interface ProcessingResult {
49
+ success: boolean;
50
+ order: Order;
51
+ message: string;
52
+ }
53
+ /**
54
+ * Order validation schema using Zod
55
+ */
56
+ export declare const OrderSchema: z.ZodObject<{
57
+ id: z.ZodString;
58
+ type: z.ZodNativeEnum<typeof OrderType>;
59
+ status: z.ZodDefault<z.ZodNativeEnum<typeof OrderStatus>>;
60
+ customerId: z.ZodString;
61
+ items: z.ZodArray<z.ZodObject<{
62
+ productId: z.ZodString;
63
+ quantity: z.ZodNumber;
64
+ price: z.ZodNumber;
65
+ }, "strip", z.ZodTypeAny, {
66
+ price: number;
67
+ productId: string;
68
+ quantity: number;
69
+ }, {
70
+ price: number;
71
+ productId: string;
72
+ quantity: number;
73
+ }>, "many">;
74
+ createdAt: z.ZodDate;
75
+ updatedAt: z.ZodDate;
76
+ }, "strip", z.ZodTypeAny, {
77
+ type: OrderType;
78
+ status: OrderStatus;
79
+ id: string;
80
+ createdAt: Date;
81
+ customerId: string;
82
+ items: {
83
+ price: number;
84
+ productId: string;
85
+ quantity: number;
86
+ }[];
87
+ updatedAt: Date;
88
+ }, {
89
+ type: OrderType;
90
+ id: string;
91
+ createdAt: Date;
92
+ customerId: string;
93
+ items: {
94
+ price: number;
95
+ productId: string;
96
+ quantity: number;
97
+ }[];
98
+ updatedAt: Date;
99
+ status?: OrderStatus | undefined;
100
+ }>;
101
+ /**
102
+ * OrderProcessor strategy interface
103
+ * All order processors must implement this interface
104
+ */
105
+ export interface OrderProcessor {
106
+ /**
107
+ * Process an order according to its type
108
+ * @param order - The order to process
109
+ * @returns Processing result with updated order status
110
+ * @throws {Error} If processing fails
111
+ */
112
+ process(order: Order): Promise<ProcessingResult>;
113
+ /**
114
+ * Validate that the order can be processed
115
+ * @param order - The order to validate
116
+ * @returns true if valid, false otherwise
117
+ */
118
+ validate(order: Order): boolean;
119
+ }
120
+ /**
121
+ * Digital order processor strategy
122
+ * Handles delivery of digital products via email
123
+ */
124
+ export declare class DigitalOrderProcessor implements OrderProcessor {
125
+ /**
126
+ * Process a digital order
127
+ * @param order - The digital order to process
128
+ * @returns Processing result with delivered status
129
+ * @throws {Error} If email delivery fails
130
+ */
131
+ process(order: Order): Promise<ProcessingResult>;
132
+ /**
133
+ * Validate digital order requirements
134
+ * @param order - The order to validate
135
+ * @returns true if valid for digital processing
136
+ */
137
+ validate(order: Order): boolean;
138
+ /**
139
+ * Send download link email to customer
140
+ * @param order - The order containing customer information
141
+ * @throws {Error} If email cannot be sent
142
+ */
143
+ private sendDownloadLinkEmail;
144
+ }
145
+ /**
146
+ * Physical order processor strategy
147
+ * Handles shipping of physical products
148
+ */
149
+ export declare class PhysicalOrderProcessor implements OrderProcessor {
150
+ /**
151
+ * Process a physical order
152
+ * @param order - The physical order to process
153
+ * @returns Processing result with shipped status
154
+ * @throws {Error} If shipping label creation fails
155
+ */
156
+ process(order: Order): Promise<ProcessingResult>;
157
+ /**
158
+ * Validate physical order requirements
159
+ * @param order - The order to validate
160
+ * @returns true if valid for physical processing
161
+ */
162
+ validate(order: Order): boolean;
163
+ /**
164
+ * Create shipping label for the order
165
+ * @param order - The order to create label for
166
+ * @throws {Error} If shipping label cannot be created
167
+ */
168
+ private createShippingLabel;
169
+ }
170
+ /**
171
+ * Subscription order processor strategy
172
+ * Handles activation of subscription services
173
+ */
174
+ export declare class SubscriptionOrderProcessor implements OrderProcessor {
175
+ /**
176
+ * Process a subscription order
177
+ * @param order - The subscription order to process
178
+ * @returns Processing result with active status
179
+ * @throws {Error} If subscription activation fails
180
+ */
181
+ process(order: Order): Promise<ProcessingResult>;
182
+ /**
183
+ * Validate subscription order requirements
184
+ * @param order - The order to validate
185
+ * @returns true if valid for subscription processing
186
+ */
187
+ validate(order: Order): boolean;
188
+ /**
189
+ * Activate subscription service
190
+ * @param order - The order containing subscription details
191
+ * @throws {Error} If subscription cannot be activated
192
+ */
193
+ private activateSubscription;
194
+ }
195
+ /**
196
+ * OrderProcessor factory
197
+ * Responsible for selecting the appropriate processor based on order type
198
+ * Follows Factory pattern for dependency injection
199
+ */
200
+ export declare class OrderProcessorFactory {
201
+ private readonly processors;
202
+ constructor();
203
+ /**
204
+ * Get the appropriate processor for the given order type
205
+ * @param orderType - The type of order
206
+ * @returns The appropriate order processor
207
+ * @throws {Error} If no processor is found for the order type
208
+ */
209
+ getProcessor(orderType: OrderType): OrderProcessor;
210
+ /**
211
+ * Register a new processor for a specific order type
212
+ * @param orderType - The order type to handle
213
+ * @param processor - The processor implementation
214
+ */
215
+ registerProcessor(orderType: OrderType, processor: OrderProcessor): void;
216
+ /**
217
+ * Check if a processor is registered for the given order type
218
+ * @param orderType - The order type to check
219
+ * @returns true if a processor exists
220
+ */
221
+ hasProcessor(orderType: OrderType): boolean;
222
+ }
223
+ /**
224
+ * OrderProcessingService
225
+ * Main service that coordinates order processing using the appropriate strategy
226
+ */
227
+ export declare class OrderProcessingService {
228
+ private readonly processorFactory;
229
+ constructor(processorFactory?: OrderProcessorFactory);
230
+ /**
231
+ * Process an order using the appropriate strategy
232
+ * @param orderInput - The order data to process (may be unvalidated)
233
+ * @returns Processing result with updated order
234
+ * @throws {Error} If validation fails or processing encounters an error
235
+ */
236
+ processOrder(orderInput: unknown): Promise<ProcessingResult>;
237
+ }
238
+ /**
239
+ * Example usage demonstrating the refactored code
240
+ */
241
+ export declare function exampleUsage(): Promise<void>;
242
+ //# sourceMappingURL=order-processor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-processor.d.ts","sourceRoot":"","sources":["../../src/utils/order-processor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,oBAAY,SAAS;IACnB,OAAO,YAAY;IACnB,QAAQ,aAAa;IACrB,YAAY,iBAAiB;CAC9B;AAED;;GAEG;AACH,oBAAY,WAAW;IACrB,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,OAAO,YAAY;IACnB,MAAM,WAAW;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,WAAW,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;QACX,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;KACf,CAAC,CAAC;IACH,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EActB,CAAC;AAEH;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;O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