@schematichq/schematic-js 1.4.0 → 1.5.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.
@@ -67,11 +67,11 @@ export declare interface CheckFlagResponseData {
67
67
  featureUsageEvent?: string | null;
68
68
  /**
69
69
  * Deprecated: Use Entitlement.MetricPeriod instead.
70
- * @type {string}
70
+ * @type {MetricPeriod}
71
71
  * @memberof CheckFlagResponseData
72
72
  * @deprecated
73
73
  */
74
- featureUsagePeriod?: string | null;
74
+ featureUsagePeriod?: MetricPeriod | null;
75
75
  /**
76
76
  * Deprecated: Use Entitlement.MetricResetAt instead.
77
77
  * @type {Date}
@@ -105,10 +105,10 @@ export declare interface CheckFlagResponseData {
105
105
  ruleId?: string | null;
106
106
  /**
107
107
  * If a rule was found, its type
108
- * @type {string}
108
+ * @type {RuleType}
109
109
  * @memberof CheckFlagResponseData
110
110
  */
111
- ruleType?: string | null;
111
+ ruleType?: RuleType_2 | null;
112
112
  /**
113
113
  * If user keys were provided and matched a user, its ID
114
114
  * @type {string}
@@ -130,8 +130,14 @@ export declare type CheckFlagReturn = {
130
130
  featureUsageExceeded?: boolean;
131
131
  /** If company keys were provided and matched a company, its ID */
132
132
  companyId?: string;
133
- /** If the company has a credit-based entitlement for this feature, the remaining credit amount */
133
+ /** If the company has a credit-based entitlement for this feature, the ID of the credit */
134
+ creditId?: string;
135
+ /** If the company has a credit-based entitlement for this feature, the credit available to fund new consumption excluding any open lease hold (the value lease-holding SDKs gate on) */
134
136
  creditRemaining?: number;
137
+ /** If the company has a credit-based entitlement for this feature, the unspent amount held by an open credit lease, 0 when none is open */
138
+ creditReserved?: number;
139
+ /** If the company has a credit-based entitlement for this feature, the spendable balance including any open lease hold (creditRemaining + creditReserved); the number to display to end users */
140
+ creditSettled?: number;
135
141
  /** If an error occurred while checking the flag, the error message */
136
142
  error?: string;
137
143
  /** If a numeric feature entitlement rule was matched, its allocation */
@@ -192,6 +198,14 @@ declare interface CheckFlagsResponse {
192
198
  * @interface CheckFlagsResponseData
193
199
  */
194
200
  declare interface CheckFlagsResponseData {
201
+ /**
202
+ * Lease-aware credit balances keyed by credit ID, covering every credit type the company holds a balance in
203
+ * @type {{ [key: string]: CompanyCreditBalance; }}
204
+ * @memberof CheckFlagsResponseData
205
+ */
206
+ creditBalances?: {
207
+ [key: string]: CompanyCreditBalance;
208
+ };
195
209
  /**
196
210
  *
197
211
  * @type {Array<CheckFlagResponseData>}
@@ -225,6 +239,62 @@ export declare const CheckPlanReturnFromJSON: (json: any) => CheckPlanReturn;
225
239
 
226
240
  export declare type CheckPlanReturnListenerFn = (value: CheckPlanReturn) => void;
227
241
 
242
+ /**
243
+ * Schematic API
244
+ * Schematic API
245
+ *
246
+ * The version of the OpenAPI document: 0.1
247
+ *
248
+ *
249
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
250
+ * https://openapi-generator.tech
251
+ * Do not edit the class manually.
252
+ */
253
+ /**
254
+ *
255
+ * @export
256
+ * @interface CompanyCreditBalance
257
+ */
258
+ export declare interface CompanyCreditBalance {
259
+ /**
260
+ * Remaining credit, excluding any open lease hold (the value SDKs gate on)
261
+ * @type {number}
262
+ * @memberof CompanyCreditBalance
263
+ */
264
+ remaining: number;
265
+ /**
266
+ * Amount held by the company's open credit lease, 0 when none is open
267
+ * @type {number}
268
+ * @memberof CompanyCreditBalance
269
+ */
270
+ reserved: number;
271
+ /**
272
+ * Spendable balance including the open lease hold (remaining + reserved)
273
+ * @type {number}
274
+ * @memberof CompanyCreditBalance
275
+ */
276
+ settled: number;
277
+ }
278
+
279
+ /** A company's lease-aware balance for a single credit type */
280
+ export declare type CreditBalance = {
281
+ /** Spendable balance including any open lease hold (remaining + reserved); the number to display to end users */
282
+ settled: number;
283
+ /** Remaining credit excluding any open lease hold (the value lease-holding SDKs gate on) */
284
+ remaining: number;
285
+ /** Amount held by the company's open credit lease, 0 when none is open */
286
+ reserved: number;
287
+ };
288
+
289
+ export declare type CreditBalanceListenerFn = CreditBalancesListenerFn | EmptyListenerFn;
290
+
291
+ /** A company's lease-aware credit balances keyed by credit ID */
292
+ export declare type CreditBalances = Record<string, CreditBalance>;
293
+
294
+ export declare const CreditBalancesFromJSON: (json: any) => CreditBalances;
295
+
296
+ export declare type CreditBalancesListenerFn = (value: CreditBalances) => void;
297
+
228
298
  /**
229
299
  *
230
300
  * @export
@@ -406,6 +476,12 @@ declare interface FeatureEntitlement {
406
476
  * @memberof FeatureEntitlement
407
477
  */
408
478
  allocation?: number | null;
479
+ /**
480
+ * If the company has a credit-based entitlement for this feature, the credit cost per unit of usage
481
+ * @type {number}
482
+ * @memberof FeatureEntitlement
483
+ */
484
+ consumptionRate?: number | null;
409
485
  /**
410
486
  * If the company has a credit-based entitlement for this feature, the ID of the credit
411
487
  * @type {string}
@@ -413,11 +489,23 @@ declare interface FeatureEntitlement {
413
489
  */
414
490
  creditId?: string | null;
415
491
  /**
416
- * If the company has a credit-based entitlement for this feature, the remaining credit amount
492
+ * If the company has a credit-based entitlement for this feature, the credit available to fund new consumption or a new lease hold — open lease holds are excluded. Clients that hold a lease should gate on this plus their own unspent hold; clients with no lease awareness should use credit_settled instead
417
493
  * @type {number}
418
494
  * @memberof FeatureEntitlement
419
495
  */
420
496
  creditRemaining?: number | null;
497
+ /**
498
+ * If the company has a credit-based entitlement for this feature, the unspent amount held by an open credit lease. Returns to credit_remaining when the lease is released
499
+ * @type {number}
500
+ * @memberof FeatureEntitlement
501
+ */
502
+ creditReserved?: number | null;
503
+ /**
504
+ * If the company has a credit-based entitlement for this feature, the balance net of actual consumption, unaffected by open lease holds (credit_remaining plus credit_reserved). The number to display to end users
505
+ * @type {number}
506
+ * @memberof FeatureEntitlement
507
+ */
508
+ creditSettled?: number | null;
421
509
  /**
422
510
  * If the company has a credit-based entitlement for this feature, the total credit amount
423
511
  * @type {number}
@@ -436,6 +524,12 @@ declare interface FeatureEntitlement {
436
524
  * @memberof FeatureEntitlement
437
525
  */
438
526
  eventName?: string | null;
527
+ /**
528
+ * For event-based or credit-metered feature entitlements, the event subtype whose usage is tracked
529
+ * @type {string}
530
+ * @memberof FeatureEntitlement
531
+ */
532
+ eventSubtype?: string | null;
439
533
  /**
440
534
  * The ID of the feature
441
535
  * @type {string}
@@ -450,10 +544,10 @@ declare interface FeatureEntitlement {
450
544
  featureKey: string;
451
545
  /**
452
546
  * For event-based feature entitlements, the period over which usage is tracked
453
- * @type {string}
547
+ * @type {MetricPeriod}
454
548
  * @memberof FeatureEntitlement
455
549
  */
456
- metricPeriod?: FeatureEntitlementMetricPeriodEnum | null;
550
+ metricPeriod?: MetricPeriod | null;
457
551
  /**
458
552
  * For event-based feature entitlements, when the usage period will reset
459
553
  * @type {Date}
@@ -462,10 +556,10 @@ declare interface FeatureEntitlement {
462
556
  metricResetAt?: Date | null;
463
557
  /**
464
558
  * For event-based feature entitlements that have a monthly period, whether that monthly reset is based on the calendar month or a billing cycle
465
- * @type {string}
559
+ * @type {MetricPeriodMonthReset}
466
560
  * @memberof FeatureEntitlement
467
561
  */
468
- monthReset?: FeatureEntitlementMonthResetEnum | null;
562
+ monthReset?: MetricPeriodMonthReset | null;
469
563
  /**
470
564
  * For usage-based pricing, the soft limit for overage charges or the next tier boundary
471
565
  * @type {number}
@@ -486,34 +580,36 @@ declare interface FeatureEntitlement {
486
580
  valueType: EntitlementValueType;
487
581
  }
488
582
 
583
+ export declare type FlagCheckListenerFn = CheckFlagReturnListenerFn | EmptyListenerFn;
584
+
585
+ export declare type FlagValueListenerFn = BooleanListenerFn | EmptyListenerFn;
586
+
587
+ /** A record of unique key-value pairs used for identifying a company or user */
588
+ export declare type Keys = Record<string, string>;
589
+
489
590
  /**
591
+ *
490
592
  * @export
491
593
  */
492
- declare const FeatureEntitlementMetricPeriodEnum: {
594
+ declare const MetricPeriod: {
493
595
  readonly AllTime: "all_time";
494
596
  readonly CurrentDay: "current_day";
495
597
  readonly CurrentMonth: "current_month";
496
598
  readonly CurrentWeek: "current_week";
497
599
  };
498
600
 
499
- declare type FeatureEntitlementMetricPeriodEnum = (typeof FeatureEntitlementMetricPeriodEnum)[keyof typeof FeatureEntitlementMetricPeriodEnum];
601
+ declare type MetricPeriod = (typeof MetricPeriod)[keyof typeof MetricPeriod];
500
602
 
501
603
  /**
604
+ *
502
605
  * @export
503
606
  */
504
- declare const FeatureEntitlementMonthResetEnum: {
505
- readonly FirstOfMonth: "first_of_month";
607
+ declare const MetricPeriodMonthReset: {
506
608
  readonly BillingCycle: "billing_cycle";
609
+ readonly FirstOfMonth: "first_of_month";
507
610
  };
508
611
 
509
- declare type FeatureEntitlementMonthResetEnum = (typeof FeatureEntitlementMonthResetEnum)[keyof typeof FeatureEntitlementMonthResetEnum];
510
-
511
- export declare type FlagCheckListenerFn = CheckFlagReturnListenerFn | EmptyListenerFn;
512
-
513
- export declare type FlagValueListenerFn = BooleanListenerFn | EmptyListenerFn;
514
-
515
- /** A record of unique key-value pairs used for identifying a company or user */
516
- export declare type Keys = Record<string, string>;
612
+ declare type MetricPeriodMonthReset = (typeof MetricPeriodMonthReset)[keyof typeof MetricPeriodMonthReset];
517
613
 
518
614
  export declare type PendingListenerFn = BooleanListenerFn | EmptyListenerFn;
519
615
 
@@ -536,6 +632,22 @@ export declare enum RuleType {
536
632
  DEFAULT = "default"
537
633
  }
538
634
 
635
+ /**
636
+ *
637
+ * @export
638
+ */
639
+ declare const RuleType_2: {
640
+ readonly CompanyOverride: "company_override";
641
+ readonly CompanyOverrideUsageExceeded: "company_override_usage_exceeded";
642
+ readonly Default: "default";
643
+ readonly GlobalOverride: "global_override";
644
+ readonly PlanEntitlement: "plan_entitlement";
645
+ readonly PlanEntitlementUsageExceeded: "plan_entitlement_usage_exceeded";
646
+ readonly Standard: "standard";
647
+ };
648
+
649
+ declare type RuleType_2 = (typeof RuleType_2)[keyof typeof RuleType_2];
650
+
539
651
  export declare class Schematic {
540
652
  private additionalHeaders;
541
653
  private apiKey;
@@ -552,11 +664,17 @@ export declare class Schematic {
552
664
  private isPending;
553
665
  private isPendingListeners;
554
666
  private planListeners;
667
+ private creditBalanceListeners;
555
668
  private storage;
669
+ private persistFlagState;
670
+ private flagStateCacheKey;
671
+ private flagStateCacheMaxAgeMs;
672
+ private cachedFlagState;
556
673
  private useWebSocket;
557
674
  private checks;
558
675
  private featureUsageEventMap;
559
676
  private planChecks;
677
+ private creditBalances;
560
678
  private webSocketUrl;
561
679
  private webSocketConnectionTimeout;
562
680
  private webSocketReconnect;
@@ -663,6 +781,13 @@ export declare class Schematic {
663
781
  private stopRetryTimer;
664
782
  private flushEventQueue;
665
783
  private getAnonymousId;
784
+ private hasCachedValuesForContext;
785
+ private readFlagStateCache;
786
+ private reviveCachedCheck;
787
+ private reviveCachedPlan;
788
+ private reviveCachedCreditBalances;
789
+ private hydrateFlagStateFromCache;
790
+ private persistContextToCache;
666
791
  private handleEvent;
667
792
  private sendEvent;
668
793
  private storeEvent;
@@ -762,6 +887,10 @@ export declare class Schematic {
762
887
  addIsPendingListener: (listener: PendingListenerFn) => () => void;
763
888
  private setIsPending;
764
889
  getPlan: () => CheckPlanReturn | undefined;
890
+ /** Get the company's lease-aware credit balances for the current context, keyed by credit ID */
891
+ getCreditBalances: () => CreditBalances;
892
+ /** Get the company's lease-aware balance for a single credit type in the current context */
893
+ getCreditBalance: (creditId: string) => CreditBalance | undefined;
765
894
  getFlagCheck: (flagKey: string) => CheckFlagReturn | undefined;
766
895
  getFlagValue: (flagKey: string) => boolean | undefined;
767
896
  /** Register an event listener that will be notified with the boolean value for a given flag when this value changes */
@@ -769,11 +898,14 @@ export declare class Schematic {
769
898
  /** Register an event listener that will be notified with the full flag check response for a given flag whenever this value changes */
770
899
  addFlagCheckListener: (flagKey: string, listener: FlagCheckListenerFn) => () => void;
771
900
  addPlanListener: (listener: PlanListenerFn) => () => void;
901
+ /** Register an event listener that will be notified with the company's credit balances (keyed by credit ID) whenever they change */
902
+ addCreditBalanceListener: (listener: CreditBalanceListenerFn) => () => void;
772
903
  private notifyFlagCheckListeners;
773
904
  /** Add or update a CheckFlagReturn in the featureUsageEventMap */
774
905
  private updateFeatureUsageEventMap;
775
906
  private notifyFlagValueListeners;
776
907
  private notifyPlanListeners;
908
+ private notifyCreditBalanceListeners;
777
909
  }
778
910
 
779
911
  /** Context for checking flags and sending events */
@@ -798,6 +930,12 @@ export declare type SchematicOptions = {
798
930
  offline?: boolean;
799
931
  /** Optionally provide a custom storage persister for client-side storage */
800
932
  storage?: StoragePersister;
933
+ /** Persist flag check results (and plan) keyed by context to the storage persister so subsequent page loads can boot with last-known values rather than fallbacks. Defaults to true; set false to disable.
934
+ *
935
+ * When a cache hit is found on `setContext`, `isPending` flips to false synchronously even if the cache only contains a subset of the flags the app subscribes to — uncached flags will resolve via configured fallbacks (`flagCheckDefaults`/`flagValueDefaults`) until the WebSocket reconciles. */
936
+ persistFlagState?: boolean;
937
+ /** Maximum age (ms) of a persisted cache entry before it is treated as stale on hydration. Per-context; older entries are dropped at construction time and on the next persist. Defaults to 7 days. */
938
+ flagStateCacheMaxAgeMs?: number;
801
939
  /** Use a WebSocket connection for real-time flag checks; if using this, run the cleanup function to close the connection */
802
940
  useWebSocket?: boolean;
803
941
  /** Optionally provide a custom WebSocket URL */