Line data Source code
1 : import type {
2 : BillingCommands,
3 : BillingEventsReader,
4 : BillingOperationLifecycle,
5 : BillingOperationPointerStorage,
6 : BillingOperationTelemetryEvent,
7 : BillingScopeSource,
8 : BillingSession,
9 : BillingStatusReader,
10 : CapabilitiesReader,
11 : CreditsReader,
12 : HostedBillingDestination,
13 : PaymentMethodsReader,
14 : PlansReader,
15 : TopupCommand
16 : } from '@comfyorg/account-core/billing'
17 : import {
18 : createBillingCommands,
19 : createBillingEventsReader,
20 : createBillingOperationLifecycle,
21 : createBillingStatusReader,
22 : createCapabilitiesReader,
23 : createCreditsReader,
24 : createPaymentMethodsReader,
25 : createPlansReader,
26 : createSessionBillingTransport,
27 : createTopupCommand,
28 : sessionBillingScopeSource
29 : } from '@comfyorg/account-core/billing'
30 :
31 : export interface BillingSdkOptions {
32 : readonly session: BillingSession
33 : readonly scopeSource?: BillingScopeSource
34 : readonly resolveUrl: (route: string) => string
35 : readonly workspaceId: () => string | undefined
36 : readonly pointerStorage?: BillingOperationPointerStorage
37 : readonly hostedDestination: () => HostedBillingDestination
38 : readonly onTelemetry: (event: BillingOperationTelemetryEvent) => void
39 : readonly fetchImpl?: typeof fetch
40 : }
41 :
42 : export interface BillingSdk {
43 : readonly lifecycle: BillingOperationLifecycle
44 : readonly status: BillingStatusReader
45 : readonly credits: CreditsReader
46 : readonly capabilities: CapabilitiesReader
47 : readonly plans: PlansReader
48 : readonly paymentMethods: PaymentMethodsReader
49 : readonly events: BillingEventsReader
50 : readonly topup: TopupCommand
51 : readonly commands: BillingCommands
52 : readonly dispose: () => void
53 : }
54 :
55 0 : export function createBillingSdk(options: BillingSdkOptions): BillingSdk {
56 0 : const {
57 : session,
58 : resolveUrl,
59 : workspaceId,
60 : pointerStorage,
61 : hostedDestination,
62 : onTelemetry,
63 : fetchImpl
64 : } = options
65 :
66 0 : const transport = createSessionBillingTransport({
67 : session,
68 : resolveUrl,
69 : workspaceId,
70 : ...(fetchImpl === undefined ? {} : { fetchImpl })
71 : })
72 0 : const scopeSource = options.scopeSource ?? sessionBillingScopeSource(session)
73 0 : const status = createBillingStatusReader({ transport, scopeSource })
74 0 : const credits = createCreditsReader({ transport, scopeSource })
75 0 : const capabilities = createCapabilitiesReader({ transport, scopeSource })
76 0 : const plans = createPlansReader({ transport, scopeSource })
77 0 : const paymentMethods = createPaymentMethodsReader({ transport, scopeSource })
78 0 : const events = createBillingEventsReader({ transport, scopeSource })
79 0 : const lifecycle = createBillingOperationLifecycle({
80 : transport,
81 : scopeSource,
82 : statusReader: status,
83 : ...(pointerStorage === undefined ? {} : { pointerStorage }),
84 : hostedDestination,
85 : onTelemetry
86 : })
87 : const topup = createTopupCommand({
88 : transport,
89 : lifecycle,
90 : capabilities,
91 : credits
92 : })
93 : const commands = createBillingCommands({
94 : transport,
95 : lifecycle,
96 : statusReader: status,
97 : capabilities,
98 : credits
99 : })
100 :
101 0 : return {
102 : lifecycle,
103 : status,
104 : credits,
105 : capabilities,
106 : plans,
107 : paymentMethods,
108 : events,
109 : topup,
110 : commands,
111 : dispose: () => {
112 0 : lifecycle.dispose()
113 0 : status.dispose()
114 0 : credits.dispose()
115 0 : capabilities.dispose()
116 0 : plans.dispose()
117 0 : paymentMethods.dispose()
118 0 : events.dispose()
119 : }
120 : }
121 : }
|