@solvapay/next 1.0.0-preview.9 → 1.0.1-preview.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.
- package/LICENSE.md +21 -0
- package/README.md +271 -20
- package/dist/chunk-F7TBIH6W.js +74 -0
- package/dist/index.cjs +303 -76
- package/dist/index.d.cts +284 -52
- package/dist/index.d.ts +284 -52
- package/dist/index.js +234 -71
- package/dist/middleware.cjs +111 -0
- package/dist/middleware.d.cts +178 -0
- package/dist/middleware.d.ts +178 -0
- package/dist/middleware.js +8 -0
- package/package.json +16 -10
package/dist/index.d.cts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
|
-
import
|
|
2
|
+
import * as _solvapay_server from '@solvapay/server';
|
|
3
|
+
import { AuthenticatedUser, SolvaPay } from '@solvapay/server';
|
|
4
|
+
export { AuthMiddlewareOptions, SupabaseAuthMiddlewareOptions, createAuthMiddleware, createSupabaseAuthMiddleware } from './middleware.cjs';
|
|
5
|
+
import '@solvapay/auth';
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
|
-
*
|
|
8
|
+
* Purchase Cache Utilities
|
|
6
9
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* like request deduplication and caching.
|
|
10
|
+
* Cache management functions for purchase data.
|
|
11
|
+
* Separated from index.ts to avoid circular dependencies.
|
|
10
12
|
*/
|
|
11
|
-
|
|
12
13
|
/**
|
|
13
14
|
* Request deduplication and caching options
|
|
14
15
|
*/
|
|
@@ -30,28 +31,269 @@ interface RequestDeduplicationOptions {
|
|
|
30
31
|
cacheErrors?: boolean;
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
33
|
-
*
|
|
34
|
+
* Purchase check result
|
|
34
35
|
*/
|
|
35
|
-
interface
|
|
36
|
+
interface PurchaseCheckResult {
|
|
36
37
|
customerRef: string;
|
|
37
38
|
email?: string;
|
|
38
39
|
name?: string;
|
|
39
|
-
|
|
40
|
+
purchases: Array<{
|
|
40
41
|
reference: string;
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
productName?: string;
|
|
43
|
+
productReference?: string;
|
|
43
44
|
status?: string;
|
|
44
45
|
startDate?: string;
|
|
46
|
+
planSnapshot?: {
|
|
47
|
+
meterId?: string;
|
|
48
|
+
limit?: number;
|
|
49
|
+
freeUnits?: number;
|
|
50
|
+
};
|
|
51
|
+
usage?: {
|
|
52
|
+
used?: number;
|
|
53
|
+
overageUnits?: number;
|
|
54
|
+
overageCost?: number;
|
|
55
|
+
periodStart?: string;
|
|
56
|
+
periodEnd?: string;
|
|
57
|
+
};
|
|
45
58
|
[key: string]: unknown;
|
|
46
59
|
}>;
|
|
47
60
|
}
|
|
48
61
|
/**
|
|
49
|
-
*
|
|
62
|
+
* Clear purchase cache for a specific user.
|
|
63
|
+
*
|
|
64
|
+
* Useful when you know purchase status has changed (e.g., after a successful
|
|
65
|
+
* checkout, purchase update, or cancellation). This forces the next
|
|
66
|
+
* `checkPurchase()` call to fetch fresh data from the backend.
|
|
67
|
+
*
|
|
68
|
+
* @param userId - User ID to clear cache for
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { clearPurchaseCache } from '@solvapay/next';
|
|
73
|
+
*
|
|
74
|
+
* // After successful payment
|
|
75
|
+
* await processPayment(request, body);
|
|
76
|
+
* clearPurchaseCache(userId); // Force refresh on next check
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @see {@link checkPurchase} for purchase checking
|
|
80
|
+
* @see {@link clearAllPurchaseCache} to clear all cache entries
|
|
81
|
+
* @since 1.0.0
|
|
82
|
+
*/
|
|
83
|
+
declare function clearPurchaseCache(userId: string): void;
|
|
84
|
+
/**
|
|
85
|
+
* Clear all purchase cache entries.
|
|
86
|
+
*
|
|
87
|
+
* Useful for testing, debugging, or when you need to force fresh lookups
|
|
88
|
+
* for all users. This clears both the in-flight request cache and the
|
|
89
|
+
* result cache.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { clearAllPurchaseCache } from '@solvapay/next';
|
|
94
|
+
*
|
|
95
|
+
* // In a test setup
|
|
96
|
+
* beforeEach(() => {
|
|
97
|
+
* clearAllPurchaseCache();
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @see {@link clearPurchaseCache} to clear cache for a specific user
|
|
102
|
+
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
103
|
+
* @since 1.0.0
|
|
104
|
+
*/
|
|
105
|
+
declare function clearAllPurchaseCache(): void;
|
|
106
|
+
/**
|
|
107
|
+
* Get purchase cache statistics for monitoring and debugging.
|
|
108
|
+
*
|
|
109
|
+
* Returns the current state of the purchase cache, including:
|
|
110
|
+
* - Number of in-flight requests (being deduplicated)
|
|
111
|
+
* - Number of cached results
|
|
112
|
+
*
|
|
113
|
+
* @returns Cache statistics object
|
|
114
|
+
* @returns inFlight - Number of currently in-flight requests
|
|
115
|
+
* @returns cached - Number of cached results
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* import { getPurchaseCacheStats } from '@solvapay/next';
|
|
120
|
+
*
|
|
121
|
+
* // In a monitoring endpoint
|
|
122
|
+
* export async function GET() {
|
|
123
|
+
* const stats = getPurchaseCacheStats();
|
|
124
|
+
* return Response.json({
|
|
125
|
+
* cache: {
|
|
126
|
+
* inFlight: stats.inFlight,
|
|
127
|
+
* cached: stats.cached
|
|
128
|
+
* }
|
|
129
|
+
* });
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @see {@link checkPurchase} for purchase checking
|
|
134
|
+
* @since 1.0.0
|
|
135
|
+
*/
|
|
136
|
+
declare function getPurchaseCacheStats(): {
|
|
137
|
+
inFlight: number;
|
|
138
|
+
cached: number;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Next.js Authentication Helpers
|
|
143
|
+
*
|
|
144
|
+
* Next.js-specific wrappers for authentication helpers.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Get authenticated user information from a Next.js request.
|
|
149
|
+
*
|
|
150
|
+
* This is a Next.js-specific wrapper around `getAuthenticatedUserCore` that
|
|
151
|
+
* returns NextResponse for errors instead of ErrorResult. Extracts user ID,
|
|
152
|
+
* email, and name from authenticated requests.
|
|
153
|
+
*
|
|
154
|
+
* @param request - Next.js request object (NextRequest or Request)
|
|
155
|
+
* @param options - Configuration options
|
|
156
|
+
* @param options.includeEmail - Whether to extract email from JWT token (default: true)
|
|
157
|
+
* @param options.includeName - Whether to extract name from JWT token (default: true)
|
|
158
|
+
* @returns Authenticated user info or NextResponse error
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* import { NextRequest, NextResponse } from 'next/server';
|
|
163
|
+
* import { getAuthenticatedUser } from '@solvapay/next';
|
|
164
|
+
*
|
|
165
|
+
* export async function GET(request: NextRequest) {
|
|
166
|
+
* const userResult = await getAuthenticatedUser(request);
|
|
167
|
+
*
|
|
168
|
+
* if (userResult instanceof NextResponse) {
|
|
169
|
+
* return userResult; // Error response
|
|
170
|
+
* }
|
|
171
|
+
*
|
|
172
|
+
* const { userId, email, name } = userResult;
|
|
173
|
+
* return NextResponse.json({ userId, email, name });
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @see {@link getAuthenticatedUserCore} for the core implementation
|
|
178
|
+
* @since 1.0.0
|
|
179
|
+
*/
|
|
180
|
+
declare function getAuthenticatedUser(request: globalThis.Request, options?: {
|
|
181
|
+
includeEmail?: boolean;
|
|
182
|
+
includeName?: boolean;
|
|
183
|
+
}): Promise<AuthenticatedUser | NextResponse>;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Next.js Customer Helpers
|
|
187
|
+
*
|
|
188
|
+
* Next.js-specific wrappers for customer helpers.
|
|
50
189
|
*/
|
|
51
|
-
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Sync customer - Next.js wrapper
|
|
193
|
+
*
|
|
194
|
+
* @param request - Next.js request object
|
|
195
|
+
* @param options - Configuration options
|
|
196
|
+
* @returns Customer reference or NextResponse error
|
|
197
|
+
*/
|
|
198
|
+
declare function syncCustomer(request: globalThis.Request, options?: {
|
|
199
|
+
solvaPay?: SolvaPay;
|
|
200
|
+
includeEmail?: boolean;
|
|
201
|
+
includeName?: boolean;
|
|
202
|
+
}): Promise<string | NextResponse>;
|
|
203
|
+
|
|
204
|
+
declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
205
|
+
planRef: string;
|
|
206
|
+
productRef: string;
|
|
207
|
+
}, options?: {
|
|
208
|
+
solvaPay?: SolvaPay;
|
|
209
|
+
includeEmail?: boolean;
|
|
210
|
+
includeName?: boolean;
|
|
211
|
+
}): Promise<{
|
|
212
|
+
id: string;
|
|
213
|
+
clientSecret: string;
|
|
214
|
+
publishableKey: string;
|
|
215
|
+
accountId?: string;
|
|
216
|
+
customerRef: string;
|
|
217
|
+
} | NextResponse>;
|
|
218
|
+
declare function processPaymentIntent(request: globalThis.Request, body: {
|
|
219
|
+
paymentIntentId: string;
|
|
220
|
+
productRef: string;
|
|
221
|
+
planRef?: string;
|
|
222
|
+
}, options?: {
|
|
223
|
+
solvaPay?: SolvaPay;
|
|
224
|
+
}): Promise<_solvapay_server.ProcessPaymentResult | NextResponse>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Next.js Checkout Helpers
|
|
228
|
+
*/
|
|
229
|
+
|
|
230
|
+
declare function createCheckoutSession(request: globalThis.Request, body: {
|
|
231
|
+
productRef: string;
|
|
232
|
+
planRef?: string;
|
|
233
|
+
returnUrl?: string;
|
|
234
|
+
}, options?: {
|
|
235
|
+
solvaPay?: SolvaPay;
|
|
236
|
+
includeEmail?: boolean;
|
|
237
|
+
includeName?: boolean;
|
|
238
|
+
returnUrl?: string;
|
|
239
|
+
}): Promise<{
|
|
240
|
+
sessionId: string;
|
|
241
|
+
checkoutUrl: string;
|
|
242
|
+
} | NextResponse>;
|
|
243
|
+
declare function createCustomerSession(request: globalThis.Request, options?: {
|
|
244
|
+
solvaPay?: SolvaPay;
|
|
245
|
+
includeEmail?: boolean;
|
|
246
|
+
includeName?: boolean;
|
|
247
|
+
}): Promise<{
|
|
248
|
+
sessionId: string;
|
|
249
|
+
customerUrl: string;
|
|
250
|
+
} | NextResponse>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Next.js Purchase Cancellation Helpers
|
|
254
|
+
*/
|
|
255
|
+
/**
|
|
256
|
+
* Cancel purchase - Next.js wrapper
|
|
257
|
+
*
|
|
258
|
+
* @param request - Next.js request object
|
|
259
|
+
* @param body - Cancellation parameters
|
|
260
|
+
* @param options - Configuration options
|
|
261
|
+
* @returns Cancelled purchase response or NextResponse error
|
|
262
|
+
*/
|
|
263
|
+
declare function cancelRenewal(request: globalThis.Request, body: {
|
|
264
|
+
purchaseRef: string;
|
|
265
|
+
reason?: string;
|
|
266
|
+
}, options?: {
|
|
267
|
+
solvaPay?: SolvaPay;
|
|
268
|
+
}): Promise<Record<string, unknown> | NextResponse>;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Next.js Plans Helper
|
|
272
|
+
*/
|
|
273
|
+
declare function listPlans(request: globalThis.Request): Promise<{
|
|
274
|
+
plans: Array<Record<string, unknown>>;
|
|
275
|
+
productRef: string;
|
|
276
|
+
} | NextResponse>;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* SolvaPay Next.js SDK
|
|
280
|
+
*
|
|
281
|
+
* Framework-specific helpers and utilities for Next.js API routes.
|
|
282
|
+
* These utilities provide common patterns with built-in optimizations
|
|
283
|
+
* like request deduplication and caching.
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Options for checking purchases
|
|
288
|
+
*/
|
|
289
|
+
interface CheckPurchaseOptions {
|
|
52
290
|
/**
|
|
53
291
|
* Request deduplication options
|
|
54
|
-
*
|
|
292
|
+
*
|
|
293
|
+
* Default values:
|
|
294
|
+
* - `cacheTTL`: 2000ms
|
|
295
|
+
* - `maxCacheSize`: 1000
|
|
296
|
+
* - `cacheErrors`: true
|
|
55
297
|
*/
|
|
56
298
|
deduplication?: RequestDeduplicationOptions;
|
|
57
299
|
/**
|
|
@@ -71,59 +313,49 @@ interface CheckSubscriptionOptions {
|
|
|
71
313
|
includeName?: boolean;
|
|
72
314
|
}
|
|
73
315
|
/**
|
|
74
|
-
* Check user
|
|
316
|
+
* Check user purchase status with automatic deduplication and caching.
|
|
75
317
|
*
|
|
76
|
-
* This helper function:
|
|
318
|
+
* This Next.js helper function provides optimized purchase checking with:
|
|
319
|
+
* - Automatic request deduplication (concurrent requests share the same promise)
|
|
320
|
+
* - Short-term caching (2 seconds) to prevent duplicate sequential requests
|
|
321
|
+
* - Fast path optimization using cached customer references from client
|
|
322
|
+
* - Automatic customer creation if needed
|
|
323
|
+
*
|
|
324
|
+
* The function:
|
|
77
325
|
* 1. Extracts user ID from request (via requireUserId from @solvapay/auth)
|
|
78
|
-
* 2. Gets user email and name from Supabase JWT token
|
|
79
|
-
* 3.
|
|
80
|
-
* 4.
|
|
81
|
-
* 5.
|
|
326
|
+
* 2. Gets user email and name from Supabase JWT token (if available)
|
|
327
|
+
* 3. Validates cached customer reference (if provided via header)
|
|
328
|
+
* 4. Ensures customer exists in SolvaPay backend
|
|
329
|
+
* 5. Returns customer purchase information
|
|
82
330
|
*
|
|
83
|
-
* @param request - Next.js request object (NextRequest extends Request
|
|
331
|
+
* @param request - Next.js request object (NextRequest extends Request)
|
|
84
332
|
* @param options - Configuration options
|
|
85
|
-
* @
|
|
333
|
+
* @param options.deduplication - Request deduplication options (see {@link RequestDeduplicationOptions})
|
|
334
|
+
* @param options.solvaPay - Optional SolvaPay instance (creates new one if not provided)
|
|
335
|
+
* @param options.includeEmail - Whether to include email in response (default: true)
|
|
336
|
+
* @param options.includeName - Whether to include name in response (default: true)
|
|
337
|
+
* @returns Purchase check result with customer data and purchases, or NextResponse error
|
|
86
338
|
*
|
|
87
339
|
* @example
|
|
88
340
|
* ```typescript
|
|
89
|
-
* import { NextRequest } from 'next/server';
|
|
90
|
-
* import {
|
|
341
|
+
* import { NextRequest, NextResponse } from 'next/server';
|
|
342
|
+
* import { checkPurchase } from '@solvapay/next';
|
|
91
343
|
*
|
|
92
344
|
* export async function GET(request: NextRequest) {
|
|
93
|
-
* const result = await
|
|
345
|
+
* const result = await checkPurchase(request);
|
|
346
|
+
*
|
|
94
347
|
* if (result instanceof NextResponse) {
|
|
95
348
|
* return result; // Error response
|
|
96
349
|
* }
|
|
350
|
+
*
|
|
97
351
|
* return NextResponse.json(result);
|
|
98
352
|
* }
|
|
99
353
|
* ```
|
|
100
|
-
*/
|
|
101
|
-
declare function checkSubscription(request: Request, options?: CheckSubscriptionOptions): Promise<SubscriptionCheckResult | NextResponse>;
|
|
102
|
-
/**
|
|
103
|
-
* Clear subscription cache for a specific user
|
|
104
|
-
*
|
|
105
|
-
* Useful when you know subscription status has changed
|
|
106
|
-
* (e.g., after a successful checkout or subscription update)
|
|
107
|
-
*
|
|
108
|
-
* @param userId - User ID to clear cache for
|
|
109
|
-
*/
|
|
110
|
-
declare function clearSubscriptionCache(userId: string): void;
|
|
111
|
-
/**
|
|
112
|
-
* Clear all subscription cache entries
|
|
113
354
|
*
|
|
114
|
-
*
|
|
355
|
+
* @see {@link clearPurchaseCache} for cache management
|
|
356
|
+
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
357
|
+
* @since 1.0.0
|
|
115
358
|
*/
|
|
116
|
-
declare function
|
|
117
|
-
/**
|
|
118
|
-
* Get subscription cache statistics
|
|
119
|
-
*
|
|
120
|
-
* Useful for monitoring and debugging
|
|
121
|
-
*
|
|
122
|
-
* @returns Cache statistics
|
|
123
|
-
*/
|
|
124
|
-
declare function getSubscriptionCacheStats(): {
|
|
125
|
-
inFlight: number;
|
|
126
|
-
cached: number;
|
|
127
|
-
};
|
|
359
|
+
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
128
360
|
|
|
129
|
-
export { type
|
|
361
|
+
export { type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createCheckoutSession, createCustomerSession, createPaymentIntent, getAuthenticatedUser, getPurchaseCacheStats, listPlans, processPaymentIntent, syncCustomer };
|