@schematichq/schematic-typescript-node 1.6.0 → 1.6.2

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.
Files changed (75) hide show
  1. package/README.md +51 -28
  2. package/dist/api/resources/credits/client/requests/AcquireCreditLeaseRequestBody.d.ts +1 -0
  3. package/dist/api/resources/credits/client/requests/ExtendCreditLeaseRequestBody.d.ts +3 -0
  4. package/dist/api/resources/entitlements/client/requests/CreateBillingLinkedPlanEntitlementRequestBody.d.ts +4 -0
  5. package/dist/api/resources/entitlements/client/requests/CreatePlanEntitlementRequestBody.d.ts +4 -0
  6. package/dist/api/resources/entitlements/client/requests/UpdatePlanEntitlementRequestBody.d.ts +4 -0
  7. package/dist/api/resources/features/client/requests/CheckAndReserveFlagRequestBody.d.ts +4 -2
  8. package/dist/api/resources/planmigrations/client/Client.d.ts +4 -2
  9. package/dist/api/resources/planmigrations/client/requests/CountMigrationsRequest.d.ts +3 -1
  10. package/dist/api/resources/planmigrations/client/requests/ListMigrationsRequest.d.ts +3 -1
  11. package/dist/api/resources/planmigrations/types/CountMigrationsParams.d.ts +1 -0
  12. package/dist/api/resources/planmigrations/types/ListMigrationsParams.d.ts +1 -0
  13. package/dist/api/resources/plans/client/Client.d.ts +4 -0
  14. package/dist/api/resources/plans/client/requests/CountPlansRequest.d.ts +6 -0
  15. package/dist/api/resources/plans/client/requests/ListPlansRequest.d.ts +6 -0
  16. package/dist/api/resources/plans/types/CountPlansParams.d.ts +4 -0
  17. package/dist/api/resources/plans/types/ListPlansParams.d.ts +4 -0
  18. package/dist/api/types/BillingArrearsCadence.d.ts +1 -0
  19. package/dist/api/types/BillingCreditGrantReason.d.ts +3 -0
  20. package/dist/api/types/BillingCreditResponseData.d.ts +1 -0
  21. package/dist/api/types/BillingPlanCreditGrantBillingMode.d.ts +5 -0
  22. package/dist/api/types/BillingPlanCreditGrantPriceTierResponseData.d.ts +10 -0
  23. package/dist/api/types/BillingPlanCreditGrantResponseData.d.ts +12 -0
  24. package/dist/api/types/CompanyPlanCreditGrantView.d.ts +3 -0
  25. package/dist/api/types/CreateBillingPlanCreditGrantRequestBody.d.ts +11 -1
  26. package/dist/api/types/CreateEntitlementInBundleRequestBody.d.ts +4 -0
  27. package/dist/api/types/CreditEventLedgerResponseData.d.ts +1 -0
  28. package/dist/api/types/CreditGrantPriceTierRequestBody.d.ts +8 -0
  29. package/dist/api/types/CreditTransferReason.d.ts +7 -0
  30. package/dist/api/types/PlanCreditGrantView.d.ts +3 -0
  31. package/dist/api/types/PlanEntitlementResponseData.d.ts +4 -0
  32. package/dist/api/types/PlanVersionMigrationResponseData.d.ts +2 -0
  33. package/dist/api/types/UpdateBillingPlanCreditGrantRequestBody.d.ts +11 -1
  34. package/dist/api/types/UsageBasedEntitlementRequestBody.d.ts +4 -0
  35. package/dist/api/types/index.d.ts +4 -0
  36. package/dist/cache/redis.d.ts +11 -0
  37. package/dist/credits/check.d.ts +10 -5
  38. package/dist/credits/index.d.ts +4 -4
  39. package/dist/credits/lease-manager.d.ts +74 -2
  40. package/dist/credits/lease-store.d.ts +27 -11
  41. package/dist/credits/redis-lease-store.d.ts +2 -2
  42. package/dist/credits/server-check.d.ts +40 -0
  43. package/dist/credits/track.d.ts +1 -1
  44. package/dist/credits/types.d.ts +37 -4
  45. package/dist/index.d.ts +1 -1
  46. package/dist/index.js +12 -10
  47. package/dist/serialization/resources/credits/client/requests/ExtendCreditLeaseRequestBody.d.ts +1 -0
  48. package/dist/serialization/resources/entitlements/client/requests/CreateBillingLinkedPlanEntitlementRequestBody.d.ts +4 -0
  49. package/dist/serialization/resources/entitlements/client/requests/CreatePlanEntitlementRequestBody.d.ts +4 -0
  50. package/dist/serialization/resources/entitlements/client/requests/UpdatePlanEntitlementRequestBody.d.ts +4 -0
  51. package/dist/serialization/resources/features/client/requests/CheckAndReserveFlagRequestBody.d.ts +1 -0
  52. package/dist/serialization/resources/planmigrations/types/CountMigrationsParams.d.ts +1 -0
  53. package/dist/serialization/resources/planmigrations/types/ListMigrationsParams.d.ts +1 -0
  54. package/dist/serialization/resources/plans/types/CountPlansParams.d.ts +2 -0
  55. package/dist/serialization/resources/plans/types/ListPlansParams.d.ts +2 -0
  56. package/dist/serialization/types/BillingArrearsCadence.d.ts +1 -1
  57. package/dist/serialization/types/BillingCreditGrantReason.d.ts +1 -1
  58. package/dist/serialization/types/BillingCreditResponseData.d.ts +2 -0
  59. package/dist/serialization/types/BillingPlanCreditGrantBillingMode.d.ts +7 -0
  60. package/dist/serialization/types/BillingPlanCreditGrantPriceTierResponseData.d.ts +12 -0
  61. package/dist/serialization/types/BillingPlanCreditGrantResponseData.d.ts +10 -0
  62. package/dist/serialization/types/CompanyPlanCreditGrantView.d.ts +5 -0
  63. package/dist/serialization/types/CreateBillingPlanCreditGrantRequestBody.d.ts +8 -0
  64. package/dist/serialization/types/CreateEntitlementInBundleRequestBody.d.ts +4 -0
  65. package/dist/serialization/types/CreditEventLedgerResponseData.d.ts +2 -0
  66. package/dist/serialization/types/CreditGrantPriceTierRequestBody.d.ts +11 -0
  67. package/dist/serialization/types/CreditTransferReason.d.ts +7 -0
  68. package/dist/serialization/types/PlanCreditGrantView.d.ts +5 -0
  69. package/dist/serialization/types/PlanEntitlementResponseData.d.ts +4 -0
  70. package/dist/serialization/types/PlanVersionMigrationResponseData.d.ts +2 -0
  71. package/dist/serialization/types/UpdateBillingPlanCreditGrantRequestBody.d.ts +8 -0
  72. package/dist/serialization/types/UsageBasedEntitlementRequestBody.d.ts +4 -0
  73. package/dist/serialization/types/index.d.ts +4 -0
  74. package/dist/wrapper.d.ts +91 -26
  75. package/package.json +5 -1
