@stackbe/sdk 0.4.0 → 0.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.
package/dist/index.d.mts CHANGED
@@ -11,6 +11,7 @@ declare class HttpClient {
11
11
  private request;
12
12
  get<T>(path: string, params?: Record<string, string | number | undefined>): Promise<T>;
13
13
  post<T>(path: string, body?: unknown, params?: Record<string, string | number | undefined>): Promise<T>;
14
+ put<T>(path: string, body?: unknown, params?: Record<string, string | number | undefined>): Promise<T>;
14
15
  patch<T>(path: string, body?: unknown): Promise<T>;
15
16
  delete<T>(path: string): Promise<T>;
16
17
  }
@@ -89,6 +90,69 @@ interface CustomerUsageResponse {
89
90
  billingPeriod: string;
90
91
  metrics: UsageMetric[];
91
92
  }
93
+ interface SyncUsageOptions {
94
+ /** Idempotency key to prevent duplicate syncs */
95
+ idempotencyKey?: string;
96
+ }
97
+ interface SyncUsageResponse {
98
+ success: boolean;
99
+ currentUsage: number;
100
+ billingPeriod: string;
101
+ limit: number | null;
102
+ remaining: number | null;
103
+ }
104
+ interface CheckUsageWithAddonsResponse extends CheckUsageResponse {
105
+ /** Limit from plan (before addons) */
106
+ planLimit: number | null;
107
+ /** Total addon credits available */
108
+ addonCredits: number;
109
+ /** Total effective limit (plan + addons) */
110
+ effectiveLimit: number | null;
111
+ }
112
+ interface TrackUsageWithAddonsResponse extends TrackUsageResponse {
113
+ /** Usage deducted from plan allocation */
114
+ planUsage: number;
115
+ /** Usage deducted from addon credits */
116
+ addonUsage: number;
117
+ }
118
+ interface UsageAddon {
119
+ id: string;
120
+ appId: string;
121
+ metricId: string;
122
+ metricName: string;
123
+ name: string;
124
+ description?: string;
125
+ quantity: number;
126
+ priceCents: number;
127
+ currency: string;
128
+ status: 'active' | 'archived';
129
+ stripePriceId?: string;
130
+ createdAt: string;
131
+ }
132
+ interface UsageAddonPurchase {
133
+ id: string;
134
+ addonId: string;
135
+ addonName: string;
136
+ customerId: string;
137
+ metricId: string;
138
+ metricName: string;
139
+ quantity: number;
140
+ usedQuantity: number;
141
+ remainingQuantity: number;
142
+ priceCents: number;
143
+ currency: string;
144
+ billingPeriod: string;
145
+ expiresAt?: string;
146
+ createdAt: string;
147
+ }
148
+ interface CustomerCreditsResponse {
149
+ customerId: string;
150
+ metricName: string;
151
+ totalPurchased: number;
152
+ totalUsed: number;
153
+ remaining: number;
154
+ purchases: UsageAddonPurchase[];
155
+ }
92
156
  interface CheckEntitlementResponse {
93
157
  /** Whether the customer has access to this feature */
94
158
  hasAccess: boolean;
@@ -409,6 +473,89 @@ declare class UsageClient {
409
473
  trackAndCheck(customerId: string, metric: string, options?: TrackUsageOptions): Promise<TrackUsageResponse & {
410
474
  allowed: boolean;
411
475
  }>;
476
+ /**
477
+ * Sync usage to an absolute value (instead of incrementing).
478
+ * Use this when your app tracks usage internally and reports the final count periodically.
479
+ *
480
+ * @example
481
+ * ```typescript
482
+ * // Your app tracks 847 billable tickets internally
483
+ * await stackbe.usage.sync('cust_123', 'tickets', 847);
484
+ * ```
485
+ */
486
+ sync(customerId: string, metric: string, value: number, options?: SyncUsageOptions): Promise<SyncUsageResponse>;
487
+ /**
488
+ * Check usage limits including purchased add-on credits.
489
+ *
490
+ * @example
491
+ * ```typescript
492
+ * const result = await stackbe.usage.checkWithAddons('cust_123', 'tickets');
493
+ *
494
+ * console.log(`Plan limit: ${result.planLimit}`);
495
+ * console.log(`Addon credits: ${result.addonCredits}`);
496
+ * console.log(`Effective limit: ${result.effectiveLimit}`);
497
+ *
498
+ * if (!result.allowed) {
499
+ * // Prompt to purchase more credits
500
+ * }
501
+ * ```
502
+ */
503
+ checkWithAddons(customerId: string, metric: string): Promise<CheckUsageWithAddonsResponse>;
504
+ /**
505
+ * Track usage with automatic add-on credit deduction.
506
+ * When plan limit is exceeded, usage is deducted from purchased add-on credits.
507
+ *
508
+ * @example
509
+ * ```typescript
510
+ * const result = await stackbe.usage.trackWithAddons('cust_123', 'tickets');
511
+ *
512
+ * if (result.addonUsage > 0) {
513
+ * console.log(`Used ${result.addonUsage} from addon credits`);
514
+ * }
515
+ * ```
516
+ */
517
+ trackWithAddons(customerId: string, metric: string, options?: TrackUsageOptions): Promise<TrackUsageWithAddonsResponse>;
518
+ /**
519
+ * List available add-on packs for purchase.
520
+ *
521
+ * @example
522
+ * ```typescript
523
+ * const addons = await stackbe.usage.listAddons();
524
+ *
525
+ * for (const addon of addons) {
526
+ * console.log(`${addon.name}: ${addon.quantity} ${addon.metricName} for $${addon.priceCents / 100}`);
527
+ * }
528
+ * ```
529
+ */
530
+ listAddons(): Promise<UsageAddon[]>;
531
+ /**
532
+ * Purchase an add-on pack for a customer.
533
+ * Credits are immediately available after purchase.
534
+ *
535
+ * @example
536
+ * ```typescript
537
+ * // Purchase a 10k ticket pack
538
+ * const purchase = await stackbe.usage.purchaseAddon('cust_123', 'addon_xyz');
539
+ * console.log(`Purchased ${purchase.quantity} credits`);
540
+ * ```
541
+ */
542
+ purchaseAddon(customerId: string, addonId: string): Promise<UsageAddonPurchase>;
543
+ /**
544
+ * Get customer's purchased add-on credits for a metric.
545
+ *
546
+ * @example
547
+ * ```typescript
548
+ * const credits = await stackbe.usage.getCredits('cust_123', 'tickets');
549
+ *
550
+ * console.log(`Total purchased: ${credits.totalPurchased}`);
551
+ * console.log(`Remaining: ${credits.remaining}`);
552
+ *
553
+ * for (const purchase of credits.purchases) {
554
+ * console.log(`${purchase.addonName}: ${purchase.remainingQuantity} left`);
555
+ * }
556
+ * ```
557
+ */
558
+ getCredits(customerId: string, metric: string): Promise<CustomerCreditsResponse>;
412
559
  }
413
560
 
414
561
  declare class EntitlementsClient {
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ declare class HttpClient {
11
11
  private request;
12
12
  get<T>(path: string, params?: Record<string, string | number | undefined>): Promise<T>;
13
13
  post<T>(path: string, body?: unknown, params?: Record<string, string | number | undefined>): Promise<T>;
14
+ put<T>(path: string, body?: unknown, params?: Record<string, string | number | undefined>): Promise<T>;
14
15
  patch<T>(path: string, body?: unknown): Promise<T>;
15
16
  delete<T>(path: string): Promise<T>;
16
17
  }
@@ -89,6 +90,69 @@ interface CustomerUsageResponse {
89
90
  billingPeriod: string;
90
91
  metrics: UsageMetric[];
91
92
  }
93
+ interface SyncUsageOptions {
94
+ /** Idempotency key to prevent duplicate syncs */
95
+ idempotencyKey?: string;
96
+ }
97
+ interface SyncUsageResponse {
98
+ success: boolean;
99
+ currentUsage: number;
100
+ billingPeriod: string;
101
+ limit: number | null;
102
+ remaining: number | null;
103
+ }
104
+ interface CheckUsageWithAddonsResponse extends CheckUsageResponse {
105
+ /** Limit from plan (before addons) */
106
+ planLimit: number | null;
107
+ /** Total addon credits available */
108
+ addonCredits: number;
109
+ /** Total effective limit (plan + addons) */
110
+ effectiveLimit: number | null;
111
+ }
112
+ interface TrackUsageWithAddonsResponse extends TrackUsageResponse {
113
+ /** Usage deducted from plan allocation */
114
+ planUsage: number;
115
+ /** Usage deducted from addon credits */
116
+ addonUsage: number;
117
+ }
118
+ interface UsageAddon {
119
+ id: string;
120
+ appId: string;
121
+ metricId: string;
122
+ metricName: string;
123
+ name: string;
124
+ description?: string;
125
+ quantity: number;
126
+ priceCents: number;
127
+ currency: string;
128
+ status: 'active' | 'archived';
129
+ stripePriceId?: string;
130
+ createdAt: string;
131
+ }
132
+ interface UsageAddonPurchase {
133
+ id: string;
134
+ addonId: string;
135
+ addonName: string;
136
+ customerId: string;
137
+ metricId: string;
138
+ metricName: string;
139
+ quantity: number;
140
+ usedQuantity: number;
141
+ remainingQuantity: number;
142
+ priceCents: number;
143
+ currency: string;
144
+ billingPeriod: string;
145
+ expiresAt?: string;
146
+ createdAt: string;
147
+ }
148
+ interface CustomerCreditsResponse {
149
+ customerId: string;
150
+ metricName: string;
151
+ totalPurchased: number;
152
+ totalUsed: number;
153
+ remaining: number;
154
+ purchases: UsageAddonPurchase[];
155
+ }
92
156
  interface CheckEntitlementResponse {
93
157
  /** Whether the customer has access to this feature */
94
158
  hasAccess: boolean;
@@ -409,6 +473,89 @@ declare class UsageClient {
409
473
  trackAndCheck(customerId: string, metric: string, options?: TrackUsageOptions): Promise<TrackUsageResponse & {
410
474
  allowed: boolean;
411
475
  }>;
476
+ /**
477
+ * Sync usage to an absolute value (instead of incrementing).
478
+ * Use this when your app tracks usage internally and reports the final count periodically.
479
+ *
480
+ * @example
481
+ * ```typescript
482
+ * // Your app tracks 847 billable tickets internally
483
+ * await stackbe.usage.sync('cust_123', 'tickets', 847);
484
+ * ```
485
+ */
486
+ sync(customerId: string, metric: string, value: number, options?: SyncUsageOptions): Promise<SyncUsageResponse>;
487
+ /**
488
+ * Check usage limits including purchased add-on credits.
489
+ *
490
+ * @example
491
+ * ```typescript
492
+ * const result = await stackbe.usage.checkWithAddons('cust_123', 'tickets');
493
+ *
494
+ * console.log(`Plan limit: ${result.planLimit}`);
495
+ * console.log(`Addon credits: ${result.addonCredits}`);
496
+ * console.log(`Effective limit: ${result.effectiveLimit}`);
497
+ *
498
+ * if (!result.allowed) {
499
+ * // Prompt to purchase more credits
500
+ * }
501
+ * ```
502
+ */
503
+ checkWithAddons(customerId: string, metric: string): Promise<CheckUsageWithAddonsResponse>;
504
+ /**
505
+ * Track usage with automatic add-on credit deduction.
506
+ * When plan limit is exceeded, usage is deducted from purchased add-on credits.
507
+ *
508
+ * @example
509
+ * ```typescript
510
+ * const result = await stackbe.usage.trackWithAddons('cust_123', 'tickets');
511
+ *
512
+ * if (result.addonUsage > 0) {
513
+ * console.log(`Used ${result.addonUsage} from addon credits`);
514
+ * }
515
+ * ```
516
+ */
517
+ trackWithAddons(customerId: string, metric: string, options?: TrackUsageOptions): Promise<TrackUsageWithAddonsResponse>;
518
+ /**
519
+ * List available add-on packs for purchase.
520
+ *
521
+ * @example
522
+ * ```typescript
523
+ * const addons = await stackbe.usage.listAddons();
524
+ *
525
+ * for (const addon of addons) {
526
+ * console.log(`${addon.name}: ${addon.quantity} ${addon.metricName} for $${addon.priceCents / 100}`);
527
+ * }
528
+ * ```
529
+ */
530
+ listAddons(): Promise<UsageAddon[]>;
531
+ /**
532
+ * Purchase an add-on pack for a customer.
533
+ * Credits are immediately available after purchase.
534
+ *
535
+ * @example
536
+ * ```typescript
537
+ * // Purchase a 10k ticket pack
538
+ * const purchase = await stackbe.usage.purchaseAddon('cust_123', 'addon_xyz');
539
+ * console.log(`Purchased ${purchase.quantity} credits`);
540
+ * ```
541
+ */
542
+ purchaseAddon(customerId: string, addonId: string): Promise<UsageAddonPurchase>;
543
+ /**
544
+ * Get customer's purchased add-on credits for a metric.
545
+ *
546
+ * @example
547
+ * ```typescript
548
+ * const credits = await stackbe.usage.getCredits('cust_123', 'tickets');
549
+ *
550
+ * console.log(`Total purchased: ${credits.totalPurchased}`);
551
+ * console.log(`Remaining: ${credits.remaining}`);
552
+ *
553
+ * for (const purchase of credits.purchases) {
554
+ * console.log(`${purchase.addonName}: ${purchase.remainingQuantity} left`);
555
+ * }
556
+ * ```
557
+ */
558
+ getCredits(customerId: string, metric: string): Promise<CustomerCreditsResponse>;
412
559
  }
413
560
 
414
561
  declare class EntitlementsClient {
package/dist/index.js CHANGED
@@ -157,6 +157,9 @@ var HttpClient = class {
157
157
  async post(path, body, params) {
158
158
  return this.request("POST", path, { body, params });
159
159
  }
160
+ async put(path, body, params) {
161
+ return this.request("PUT", path, { body, params });
162
+ }
160
163
  async patch(path, body) {
161
164
  return this.request("PATCH", path, { body });
162
165
  }
@@ -256,6 +259,122 @@ var UsageClient = class {
256
259
  allowed
257
260
  };
258
261
  }
262
+ /**
263
+ * Sync usage to an absolute value (instead of incrementing).
264
+ * Use this when your app tracks usage internally and reports the final count periodically.
265
+ *
266
+ * @example
267
+ * ```typescript
268
+ * // Your app tracks 847 billable tickets internally
269
+ * await stackbe.usage.sync('cust_123', 'tickets', 847);
270
+ * ```
271
+ */
272
+ async sync(customerId, metric, value, options = {}) {
273
+ const headers = {};
274
+ if (options.idempotencyKey) {
275
+ headers["Idempotency-Key"] = options.idempotencyKey;
276
+ }
277
+ return this.http.put("/v1/usage/sync", {
278
+ customerId,
279
+ metric,
280
+ value
281
+ });
282
+ }
283
+ /**
284
+ * Check usage limits including purchased add-on credits.
285
+ *
286
+ * @example
287
+ * ```typescript
288
+ * const result = await stackbe.usage.checkWithAddons('cust_123', 'tickets');
289
+ *
290
+ * console.log(`Plan limit: ${result.planLimit}`);
291
+ * console.log(`Addon credits: ${result.addonCredits}`);
292
+ * console.log(`Effective limit: ${result.effectiveLimit}`);
293
+ *
294
+ * if (!result.allowed) {
295
+ * // Prompt to purchase more credits
296
+ * }
297
+ * ```
298
+ */
299
+ async checkWithAddons(customerId, metric) {
300
+ return this.http.get(
301
+ `/v1/customers/${customerId}/usage/check-with-addons`,
302
+ { metric }
303
+ );
304
+ }
305
+ /**
306
+ * Track usage with automatic add-on credit deduction.
307
+ * When plan limit is exceeded, usage is deducted from purchased add-on credits.
308
+ *
309
+ * @example
310
+ * ```typescript
311
+ * const result = await stackbe.usage.trackWithAddons('cust_123', 'tickets');
312
+ *
313
+ * if (result.addonUsage > 0) {
314
+ * console.log(`Used ${result.addonUsage} from addon credits`);
315
+ * }
316
+ * ```
317
+ */
318
+ async trackWithAddons(customerId, metric, options = {}) {
319
+ return this.http.post("/v1/usage/addons/track", {
320
+ customerId,
321
+ metric,
322
+ quantity: options.quantity ?? 1
323
+ });
324
+ }
325
+ /**
326
+ * List available add-on packs for purchase.
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * const addons = await stackbe.usage.listAddons();
331
+ *
332
+ * for (const addon of addons) {
333
+ * console.log(`${addon.name}: ${addon.quantity} ${addon.metricName} for $${addon.priceCents / 100}`);
334
+ * }
335
+ * ```
336
+ */
337
+ async listAddons() {
338
+ return this.http.get("/v1/usage/addons");
339
+ }
340
+ /**
341
+ * Purchase an add-on pack for a customer.
342
+ * Credits are immediately available after purchase.
343
+ *
344
+ * @example
345
+ * ```typescript
346
+ * // Purchase a 10k ticket pack
347
+ * const purchase = await stackbe.usage.purchaseAddon('cust_123', 'addon_xyz');
348
+ * console.log(`Purchased ${purchase.quantity} credits`);
349
+ * ```
350
+ */
351
+ async purchaseAddon(customerId, addonId) {
352
+ return this.http.post("/v1/usage/addons/purchase", {
353
+ customerId,
354
+ addonId
355
+ });
356
+ }
357
+ /**
358
+ * Get customer's purchased add-on credits for a metric.
359
+ *
360
+ * @example
361
+ * ```typescript
362
+ * const credits = await stackbe.usage.getCredits('cust_123', 'tickets');
363
+ *
364
+ * console.log(`Total purchased: ${credits.totalPurchased}`);
365
+ * console.log(`Remaining: ${credits.remaining}`);
366
+ *
367
+ * for (const purchase of credits.purchases) {
368
+ * console.log(`${purchase.addonName}: ${purchase.remainingQuantity} left`);
369
+ * }
370
+ * ```
371
+ */
372
+ async getCredits(customerId, metric) {
373
+ return this.http.get(
374
+ `/v1/customers/${customerId}/usage/credits`,
375
+ { metric }
376
+ );
377
+ }
259
378
  };
260
379
 
261
380
  // src/entitlements.ts
package/dist/index.mjs CHANGED
@@ -124,6 +124,9 @@ var HttpClient = class {
124
124
  async post(path, body, params) {
125
125
  return this.request("POST", path, { body, params });
126
126
  }
127
+ async put(path, body, params) {
128
+ return this.request("PUT", path, { body, params });
129
+ }
127
130
  async patch(path, body) {
128
131
  return this.request("PATCH", path, { body });
129
132
  }
@@ -223,6 +226,122 @@ var UsageClient = class {
223
226
  allowed
224
227
  };
225
228
  }
229
+ /**
230
+ * Sync usage to an absolute value (instead of incrementing).
231
+ * Use this when your app tracks usage internally and reports the final count periodically.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * // Your app tracks 847 billable tickets internally
236
+ * await stackbe.usage.sync('cust_123', 'tickets', 847);
237
+ * ```
238
+ */
239
+ async sync(customerId, metric, value, options = {}) {
240
+ const headers = {};
241
+ if (options.idempotencyKey) {
242
+ headers["Idempotency-Key"] = options.idempotencyKey;
243
+ }
244
+ return this.http.put("/v1/usage/sync", {
245
+ customerId,
246
+ metric,
247
+ value
248
+ });
249
+ }
250
+ /**
251
+ * Check usage limits including purchased add-on credits.
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * const result = await stackbe.usage.checkWithAddons('cust_123', 'tickets');
256
+ *
257
+ * console.log(`Plan limit: ${result.planLimit}`);
258
+ * console.log(`Addon credits: ${result.addonCredits}`);
259
+ * console.log(`Effective limit: ${result.effectiveLimit}`);
260
+ *
261
+ * if (!result.allowed) {
262
+ * // Prompt to purchase more credits
263
+ * }
264
+ * ```
265
+ */
266
+ async checkWithAddons(customerId, metric) {
267
+ return this.http.get(
268
+ `/v1/customers/${customerId}/usage/check-with-addons`,
269
+ { metric }
270
+ );
271
+ }
272
+ /**
273
+ * Track usage with automatic add-on credit deduction.
274
+ * When plan limit is exceeded, usage is deducted from purchased add-on credits.
275
+ *
276
+ * @example
277
+ * ```typescript
278
+ * const result = await stackbe.usage.trackWithAddons('cust_123', 'tickets');
279
+ *
280
+ * if (result.addonUsage > 0) {
281
+ * console.log(`Used ${result.addonUsage} from addon credits`);
282
+ * }
283
+ * ```
284
+ */
285
+ async trackWithAddons(customerId, metric, options = {}) {
286
+ return this.http.post("/v1/usage/addons/track", {
287
+ customerId,
288
+ metric,
289
+ quantity: options.quantity ?? 1
290
+ });
291
+ }
292
+ /**
293
+ * List available add-on packs for purchase.
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * const addons = await stackbe.usage.listAddons();
298
+ *
299
+ * for (const addon of addons) {
300
+ * console.log(`${addon.name}: ${addon.quantity} ${addon.metricName} for $${addon.priceCents / 100}`);
301
+ * }
302
+ * ```
303
+ */
304
+ async listAddons() {
305
+ return this.http.get("/v1/usage/addons");
306
+ }
307
+ /**
308
+ * Purchase an add-on pack for a customer.
309
+ * Credits are immediately available after purchase.
310
+ *
311
+ * @example
312
+ * ```typescript
313
+ * // Purchase a 10k ticket pack
314
+ * const purchase = await stackbe.usage.purchaseAddon('cust_123', 'addon_xyz');
315
+ * console.log(`Purchased ${purchase.quantity} credits`);
316
+ * ```
317
+ */
318
+ async purchaseAddon(customerId, addonId) {
319
+ return this.http.post("/v1/usage/addons/purchase", {
320
+ customerId,
321
+ addonId
322
+ });
323
+ }
324
+ /**
325
+ * Get customer's purchased add-on credits for a metric.
326
+ *
327
+ * @example
328
+ * ```typescript
329
+ * const credits = await stackbe.usage.getCredits('cust_123', 'tickets');
330
+ *
331
+ * console.log(`Total purchased: ${credits.totalPurchased}`);
332
+ * console.log(`Remaining: ${credits.remaining}`);
333
+ *
334
+ * for (const purchase of credits.purchases) {
335
+ * console.log(`${purchase.addonName}: ${purchase.remainingQuantity} left`);
336
+ * }
337
+ * ```
338
+ */
339
+ async getCredits(customerId, metric) {
340
+ return this.http.get(
341
+ `/v1/customers/${customerId}/usage/credits`,
342
+ { metric }
343
+ );
344
+ }
226
345
  };
227
346
 
228
347
  // src/entitlements.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackbe/sdk",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Official JavaScript/TypeScript SDK for StackBE - the billing backend for your side project",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",