@stackbe/sdk 0.2.0 → 0.3.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/README.md +263 -77
- package/dist/index.d.mts +150 -17
- package/dist/index.d.ts +150 -17
- package/dist/index.js +156 -21
- package/dist/index.mjs +156 -21
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -121,15 +121,43 @@ interface MagicLinkResponse {
|
|
|
121
121
|
message: string;
|
|
122
122
|
}
|
|
123
123
|
interface VerifyTokenResponse {
|
|
124
|
+
success: boolean;
|
|
124
125
|
valid: boolean;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
/** Customer ID */
|
|
127
|
+
customerId: string;
|
|
128
|
+
/** Customer email */
|
|
129
|
+
email: string;
|
|
130
|
+
/** Session JWT token - use this for subsequent authenticated requests */
|
|
131
|
+
sessionToken: string;
|
|
132
|
+
/** Tenant ID (your StackBE tenant) */
|
|
133
|
+
tenantId?: string;
|
|
134
|
+
/** Organization ID if in org context */
|
|
135
|
+
organizationId?: string;
|
|
136
|
+
/** Role in the organization */
|
|
137
|
+
orgRole?: 'owner' | 'admin' | 'member';
|
|
138
|
+
/** URL to redirect to after verification */
|
|
139
|
+
redirectUrl?: string;
|
|
128
140
|
}
|
|
129
141
|
interface SessionResponse {
|
|
130
|
-
|
|
142
|
+
valid: boolean;
|
|
143
|
+
/** Customer ID */
|
|
144
|
+
customerId: string;
|
|
145
|
+
/** Customer email */
|
|
146
|
+
email: string;
|
|
147
|
+
/** Session expiration time */
|
|
148
|
+
expiresAt?: string;
|
|
149
|
+
/** Tenant ID (your StackBE tenant) */
|
|
150
|
+
tenantId?: string;
|
|
151
|
+
/** Organization ID if in org context */
|
|
152
|
+
organizationId?: string;
|
|
153
|
+
/** Role in the organization */
|
|
154
|
+
orgRole?: 'owner' | 'admin' | 'member';
|
|
155
|
+
/** Customer details (if included) */
|
|
156
|
+
customer?: Customer;
|
|
157
|
+
/** Current subscription (if any) */
|
|
131
158
|
subscription?: Subscription;
|
|
132
|
-
|
|
159
|
+
/** Feature entitlements */
|
|
160
|
+
entitlements?: Record<string, boolean | number | string>;
|
|
133
161
|
}
|
|
134
162
|
interface CreateCheckoutOptions {
|
|
135
163
|
/** Customer ID or email */
|
|
@@ -189,16 +217,109 @@ interface UpdateSubscriptionOptions {
|
|
|
189
217
|
/** Proration behavior */
|
|
190
218
|
prorate?: boolean;
|
|
191
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Error codes returned by StackBE API.
|
|
222
|
+
* Use these to handle specific error cases in your application.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* try {
|
|
227
|
+
* await stackbe.auth.verifyToken(token);
|
|
228
|
+
* } catch (error) {
|
|
229
|
+
* if (error instanceof StackBEError) {
|
|
230
|
+
* switch (error.code) {
|
|
231
|
+
* case 'TOKEN_EXPIRED':
|
|
232
|
+
* return res.redirect('/login?error=expired');
|
|
233
|
+
* case 'TOKEN_ALREADY_USED':
|
|
234
|
+
* return res.redirect('/login?error=used');
|
|
235
|
+
* case 'SESSION_EXPIRED':
|
|
236
|
+
* return res.redirect('/login');
|
|
237
|
+
* default:
|
|
238
|
+
* return res.status(500).json({ error: 'Authentication failed' });
|
|
239
|
+
* }
|
|
240
|
+
* }
|
|
241
|
+
* }
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
type StackBEErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_ALREADY_USED' | 'TOKEN_INVALID' | 'SESSION_EXPIRED' | 'SESSION_INVALID' | 'UNAUTHORIZED' | 'NOT_FOUND' | 'CUSTOMER_NOT_FOUND' | 'SUBSCRIPTION_NOT_FOUND' | 'PLAN_NOT_FOUND' | 'APP_NOT_FOUND' | 'VALIDATION_ERROR' | 'MISSING_REQUIRED_FIELD' | 'INVALID_EMAIL' | 'USAGE_LIMIT_EXCEEDED' | 'METRIC_NOT_FOUND' | 'FEATURE_NOT_AVAILABLE' | 'NO_ACTIVE_SUBSCRIPTION' | 'TIMEOUT' | 'NETWORK_ERROR' | 'UNKNOWN_ERROR';
|
|
192
245
|
interface StackBEErrorResponse {
|
|
193
246
|
statusCode: number;
|
|
194
247
|
message: string;
|
|
195
248
|
error: string;
|
|
249
|
+
code?: StackBEErrorCode;
|
|
196
250
|
}
|
|
197
251
|
declare class StackBEError extends Error {
|
|
198
252
|
readonly statusCode: number;
|
|
199
|
-
readonly code:
|
|
200
|
-
constructor(message: string, statusCode: number, code: string);
|
|
253
|
+
readonly code: StackBEErrorCode;
|
|
254
|
+
constructor(message: string, statusCode: number, code: StackBEErrorCode | string);
|
|
255
|
+
/** Check if this is a specific error type */
|
|
256
|
+
is(code: StackBEErrorCode): boolean;
|
|
257
|
+
/** Check if this is an auth-related error */
|
|
258
|
+
isAuthError(): boolean;
|
|
259
|
+
/** Check if this is a "not found" error */
|
|
260
|
+
isNotFoundError(): boolean;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Webhook event types emitted by StackBE.
|
|
264
|
+
* Subscribe to these events to react to changes in your customers' subscriptions.
|
|
265
|
+
*/
|
|
266
|
+
type WebhookEventType = 'subscription_created' | 'subscription_updated' | 'subscription_cancelled' | 'subscription_renewed' | 'trial_started' | 'trial_ended' | 'payment_succeeded' | 'payment_failed' | 'customer_created' | 'customer_updated';
|
|
267
|
+
/** Base webhook event structure */
|
|
268
|
+
interface WebhookEvent<T extends WebhookEventType = WebhookEventType, P = unknown> {
|
|
269
|
+
/** Event ID */
|
|
270
|
+
id: string;
|
|
271
|
+
/** Event type */
|
|
272
|
+
type: T;
|
|
273
|
+
/** Tenant ID */
|
|
274
|
+
tenantId: string;
|
|
275
|
+
/** App ID */
|
|
276
|
+
appId: string;
|
|
277
|
+
/** Event payload */
|
|
278
|
+
data: P;
|
|
279
|
+
/** When the event was created */
|
|
280
|
+
createdAt: string;
|
|
201
281
|
}
|
|
282
|
+
/** Subscription webhook payload */
|
|
283
|
+
interface SubscriptionWebhookPayload {
|
|
284
|
+
id: string;
|
|
285
|
+
customerId: string;
|
|
286
|
+
planId: string;
|
|
287
|
+
planName: string;
|
|
288
|
+
status: 'active' | 'canceled' | 'past_due' | 'trialing' | 'paused';
|
|
289
|
+
currentPeriodStart: string;
|
|
290
|
+
currentPeriodEnd: string;
|
|
291
|
+
cancelAtPeriodEnd: boolean;
|
|
292
|
+
trialEnd?: string;
|
|
293
|
+
}
|
|
294
|
+
/** Customer webhook payload */
|
|
295
|
+
interface CustomerWebhookPayload {
|
|
296
|
+
id: string;
|
|
297
|
+
email: string;
|
|
298
|
+
name?: string;
|
|
299
|
+
metadata?: Record<string, unknown>;
|
|
300
|
+
}
|
|
301
|
+
/** Payment webhook payload */
|
|
302
|
+
interface PaymentWebhookPayload {
|
|
303
|
+
id: string;
|
|
304
|
+
subscriptionId: string;
|
|
305
|
+
customerId: string;
|
|
306
|
+
amount: number;
|
|
307
|
+
currency: string;
|
|
308
|
+
status: 'succeeded' | 'failed';
|
|
309
|
+
failureReason?: string;
|
|
310
|
+
}
|
|
311
|
+
type SubscriptionCreatedEvent = WebhookEvent<'subscription_created', SubscriptionWebhookPayload>;
|
|
312
|
+
type SubscriptionUpdatedEvent = WebhookEvent<'subscription_updated', SubscriptionWebhookPayload>;
|
|
313
|
+
type SubscriptionCancelledEvent = WebhookEvent<'subscription_cancelled', SubscriptionWebhookPayload>;
|
|
314
|
+
type SubscriptionRenewedEvent = WebhookEvent<'subscription_renewed', SubscriptionWebhookPayload>;
|
|
315
|
+
type TrialStartedEvent = WebhookEvent<'trial_started', SubscriptionWebhookPayload>;
|
|
316
|
+
type TrialEndedEvent = WebhookEvent<'trial_ended', SubscriptionWebhookPayload>;
|
|
317
|
+
type PaymentSucceededEvent = WebhookEvent<'payment_succeeded', PaymentWebhookPayload>;
|
|
318
|
+
type PaymentFailedEvent = WebhookEvent<'payment_failed', PaymentWebhookPayload>;
|
|
319
|
+
type CustomerCreatedEvent = WebhookEvent<'customer_created', CustomerWebhookPayload>;
|
|
320
|
+
type CustomerUpdatedEvent = WebhookEvent<'customer_updated', CustomerWebhookPayload>;
|
|
321
|
+
/** Union of all webhook event types */
|
|
322
|
+
type AnyWebhookEvent = SubscriptionCreatedEvent | SubscriptionUpdatedEvent | SubscriptionCancelledEvent | SubscriptionRenewedEvent | TrialStartedEvent | TrialEndedEvent | PaymentSucceededEvent | PaymentFailedEvent | CustomerCreatedEvent | CustomerUpdatedEvent;
|
|
202
323
|
|
|
203
324
|
declare class UsageClient {
|
|
204
325
|
private http;
|
|
@@ -598,19 +719,27 @@ declare class AuthClient {
|
|
|
598
719
|
* Verify a magic link token and get a session token.
|
|
599
720
|
* Call this when the user clicks the magic link.
|
|
600
721
|
*
|
|
722
|
+
* Returns the session token along with tenant and organization context.
|
|
723
|
+
*
|
|
601
724
|
* @example
|
|
602
725
|
* ```typescript
|
|
603
726
|
* // In your /verify route handler
|
|
604
727
|
* const { token } = req.query;
|
|
605
728
|
*
|
|
606
|
-
*
|
|
729
|
+
* try {
|
|
730
|
+
* const result = await stackbe.auth.verifyToken(token);
|
|
607
731
|
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
* res.cookie('session', result.token, { httpOnly: true });
|
|
732
|
+
* // result includes: customerId, email, sessionToken, tenantId, organizationId
|
|
733
|
+
* res.cookie('session', result.sessionToken, { httpOnly: true });
|
|
611
734
|
* res.redirect('/dashboard');
|
|
612
|
-
* }
|
|
613
|
-
*
|
|
735
|
+
* } catch (error) {
|
|
736
|
+
* if (error instanceof StackBEError) {
|
|
737
|
+
* if (error.code === 'TOKEN_EXPIRED') {
|
|
738
|
+
* res.redirect('/login?error=expired');
|
|
739
|
+
* } else if (error.code === 'TOKEN_ALREADY_USED') {
|
|
740
|
+
* res.redirect('/login?error=used');
|
|
741
|
+
* }
|
|
742
|
+
* }
|
|
614
743
|
* }
|
|
615
744
|
* ```
|
|
616
745
|
*/
|
|
@@ -619,6 +748,8 @@ declare class AuthClient {
|
|
|
619
748
|
* Get the current session for an authenticated customer.
|
|
620
749
|
* Use this to validate session tokens and get customer data.
|
|
621
750
|
*
|
|
751
|
+
* Returns session info including tenant and organization context extracted from the JWT.
|
|
752
|
+
*
|
|
622
753
|
* @example
|
|
623
754
|
* ```typescript
|
|
624
755
|
* // Validate session on each request
|
|
@@ -627,9 +758,11 @@ declare class AuthClient {
|
|
|
627
758
|
* const session = await stackbe.auth.getSession(sessionToken);
|
|
628
759
|
*
|
|
629
760
|
* if (session) {
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
761
|
+
* console.log(session.customerId);
|
|
762
|
+
* console.log(session.tenantId); // Tenant context
|
|
763
|
+
* console.log(session.organizationId); // Org context (if applicable)
|
|
764
|
+
* console.log(session.subscription);
|
|
765
|
+
* console.log(session.entitlements);
|
|
633
766
|
* }
|
|
634
767
|
* ```
|
|
635
768
|
*/
|
|
@@ -781,4 +914,4 @@ declare class StackBE {
|
|
|
781
914
|
}): (req: any, res: any, next: any) => Promise<any>;
|
|
782
915
|
}
|
|
783
916
|
|
|
784
|
-
export { AuthClient, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type CreateCheckoutOptions, type CreateCustomerOptions, type Customer, type CustomerUsageResponse, type CustomerWithSubscription, CustomersClient, EntitlementsClient, type EntitlementsResponse, type MagicLinkOptions, type MagicLinkResponse, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorResponse, type Subscription, type SubscriptionWithPlan, SubscriptionsClient, type TrackUsageOptions, type TrackUsageResponse, type UpdateCustomerOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type VerifyTokenResponse };
|
|
917
|
+
export { type AnyWebhookEvent, AuthClient, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type CreateCheckoutOptions, type CreateCustomerOptions, type Customer, type CustomerCreatedEvent, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, EntitlementsClient, type EntitlementsResponse, type MagicLinkOptions, type MagicLinkResponse, type PaymentFailedEvent, type PaymentSucceededEvent, type PaymentWebhookPayload, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialStartedEvent, type UpdateCustomerOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|
package/dist/index.d.ts
CHANGED
|
@@ -121,15 +121,43 @@ interface MagicLinkResponse {
|
|
|
121
121
|
message: string;
|
|
122
122
|
}
|
|
123
123
|
interface VerifyTokenResponse {
|
|
124
|
+
success: boolean;
|
|
124
125
|
valid: boolean;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
/** Customer ID */
|
|
127
|
+
customerId: string;
|
|
128
|
+
/** Customer email */
|
|
129
|
+
email: string;
|
|
130
|
+
/** Session JWT token - use this for subsequent authenticated requests */
|
|
131
|
+
sessionToken: string;
|
|
132
|
+
/** Tenant ID (your StackBE tenant) */
|
|
133
|
+
tenantId?: string;
|
|
134
|
+
/** Organization ID if in org context */
|
|
135
|
+
organizationId?: string;
|
|
136
|
+
/** Role in the organization */
|
|
137
|
+
orgRole?: 'owner' | 'admin' | 'member';
|
|
138
|
+
/** URL to redirect to after verification */
|
|
139
|
+
redirectUrl?: string;
|
|
128
140
|
}
|
|
129
141
|
interface SessionResponse {
|
|
130
|
-
|
|
142
|
+
valid: boolean;
|
|
143
|
+
/** Customer ID */
|
|
144
|
+
customerId: string;
|
|
145
|
+
/** Customer email */
|
|
146
|
+
email: string;
|
|
147
|
+
/** Session expiration time */
|
|
148
|
+
expiresAt?: string;
|
|
149
|
+
/** Tenant ID (your StackBE tenant) */
|
|
150
|
+
tenantId?: string;
|
|
151
|
+
/** Organization ID if in org context */
|
|
152
|
+
organizationId?: string;
|
|
153
|
+
/** Role in the organization */
|
|
154
|
+
orgRole?: 'owner' | 'admin' | 'member';
|
|
155
|
+
/** Customer details (if included) */
|
|
156
|
+
customer?: Customer;
|
|
157
|
+
/** Current subscription (if any) */
|
|
131
158
|
subscription?: Subscription;
|
|
132
|
-
|
|
159
|
+
/** Feature entitlements */
|
|
160
|
+
entitlements?: Record<string, boolean | number | string>;
|
|
133
161
|
}
|
|
134
162
|
interface CreateCheckoutOptions {
|
|
135
163
|
/** Customer ID or email */
|
|
@@ -189,16 +217,109 @@ interface UpdateSubscriptionOptions {
|
|
|
189
217
|
/** Proration behavior */
|
|
190
218
|
prorate?: boolean;
|
|
191
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Error codes returned by StackBE API.
|
|
222
|
+
* Use these to handle specific error cases in your application.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* try {
|
|
227
|
+
* await stackbe.auth.verifyToken(token);
|
|
228
|
+
* } catch (error) {
|
|
229
|
+
* if (error instanceof StackBEError) {
|
|
230
|
+
* switch (error.code) {
|
|
231
|
+
* case 'TOKEN_EXPIRED':
|
|
232
|
+
* return res.redirect('/login?error=expired');
|
|
233
|
+
* case 'TOKEN_ALREADY_USED':
|
|
234
|
+
* return res.redirect('/login?error=used');
|
|
235
|
+
* case 'SESSION_EXPIRED':
|
|
236
|
+
* return res.redirect('/login');
|
|
237
|
+
* default:
|
|
238
|
+
* return res.status(500).json({ error: 'Authentication failed' });
|
|
239
|
+
* }
|
|
240
|
+
* }
|
|
241
|
+
* }
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
type StackBEErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_ALREADY_USED' | 'TOKEN_INVALID' | 'SESSION_EXPIRED' | 'SESSION_INVALID' | 'UNAUTHORIZED' | 'NOT_FOUND' | 'CUSTOMER_NOT_FOUND' | 'SUBSCRIPTION_NOT_FOUND' | 'PLAN_NOT_FOUND' | 'APP_NOT_FOUND' | 'VALIDATION_ERROR' | 'MISSING_REQUIRED_FIELD' | 'INVALID_EMAIL' | 'USAGE_LIMIT_EXCEEDED' | 'METRIC_NOT_FOUND' | 'FEATURE_NOT_AVAILABLE' | 'NO_ACTIVE_SUBSCRIPTION' | 'TIMEOUT' | 'NETWORK_ERROR' | 'UNKNOWN_ERROR';
|
|
192
245
|
interface StackBEErrorResponse {
|
|
193
246
|
statusCode: number;
|
|
194
247
|
message: string;
|
|
195
248
|
error: string;
|
|
249
|
+
code?: StackBEErrorCode;
|
|
196
250
|
}
|
|
197
251
|
declare class StackBEError extends Error {
|
|
198
252
|
readonly statusCode: number;
|
|
199
|
-
readonly code:
|
|
200
|
-
constructor(message: string, statusCode: number, code: string);
|
|
253
|
+
readonly code: StackBEErrorCode;
|
|
254
|
+
constructor(message: string, statusCode: number, code: StackBEErrorCode | string);
|
|
255
|
+
/** Check if this is a specific error type */
|
|
256
|
+
is(code: StackBEErrorCode): boolean;
|
|
257
|
+
/** Check if this is an auth-related error */
|
|
258
|
+
isAuthError(): boolean;
|
|
259
|
+
/** Check if this is a "not found" error */
|
|
260
|
+
isNotFoundError(): boolean;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Webhook event types emitted by StackBE.
|
|
264
|
+
* Subscribe to these events to react to changes in your customers' subscriptions.
|
|
265
|
+
*/
|
|
266
|
+
type WebhookEventType = 'subscription_created' | 'subscription_updated' | 'subscription_cancelled' | 'subscription_renewed' | 'trial_started' | 'trial_ended' | 'payment_succeeded' | 'payment_failed' | 'customer_created' | 'customer_updated';
|
|
267
|
+
/** Base webhook event structure */
|
|
268
|
+
interface WebhookEvent<T extends WebhookEventType = WebhookEventType, P = unknown> {
|
|
269
|
+
/** Event ID */
|
|
270
|
+
id: string;
|
|
271
|
+
/** Event type */
|
|
272
|
+
type: T;
|
|
273
|
+
/** Tenant ID */
|
|
274
|
+
tenantId: string;
|
|
275
|
+
/** App ID */
|
|
276
|
+
appId: string;
|
|
277
|
+
/** Event payload */
|
|
278
|
+
data: P;
|
|
279
|
+
/** When the event was created */
|
|
280
|
+
createdAt: string;
|
|
201
281
|
}
|
|
282
|
+
/** Subscription webhook payload */
|
|
283
|
+
interface SubscriptionWebhookPayload {
|
|
284
|
+
id: string;
|
|
285
|
+
customerId: string;
|
|
286
|
+
planId: string;
|
|
287
|
+
planName: string;
|
|
288
|
+
status: 'active' | 'canceled' | 'past_due' | 'trialing' | 'paused';
|
|
289
|
+
currentPeriodStart: string;
|
|
290
|
+
currentPeriodEnd: string;
|
|
291
|
+
cancelAtPeriodEnd: boolean;
|
|
292
|
+
trialEnd?: string;
|
|
293
|
+
}
|
|
294
|
+
/** Customer webhook payload */
|
|
295
|
+
interface CustomerWebhookPayload {
|
|
296
|
+
id: string;
|
|
297
|
+
email: string;
|
|
298
|
+
name?: string;
|
|
299
|
+
metadata?: Record<string, unknown>;
|
|
300
|
+
}
|
|
301
|
+
/** Payment webhook payload */
|
|
302
|
+
interface PaymentWebhookPayload {
|
|
303
|
+
id: string;
|
|
304
|
+
subscriptionId: string;
|
|
305
|
+
customerId: string;
|
|
306
|
+
amount: number;
|
|
307
|
+
currency: string;
|
|
308
|
+
status: 'succeeded' | 'failed';
|
|
309
|
+
failureReason?: string;
|
|
310
|
+
}
|
|
311
|
+
type SubscriptionCreatedEvent = WebhookEvent<'subscription_created', SubscriptionWebhookPayload>;
|
|
312
|
+
type SubscriptionUpdatedEvent = WebhookEvent<'subscription_updated', SubscriptionWebhookPayload>;
|
|
313
|
+
type SubscriptionCancelledEvent = WebhookEvent<'subscription_cancelled', SubscriptionWebhookPayload>;
|
|
314
|
+
type SubscriptionRenewedEvent = WebhookEvent<'subscription_renewed', SubscriptionWebhookPayload>;
|
|
315
|
+
type TrialStartedEvent = WebhookEvent<'trial_started', SubscriptionWebhookPayload>;
|
|
316
|
+
type TrialEndedEvent = WebhookEvent<'trial_ended', SubscriptionWebhookPayload>;
|
|
317
|
+
type PaymentSucceededEvent = WebhookEvent<'payment_succeeded', PaymentWebhookPayload>;
|
|
318
|
+
type PaymentFailedEvent = WebhookEvent<'payment_failed', PaymentWebhookPayload>;
|
|
319
|
+
type CustomerCreatedEvent = WebhookEvent<'customer_created', CustomerWebhookPayload>;
|
|
320
|
+
type CustomerUpdatedEvent = WebhookEvent<'customer_updated', CustomerWebhookPayload>;
|
|
321
|
+
/** Union of all webhook event types */
|
|
322
|
+
type AnyWebhookEvent = SubscriptionCreatedEvent | SubscriptionUpdatedEvent | SubscriptionCancelledEvent | SubscriptionRenewedEvent | TrialStartedEvent | TrialEndedEvent | PaymentSucceededEvent | PaymentFailedEvent | CustomerCreatedEvent | CustomerUpdatedEvent;
|
|
202
323
|
|
|
203
324
|
declare class UsageClient {
|
|
204
325
|
private http;
|
|
@@ -598,19 +719,27 @@ declare class AuthClient {
|
|
|
598
719
|
* Verify a magic link token and get a session token.
|
|
599
720
|
* Call this when the user clicks the magic link.
|
|
600
721
|
*
|
|
722
|
+
* Returns the session token along with tenant and organization context.
|
|
723
|
+
*
|
|
601
724
|
* @example
|
|
602
725
|
* ```typescript
|
|
603
726
|
* // In your /verify route handler
|
|
604
727
|
* const { token } = req.query;
|
|
605
728
|
*
|
|
606
|
-
*
|
|
729
|
+
* try {
|
|
730
|
+
* const result = await stackbe.auth.verifyToken(token);
|
|
607
731
|
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
* res.cookie('session', result.token, { httpOnly: true });
|
|
732
|
+
* // result includes: customerId, email, sessionToken, tenantId, organizationId
|
|
733
|
+
* res.cookie('session', result.sessionToken, { httpOnly: true });
|
|
611
734
|
* res.redirect('/dashboard');
|
|
612
|
-
* }
|
|
613
|
-
*
|
|
735
|
+
* } catch (error) {
|
|
736
|
+
* if (error instanceof StackBEError) {
|
|
737
|
+
* if (error.code === 'TOKEN_EXPIRED') {
|
|
738
|
+
* res.redirect('/login?error=expired');
|
|
739
|
+
* } else if (error.code === 'TOKEN_ALREADY_USED') {
|
|
740
|
+
* res.redirect('/login?error=used');
|
|
741
|
+
* }
|
|
742
|
+
* }
|
|
614
743
|
* }
|
|
615
744
|
* ```
|
|
616
745
|
*/
|
|
@@ -619,6 +748,8 @@ declare class AuthClient {
|
|
|
619
748
|
* Get the current session for an authenticated customer.
|
|
620
749
|
* Use this to validate session tokens and get customer data.
|
|
621
750
|
*
|
|
751
|
+
* Returns session info including tenant and organization context extracted from the JWT.
|
|
752
|
+
*
|
|
622
753
|
* @example
|
|
623
754
|
* ```typescript
|
|
624
755
|
* // Validate session on each request
|
|
@@ -627,9 +758,11 @@ declare class AuthClient {
|
|
|
627
758
|
* const session = await stackbe.auth.getSession(sessionToken);
|
|
628
759
|
*
|
|
629
760
|
* if (session) {
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
761
|
+
* console.log(session.customerId);
|
|
762
|
+
* console.log(session.tenantId); // Tenant context
|
|
763
|
+
* console.log(session.organizationId); // Org context (if applicable)
|
|
764
|
+
* console.log(session.subscription);
|
|
765
|
+
* console.log(session.entitlements);
|
|
633
766
|
* }
|
|
634
767
|
* ```
|
|
635
768
|
*/
|
|
@@ -781,4 +914,4 @@ declare class StackBE {
|
|
|
781
914
|
}): (req: any, res: any, next: any) => Promise<any>;
|
|
782
915
|
}
|
|
783
916
|
|
|
784
|
-
export { AuthClient, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type CreateCheckoutOptions, type CreateCustomerOptions, type Customer, type CustomerUsageResponse, type CustomerWithSubscription, CustomersClient, EntitlementsClient, type EntitlementsResponse, type MagicLinkOptions, type MagicLinkResponse, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorResponse, type Subscription, type SubscriptionWithPlan, SubscriptionsClient, type TrackUsageOptions, type TrackUsageResponse, type UpdateCustomerOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type VerifyTokenResponse };
|
|
917
|
+
export { type AnyWebhookEvent, AuthClient, type CancelSubscriptionOptions, type CancelSubscriptionResponse, type CheckEntitlementResponse, type CheckUsageResponse, CheckoutClient, type CheckoutSessionResponse, type CreateCheckoutOptions, type CreateCustomerOptions, type Customer, type CustomerCreatedEvent, type CustomerUpdatedEvent, type CustomerUsageResponse, type CustomerWebhookPayload, type CustomerWithSubscription, CustomersClient, EntitlementsClient, type EntitlementsResponse, type MagicLinkOptions, type MagicLinkResponse, type PaymentFailedEvent, type PaymentSucceededEvent, type PaymentWebhookPayload, type SessionResponse, StackBE, type StackBEConfig, StackBEError, type StackBEErrorCode, type StackBEErrorResponse, type Subscription, type SubscriptionCancelledEvent, type SubscriptionCreatedEvent, type SubscriptionRenewedEvent, type SubscriptionUpdatedEvent, type SubscriptionWebhookPayload, type SubscriptionWithPlan, SubscriptionsClient, type TrackUsageOptions, type TrackUsageResponse, type TrialEndedEvent, type TrialStartedEvent, type UpdateCustomerOptions, type UpdateSubscriptionOptions, UsageClient, type UsageMetric, type VerifyTokenResponse, type WebhookEvent, type WebhookEventType };
|