package/dist/wrapper.d.ts CHANGED
@@ -55,36 +55,51 @@ export interface SchematicOptions {
55
55
  /** The default maximum time to wait for a response in milliseconds */
56
56
  timeoutMs?: number;
57
57
  /**
58
- * Enable client-side credit lease + reservation behavior on `check` /
59
- * `trackWithReservation`. Omit to keep the SDK lease-unaware.
58
+ * Enable credit reservation behavior on `check` / `trackWithReservation`.
59
+ * Omit to keep the SDK credit-unaware.
60
60
  *
61
- * Lease-bearing checks require DataStream (or replicator mode) so the
62
- * SDK has access to the cached flag + company state needed for local
63
- * gating against the lease balance.
61
+ * Client mode (local leases) requires DataStream (or replicator mode) so
62
+ * the SDK has the cached flag + company state it needs to gate locally
63
+ * against the lease balance. Without DataStream the SDK gates in server
64
+ * mode instead — one `check-and-reserve` API call per check. See
65
+ * `CreditLeaseConfig.mode`.
64
66
  */
65
67
  creditLeases?: CreditLeaseConfig;
66
68
  }
69
+ /**
70
+ * Options for a single flag check.
71
+ *
72
+ * `creditCost`, `usage` and `eventUsage` are preflight inputs: they ask
73
+ * "would this action be allowed?" rather than "is it allowed now". They apply
74
+ * on every evaluation path, forwarded to the WASM rules engine's
75
+ * `checkFlagWithOptions` when the flag is evaluated locally (DataStream) and
76
+ * sent as the request body's `preflight` when it is evaluated over the REST
77
+ * API. The engine picks the most specific knob for each condition it
78
+ * evaluates. No reservation is issued; for lease-backed gating use `check()`.
79
+ */
67
80
  export interface CheckFlagOptions {
68
81
  /** Default value to return on error. Can be a boolean or a function returning a boolean. If not provided, uses configured flag defaults */
69
82
  defaultValue?: boolean | (() => boolean);
70
83
  /** The maximum time to wait for a response in milliseconds */
71
84
  timeoutMs?: number;
72
85
  /**
73
- * Preflight inputs forwarded to the WASM rules engine's
74
- * `checkFlagWithOptions`. The engine picks the most specific knob for each
75
- * condition it evaluates. Only applied when the flag is evaluated locally
76
- * (DataStream); the REST fallback path does not support preflight and
77
- * ignores these. No reservation is issued — for lease-backed gating use
78
- * `check()` instead.
86
+ * Pre-computed cost of the action, keyed by credit id. Highest precedence
87
+ * for credit-balance gates. A cost of zero means the action is free, not
88
+ * that the input is absent.
79
89
  */
80
- /** Pre-computed per-credit-id cost. Highest precedence for credit-balance gates. */
81
90
  creditCost?: Record<string, number>;
82
- /** Single integer quantity applied to whatever numeric condition is being evaluated. */
91
+ /**
92
+ * Quantity applied to whatever numeric condition is being evaluated. Zero
93
+ * has no effect. Both the REST request body and the local (WASM) engine
94
+ * take an integer, so a fraction rounds up rather than letting the check
95
+ * pass on less usage than the action is about to record.
96
+ */
83
97
  usage?: number;
84
98
  /**
85
99
  * Simulated quantity scoped to a specific event subtype. Preferred over
86
100
  * `usage` when the subtype is known. Deliberately singular: one check
87
- * preflights one action.
101
+ * preflights one action. `quantity` rounds up the same way `usage` does,
102
+ * and zero has no effect.
88
103
  */
89
104
  eventUsage?: {
90
105
  eventSubtype: string;
@@ -145,6 +160,10 @@ export declare class SchematicClient extends BaseClient {
145
160
  private reservations?;
146
161
  private leaseBackendShared;
147
162
  private prewarmResolveTimeoutMs;
163
+ private creditLeaseMode?;
164
+ private serverReservationTTL;
165
+ private closing;
166
+ private readonly pendingPrewarms;
148
167
  /**
149
168
  * Creates a new instance of the SchematicClient
150
169
  * @param opts - Configuration options for the client
@@ -154,6 +173,23 @@ export declare class SchematicClient extends BaseClient {
154
173
  * Returns whether DataStream is enabled and available
155
174
  */
156
175
  private useDataStream;
176
+ /**
177
+ * Whether the configured mode wants the local lease plumbing (stores,
178
+ * manager, sweeper). Read during construction, after the DataStream client
179
+ * has been wired, so `auto` can resolve against it.
180
+ */
181
+ private creditLeaseModeUsesLeases;
182
+ /**
183
+ * Which reservation mode a `check()` with `usage` resolves to right now.
184
+ * `undefined` means no credit gating at all — `creditLeases` isn't
185
+ * configured, or the client is offline.
186
+ *
187
+ * `auto` is resolved per check rather than once at startup so a DataStream
188
+ * that failed to start after construction (its `start()` rejection clears
189
+ * `datastreamClient`) falls to server mode instead of silently dropping
190
+ * every check to a plain, ungated flag check.
191
+ */
192
+ private effectiveLeaseMode;
157
193
  /**
158
194
  * Checks the value of a feature flag for the given evaluation context
159
195
  * @param evalCtx - The context (company and/or user) for evaluating the feature flag
@@ -201,6 +237,11 @@ export declare class SchematicClient extends BaseClient {
201
237
  * are still drawing on — doing so would refund the grant server-side and
202
238
  * pull the balance out from under them. Shared leases reclaim themselves:
203
239
  * they expire (client- and server-side) or are fully consumed.
240
+ *
241
+ * Lease work already in flight is waited out before the release, so an
242
+ * acquire that lands mid-shutdown is one the release can see. The wait and
243
+ * the release share one `SHUTDOWN_DRAIN_TIMEOUT_MS` budget, so close()
244
+ * stays bounded however slow the store or the wire is.
204
245
  * @returns Promise that resolves when everything has been stopped
205
246
  */
206
247
  close(): Promise<void>;
@@ -218,10 +259,10 @@ export declare class SchematicClient extends BaseClient {
218
259
  */
219
260
  prewarm(evalCtx: api.CheckFlagRequestBody, creditTypeIds: string[]): Promise<void>;
220
261
  /**
221
- * Like `resolveCompanyId` but actively fetches the company over the
222
- * datastream when only secondary keys are supplied, warming the cache as a
223
- * side effect. Returns the resolved id, or undefined if the company never
224
- * surfaced within `prewarmResolveTimeoutMs`.
262
+ * Like `resolveCompanyId` but, on a cache miss, actively fetches the
263
+ * company over the datastream, warming the cache as a side effect. Returns
264
+ * the resolved id, the `comp_`-prefixed id the keys carry if the company
265
+ * never surfaced within `prewarmResolveTimeoutMs`, or undefined.
225
266
  *
226
267
  * `identify` does not push a company into the datastream cache — companies
227
268
  * are only streamed in response to a request. So we call `getCompany`
@@ -232,19 +273,24 @@ export declare class SchematicClient extends BaseClient {
232
273
  */
233
274
  private resolveCompanyIdWithWait;
234
275
  /**
235
- * Lease-aware feature check. When `creditLeases` is configured and the
276
+ * Credit-aware feature check. When `creditLeases` is configured and the
236
277
  * caller passes `usage` (optionally qualified by `eventSubtype`), this
237
- * acquires a lease (if needed), gates the check against the lease's local
238
- * balance via WASM, and returns a reservation handle on success — pass
239
- * that handle to `trackWithReservation` when the work completes.
278
+ * gates the check against the company's credit balance and returns a
279
+ * reservation handle on success — pass that handle to
280
+ * `trackWithReservation` when the work completes.
281
+ *
282
+ * In client mode (DataStream enabled) the hold is carved out of a local
283
+ * lease and the flag is evaluated by the WASM engine. In server mode it is
284
+ * a single `check-and-reserve` API call that evaluates the flag and takes
285
+ * the hold server-side. `creditLeases.mode` picks; the default (`auto`)
286
+ * uses client mode when DataStream is enabled and server mode otherwise.
240
287
  *
241
288
  * When `creditLeases` is not configured (or `usage` is omitted) this falls
242
289
  * through to a plain flag check and returns `{allowed: value}` with no
243
290
  * reservation, byte-compatible with `checkFlag` semantics. The caller's
244
291
  * preflight (`usage` / `eventSubtype`) is still threaded through the plain
245
- * check, so any client-side evaluation path (datastream, replicator)
246
- * gates on the post-call balance — just without a reservation. Only the
247
- * REST fallback ignores preflight.
292
+ * check, so every evaluation path (datastream, replicator, REST) gates on
293
+ * the post-call balance — just without a reservation.
248
294
  */
249
295
  check(evalCtx: api.CheckFlagRequestBody, key: string, options?: CheckOptions): Promise<CheckResult>;
250
296
  /**
@@ -255,6 +301,10 @@ export declare class SchematicClient extends BaseClient {
255
301
  * `actualQuantity × consumption_rate` from the company's real credit
256
302
  * balance.
257
303
  *
304
+ * A server-mode handle has no local hold to refund: the Track event carries
305
+ * the reservation id, and the server settles the hold (refunding the
306
+ * unspent slice) when it processes the event.
307
+ *
258
308
  * If the work outlived the reservation's TTL and the sweeper already
259
309
  * returned the hold to the lease, the local refund has happened but the
260
310
  * usage must still be billed — so we emit the Track anyway (a "recovery"
@@ -269,7 +319,15 @@ export declare class SchematicClient extends BaseClient {
269
319
  * accidental second `trackWithReservation`, collapses to a single billed
270
320
  * event — across pods and process restarts, not just within one process.
271
321
  */
272
- trackWithReservation(reservation: Reservation, actualQuantity: number, options?: TrackWithReservationOptions): Promise<void>;
322
+ trackWithReservation(reservation: Reservation | undefined, actualQuantity: number, options?: TrackWithReservationOptions): Promise<void>;
323
+ /**
324
+ * The Schematic company id for an evaluation context, resolved in the
325
+ * server's order: every supplied key/value pair is an ordinary entity key
326
+ * and gets looked up first; only when nothing matches is a value read as
327
+ * the company's own id, by its `comp_` prefix rather than by the name of
328
+ * the key it sits under. An account is free to define a key called `id`
329
+ * holding its own identifier, so the name alone settles nothing.
330
+ */
273
331
  private resolveCompanyId;
274
332
  /**
275
333
  * Send a non-blocking event to create or update companies and users.
@@ -286,6 +344,13 @@ export declare class SchematicClient extends BaseClient {
286
344
  * @throws Will log error if event enqueueing fails
287
345
  */
288
346
  track(body: api.EventBodyTrack, options?: TrackOptions): Promise<void>;
347
+ /**
348
+ * Enqueue a track event, optimistically bumping the cached company metric
349
+ * with it unless `updateMetrics` says not to. The bump is a local
350
+ * prediction of what the stream will push back, so it belongs only to an
351
+ * event that records usage the server has not already counted.
352
+ */
353
+ private emitTrack;
289
354
  /**
290
355
  * Sets the default value for a specific feature flag
291
356
  * @param flag - The feature flag identifier
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schematichq/schematic-typescript-node",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -57,6 +57,10 @@
57
57
  "crypto": false,
58
58
  "timers": false
59
59
  },
60
+ "resolutions": {
61
+ "sharp": "^0.35.4",
62
+ "undici": "7.29.0"
63
+ },
60
64
  "packageManager": "yarn@1.22.22",
61
65
  "engines": {
62
66
  "node": ">=18.0.0"