Line data Source code
1 : import { zBillingStatusResponse } from '@comfyorg/ingest-types/zod'
2 : import { z } from 'zod'
3 :
4 : import type { BillingResult, BillingTransport } from './billingContracts'
5 : import type { BillingScope, BillingScopeSource } from './billingScope'
6 : import { createScopedReader } from './scopedReader'
7 :
8 2356 : export const BILLING_STATUS_ROUTE = '/billing/status'
9 :
10 2356 : const BillingStatusBodySchema = zBillingStatusResponse.extend({
11 : ended_at: zBillingStatusResponse.shape.ended_at.optional(),
12 : next_charge: zBillingStatusResponse.shape.next_charge.optional(),
13 : subscription_tier: zBillingStatusResponse.shape.subscription_tier.or(
14 : z.string().transform(() => undefined)
15 : )
16 : })
17 :
18 : export type BillingStatusData = z.infer<typeof BillingStatusBodySchema>
19 :
20 : export type BillingStatusScope = BillingScope
21 :
22 : export interface BillingStatusSnapshot {
23 : readonly status: BillingStatusData
24 : readonly scope: BillingStatusScope
25 : readonly readAt: number
26 : }
27 :
28 : export interface BillingStatusReadOptions {
29 : readonly signal?: AbortSignal
30 : }
31 :
32 : export interface BillingStatusReader {
33 : read: (
34 : options?: BillingStatusReadOptions
35 : ) => Promise<BillingResult<BillingStatusSnapshot>>
36 : getSnapshot: () => BillingStatusSnapshot | undefined
37 : dispose: () => void
38 : }
39 :
40 : export interface BillingStatusReaderOptions {
41 : readonly transport: BillingTransport
42 : /** Where the core learns which user, workspace, and role it runs as. */
43 : readonly scopeSource: BillingScopeSource
44 : readonly now?: () => number
45 : }
46 :
47 0 : export function createBillingStatusReader(
48 : options: BillingStatusReaderOptions
49 : ): BillingStatusReader {
50 0 : const { transport, scopeSource, now = Date.now } = options
51 :
52 0 : const reader = createScopedReader<
53 : BillingStatusData,
54 : BillingStatusSnapshot,
55 : BillingStatusReadOptions
56 : >({
57 : transport,
58 : scopeSource,
59 : route: BILLING_STATUS_ROUTE,
60 : parse: (body) => BillingStatusBodySchema.safeParse(body),
61 : project: ({ data }, scope) => ({
62 : status: 'ok',
63 : value: { status: data, scope, readAt: now() }
64 : })
65 : })
66 :
67 0 : return {
68 : read: reader.read,
69 : getSnapshot: reader.getSnapshot,
70 : dispose: reader.dispose
71 : }
72 : }
|