@ductape/sdk 0.0.4-v54 → 0.0.4-v56

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.
@@ -2,8 +2,11 @@
2
2
  * Quota Service
3
3
  *
4
4
  * Code-first API for defining and managing quotas with weighted distribution.
5
+ * Uses ProductBuilder for CRUD operations and ProcessorService for execution.
6
+ * Implements health-aware weighted selection with minimal latency.
5
7
  */
6
8
  import { IQuotaDefineOptions, IQuotaSchema, IQuotaRunOptions, IQuotaDispatchOptions, IDefinedQuota, IResilienceServiceConfig } from './types';
9
+ import { IProductQuota } from '../types/productsBuilder.types';
7
10
  /**
8
11
  * Error class for quota operations
9
12
  */
@@ -13,75 +16,61 @@ export declare class QuotaError extends Error {
13
16
  constructor(message: string, code: string, details?: unknown);
14
17
  }
15
18
  /**
16
- * Quota Service
17
- *
18
- * Provides code-first API for defining, creating, and managing quotas.
19
+ * Result of a quota run with provider information
19
20
  */
21
+ export interface IQuotaRunResult<T = unknown> {
22
+ data: T;
23
+ provider: string;
24
+ latency: number;
25
+ wasHealthy: boolean;
26
+ retriesUsed: number;
27
+ }
20
28
  export declare class QuotaService {
21
- private apiService;
29
+ private config;
30
+ private productBuilders;
31
+ private processorCache;
22
32
  constructor(config: IResilienceServiceConfig);
23
33
  /**
24
- * Define a quota using code-first API
25
- *
26
- * @example
27
- * ```ts
28
- * const quota = await quotaService.define({
29
- * product: 'my-product',
30
- * tag: 'sms-quota',
31
- * name: 'SMS Provider Quota',
32
- * input: {
33
- * phone: { type: 'string', required: true },
34
- * message: { type: 'string', required: true },
35
- * },
36
- * handler: async (ctx) => {
37
- * ctx.provider('twilio')
38
- * .weight(70)
39
- * .healthcheck('twilio-health')
40
- * .app('twilio-app')
41
- * .action('send-sms')
42
- * .mapInput((input) => ({ body: { to: input.phone, body: input.message } }))
43
- * .mapOutput((res) => ({ messageId: res.sid }))
44
- * .retries(2);
45
- *
46
- * ctx.provider('nexmo')
47
- * .weight(30)
48
- * .healthcheck('nexmo-health')
49
- * .app('nexmo-app')
50
- * .action('send-sms')
51
- * .mapInput((input) => ({ body: { to: input.phone, text: input.message } }))
52
- * .mapOutput((res) => ({ messageId: res['message-id'] }))
53
- * .retries(2);
54
- * },
55
- * });
56
- * ```
34
+ * Gets or creates a cached ProductBuilder
57
35
  */
58
- define<TInput = Record<string, unknown>>(options: IQuotaDefineOptions<TInput>): Promise<IDefinedQuota>;
36
+ private getProductBuilder;
59
37
  /**
60
- * Create a quota from a defined quota or schema
38
+ * Gets or creates a cached ProcessorService for minimal latency
61
39
  */
62
- create(product: string, quota: IDefinedQuota | IQuotaSchema): Promise<IQuotaSchema>;
63
- /**
64
- * Update an existing quota
65
- */
66
- update<TInput = Record<string, unknown>>(product: string, tag: string, quota: Partial<IQuotaDefineOptions<TInput>> | Partial<IQuotaSchema>): Promise<IQuotaSchema>;
40
+ private getProcessor;
41
+ define<TInput = Record<string, unknown>>(options: IQuotaDefineOptions<TInput>): Promise<IDefinedQuota>;
42
+ create(product: string, quota: IDefinedQuota | IQuotaSchema): Promise<IProductQuota>;
43
+ fetch(product: string, tag: string): Promise<IProductQuota | null>;
44
+ fetchAll(product: string): Promise<IProductQuota[]>;
45
+ update(product: string, tag: string, data: Partial<IProductQuota>): Promise<void>;
46
+ delete(product: string, tag: string): Promise<void>;
67
47
  /**
68
- * Fetch a quota by tag
48
+ * Run a quota with health-aware weighted provider selection
49
+ *
50
+ * Optimization strategies:
51
+ * 1. Parallel health status lookups from Redis (O(1) per provider)
52
+ * 2. Cached processor instances to avoid re-initialization
53
+ * 3. Pre-computed weighted selection for O(n) provider selection
54
+ * 4. Exponential backoff only on failures
69
55
  */
70
- fetch(product: string, tag: string): Promise<IQuotaSchema>;
56
+ run<TOutput = unknown>(options: IQuotaRunOptions): Promise<IQuotaRunResult<TOutput>>;
71
57
  /**
72
- * Fetch all quotas for a product
58
+ * Parallel health status lookup from Redis - O(n) with parallel I/O
73
59
  */
74
- fetchAll(product: string): Promise<IQuotaSchema[]>;
60
+ private getHealthStatusesParallel;
75
61
  /**
76
- * Delete a quota
62
+ * Fast weighted selection with health-aware prioritization
77
63
  */
78
- delete(product: string, tag: string): Promise<void>;
64
+ private selectHealthyProvider;
79
65
  /**
80
- * Run a quota
66
+ * Execute provider action with exponential backoff retries
81
67
  */
82
- run<TOutput = unknown>(options: IQuotaRunOptions): Promise<TOutput>;
68
+ private executeWithRetries;
83
69
  /**
84
- * Dispatch/schedule a quota for later execution
70
+ * Dispatch quota for deferred execution via job queue
71
+ *
72
+ * Note: For immediate execution, use run() instead.
73
+ * Dispatch schedules the quota for background processing.
85
74
  */
86
75
  dispatch(options: IQuotaDispatchOptions): Promise<{
87
76
  jobId: string;