@avelonjs/core 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,133 @@
1
+ import type { CapabilityMembers, CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal identity features used to narrow the authentication facade. */
4
+ export interface IdentityCapabilities<TFactor extends string = string> {
5
+ /** Whether password registration and sign-in are available. */
6
+ readonly passwords: boolean
7
+ /** Whether passwordless magic links are available. */
8
+ readonly magicLinks: boolean
9
+ /** Whether OAuth identities may be linked. */
10
+ readonly oauth: boolean
11
+ /** Whether organization membership is available. */
12
+ readonly organizations: boolean
13
+ /** Supported factors retained as a literal readonly list; empty means no MFA surface. */
14
+ readonly mfa: readonly TFactor[]
15
+ }
16
+
17
+ /** Authentication capability alias matching the application-facing `Auth` vocabulary. */
18
+ export type AuthCapabilities<TFactor extends string = string> = IdentityCapabilities<TFactor>
19
+
20
+ /** Organization summary returned by organization-capable identity drivers. */
21
+ export interface IdentityOrganization {
22
+ /** Stable organization identifier. */
23
+ id: string
24
+ /** Display name. */
25
+ name: string
26
+ }
27
+
28
+ /** Multi-factor challenge issued by an identity driver. */
29
+ export interface MfaChallenge<TFactor extends string = string> {
30
+ /** Stable challenge identifier. */
31
+ id: string
32
+ /** Framework-neutral factor kind. */
33
+ factor: TFactor
34
+ /** Time after which the challenge is invalid. */
35
+ expiresAt: Date
36
+ }
37
+
38
+ /**
39
+ * Base identity contract available independently of optional sign-in features. The driver factory
40
+ * receives `RequestCookies` through `avelon.config.ts` wiring, allowing `user`, `session`, and
41
+ * `signOut` to access request cookies without adapter imports or context parameters.
42
+ */
43
+ export interface IdentityDriver<
44
+ TCapabilities extends IdentityCapabilities = IdentityCapabilities,
45
+ TRaw = unknown,
46
+ TActor = unknown,
47
+ TSession = unknown,
48
+ > extends DriverContract<TCapabilities, TRaw> {
49
+ /** Returns the current actor or null when the request is anonymous. */
50
+ user(): Promise<TActor | null>
51
+ /** Returns the current session or null when no session exists. */
52
+ session(): Promise<TSession | null>
53
+ /** Ends the current session. */
54
+ signOut(): Promise<void>
55
+ }
56
+
57
+ /** Identity driver alias matching the application-facing `Auth` vocabulary. */
58
+ export type AuthDriver<
59
+ TCapabilities extends IdentityCapabilities = IdentityCapabilities,
60
+ TRaw = unknown,
61
+ TActor = unknown,
62
+ TSession = unknown,
63
+ > = IdentityDriver<TCapabilities, TRaw, TActor, TSession>
64
+
65
+ /** Password operations exposed only by password-capable identity drivers. */
66
+ export interface PasswordIdentitySurface<TActor = unknown> {
67
+ /** Registers an actor with an email address and password. */
68
+ register(email: string, password: string): Promise<TActor>
69
+ /** Authenticates an actor with an email address and password. */
70
+ signInWithPassword(email: string, password: string): Promise<TActor>
71
+ /** Sends a password-reset request without revealing account existence. */
72
+ sendPasswordReset(email: string): Promise<void>
73
+ /** Replaces a password after validating a password-recovery token. */
74
+ resetPassword(token: string, password: string): Promise<void>
75
+ /** Changes the current actor's password. */
76
+ updatePassword(password: string): Promise<void>
77
+ }
78
+
79
+ /** Passwordless operations exposed only by magic-link-capable identity drivers. */
80
+ export interface MagicLinkIdentitySurface {
81
+ /** Sends a sign-in link to an email address. */
82
+ sendMagicLink(email: string, redirectTo?: string): Promise<void>
83
+ }
84
+
85
+ /** OAuth linking exposed only when the identity driver supports linked identities. */
86
+ export interface OAuthIdentitySurface {
87
+ /** Links a completed social authorization to the current actor. */
88
+ linkOAuthIdentity(provider: string, code: string): Promise<void>
89
+ }
90
+
91
+ /** Organization operations exposed only by organization-capable identity drivers. */
92
+ export interface OrganizationIdentitySurface {
93
+ /** Lists organizations available to the current actor. */
94
+ organizations(): Promise<readonly IdentityOrganization[]>
95
+ /** Selects the active organization for the current session. */
96
+ useOrganization(id: string): Promise<void>
97
+ }
98
+
99
+ /** Multi-factor operations exposed only by drivers declaring at least one factor. */
100
+ export interface MfaIdentitySurface<TFactor extends string = string> {
101
+ /** Begins a multi-factor challenge for the current actor. */
102
+ challengeMfa(factor?: TFactor): Promise<MfaChallenge<TFactor>>
103
+ /** Verifies a challenge response and completes the pending authentication. */
104
+ verifyMfa(challengeId: string, code: string): Promise<void>
105
+ }
106
+
107
+ type ActorOf<TDriver extends IdentityDriver> =
108
+ TDriver extends IdentityDriver<IdentityCapabilities, unknown, infer TActor, unknown>
109
+ ? TActor
110
+ : never
111
+
112
+ type MfaIdentityFor<TCapabilities extends IdentityCapabilities> = [
113
+ CapabilityMembers<TCapabilities['mfa']>,
114
+ ] extends [never]
115
+ ? object
116
+ : MfaIdentitySurface<CapabilityMembers<TCapabilities['mfa']>>
117
+
118
+ /** Identity facade narrowed to the exact capabilities of its configured driver. */
119
+ export type Identity<TDriver extends IdentityDriver> = Pick<
120
+ TDriver,
121
+ 'name' | 'instance' | 'capabilities' | 'user' | 'session' | 'signOut' | 'raw'
122
+ > &
123
+ CapabilitySurface<
124
+ TDriver['capabilities']['passwords'],
125
+ PasswordIdentitySurface<ActorOf<TDriver>>
126
+ > &
127
+ CapabilitySurface<TDriver['capabilities']['magicLinks'], MagicLinkIdentitySurface> &
128
+ CapabilitySurface<TDriver['capabilities']['oauth'], OAuthIdentitySurface> &
129
+ CapabilitySurface<TDriver['capabilities']['organizations'], OrganizationIdentitySurface> &
130
+ MfaIdentityFor<TDriver['capabilities']>
131
+
132
+ /** Authentication facade alias matching the application-facing `Auth` vocabulary. */
133
+ export type Auth<TDriver extends IdentityDriver> = Identity<TDriver>
@@ -0,0 +1,61 @@
1
+ import type { CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal structured logging capabilities. */
4
+ export interface LogCapabilities {
5
+ /** Whether distributed traces and spans are available. */
6
+ readonly traces: boolean
7
+ }
8
+
9
+ /** Portable structured log severity. */
10
+ export type LogLevel = 'debug' | 'info' | 'warning' | 'error'
11
+
12
+ /** Structured log record emitted by application code. */
13
+ export interface LogRecord {
14
+ /** Severity used for filtering and alerting. */
15
+ level: LogLevel
16
+ /** Human-readable event message. */
17
+ message: string
18
+ /** Framework-owned structured context. */
19
+ context?: Readonly<Record<string, unknown>>
20
+ /** Event time, defaulting to the current time when omitted. */
21
+ timestamp?: Date
22
+ }
23
+
24
+ /** Active trace span returned by a tracing-capable driver. */
25
+ export interface TraceSpan {
26
+ /** Stable trace identifier. */
27
+ readonly traceId: string
28
+ /** Stable span identifier. */
29
+ readonly spanId: string
30
+ /** Adds a structured attribute to the span. */
31
+ attribute(name: string, value: string | number | boolean): void
32
+ /** Records an error without exposing a vendor event shape. */
33
+ recordError(error: unknown): void
34
+ /** Finishes the span. */
35
+ end(): void
36
+ }
37
+
38
+ /** Base structured logging contract. */
39
+ export interface LogDriver<
40
+ TCapabilities extends LogCapabilities = LogCapabilities,
41
+ TRaw = unknown,
42
+ > extends DriverContract<TCapabilities, TRaw> {
43
+ /** Writes one structured log record. */
44
+ write(record: LogRecord): Promise<void>
45
+ }
46
+
47
+ /** Trace methods exposed only by tracing-capable log drivers. */
48
+ export interface TraceLogSurface {
49
+ /** Starts a named trace span with optional structured attributes. */
50
+ startSpan(
51
+ name: string,
52
+ attributes?: Readonly<Record<string, string | number | boolean>>,
53
+ ): TraceSpan
54
+ }
55
+
56
+ /** Logging facade narrowed to its driver's tracing capability. */
57
+ export type Logs<TDriver extends LogDriver> = Pick<
58
+ TDriver,
59
+ 'name' | 'instance' | 'capabilities' | 'write' | 'raw'
60
+ > &
61
+ CapabilitySurface<TDriver['capabilities']['traces'], TraceLogSurface>
@@ -0,0 +1,73 @@
1
+ import type { CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal mail features used to narrow delivery methods. */
4
+ export interface MailCapabilities {
5
+ /** Whether provider-hosted templates may be sent. */
6
+ readonly templates: boolean
7
+ }
8
+
9
+ /** Portable attachment included with a mail message. */
10
+ export interface MailAttachment {
11
+ /** Attachment filename presented to the recipient. */
12
+ filename: string
13
+ /** Attachment bytes. */
14
+ content: Uint8Array
15
+ /** Media type when known. */
16
+ contentType?: string
17
+ }
18
+
19
+ /** Transport-neutral transactional email message. */
20
+ export interface MailMessage {
21
+ /** Recipient addresses. */
22
+ to: string | readonly string[]
23
+ /** Sender address, or the configured instance default when omitted. */
24
+ from?: string
25
+ /** Address or addresses that receive recipient replies. */
26
+ replyTo?: string | readonly string[]
27
+ /** Carbon-copy recipient addresses. */
28
+ cc?: readonly string[]
29
+ /** Blind-carbon-copy recipient addresses. */
30
+ bcc?: readonly string[]
31
+ /** Message subject. */
32
+ subject: string
33
+ /** Plain-text message body. */
34
+ text?: string
35
+ /** HTML message body. */
36
+ html?: string
37
+ /** File attachments. */
38
+ attachments?: readonly MailAttachment[]
39
+ }
40
+
41
+ /** Result of accepting a message for delivery. */
42
+ export interface MailReceipt {
43
+ /** Stable message identifier. */
44
+ id: string
45
+ /** Recipient addresses accepted by the driver. */
46
+ accepted: readonly string[]
47
+ }
48
+
49
+ /** Base transactional mail contract. */
50
+ export interface MailDriver<
51
+ TCapabilities extends MailCapabilities = MailCapabilities,
52
+ TRaw = unknown,
53
+ > extends DriverContract<TCapabilities, TRaw> {
54
+ /** Sends one transport-neutral transactional message. */
55
+ send(message: MailMessage): Promise<MailReceipt>
56
+ }
57
+
58
+ /** Hosted-template delivery exposed only by template-capable mail drivers. */
59
+ export interface TemplateMailSurface {
60
+ /** Sends a provider-hosted template with serializable variables. */
61
+ sendTemplate(
62
+ template: string,
63
+ to: string | readonly string[],
64
+ variables: Readonly<Record<string, unknown>>,
65
+ ): Promise<MailReceipt>
66
+ }
67
+
68
+ /** Mail facade narrowed to its driver's template capability. */
69
+ export type Mail<TDriver extends MailDriver> = Pick<
70
+ TDriver,
71
+ 'name' | 'instance' | 'capabilities' | 'send' | 'raw'
72
+ > &
73
+ CapabilitySurface<TDriver['capabilities']['templates'], TemplateMailSurface>
@@ -0,0 +1,64 @@
1
+ import type { CapabilityMembers, DriverContract } from './common'
2
+
3
+ /** Portable notification delivery channels. */
4
+ export type NotificationChannel = 'push' | 'sms' | 'inApp'
5
+
6
+ /** Literal notification channels supported by a driver instance. */
7
+ export interface NotificationCapabilities<
8
+ TChannel extends NotificationChannel = NotificationChannel,
9
+ > {
10
+ /** Delivery channels retained as a literal readonly list. */
11
+ readonly channels: readonly TChannel[]
12
+ }
13
+
14
+ /** Transport-neutral notification request. */
15
+ export interface NotificationMessage<TData = unknown> {
16
+ /** Framework actor or destination identifier. */
17
+ recipient: string
18
+ /** Notification title when the channel presents one. */
19
+ title?: string
20
+ /** Human-readable notification body. */
21
+ body: string
22
+ /** Serializable channel-specific application data. */
23
+ data?: TData
24
+ }
25
+
26
+ /** Result of accepting a notification for delivery. */
27
+ export interface NotificationReceipt {
28
+ /** Stable delivery identifier. */
29
+ id: string
30
+ /** Channel used for delivery. */
31
+ channel: NotificationChannel
32
+ }
33
+
34
+ /** Push, SMS, and in-app notification contract. */
35
+ export interface NotificationDriver<
36
+ TCapabilities extends NotificationCapabilities = NotificationCapabilities,
37
+ TRaw = unknown,
38
+ > extends DriverContract<TCapabilities, TRaw> {
39
+ /** Sends a message through a notification channel. */
40
+ send<TData>(
41
+ channel: NotificationChannel,
42
+ message: NotificationMessage<TData>,
43
+ ): Promise<NotificationReceipt>
44
+ }
45
+
46
+ /** Notification facade restricted to its driver's literal channel list. */
47
+ export interface NotificationSurface<TDriver extends NotificationDriver> {
48
+ /** Driver implementation name. */
49
+ readonly name: TDriver['name']
50
+ /** Configured notification connection name. */
51
+ readonly instance: TDriver['instance']
52
+ /** Exact notification channel declaration. */
53
+ readonly capabilities: TDriver['capabilities']
54
+ /** Sends a notification through a configured channel. */
55
+ send<TData>(
56
+ channel: CapabilityMembers<TDriver['capabilities']['channels']>,
57
+ message: NotificationMessage<TData>,
58
+ ): Promise<NotificationReceipt>
59
+ /** Returns the driver's typed underlying client. */
60
+ raw(): ReturnType<TDriver['raw']>
61
+ }
62
+
63
+ /** Notification facade narrowed to configured channel literals. */
64
+ export type Notifications<TDriver extends NotificationDriver> = NotificationSurface<TDriver>
@@ -0,0 +1,107 @@
1
+ import type { CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal payment features used to narrow commerce operations. */
4
+ export interface PaymentCapabilities {
5
+ /** Whether recurring subscriptions are available. */
6
+ readonly subscriptions: boolean
7
+ /** Whether hosted checkout sessions are available. */
8
+ readonly checkout: boolean
9
+ /** Whether signed webhook events can be normalized. */
10
+ readonly webhooks: boolean
11
+ }
12
+
13
+ /** Framework-neutral payment customer. */
14
+ export interface PaymentCustomer {
15
+ /** Stable customer identifier. */
16
+ id: string
17
+ /** Customer email when known. */
18
+ email?: string
19
+ /** Application metadata attached to the customer. */
20
+ metadata: Readonly<Record<string, string>>
21
+ }
22
+
23
+ /** Framework-neutral recurring subscription. */
24
+ export interface PaymentSubscription {
25
+ /** Stable subscription identifier. */
26
+ id: string
27
+ /** Stable customer identifier. */
28
+ customerId: string
29
+ /** Application price or plan identifier. */
30
+ priceId: string
31
+ /** Normalized lifecycle status. */
32
+ status: 'trialing' | 'active' | 'past_due' | 'canceled' | 'paused'
33
+ /** End of the current billing period when known. */
34
+ currentPeriodEndsAt?: Date
35
+ }
36
+
37
+ /** Hosted checkout session created by a payment driver. */
38
+ export interface CheckoutSession {
39
+ /** Stable checkout session identifier. */
40
+ id: string
41
+ /** URL where the actor completes checkout. */
42
+ url: string
43
+ /** Time after which the checkout URL is invalid. */
44
+ expiresAt?: Date
45
+ }
46
+
47
+ /** Normalized payment webhook event. */
48
+ export interface PaymentWebhook<TData = unknown> {
49
+ /** Stable event identifier used for idempotency. */
50
+ id: string
51
+ /** Framework-neutral event name. */
52
+ type: string
53
+ /** Normalized event data. */
54
+ data: TData
55
+ /** Time the payment system created the event. */
56
+ occurredAt: Date
57
+ }
58
+
59
+ /** Base customer operations provided by every payment driver. */
60
+ export interface PaymentDriver<
61
+ TCapabilities extends PaymentCapabilities = PaymentCapabilities,
62
+ TRaw = unknown,
63
+ > extends DriverContract<TCapabilities, TRaw> {
64
+ /** Creates a payment customer. */
65
+ createCustomer(input: {
66
+ readonly email?: string
67
+ readonly metadata?: Readonly<Record<string, string>>
68
+ }): Promise<PaymentCustomer>
69
+ /** Retrieves a payment customer. */
70
+ customer(id: string): Promise<PaymentCustomer>
71
+ }
72
+
73
+ /** Subscription methods exposed only by subscription-capable payment drivers. */
74
+ export interface SubscriptionPaymentSurface {
75
+ /** Creates a recurring subscription for a customer and price. */
76
+ subscribe(customerId: string, priceId: string): Promise<PaymentSubscription>
77
+ /** Retrieves a recurring subscription. */
78
+ subscription(id: string): Promise<PaymentSubscription>
79
+ /** Cancels a recurring subscription. */
80
+ cancelSubscription(id: string): Promise<PaymentSubscription>
81
+ }
82
+
83
+ /** Checkout methods exposed only by checkout-capable payment drivers. */
84
+ export interface CheckoutPaymentSurface {
85
+ /** Creates a hosted checkout session. */
86
+ checkout(input: {
87
+ readonly customerId?: string
88
+ readonly priceId: string
89
+ readonly successUrl: string
90
+ readonly cancelUrl: string
91
+ }): Promise<CheckoutSession>
92
+ }
93
+
94
+ /** Webhook methods exposed only by webhook-capable payment drivers. */
95
+ export interface WebhookPaymentSurface {
96
+ /** Verifies and normalizes a raw webhook request. */
97
+ webhook(payload: Uint8Array, signature: string): Promise<PaymentWebhook>
98
+ }
99
+
100
+ /** Payments facade narrowed to subscriptions, checkout, and webhook capabilities. */
101
+ export type Payments<TDriver extends PaymentDriver> = Pick<
102
+ TDriver,
103
+ 'name' | 'instance' | 'capabilities' | 'createCustomer' | 'customer' | 'raw'
104
+ > &
105
+ CapabilitySurface<TDriver['capabilities']['subscriptions'], SubscriptionPaymentSurface> &
106
+ CapabilitySurface<TDriver['capabilities']['checkout'], CheckoutPaymentSurface> &
107
+ CapabilitySurface<TDriver['capabilities']['webhooks'], WebhookPaymentSurface>
@@ -0,0 +1,87 @@
1
+ import type { CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal durable queue features used to narrow queue operations. */
4
+ export interface QueueCapabilities {
5
+ /** Whether jobs may be scheduled for later delivery. */
6
+ readonly delayed: boolean
7
+ /** Whether failed jobs may be retried. */
8
+ readonly retries: boolean
9
+ /** Whether terminal failures are retained for inspection and replay. */
10
+ readonly deadLetter: boolean
11
+ }
12
+
13
+ /** Serializable job submitted to a queue driver. */
14
+ export interface QueueJob<TPayload = unknown> {
15
+ /** Registered job name. */
16
+ name: string
17
+ /** Serializable job payload. */
18
+ payload: TPayload
19
+ /** Named destination queue. */
20
+ queue?: string
21
+ }
22
+
23
+ /** Delivery metadata supplied while draining a queue. */
24
+ export interface QueueReceipt<TPayload = unknown> {
25
+ /** Stable queued-job identifier. */
26
+ id: string
27
+ /** Deserialized job. */
28
+ job: QueueJob<TPayload>
29
+ /** One-based execution attempt. */
30
+ attempt: number
31
+ }
32
+
33
+ /** Failed job retained by a dead-letter-capable driver. */
34
+ export interface FailedQueueJob extends QueueReceipt {
35
+ /** Framework-normalized failure message. */
36
+ error: string
37
+ /** Time the terminal failure was recorded. */
38
+ failedAt: Date
39
+ }
40
+
41
+ /** Base enqueue and drain contract. */
42
+ export interface QueueDriver<
43
+ TCapabilities extends QueueCapabilities = QueueCapabilities,
44
+ TRaw = unknown,
45
+ > extends DriverContract<TCapabilities, TRaw> {
46
+ /** Enqueues a job for immediate delivery and returns its identifier. */
47
+ enqueue<TPayload>(job: QueueJob<TPayload>): Promise<string>
48
+ /**
49
+ * Drains available jobs. Handler resolution removes a job; a thrown error releases it for
50
+ * redelivery with `attempt` incremented, subject to the `retries` and `deadLetter` capabilities.
51
+ */
52
+ drain(
53
+ handler: (receipt: QueueReceipt) => Promise<void>,
54
+ options?: { readonly queue?: string; readonly limit?: number },
55
+ ): Promise<number>
56
+ }
57
+
58
+ /** Delayed delivery exposed only by drivers that support scheduling. */
59
+ export interface DelayedQueueSurface {
60
+ /** Enqueues a job no earlier than the requested time. */
61
+ enqueueAt<TPayload>(job: QueueJob<TPayload>, availableAt: Date): Promise<string>
62
+ }
63
+
64
+ /** Retry operations exposed only by retry-capable queue drivers. */
65
+ export interface RetryQueueSurface {
66
+ /** Releases a failed attempt using an optional delay. */
67
+ retry(id: string, delaySeconds?: number): Promise<void>
68
+ }
69
+
70
+ /** Dead-letter operations exposed only when terminal failures are retained. */
71
+ export interface DeadLetterQueueSurface {
72
+ /** Lists retained terminal failures. */
73
+ failed(queue?: string): Promise<readonly FailedQueueJob[]>
74
+ /** Replays a retained terminal failure. */
75
+ replay(id: string): Promise<void>
76
+ /** Permanently removes a retained terminal failure. */
77
+ forget(id: string): Promise<void>
78
+ }
79
+
80
+ /** Queue facade narrowed to delayed, retry, and dead-letter capabilities. */
81
+ export type Queue<TDriver extends QueueDriver> = Pick<
82
+ TDriver,
83
+ 'name' | 'instance' | 'capabilities' | 'enqueue' | 'drain' | 'raw'
84
+ > &
85
+ CapabilitySurface<TDriver['capabilities']['delayed'], DelayedQueueSurface> &
86
+ CapabilitySurface<TDriver['capabilities']['retries'], RetryQueueSurface> &
87
+ CapabilitySurface<TDriver['capabilities']['deadLetter'], DeadLetterQueueSurface>
@@ -0,0 +1,66 @@
1
+ import type { CapabilityMembers, DriverContract } from './common'
2
+
3
+ /** Portable rate-limit algorithms. */
4
+ export type RateLimitAlgorithm = 'tokenBucket' | 'slidingWindow'
5
+
6
+ /** Literal rate-limit algorithms supported by a driver instance. */
7
+ export interface RateLimitCapabilities<TAlgorithm extends RateLimitAlgorithm = RateLimitAlgorithm> {
8
+ /** Algorithms retained as a literal readonly list. */
9
+ readonly algorithms: readonly TAlgorithm[]
10
+ }
11
+
12
+ /** Portable rate-limit policy. */
13
+ export interface RateLimitPolicy<TAlgorithm extends RateLimitAlgorithm = RateLimitAlgorithm> {
14
+ /** Algorithm used to evaluate the key. */
15
+ algorithm: TAlgorithm
16
+ /** Maximum accepted operations in the configured window or bucket. */
17
+ limit: number
18
+ /** Window or refill duration in seconds. */
19
+ intervalSeconds: number
20
+ }
21
+
22
+ /** Result of consuming capacity from a rate limit. */
23
+ export interface RateLimitDecision {
24
+ /** Whether the operation may proceed. */
25
+ allowed: boolean
26
+ /** Capacity remaining after this decision. */
27
+ remaining: number
28
+ /** Epoch time in milliseconds when full capacity resets. */
29
+ resetsAt: number
30
+ /** Milliseconds until retry when the operation was denied. */
31
+ retryAfterMs?: number
32
+ }
33
+
34
+ /** Token-bucket and sliding-window rate-limit contract. */
35
+ export interface RateLimitDriver<
36
+ TCapabilities extends RateLimitCapabilities = RateLimitCapabilities,
37
+ TRaw = unknown,
38
+ > extends DriverContract<TCapabilities, TRaw> {
39
+ /** Consumes capacity for a key under the supplied policy. */
40
+ consume(key: string, policy: RateLimitPolicy, cost?: number): Promise<RateLimitDecision>
41
+ /** Clears accumulated state for a key. */
42
+ reset(key: string): Promise<void>
43
+ }
44
+
45
+ /** Rate-limit facade restricted to its driver's literal algorithm list. */
46
+ export interface RateLimitSurface<TDriver extends RateLimitDriver> {
47
+ /** Driver implementation name. */
48
+ readonly name: TDriver['name']
49
+ /** Configured limiter name. */
50
+ readonly instance: TDriver['instance']
51
+ /** Exact algorithm capability declaration. */
52
+ readonly capabilities: TDriver['capabilities']
53
+ /** Consumes capacity using one configured algorithm. */
54
+ consume(
55
+ key: string,
56
+ policy: RateLimitPolicy<CapabilityMembers<TDriver['capabilities']['algorithms']>>,
57
+ cost?: number,
58
+ ): Promise<RateLimitDecision>
59
+ /** Clears accumulated state for a key. */
60
+ reset(key: string): Promise<void>
61
+ /** Returns the driver's typed underlying client. */
62
+ raw(): ReturnType<TDriver['raw']>
63
+ }
64
+
65
+ /** Rate-limit facade narrowed to configured algorithm literals. */
66
+ export type RateLimit<TDriver extends RateLimitDriver> = RateLimitSurface<TDriver>
@@ -0,0 +1,63 @@
1
+ import type { CapabilitySurface, DriverContract } from './common'
2
+
3
+ /** Literal realtime features used to narrow channel operations. */
4
+ export interface RealtimeCapabilities {
5
+ /** Whether channels expose member presence. */
6
+ readonly presence: boolean
7
+ /** Whether application messages may be broadcast. */
8
+ readonly broadcast: boolean
9
+ }
10
+
11
+ /** Message delivered through a realtime channel. */
12
+ export interface RealtimeMessage<TPayload = unknown> {
13
+ /** Channel event name. */
14
+ event: string
15
+ /** Serializable message payload. */
16
+ payload: TPayload
17
+ /** Channel that delivered the message. */
18
+ channel: string
19
+ }
20
+
21
+ /** Active realtime subscription. */
22
+ export interface RealtimeSubscription {
23
+ /** Channel name. */
24
+ readonly channel: string
25
+ /** Stops delivery and releases the subscription. */
26
+ unsubscribe(): Promise<void>
27
+ }
28
+
29
+ /** Base realtime channel subscription contract. */
30
+ export interface RealtimeDriver<
31
+ TCapabilities extends RealtimeCapabilities = RealtimeCapabilities,
32
+ TRaw = unknown,
33
+ > extends DriverContract<TCapabilities, TRaw> {
34
+ /** Subscribes to messages on a named channel. */
35
+ subscribe<TPayload = unknown>(
36
+ channel: string,
37
+ handler: (message: RealtimeMessage<TPayload>) => void | Promise<void>,
38
+ ): Promise<RealtimeSubscription>
39
+ }
40
+
41
+ /** Presence operations exposed only by presence-capable realtime drivers. */
42
+ export interface PresenceRealtimeSurface {
43
+ /** Joins channel presence with explicit serializable member state. */
44
+ joinPresence(channel: string, member: Readonly<Record<string, unknown>>): Promise<void>
45
+ /** Leaves channel presence. */
46
+ leavePresence(channel: string): Promise<void>
47
+ /** Lists current channel members. */
48
+ presence(channel: string): Promise<readonly Readonly<Record<string, unknown>>[]>
49
+ }
50
+
51
+ /** Broadcast operations exposed only by broadcast-capable realtime drivers. */
52
+ export interface BroadcastRealtimeSurface {
53
+ /** Broadcasts a serializable event to a channel. */
54
+ broadcast<TPayload>(channel: string, event: string, payload: TPayload): Promise<void>
55
+ }
56
+
57
+ /** Realtime facade narrowed to presence and broadcast capabilities. */
58
+ export type Realtime<TDriver extends RealtimeDriver> = Pick<
59
+ TDriver,
60
+ 'name' | 'instance' | 'capabilities' | 'subscribe' | 'raw'
61
+ > &
62
+ CapabilitySurface<TDriver['capabilities']['presence'], PresenceRealtimeSurface> &
63
+ CapabilitySurface<TDriver['capabilities']['broadcast'], BroadcastRealtimeSurface>