@parsrun/service-adapters 0.1.0

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.
@@ -0,0 +1,782 @@
1
+ import * as _parsrun_service from '@parsrun/service';
2
+ import { createMemoryEventTransport, RpcServer, EventEmitter, ServiceClientOptions } from '@parsrun/service';
3
+ import { Logger } from '@parsrun/core';
4
+
5
+ /**
6
+ * @parsrun/service-adapters - Payments Service Definition
7
+ */
8
+ /**
9
+ * Payments Service Definition
10
+ *
11
+ * Provides payment processing, billing, usage tracking,
12
+ * and dunning capabilities as a microservice.
13
+ */
14
+ declare const paymentsServiceDefinition: _parsrun_service.ServiceDefinition<{
15
+ /**
16
+ * Get subscription details
17
+ */
18
+ getSubscription: {
19
+ input: {
20
+ subscriptionId?: string;
21
+ customerId?: string;
22
+ };
23
+ output: {
24
+ id: string;
25
+ customerId: string;
26
+ status: "active" | "canceled" | "past_due" | "trialing" | "paused";
27
+ planId: string;
28
+ planName: string;
29
+ currentPeriodStart: string;
30
+ currentPeriodEnd: string;
31
+ cancelAtPeriodEnd: boolean;
32
+ provider: string;
33
+ } | null;
34
+ description: string;
35
+ };
36
+ /**
37
+ * Get customer details
38
+ */
39
+ getCustomer: {
40
+ input: {
41
+ customerId: string;
42
+ };
43
+ output: {
44
+ id: string;
45
+ email: string;
46
+ name?: string;
47
+ metadata?: Record<string, unknown>;
48
+ provider: string;
49
+ } | null;
50
+ description: string;
51
+ };
52
+ /**
53
+ * Check quota status
54
+ */
55
+ checkQuota: {
56
+ input: {
57
+ customerId: string;
58
+ featureKey: string;
59
+ };
60
+ output: {
61
+ allowed: boolean;
62
+ remaining: number | null;
63
+ limit: number | null;
64
+ resetAt?: string;
65
+ percentage: number;
66
+ };
67
+ description: string;
68
+ };
69
+ /**
70
+ * Get usage summary
71
+ */
72
+ getUsage: {
73
+ input: {
74
+ customerId: string;
75
+ featureKey?: string;
76
+ period?: "hour" | "day" | "month";
77
+ };
78
+ output: {
79
+ features: Array<{
80
+ featureKey: string;
81
+ used: number;
82
+ limit: number | null;
83
+ percentage: number;
84
+ }>;
85
+ period: {
86
+ start: string;
87
+ end: string;
88
+ };
89
+ };
90
+ description: string;
91
+ };
92
+ /**
93
+ * Get available plans
94
+ */
95
+ getPlans: {
96
+ input: undefined;
97
+ output: {
98
+ plans: Array<{
99
+ id: string;
100
+ name: string;
101
+ displayName: string;
102
+ tier: number;
103
+ basePrice: number;
104
+ currency: string;
105
+ billingInterval: "month" | "year";
106
+ features: Array<{
107
+ featureKey: string;
108
+ limitValue: number | null;
109
+ limitPeriod: string | null;
110
+ }>;
111
+ }>;
112
+ };
113
+ description: string;
114
+ };
115
+ /**
116
+ * Get dunning status
117
+ */
118
+ getDunningStatus: {
119
+ input: {
120
+ customerId: string;
121
+ };
122
+ output: {
123
+ inDunning: boolean;
124
+ status?: "active" | "resolved" | "abandoned" | "recovered";
125
+ currentStep?: number;
126
+ totalSteps?: number;
127
+ nextActionAt?: string;
128
+ daysSinceFailure?: number;
129
+ };
130
+ description: string;
131
+ };
132
+ }, {
133
+ /**
134
+ * Create a checkout session
135
+ */
136
+ createCheckout: {
137
+ input: {
138
+ email: string;
139
+ planId: string;
140
+ successUrl: string;
141
+ cancelUrl: string;
142
+ countryCode?: string;
143
+ metadata?: Record<string, unknown>;
144
+ };
145
+ output: {
146
+ checkoutUrl: string;
147
+ sessionId: string;
148
+ customerId?: string;
149
+ provider: string;
150
+ };
151
+ description: string;
152
+ };
153
+ /**
154
+ * Cancel a subscription
155
+ */
156
+ cancelSubscription: {
157
+ input: {
158
+ subscriptionId: string;
159
+ cancelAtPeriodEnd?: boolean;
160
+ reason?: string;
161
+ };
162
+ output: {
163
+ success: boolean;
164
+ canceledAt?: string;
165
+ effectiveAt?: string;
166
+ };
167
+ description: string;
168
+ };
169
+ /**
170
+ * Update subscription
171
+ */
172
+ updateSubscription: {
173
+ input: {
174
+ subscriptionId: string;
175
+ planId?: string;
176
+ metadata?: Record<string, unknown>;
177
+ };
178
+ output: {
179
+ success: boolean;
180
+ subscription: {
181
+ id: string;
182
+ planId: string;
183
+ status: string;
184
+ };
185
+ };
186
+ description: string;
187
+ };
188
+ /**
189
+ * Create customer portal session
190
+ */
191
+ createPortalSession: {
192
+ input: {
193
+ customerId: string;
194
+ returnUrl: string;
195
+ };
196
+ output: {
197
+ portalUrl: string;
198
+ expiresAt?: string;
199
+ };
200
+ description: string;
201
+ };
202
+ /**
203
+ * Track usage
204
+ */
205
+ trackUsage: {
206
+ input: {
207
+ customerId: string;
208
+ featureKey: string;
209
+ quantity: number;
210
+ metadata?: Record<string, unknown>;
211
+ };
212
+ output: {
213
+ success: boolean;
214
+ newTotal: number;
215
+ remaining: number | null;
216
+ };
217
+ description: string;
218
+ };
219
+ /**
220
+ * Assign plan to customer
221
+ */
222
+ assignPlan: {
223
+ input: {
224
+ customerId: string;
225
+ planId: string;
226
+ expiresAt?: string;
227
+ };
228
+ output: {
229
+ success: boolean;
230
+ previousPlanId?: string;
231
+ newPlanId: string;
232
+ };
233
+ description: string;
234
+ };
235
+ /**
236
+ * Handle webhook
237
+ */
238
+ handleWebhook: {
239
+ input: {
240
+ provider: "stripe" | "paddle" | "iyzico";
241
+ payload: string;
242
+ signature: string;
243
+ };
244
+ output: {
245
+ success: boolean;
246
+ eventType?: string;
247
+ eventId?: string;
248
+ };
249
+ description: string;
250
+ };
251
+ }, {
252
+ /**
253
+ * Subscription was created
254
+ */
255
+ "subscription.created": {
256
+ data: {
257
+ subscriptionId: string;
258
+ customerId: string;
259
+ planId: string;
260
+ provider: string;
261
+ timestamp: string;
262
+ };
263
+ delivery: "at-least-once";
264
+ description: string;
265
+ };
266
+ /**
267
+ * Subscription was renewed
268
+ */
269
+ "subscription.renewed": {
270
+ data: {
271
+ subscriptionId: string;
272
+ customerId: string;
273
+ planId: string;
274
+ periodStart: string;
275
+ periodEnd: string;
276
+ timestamp: string;
277
+ };
278
+ delivery: "at-least-once";
279
+ description: string;
280
+ };
281
+ /**
282
+ * Subscription was canceled
283
+ */
284
+ "subscription.canceled": {
285
+ data: {
286
+ subscriptionId: string;
287
+ customerId: string;
288
+ reason?: string;
289
+ effectiveAt: string;
290
+ timestamp: string;
291
+ };
292
+ delivery: "at-least-once";
293
+ description: string;
294
+ };
295
+ /**
296
+ * Subscription plan changed
297
+ */
298
+ "subscription.plan_changed": {
299
+ data: {
300
+ subscriptionId: string;
301
+ customerId: string;
302
+ previousPlanId: string;
303
+ newPlanId: string;
304
+ timestamp: string;
305
+ };
306
+ delivery: "at-least-once";
307
+ description: string;
308
+ };
309
+ /**
310
+ * Payment succeeded
311
+ */
312
+ "payment.succeeded": {
313
+ data: {
314
+ paymentId: string;
315
+ customerId: string;
316
+ amount: number;
317
+ currency: string;
318
+ invoiceId?: string;
319
+ timestamp: string;
320
+ };
321
+ delivery: "at-least-once";
322
+ description: string;
323
+ };
324
+ /**
325
+ * Payment failed
326
+ */
327
+ "payment.failed": {
328
+ data: {
329
+ customerId: string;
330
+ subscriptionId?: string;
331
+ amount: number;
332
+ currency: string;
333
+ errorCode?: string;
334
+ errorMessage?: string;
335
+ timestamp: string;
336
+ };
337
+ delivery: "at-least-once";
338
+ description: string;
339
+ };
340
+ /**
341
+ * Quota exceeded
342
+ */
343
+ "quota.exceeded": {
344
+ data: {
345
+ customerId: string;
346
+ featureKey: string;
347
+ used: number;
348
+ limit: number;
349
+ timestamp: string;
350
+ };
351
+ delivery: "at-least-once";
352
+ description: string;
353
+ };
354
+ /**
355
+ * Quota threshold reached
356
+ */
357
+ "quota.threshold_reached": {
358
+ data: {
359
+ customerId: string;
360
+ featureKey: string;
361
+ percentage: number;
362
+ used: number;
363
+ limit: number;
364
+ timestamp: string;
365
+ };
366
+ delivery: "at-least-once";
367
+ description: string;
368
+ };
369
+ /**
370
+ * Dunning started
371
+ */
372
+ "dunning.started": {
373
+ data: {
374
+ customerId: string;
375
+ subscriptionId: string;
376
+ amount: number;
377
+ currency: string;
378
+ timestamp: string;
379
+ };
380
+ delivery: "at-least-once";
381
+ description: string;
382
+ };
383
+ /**
384
+ * Dunning resolved
385
+ */
386
+ "dunning.resolved": {
387
+ data: {
388
+ customerId: string;
389
+ resolution: "recovered" | "canceled" | "manual";
390
+ timestamp: string;
391
+ };
392
+ delivery: "at-least-once";
393
+ description: string;
394
+ };
395
+ }, string[]>;
396
+ /**
397
+ * Type export for the payments service definition
398
+ */
399
+ type PaymentsServiceDefinition = typeof paymentsServiceDefinition;
400
+
401
+ /**
402
+ * @parsrun/service-adapters - Payments Service Server
403
+ * Server-side implementation of the Payments microservice
404
+ */
405
+
406
+ interface PaymentsServiceServerOptions {
407
+ /** Payment provider configurations */
408
+ providers: {
409
+ default: PaymentProviderConfig;
410
+ regions?: Record<string, PaymentProviderConfig>;
411
+ };
412
+ /** Database/storage for usage tracking */
413
+ storage?: PaymentsStorage;
414
+ /** Logger */
415
+ logger?: Logger;
416
+ /** Event transport (for emitting events) */
417
+ eventTransport?: ReturnType<typeof createMemoryEventTransport>;
418
+ }
419
+ interface PaymentProviderConfig {
420
+ type: "stripe" | "paddle" | "iyzico" | "mock";
421
+ secretKey?: string;
422
+ webhookSecret?: string;
423
+ }
424
+ interface PaymentsStorage {
425
+ getUsage(customerId: string, featureKey: string): Promise<number>;
426
+ trackUsage(customerId: string, featureKey: string, quantity: number): Promise<number>;
427
+ resetUsage(customerId: string, featureKey?: string): Promise<void>;
428
+ getPlan(planId: string): Promise<Plan$1 | null>;
429
+ getPlans(): Promise<Plan$1[]>;
430
+ getCustomerPlan(customerId: string): Promise<string | null>;
431
+ setCustomerPlan(customerId: string, planId: string): Promise<void>;
432
+ }
433
+ interface Plan$1 {
434
+ id: string;
435
+ name: string;
436
+ displayName: string;
437
+ tier: number;
438
+ basePrice: number;
439
+ currency: string;
440
+ billingInterval: "month" | "year";
441
+ features: Array<{
442
+ featureKey: string;
443
+ limitValue: number | null;
444
+ limitPeriod: string | null;
445
+ }>;
446
+ }
447
+ /**
448
+ * Create Payments Service Server
449
+ */
450
+ declare function createPaymentsServiceServer(options: PaymentsServiceServerOptions): {
451
+ rpcServer: RpcServer;
452
+ eventEmitter: EventEmitter;
453
+ register: () => void;
454
+ };
455
+
456
+ /**
457
+ * @parsrun/service-adapters - Payments Service Client
458
+ * Type-safe client for the Payments microservice
459
+ */
460
+
461
+ /**
462
+ * Type-safe Payments Service Client
463
+ */
464
+ interface PaymentsServiceClient {
465
+ /**
466
+ * Get subscription details
467
+ */
468
+ getSubscription(options: {
469
+ subscriptionId?: string;
470
+ customerId?: string;
471
+ }): Promise<Subscription | null>;
472
+ /**
473
+ * Get customer details
474
+ */
475
+ getCustomer(customerId: string): Promise<Customer | null>;
476
+ /**
477
+ * Check quota for a feature
478
+ */
479
+ checkQuota(customerId: string, featureKey: string): Promise<QuotaStatus>;
480
+ /**
481
+ * Get usage summary
482
+ */
483
+ getUsage(options: {
484
+ customerId: string;
485
+ featureKey?: string;
486
+ period?: "hour" | "day" | "month";
487
+ }): Promise<UsageSummary>;
488
+ /**
489
+ * Get available plans
490
+ */
491
+ getPlans(): Promise<{
492
+ plans: Plan[];
493
+ }>;
494
+ /**
495
+ * Get dunning status
496
+ */
497
+ getDunningStatus(customerId: string): Promise<DunningStatus>;
498
+ /**
499
+ * Create checkout session
500
+ */
501
+ createCheckout(options: CreateCheckoutOptions): Promise<CheckoutSession>;
502
+ /**
503
+ * Cancel subscription
504
+ */
505
+ cancelSubscription(options: CancelSubscriptionOptions): Promise<CancelResult>;
506
+ /**
507
+ * Update subscription
508
+ */
509
+ updateSubscription(options: UpdateSubscriptionOptions): Promise<UpdateResult>;
510
+ /**
511
+ * Create customer portal session
512
+ */
513
+ createPortalSession(customerId: string, returnUrl: string): Promise<PortalSession>;
514
+ /**
515
+ * Track usage
516
+ */
517
+ trackUsage(options: TrackUsageOptions): Promise<TrackUsageResult>;
518
+ /**
519
+ * Assign plan to customer
520
+ */
521
+ assignPlan(customerId: string, planId: string, expiresAt?: string): Promise<AssignPlanResult>;
522
+ /**
523
+ * Handle webhook
524
+ */
525
+ handleWebhook(options: WebhookOptions): Promise<WebhookResult>;
526
+ /**
527
+ * Subscribe to subscription events
528
+ */
529
+ onSubscriptionCreated(handler: (event: SubscriptionCreatedEvent) => Promise<void>): () => void;
530
+ onSubscriptionRenewed(handler: (event: SubscriptionRenewedEvent) => Promise<void>): () => void;
531
+ onSubscriptionCanceled(handler: (event: SubscriptionCanceledEvent) => Promise<void>): () => void;
532
+ onSubscriptionPlanChanged(handler: (event: PlanChangedEvent) => Promise<void>): () => void;
533
+ /**
534
+ * Subscribe to payment events
535
+ */
536
+ onPaymentSucceeded(handler: (event: PaymentSucceededEvent) => Promise<void>): () => void;
537
+ onPaymentFailed(handler: (event: PaymentFailedEvent) => Promise<void>): () => void;
538
+ /**
539
+ * Subscribe to quota events
540
+ */
541
+ onQuotaExceeded(handler: (event: QuotaExceededEvent) => Promise<void>): () => void;
542
+ onQuotaThresholdReached(handler: (event: QuotaThresholdEvent) => Promise<void>): () => void;
543
+ /**
544
+ * Subscribe to dunning events
545
+ */
546
+ onDunningStarted(handler: (event: DunningStartedEvent) => Promise<void>): () => void;
547
+ onDunningResolved(handler: (event: DunningResolvedEvent) => Promise<void>): () => void;
548
+ /**
549
+ * Close the client
550
+ */
551
+ close(): Promise<void>;
552
+ }
553
+ interface Subscription {
554
+ id: string;
555
+ customerId: string;
556
+ status: "active" | "canceled" | "past_due" | "trialing" | "paused";
557
+ planId: string;
558
+ planName: string;
559
+ currentPeriodStart: string;
560
+ currentPeriodEnd: string;
561
+ cancelAtPeriodEnd: boolean;
562
+ provider: string;
563
+ }
564
+ interface Customer {
565
+ id: string;
566
+ email: string;
567
+ name?: string;
568
+ metadata?: Record<string, unknown>;
569
+ provider: string;
570
+ }
571
+ interface QuotaStatus {
572
+ allowed: boolean;
573
+ remaining: number | null;
574
+ limit: number | null;
575
+ resetAt?: string;
576
+ percentage: number;
577
+ }
578
+ interface UsageSummary {
579
+ features: Array<{
580
+ featureKey: string;
581
+ used: number;
582
+ limit: number | null;
583
+ percentage: number;
584
+ }>;
585
+ period: {
586
+ start: string;
587
+ end: string;
588
+ };
589
+ }
590
+ interface Plan {
591
+ id: string;
592
+ name: string;
593
+ displayName: string;
594
+ tier: number;
595
+ basePrice: number;
596
+ currency: string;
597
+ billingInterval: "month" | "year";
598
+ features: Array<{
599
+ featureKey: string;
600
+ limitValue: number | null;
601
+ limitPeriod: string | null;
602
+ }>;
603
+ }
604
+ interface DunningStatus {
605
+ inDunning: boolean;
606
+ status?: "active" | "resolved" | "abandoned" | "recovered";
607
+ currentStep?: number;
608
+ totalSteps?: number;
609
+ nextActionAt?: string;
610
+ daysSinceFailure?: number;
611
+ }
612
+ interface CreateCheckoutOptions {
613
+ email: string;
614
+ planId: string;
615
+ successUrl: string;
616
+ cancelUrl: string;
617
+ countryCode?: string;
618
+ metadata?: Record<string, unknown>;
619
+ }
620
+ interface CheckoutSession {
621
+ checkoutUrl: string;
622
+ sessionId: string;
623
+ customerId?: string;
624
+ provider: string;
625
+ }
626
+ interface CancelSubscriptionOptions {
627
+ subscriptionId: string;
628
+ cancelAtPeriodEnd?: boolean;
629
+ reason?: string;
630
+ }
631
+ interface CancelResult {
632
+ success: boolean;
633
+ canceledAt?: string;
634
+ effectiveAt?: string;
635
+ }
636
+ interface UpdateSubscriptionOptions {
637
+ subscriptionId: string;
638
+ planId?: string;
639
+ metadata?: Record<string, unknown>;
640
+ }
641
+ interface UpdateResult {
642
+ success: boolean;
643
+ subscription: {
644
+ id: string;
645
+ planId: string;
646
+ status: string;
647
+ };
648
+ }
649
+ interface PortalSession {
650
+ portalUrl: string;
651
+ expiresAt?: string;
652
+ }
653
+ interface TrackUsageOptions {
654
+ customerId: string;
655
+ featureKey: string;
656
+ quantity: number;
657
+ metadata?: Record<string, unknown>;
658
+ }
659
+ interface TrackUsageResult {
660
+ success: boolean;
661
+ newTotal: number;
662
+ remaining: number | null;
663
+ }
664
+ interface AssignPlanResult {
665
+ success: boolean;
666
+ previousPlanId?: string;
667
+ newPlanId: string;
668
+ }
669
+ interface WebhookOptions {
670
+ provider: "stripe" | "paddle" | "iyzico";
671
+ payload: string;
672
+ signature: string;
673
+ }
674
+ interface WebhookResult {
675
+ success: boolean;
676
+ eventType?: string;
677
+ eventId?: string;
678
+ }
679
+ interface SubscriptionCreatedEvent {
680
+ subscriptionId: string;
681
+ customerId: string;
682
+ planId: string;
683
+ provider: string;
684
+ timestamp: string;
685
+ }
686
+ interface SubscriptionRenewedEvent {
687
+ subscriptionId: string;
688
+ customerId: string;
689
+ planId: string;
690
+ periodStart: string;
691
+ periodEnd: string;
692
+ timestamp: string;
693
+ }
694
+ interface SubscriptionCanceledEvent {
695
+ subscriptionId: string;
696
+ customerId: string;
697
+ reason?: string;
698
+ effectiveAt: string;
699
+ timestamp: string;
700
+ }
701
+ interface PlanChangedEvent {
702
+ subscriptionId: string;
703
+ customerId: string;
704
+ previousPlanId: string;
705
+ newPlanId: string;
706
+ timestamp: string;
707
+ }
708
+ interface PaymentSucceededEvent {
709
+ paymentId: string;
710
+ customerId: string;
711
+ amount: number;
712
+ currency: string;
713
+ invoiceId?: string;
714
+ timestamp: string;
715
+ }
716
+ interface PaymentFailedEvent {
717
+ customerId: string;
718
+ subscriptionId?: string;
719
+ amount: number;
720
+ currency: string;
721
+ errorCode?: string;
722
+ errorMessage?: string;
723
+ timestamp: string;
724
+ }
725
+ interface QuotaExceededEvent {
726
+ customerId: string;
727
+ featureKey: string;
728
+ used: number;
729
+ limit: number;
730
+ timestamp: string;
731
+ }
732
+ interface QuotaThresholdEvent {
733
+ customerId: string;
734
+ featureKey: string;
735
+ percentage: number;
736
+ used: number;
737
+ limit: number;
738
+ timestamp: string;
739
+ }
740
+ interface DunningStartedEvent {
741
+ customerId: string;
742
+ subscriptionId: string;
743
+ amount: number;
744
+ currency: string;
745
+ timestamp: string;
746
+ }
747
+ interface DunningResolvedEvent {
748
+ customerId: string;
749
+ resolution: "recovered" | "canceled" | "manual";
750
+ timestamp: string;
751
+ }
752
+ /**
753
+ * Create Payments Service Client
754
+ *
755
+ * @example
756
+ * ```typescript
757
+ * // Embedded mode
758
+ * const payments = createPaymentsServiceClient();
759
+ *
760
+ * // HTTP mode
761
+ * const payments = createPaymentsServiceClient({
762
+ * mode: 'http',
763
+ * baseUrl: 'https://payments.example.com',
764
+ * });
765
+ *
766
+ * // Check quota before API call
767
+ * const quota = await payments.checkQuota(customerId, 'api_calls');
768
+ * if (!quota.allowed) {
769
+ * throw new Error('Quota exceeded');
770
+ * }
771
+ *
772
+ * // Track usage after API call
773
+ * await payments.trackUsage({
774
+ * customerId,
775
+ * featureKey: 'api_calls',
776
+ * quantity: 1,
777
+ * });
778
+ * ```
779
+ */
780
+ declare function createPaymentsServiceClient(options?: ServiceClientOptions): PaymentsServiceClient;
781
+
782
+ export { type PaymentsServiceClient, type PaymentsServiceDefinition, type PaymentsServiceServerOptions, createPaymentsServiceClient, createPaymentsServiceServer, paymentsServiceDefinition };