@solvapay/next 1.0.0-preview.18 → 1.0.0-preview.20
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/LICENSE.md +21 -0
- package/README.md +99 -105
- package/dist/index.cjs +170 -178
- package/dist/index.d.cts +197 -137
- package/dist/index.d.ts +197 -137
- package/dist/index.js +168 -185
- package/package.json +7 -6
package/dist/index.d.cts
CHANGED
|
@@ -3,6 +3,128 @@ import * as _solvapay_server from '@solvapay/server';
|
|
|
3
3
|
import { AuthenticatedUser, SolvaPay } from '@solvapay/server';
|
|
4
4
|
import { AuthAdapter } from '@solvapay/auth';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Purchase Cache Utilities
|
|
8
|
+
*
|
|
9
|
+
* Cache management functions for purchase data.
|
|
10
|
+
* Separated from index.ts to avoid circular dependencies.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Request deduplication and caching options
|
|
14
|
+
*/
|
|
15
|
+
interface RequestDeduplicationOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Time-to-live for cached results in milliseconds (default: 2000)
|
|
18
|
+
* Set to 0 to disable caching (only deduplicate concurrent requests)
|
|
19
|
+
*/
|
|
20
|
+
cacheTTL?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Maximum cache size before cleanup (default: 1000)
|
|
23
|
+
* When exceeded, oldest entries are removed
|
|
24
|
+
*/
|
|
25
|
+
maxCacheSize?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Whether to cache error results (default: true)
|
|
28
|
+
* When false, only successful results are cached
|
|
29
|
+
*/
|
|
30
|
+
cacheErrors?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Purchase check result
|
|
34
|
+
*/
|
|
35
|
+
interface PurchaseCheckResult {
|
|
36
|
+
customerRef: string;
|
|
37
|
+
email?: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
purchases: Array<{
|
|
40
|
+
reference: string;
|
|
41
|
+
planName?: string;
|
|
42
|
+
productReference?: string;
|
|
43
|
+
status?: string;
|
|
44
|
+
startDate?: string;
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
}>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Clear purchase cache for a specific user.
|
|
50
|
+
*
|
|
51
|
+
* Useful when you know purchase status has changed (e.g., after a successful
|
|
52
|
+
* checkout, purchase update, or cancellation). This forces the next
|
|
53
|
+
* `checkPurchase()` call to fetch fresh data from the backend.
|
|
54
|
+
*
|
|
55
|
+
* @param userId - User ID to clear cache for
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { clearPurchaseCache } from '@solvapay/next';
|
|
60
|
+
*
|
|
61
|
+
* // After successful payment
|
|
62
|
+
* await processPayment(request, body);
|
|
63
|
+
* clearPurchaseCache(userId); // Force refresh on next check
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @see {@link checkPurchase} for purchase checking
|
|
67
|
+
* @see {@link clearAllPurchaseCache} to clear all cache entries
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
declare function clearPurchaseCache(userId: string): void;
|
|
71
|
+
/**
|
|
72
|
+
* Clear all purchase cache entries.
|
|
73
|
+
*
|
|
74
|
+
* Useful for testing, debugging, or when you need to force fresh lookups
|
|
75
|
+
* for all users. This clears both the in-flight request cache and the
|
|
76
|
+
* result cache.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* import { clearAllPurchaseCache } from '@solvapay/next';
|
|
81
|
+
*
|
|
82
|
+
* // In a test setup
|
|
83
|
+
* beforeEach(() => {
|
|
84
|
+
* clearAllPurchaseCache();
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @see {@link clearPurchaseCache} to clear cache for a specific user
|
|
89
|
+
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
*/
|
|
92
|
+
declare function clearAllPurchaseCache(): void;
|
|
93
|
+
/**
|
|
94
|
+
* Get purchase cache statistics for monitoring and debugging.
|
|
95
|
+
*
|
|
96
|
+
* Returns the current state of the purchase cache, including:
|
|
97
|
+
* - Number of in-flight requests (being deduplicated)
|
|
98
|
+
* - Number of cached results
|
|
99
|
+
*
|
|
100
|
+
* @returns Cache statistics object
|
|
101
|
+
* @returns inFlight - Number of currently in-flight requests
|
|
102
|
+
* @returns cached - Number of cached results
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* import { getPurchaseCacheStats } from '@solvapay/next';
|
|
107
|
+
*
|
|
108
|
+
* // In a monitoring endpoint
|
|
109
|
+
* export async function GET() {
|
|
110
|
+
* const stats = getPurchaseCacheStats();
|
|
111
|
+
* return Response.json({
|
|
112
|
+
* cache: {
|
|
113
|
+
* inFlight: stats.inFlight,
|
|
114
|
+
* cached: stats.cached
|
|
115
|
+
* }
|
|
116
|
+
* });
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @see {@link checkPurchase} for purchase checking
|
|
121
|
+
* @since 1.0.0
|
|
122
|
+
*/
|
|
123
|
+
declare function getPurchaseCacheStats(): {
|
|
124
|
+
inFlight: number;
|
|
125
|
+
cached: number;
|
|
126
|
+
};
|
|
127
|
+
|
|
6
128
|
/**
|
|
7
129
|
* Next.js Authentication Helpers
|
|
8
130
|
*
|
|
@@ -10,11 +132,37 @@ import { AuthAdapter } from '@solvapay/auth';
|
|
|
10
132
|
*/
|
|
11
133
|
|
|
12
134
|
/**
|
|
13
|
-
* Get authenticated user
|
|
135
|
+
* Get authenticated user information from a Next.js request.
|
|
14
136
|
*
|
|
15
|
-
*
|
|
137
|
+
* This is a Next.js-specific wrapper around `getAuthenticatedUserCore` that
|
|
138
|
+
* returns NextResponse for errors instead of ErrorResult. Extracts user ID,
|
|
139
|
+
* email, and name from authenticated requests.
|
|
140
|
+
*
|
|
141
|
+
* @param request - Next.js request object (NextRequest or Request)
|
|
16
142
|
* @param options - Configuration options
|
|
143
|
+
* @param options.includeEmail - Whether to extract email from JWT token (default: true)
|
|
144
|
+
* @param options.includeName - Whether to extract name from JWT token (default: true)
|
|
17
145
|
* @returns Authenticated user info or NextResponse error
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* import { NextRequest, NextResponse } from 'next/server';
|
|
150
|
+
* import { getAuthenticatedUser } from '@solvapay/next';
|
|
151
|
+
*
|
|
152
|
+
* export async function GET(request: NextRequest) {
|
|
153
|
+
* const userResult = await getAuthenticatedUser(request);
|
|
154
|
+
*
|
|
155
|
+
* if (userResult instanceof NextResponse) {
|
|
156
|
+
* return userResult; // Error response
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* const { userId, email, name } = userResult;
|
|
160
|
+
* return NextResponse.json({ userId, email, name });
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @see {@link getAuthenticatedUserCore} for the core implementation
|
|
165
|
+
* @since 1.0.0
|
|
18
166
|
*/
|
|
19
167
|
declare function getAuthenticatedUser(request: globalThis.Request, options?: {
|
|
20
168
|
includeEmail?: boolean;
|
|
@@ -40,17 +188,9 @@ declare function syncCustomer(request: globalThis.Request, options?: {
|
|
|
40
188
|
includeName?: boolean;
|
|
41
189
|
}): Promise<string | NextResponse>;
|
|
42
190
|
|
|
43
|
-
/**
|
|
44
|
-
* Create payment intent - Next.js wrapper
|
|
45
|
-
*
|
|
46
|
-
* @param request - Next.js request object
|
|
47
|
-
* @param body - Payment intent parameters
|
|
48
|
-
* @param options - Configuration options
|
|
49
|
-
* @returns Payment intent response or NextResponse error
|
|
50
|
-
*/
|
|
51
191
|
declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
52
192
|
planRef: string;
|
|
53
|
-
|
|
193
|
+
productRef: string;
|
|
54
194
|
}, options?: {
|
|
55
195
|
solvaPay?: SolvaPay;
|
|
56
196
|
includeEmail?: boolean;
|
|
@@ -62,17 +202,9 @@ declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
|
62
202
|
accountId?: string;
|
|
63
203
|
customerRef: string;
|
|
64
204
|
} | NextResponse>;
|
|
65
|
-
|
|
66
|
-
* Process payment - Next.js wrapper
|
|
67
|
-
*
|
|
68
|
-
* @param request - Next.js request object
|
|
69
|
-
* @param body - Payment processing parameters
|
|
70
|
-
* @param options - Configuration options
|
|
71
|
-
* @returns Process payment result or NextResponse error
|
|
72
|
-
*/
|
|
73
|
-
declare function processPayment(request: globalThis.Request, body: {
|
|
205
|
+
declare function processPaymentIntent(request: globalThis.Request, body: {
|
|
74
206
|
paymentIntentId: string;
|
|
75
|
-
|
|
207
|
+
productRef: string;
|
|
76
208
|
planRef?: string;
|
|
77
209
|
}, options?: {
|
|
78
210
|
solvaPay?: SolvaPay;
|
|
@@ -80,20 +212,10 @@ declare function processPayment(request: globalThis.Request, body: {
|
|
|
80
212
|
|
|
81
213
|
/**
|
|
82
214
|
* Next.js Checkout Helpers
|
|
83
|
-
*
|
|
84
|
-
* Next.js-specific wrappers for checkout helpers.
|
|
85
215
|
*/
|
|
86
216
|
|
|
87
|
-
/**
|
|
88
|
-
* Create checkout session - Next.js wrapper
|
|
89
|
-
*
|
|
90
|
-
* @param request - Next.js request object
|
|
91
|
-
* @param body - Checkout session parameters
|
|
92
|
-
* @param options - Configuration options
|
|
93
|
-
* @returns Checkout session response or NextResponse error
|
|
94
|
-
*/
|
|
95
217
|
declare function createCheckoutSession(request: globalThis.Request, body: {
|
|
96
|
-
|
|
218
|
+
productRef: string;
|
|
97
219
|
planRef?: string;
|
|
98
220
|
returnUrl?: string;
|
|
99
221
|
}, options?: {
|
|
@@ -105,13 +227,6 @@ declare function createCheckoutSession(request: globalThis.Request, body: {
|
|
|
105
227
|
sessionId: string;
|
|
106
228
|
checkoutUrl: string;
|
|
107
229
|
} | NextResponse>;
|
|
108
|
-
/**
|
|
109
|
-
* Create customer session - Next.js wrapper
|
|
110
|
-
*
|
|
111
|
-
* @param request - Next.js request object
|
|
112
|
-
* @param options - Configuration options
|
|
113
|
-
* @returns Customer session response or NextResponse error
|
|
114
|
-
*/
|
|
115
230
|
declare function createCustomerSession(request: globalThis.Request, options?: {
|
|
116
231
|
solvaPay?: SolvaPay;
|
|
117
232
|
includeEmail?: boolean;
|
|
@@ -122,42 +237,29 @@ declare function createCustomerSession(request: globalThis.Request, options?: {
|
|
|
122
237
|
} | NextResponse>;
|
|
123
238
|
|
|
124
239
|
/**
|
|
125
|
-
* Next.js
|
|
126
|
-
*
|
|
127
|
-
* Next.js-specific wrappers for subscription helpers.
|
|
240
|
+
* Next.js Purchase Cancellation Helpers
|
|
128
241
|
*/
|
|
129
|
-
|
|
130
242
|
/**
|
|
131
|
-
* Cancel
|
|
243
|
+
* Cancel purchase - Next.js wrapper
|
|
132
244
|
*
|
|
133
245
|
* @param request - Next.js request object
|
|
134
246
|
* @param body - Cancellation parameters
|
|
135
247
|
* @param options - Configuration options
|
|
136
|
-
* @returns Cancelled
|
|
248
|
+
* @returns Cancelled purchase response or NextResponse error
|
|
137
249
|
*/
|
|
138
|
-
declare function
|
|
139
|
-
|
|
250
|
+
declare function cancelRenewal(request: globalThis.Request, body: {
|
|
251
|
+
purchaseRef: string;
|
|
140
252
|
reason?: string;
|
|
141
253
|
}, options?: {
|
|
142
254
|
solvaPay?: SolvaPay;
|
|
143
|
-
}): Promise<
|
|
255
|
+
}): Promise<Record<string, unknown> | NextResponse>;
|
|
144
256
|
|
|
145
257
|
/**
|
|
146
258
|
* Next.js Plans Helper
|
|
147
|
-
*
|
|
148
|
-
* Next.js-specific wrapper for plans helper.
|
|
149
|
-
* This is a public route - no authentication required.
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* List plans - Next.js wrapper
|
|
154
|
-
*
|
|
155
|
-
* @param request - Next.js request object
|
|
156
|
-
* @returns Plans response or NextResponse error
|
|
157
259
|
*/
|
|
158
260
|
declare function listPlans(request: globalThis.Request): Promise<{
|
|
159
|
-
plans:
|
|
160
|
-
|
|
261
|
+
plans: Array<Record<string, unknown>>;
|
|
262
|
+
productRef: string;
|
|
161
263
|
} | NextResponse>;
|
|
162
264
|
|
|
163
265
|
/**
|
|
@@ -343,48 +445,16 @@ declare function createSupabaseAuthMiddleware(options?: SupabaseAuthMiddlewareOp
|
|
|
343
445
|
*/
|
|
344
446
|
|
|
345
447
|
/**
|
|
346
|
-
*
|
|
347
|
-
*/
|
|
348
|
-
interface RequestDeduplicationOptions {
|
|
349
|
-
/**
|
|
350
|
-
* Time-to-live for cached results in milliseconds (default: 2000)
|
|
351
|
-
* Set to 0 to disable caching (only deduplicate concurrent requests)
|
|
352
|
-
*/
|
|
353
|
-
cacheTTL?: number;
|
|
354
|
-
/**
|
|
355
|
-
* Maximum cache size before cleanup (default: 1000)
|
|
356
|
-
* When exceeded, oldest entries are removed
|
|
357
|
-
*/
|
|
358
|
-
maxCacheSize?: number;
|
|
359
|
-
/**
|
|
360
|
-
* Whether to cache error results (default: true)
|
|
361
|
-
* When false, only successful results are cached
|
|
362
|
-
*/
|
|
363
|
-
cacheErrors?: boolean;
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Subscription check result
|
|
367
|
-
*/
|
|
368
|
-
interface SubscriptionCheckResult {
|
|
369
|
-
customerRef: string;
|
|
370
|
-
email?: string;
|
|
371
|
-
name?: string;
|
|
372
|
-
subscriptions: Array<{
|
|
373
|
-
reference: string;
|
|
374
|
-
planName?: string;
|
|
375
|
-
agentName?: string;
|
|
376
|
-
status?: string;
|
|
377
|
-
startDate?: string;
|
|
378
|
-
[key: string]: unknown;
|
|
379
|
-
}>;
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* Options for checking subscriptions
|
|
448
|
+
* Options for checking purchases
|
|
383
449
|
*/
|
|
384
|
-
interface
|
|
450
|
+
interface CheckPurchaseOptions {
|
|
385
451
|
/**
|
|
386
452
|
* Request deduplication options
|
|
387
|
-
*
|
|
453
|
+
*
|
|
454
|
+
* Default values:
|
|
455
|
+
* - `cacheTTL`: 2000ms
|
|
456
|
+
* - `maxCacheSize`: 1000
|
|
457
|
+
* - `cacheErrors`: true
|
|
388
458
|
*/
|
|
389
459
|
deduplication?: RequestDeduplicationOptions;
|
|
390
460
|
/**
|
|
@@ -404,59 +474,49 @@ interface CheckSubscriptionOptions {
|
|
|
404
474
|
includeName?: boolean;
|
|
405
475
|
}
|
|
406
476
|
/**
|
|
407
|
-
* Check user
|
|
477
|
+
* Check user purchase status with automatic deduplication and caching.
|
|
408
478
|
*
|
|
409
|
-
* This helper function:
|
|
479
|
+
* This Next.js helper function provides optimized purchase checking with:
|
|
480
|
+
* - Automatic request deduplication (concurrent requests share the same promise)
|
|
481
|
+
* - Short-term caching (2 seconds) to prevent duplicate sequential requests
|
|
482
|
+
* - Fast path optimization using cached customer references from client
|
|
483
|
+
* - Automatic customer creation if needed
|
|
484
|
+
*
|
|
485
|
+
* The function:
|
|
410
486
|
* 1. Extracts user ID from request (via requireUserId from @solvapay/auth)
|
|
411
|
-
* 2. Gets user email and name from Supabase JWT token
|
|
412
|
-
* 3.
|
|
413
|
-
* 4.
|
|
414
|
-
* 5.
|
|
487
|
+
* 2. Gets user email and name from Supabase JWT token (if available)
|
|
488
|
+
* 3. Validates cached customer reference (if provided via header)
|
|
489
|
+
* 4. Ensures customer exists in SolvaPay backend
|
|
490
|
+
* 5. Returns customer purchase information
|
|
415
491
|
*
|
|
416
|
-
* @param request - Next.js request object (NextRequest extends Request
|
|
492
|
+
* @param request - Next.js request object (NextRequest extends Request)
|
|
417
493
|
* @param options - Configuration options
|
|
418
|
-
* @
|
|
494
|
+
* @param options.deduplication - Request deduplication options (see {@link RequestDeduplicationOptions})
|
|
495
|
+
* @param options.solvaPay - Optional SolvaPay instance (creates new one if not provided)
|
|
496
|
+
* @param options.includeEmail - Whether to include email in response (default: true)
|
|
497
|
+
* @param options.includeName - Whether to include name in response (default: true)
|
|
498
|
+
* @returns Purchase check result with customer data and purchases, or NextResponse error
|
|
419
499
|
*
|
|
420
500
|
* @example
|
|
421
501
|
* ```typescript
|
|
422
|
-
* import { NextRequest } from 'next/server';
|
|
423
|
-
* import {
|
|
502
|
+
* import { NextRequest, NextResponse } from 'next/server';
|
|
503
|
+
* import { checkPurchase } from '@solvapay/next';
|
|
424
504
|
*
|
|
425
505
|
* export async function GET(request: NextRequest) {
|
|
426
|
-
* const result = await
|
|
506
|
+
* const result = await checkPurchase(request);
|
|
507
|
+
*
|
|
427
508
|
* if (result instanceof NextResponse) {
|
|
428
509
|
* return result; // Error response
|
|
429
510
|
* }
|
|
511
|
+
*
|
|
430
512
|
* return NextResponse.json(result);
|
|
431
513
|
* }
|
|
432
514
|
* ```
|
|
433
|
-
*/
|
|
434
|
-
declare function checkSubscription(request: Request, options?: CheckSubscriptionOptions): Promise<SubscriptionCheckResult | NextResponse>;
|
|
435
|
-
/**
|
|
436
|
-
* Clear subscription cache for a specific user
|
|
437
|
-
*
|
|
438
|
-
* Useful when you know subscription status has changed
|
|
439
|
-
* (e.g., after a successful checkout or subscription update)
|
|
440
|
-
*
|
|
441
|
-
* @param userId - User ID to clear cache for
|
|
442
|
-
*/
|
|
443
|
-
declare function clearSubscriptionCache(userId: string): void;
|
|
444
|
-
/**
|
|
445
|
-
* Clear all subscription cache entries
|
|
446
|
-
*
|
|
447
|
-
* Useful for testing or when you need to force fresh lookups
|
|
448
|
-
*/
|
|
449
|
-
declare function clearAllSubscriptionCache(): void;
|
|
450
|
-
/**
|
|
451
|
-
* Get subscription cache statistics
|
|
452
|
-
*
|
|
453
|
-
* Useful for monitoring and debugging
|
|
454
515
|
*
|
|
455
|
-
* @
|
|
516
|
+
* @see {@link clearPurchaseCache} for cache management
|
|
517
|
+
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
518
|
+
* @since 1.0.0
|
|
456
519
|
*/
|
|
457
|
-
declare function
|
|
458
|
-
inFlight: number;
|
|
459
|
-
cached: number;
|
|
460
|
-
};
|
|
520
|
+
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
461
521
|
|
|
462
|
-
export { type AuthMiddlewareOptions, type
|
|
522
|
+
export { type AuthMiddlewareOptions, type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, type SupabaseAuthMiddlewareOptions, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createAuthMiddleware, createCheckoutSession, createCustomerSession, createPaymentIntent, createSupabaseAuthMiddleware, getAuthenticatedUser, getPurchaseCacheStats, listPlans, processPaymentIntent, syncCustomer };
|