import { quotaSnapshotResource, type QuotaSnapshotRow } from '@undefineds.co/models'; import type { AuthContext } from '../../auth/AuthContext'; import type { GatewayDeployment } from '../auth/InvocationTokenCodec'; import type { InternalPodAccessTokenProvider } from '../pod/HostedPodDataAccess'; import { type PodBaseUrlResolver } from '../pod/PodBaseUrlResolver'; import type { ConnectCredentialRecord, PodCredentialRepository } from '../connect'; import type { CredentialVault, ProviderSecret } from '../credentials/CredentialVault'; import type { ProviderRegistry } from '../providers/ProviderRegistry'; import { type QuotaHandlerCapability } from './QuotaCapabilityRegistry'; import { ProviderHttpTransport } from '../../service/provider-http-transport'; export type QuotaSnapshotStatus = 'available' | 'unsupported' | 'error'; export interface QuotaWindow { name: string; used?: number; usedExact?: string; limit?: number; limitExact?: string; remaining?: number; remainingExact?: string; displayApprox?: boolean; currency?: string; resetsAt?: string; } export interface NormalizedQuotaSnapshot { id?: string; credential: string; status: QuotaSnapshotStatus; balance?: number; windows: QuotaWindow[]; observedAt: string; expiresAt: string; source: string; stale?: boolean; metadata?: Record; } export interface QuotaCredentialRecord extends ConnectCredentialRecord { baseUrl?: string; proxyUrl?: string; offeringId?: string; } export interface ProviderQuotaFetchInput { credential: QuotaCredentialRecord; secret: ProviderSecret; now: Date; signal?: AbortSignal; } export interface ProviderQuotaAdapter { readonly provider: string; readonly capability?: QuotaHandlerCapability; supports?(credential: QuotaCredentialRecord): boolean; fetch(input: ProviderQuotaFetchInput): Promise; } export interface QuotaSnapshotRepository { findFresh(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; now: Date; auth?: AuthContext; }): Promise; findLatest(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; auth?: AuthContext; }): Promise; upsert(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; snapshot: NormalizedQuotaSnapshot; auth?: AuthContext; }): Promise; } export interface ProviderQuotaStatusInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri?: string; refresh?: boolean; now?: Date; signal?: AbortSignal; auth?: AuthContext; } export interface CallerOwnedQuotaInput { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialId: string; credentialIri: string; authMode: 'apiKey' | 'deviceCodeOAuth'; baseUrl?: string; proxyUrl?: string; secret: ProviderSecret; now?: Date; signal?: AbortSignal; } export interface ProviderQuotaServiceOptions { repository: QuotaSnapshotRepository; vault: CredentialVault; adapters: ProviderQuotaAdapter[]; providerRegistry?: ProviderRegistry; credentialRepository?: PodCredentialRepository; credentials?: QuotaCredentialRecord[]; now?: () => Date; } export declare class ProviderQuotaService { private readonly repository; private readonly vault; private readonly adapters; private readonly providerRegistry?; private readonly capabilityRegistry; private readonly credentialRepository?; private readonly credentials; private readonly now; private readonly inFlightRefreshes; constructor(options: ProviderQuotaServiceOptions); status(input: ProviderQuotaStatusInput): Promise; statusCallerOwned(input: CallerOwnedQuotaInput): Promise; private statusAfterImplicitCredentialResolution; private refreshAndCache; private fetchAndCache; private findAdapter; private resolveCredential; } export declare class InMemoryQuotaSnapshotRepository implements QuotaSnapshotRepository { readonly rows: Array; findFresh(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; now: Date; }): Promise; findLatest(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; }): Promise; upsert(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; snapshot: NormalizedQuotaSnapshot; }): Promise; } type QuotaSnapshotDb = { init?: (...resources: unknown[]) => Promise; select(): { from(resource: typeof quotaSnapshotResource): { where(condition: unknown): { execute(): Promise; }; }; }; findById(resource: typeof quotaSnapshotResource, id: string): Promise; findByIri(resource: typeof quotaSnapshotResource, iri: string): Promise; updateById(resource: typeof quotaSnapshotResource, id: string, patch: unknown): Promise; updateByIri(resource: typeof quotaSnapshotResource, iri: string, patch: unknown): Promise; insert(resource: typeof quotaSnapshotResource): { values(value: unknown): { execute(): Promise; }; }; }; export interface PodQuotaSnapshotRepositoryOptions { internalPodAccess?: InternalPodAccessTokenProvider; podBaseUrlResolver?: PodBaseUrlResolver; dbFactory?: (input: { owner: string; auth?: AuthContext; fetch: typeof fetch; podUrl: string; }) => Promise; } export declare class PodQuotaSnapshotRepository implements QuotaSnapshotRepository { private readonly dbFactory; private readonly internalPodAccess?; private readonly podBaseUrlResolver?; constructor(options?: PodQuotaSnapshotRepositoryOptions); findFresh(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; now: Date; auth?: AuthContext; }): Promise; findLatest(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; credentialIri: string; auth?: AuthContext; }): Promise; upsert(input: { webId: string; deployment: GatewayDeployment; provider: string; offeringId?: string; snapshot: NormalizedQuotaSnapshot; auth?: AuthContext; }): Promise; private dbForOwner; private resolveTrustedFetch; private wrapPodFetch; } export declare function unsupportedQuotaSnapshot(input: { credential: string; source: string; now: Date; ttlMs?: number; metadata?: Record; }): NormalizedQuotaSnapshot; export declare function errorQuotaSnapshot(input: { credential: string; source: string; now: Date; status?: number; retryAfter?: string | null; ttlMs?: number; metadata?: Record; }): NormalizedQuotaSnapshot; export declare function apiKeyFromSecret(secret: ProviderSecret): string | undefined; export declare function fetchJsonWithBearer(input: { fetch: typeof fetch; transport?: ProviderHttpTransport; url: string; apiKey: string; proxy?: string; signal?: AbortSignal; }): Promise<{ ok: true; body: unknown; } | { ok: false; status: number; retryAfter?: string | null; }>; export declare function numeric(value: unknown): number | undefined; export declare function decimalAmount(value: unknown): { exact?: string; numeric?: number; displayApprox?: boolean; }; export declare function normalizeProvider(provider: string): string; export {};