|
2 | 2 |
|
3 | 3 | A TypeScript library for metering API calls with Stripe's usage-based billing capabilities. This library provides a reusable, framework-agnostic solution for tracking API usage and reporting it to Stripe for billing purposes.
|
4 | 4 |
|
| 5 | +## Architecture |
| 6 | + |
| 7 | +```mermaid |
| 8 | +classDiagram |
| 9 | + %% Interfaces |
| 10 | + class IMeteringStrategy { |
| 11 | + <<interface>> |
| 12 | + +recordUsage(customerId: string, usageValue: number, context?: any) Promise~void~ |
| 13 | + +dispose() Promise~void~ |
| 14 | + } |
| 15 | +
|
| 16 | + %% Main Classes |
| 17 | + class MeteringService { |
| 18 | + -strategy: IMeteringStrategy |
| 19 | + +recordApiCall(customerId: string, usageValue?: number, apiEndpoint?: string) Promise~void~ |
| 20 | + +dispose() Promise~void~ |
| 21 | + } |
| 22 | +
|
| 23 | + class MeteringServiceFactory { |
| 24 | + <<static>> |
| 25 | + +createService(config: MeteringServiceConfig) MeteringService |
| 26 | + } |
| 27 | +
|
| 28 | + %% Strategies |
| 29 | + class BatchedUsageRecordStrategy { |
| 30 | + -stripeClient: Stripe |
| 31 | + -batches: Map~string, UsageRecord[]~ |
| 32 | + -intervalId: NodeJS.Timeout |
| 33 | + +recordUsage(customerId: string, usageValue: number) Promise~void~ |
| 34 | + +dispose() Promise~void~ |
| 35 | + -flushAllBatches() Promise~void~ |
| 36 | + -flushBatch(batch: UsageRecord[]) Promise~void~ |
| 37 | + } |
| 38 | +
|
| 39 | + class ImmediateMeterEventStrategy { |
| 40 | + -stripeClient: Stripe |
| 41 | + -meterEventName: string |
| 42 | + -idempotencyKeyPrefix: string |
| 43 | + +recordUsage(customerId: string, usageValue: number, context?: any) Promise~void~ |
| 44 | + +dispose() Promise~void~ |
| 45 | + } |
| 46 | +
|
| 47 | + %% Error Classes |
| 48 | + class MeteringError { |
| 49 | + +name: string |
| 50 | + +message: string |
| 51 | + } |
| 52 | +
|
| 53 | + class ConfigurationError { |
| 54 | + +name: string |
| 55 | + +message: string |
| 56 | + } |
| 57 | +
|
| 58 | + class StripeApiError { |
| 59 | + +name: string |
| 60 | + +message: string |
| 61 | + +stripeCode: string |
| 62 | + } |
| 63 | +
|
| 64 | + %% Relationships |
| 65 | + MeteringService --> IMeteringStrategy : uses |
| 66 | + MeteringServiceFactory ..> MeteringService : creates |
| 67 | + BatchedUsageRecordStrategy ..|> IMeteringStrategy : implements |
| 68 | + ImmediateMeterEventStrategy ..|> IMeteringStrategy : implements |
| 69 | + MeteringError <|-- ConfigurationError : extends |
| 70 | + MeteringError <|-- StripeApiError : extends |
| 71 | +``` |
| 72 | + |
5 | 73 | ## Features
|
6 | 74 |
|
7 | 75 | - Framework-agnostic: Works with any JavaScript/TypeScript backend
|
|
0 commit comments