@solvapay/next 1.0.7 → 1.0.9-preview.1
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.cjs +21 -80
- package/dist/index.d.cts +2 -34
- package/dist/index.d.ts +2 -34
- package/dist/index.js +23 -82
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -53,7 +53,6 @@ __export(index_exports, {
|
|
|
53
53
|
module.exports = __toCommonJS(index_exports);
|
|
54
54
|
var import_server20 = require("next/server");
|
|
55
55
|
var import_server21 = require("@solvapay/server");
|
|
56
|
-
var import_core = require("@solvapay/core");
|
|
57
56
|
|
|
58
57
|
// src/cache.ts
|
|
59
58
|
function createRequestDeduplicator(options = {}) {
|
|
@@ -381,37 +380,14 @@ async function listPlans(request) {
|
|
|
381
380
|
var import_server17 = require("next/server");
|
|
382
381
|
var import_server18 = require("@solvapay/server");
|
|
383
382
|
async function trackUsage(request, body, options = {}) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
if ((0, import_server18.isErrorResult)(userResult)) {
|
|
387
|
-
return import_server17.NextResponse.json(
|
|
388
|
-
{ error: userResult.error, details: userResult.details },
|
|
389
|
-
{ status: userResult.status }
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
const { userId, email, name } = userResult;
|
|
393
|
-
const solvaPay = options.solvaPay || (0, import_server18.createSolvaPay)();
|
|
394
|
-
const customerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
395
|
-
email: email || void 0,
|
|
396
|
-
name: name || void 0
|
|
397
|
-
});
|
|
398
|
-
await solvaPay.trackUsage({
|
|
399
|
-
customerRef,
|
|
400
|
-
actionType: body.actionType,
|
|
401
|
-
units: body.units,
|
|
402
|
-
productRef: body.productRef,
|
|
403
|
-
description: body.description,
|
|
404
|
-
metadata: body.metadata
|
|
405
|
-
});
|
|
406
|
-
return import_server17.NextResponse.json({ success: true });
|
|
407
|
-
} catch (error) {
|
|
408
|
-
console.error("[trackUsage] Error:", error);
|
|
409
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
383
|
+
const result = await (0, import_server18.trackUsageCore)(request, body, options);
|
|
384
|
+
if ((0, import_server18.isErrorResult)(result)) {
|
|
410
385
|
return import_server17.NextResponse.json(
|
|
411
|
-
{ error:
|
|
412
|
-
{ status:
|
|
386
|
+
{ error: result.error, details: result.details },
|
|
387
|
+
{ status: result.status }
|
|
413
388
|
);
|
|
414
389
|
}
|
|
390
|
+
return import_server17.NextResponse.json(result);
|
|
415
391
|
}
|
|
416
392
|
|
|
417
393
|
// src/helpers/middleware.ts
|
|
@@ -486,8 +462,9 @@ function createSupabaseAuthMiddleware(options = {}) {
|
|
|
486
462
|
|
|
487
463
|
// src/index.ts
|
|
488
464
|
async function checkPurchase(request, options = {}) {
|
|
465
|
+
const deduplicator = getSharedDeduplicator(options.deduplication);
|
|
489
466
|
try {
|
|
490
|
-
const { requireUserId
|
|
467
|
+
const { requireUserId } = await import("@solvapay/auth");
|
|
491
468
|
const userIdOrError = requireUserId(request);
|
|
492
469
|
if (userIdOrError instanceof Response) {
|
|
493
470
|
const clonedResponse = userIdOrError.clone();
|
|
@@ -495,61 +472,25 @@ async function checkPurchase(request, options = {}) {
|
|
|
495
472
|
return import_server20.NextResponse.json(body, { status: userIdOrError.status });
|
|
496
473
|
}
|
|
497
474
|
const userId = userIdOrError;
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
if (customer && customer.customerRef) {
|
|
506
|
-
if (customer.externalRef && customer.externalRef === userId) {
|
|
507
|
-
const filteredPurchases = (customer.purchases || []).filter(
|
|
508
|
-
(p) => p.status === "active"
|
|
509
|
-
);
|
|
510
|
-
return {
|
|
511
|
-
customerRef: customer.customerRef,
|
|
512
|
-
email: customer.email,
|
|
513
|
-
name: customer.name,
|
|
514
|
-
purchases: filteredPurchases
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
} catch {
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
const deduplicator = getSharedDeduplicator(options.deduplication);
|
|
522
|
-
const response = await deduplicator.deduplicate(userId, async () => {
|
|
523
|
-
try {
|
|
524
|
-
const ensuredCustomerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
525
|
-
email: email || void 0,
|
|
526
|
-
name: name || void 0
|
|
475
|
+
const response = await deduplicator.deduplicate(
|
|
476
|
+
userId,
|
|
477
|
+
async () => {
|
|
478
|
+
return await (0, import_server21.checkPurchaseCore)(request, {
|
|
479
|
+
solvaPay: options.solvaPay,
|
|
480
|
+
includeEmail: options.includeEmail,
|
|
481
|
+
includeName: options.includeName
|
|
527
482
|
});
|
|
528
|
-
const customer = await solvaPay.getCustomer({ customerRef: ensuredCustomerRef });
|
|
529
|
-
const filteredPurchases = (customer.purchases || []).filter(
|
|
530
|
-
(p) => p.status === "active"
|
|
531
|
-
);
|
|
532
|
-
const result = {
|
|
533
|
-
customerRef: customer.customerRef || userId,
|
|
534
|
-
email: customer.email,
|
|
535
|
-
name: customer.name,
|
|
536
|
-
purchases: filteredPurchases
|
|
537
|
-
};
|
|
538
|
-
return result;
|
|
539
|
-
} catch (error) {
|
|
540
|
-
console.error("[checkPurchase] Error fetching customer:", error);
|
|
541
|
-
return {
|
|
542
|
-
customerRef: userId,
|
|
543
|
-
purchases: []
|
|
544
|
-
};
|
|
545
483
|
}
|
|
546
|
-
|
|
484
|
+
);
|
|
485
|
+
if ((0, import_server21.isErrorResult)(response)) {
|
|
486
|
+
return import_server20.NextResponse.json(
|
|
487
|
+
{ error: response.error, details: response.details },
|
|
488
|
+
{ status: response.status }
|
|
489
|
+
);
|
|
490
|
+
}
|
|
547
491
|
return response;
|
|
548
492
|
} catch (error) {
|
|
549
493
|
console.error("Check purchase failed:", error);
|
|
550
|
-
if (error instanceof import_core.SolvaPayError) {
|
|
551
|
-
return import_server20.NextResponse.json({ error: error.message }, { status: 500 });
|
|
552
|
-
}
|
|
553
494
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
554
495
|
return import_server20.NextResponse.json(
|
|
555
496
|
{ error: "Failed to check purchase", details: errorMessage },
|
package/dist/index.d.cts
CHANGED
|
@@ -380,40 +380,8 @@ interface CheckPurchaseOptions {
|
|
|
380
380
|
* - Fast path optimization using cached customer references from client
|
|
381
381
|
* - Automatic customer creation if needed
|
|
382
382
|
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* 2. Gets user email and name from Supabase JWT token (if available)
|
|
386
|
-
* 3. Validates cached customer reference (if provided via header)
|
|
387
|
-
* 4. Ensures customer exists in SolvaPay backend
|
|
388
|
-
* 5. Returns customer purchase information
|
|
389
|
-
*
|
|
390
|
-
* @param request - Next.js request object (NextRequest extends Request)
|
|
391
|
-
* @param options - Configuration options
|
|
392
|
-
* @param options.deduplication - Request deduplication options (see {@link RequestDeduplicationOptions})
|
|
393
|
-
* @param options.solvaPay - Optional SolvaPay instance (creates new one if not provided)
|
|
394
|
-
* @param options.includeEmail - Whether to include email in response (default: true)
|
|
395
|
-
* @param options.includeName - Whether to include name in response (default: true)
|
|
396
|
-
* @returns Purchase check result with customer data and purchases, or NextResponse error
|
|
397
|
-
*
|
|
398
|
-
* @example
|
|
399
|
-
* ```typescript
|
|
400
|
-
* import { NextRequest, NextResponse } from 'next/server';
|
|
401
|
-
* import { checkPurchase } from '@solvapay/next';
|
|
402
|
-
*
|
|
403
|
-
* export async function GET(request: NextRequest) {
|
|
404
|
-
* const result = await checkPurchase(request);
|
|
405
|
-
*
|
|
406
|
-
* if (result instanceof NextResponse) {
|
|
407
|
-
* return result; // Error response
|
|
408
|
-
* }
|
|
409
|
-
*
|
|
410
|
-
* return NextResponse.json(result);
|
|
411
|
-
* }
|
|
412
|
-
* ```
|
|
413
|
-
*
|
|
414
|
-
* @see {@link clearPurchaseCache} for cache management
|
|
415
|
-
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
416
|
-
* @since 1.0.0
|
|
383
|
+
* Delegates to `checkPurchaseCore` from `@solvapay/server` for the actual logic,
|
|
384
|
+
* wrapping with Next.js-specific deduplication and response formatting.
|
|
417
385
|
*/
|
|
418
386
|
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
419
387
|
|
package/dist/index.d.ts
CHANGED
|
@@ -380,40 +380,8 @@ interface CheckPurchaseOptions {
|
|
|
380
380
|
* - Fast path optimization using cached customer references from client
|
|
381
381
|
* - Automatic customer creation if needed
|
|
382
382
|
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* 2. Gets user email and name from Supabase JWT token (if available)
|
|
386
|
-
* 3. Validates cached customer reference (if provided via header)
|
|
387
|
-
* 4. Ensures customer exists in SolvaPay backend
|
|
388
|
-
* 5. Returns customer purchase information
|
|
389
|
-
*
|
|
390
|
-
* @param request - Next.js request object (NextRequest extends Request)
|
|
391
|
-
* @param options - Configuration options
|
|
392
|
-
* @param options.deduplication - Request deduplication options (see {@link RequestDeduplicationOptions})
|
|
393
|
-
* @param options.solvaPay - Optional SolvaPay instance (creates new one if not provided)
|
|
394
|
-
* @param options.includeEmail - Whether to include email in response (default: true)
|
|
395
|
-
* @param options.includeName - Whether to include name in response (default: true)
|
|
396
|
-
* @returns Purchase check result with customer data and purchases, or NextResponse error
|
|
397
|
-
*
|
|
398
|
-
* @example
|
|
399
|
-
* ```typescript
|
|
400
|
-
* import { NextRequest, NextResponse } from 'next/server';
|
|
401
|
-
* import { checkPurchase } from '@solvapay/next';
|
|
402
|
-
*
|
|
403
|
-
* export async function GET(request: NextRequest) {
|
|
404
|
-
* const result = await checkPurchase(request);
|
|
405
|
-
*
|
|
406
|
-
* if (result instanceof NextResponse) {
|
|
407
|
-
* return result; // Error response
|
|
408
|
-
* }
|
|
409
|
-
*
|
|
410
|
-
* return NextResponse.json(result);
|
|
411
|
-
* }
|
|
412
|
-
* ```
|
|
413
|
-
*
|
|
414
|
-
* @see {@link clearPurchaseCache} for cache management
|
|
415
|
-
* @see {@link getPurchaseCacheStats} for cache monitoring
|
|
416
|
-
* @since 1.0.0
|
|
383
|
+
* Delegates to `checkPurchaseCore` from `@solvapay/server` for the actual logic,
|
|
384
|
+
* wrapping with Next.js-specific deduplication and response formatting.
|
|
417
385
|
*/
|
|
418
386
|
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
419
387
|
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,7 @@ import {
|
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
7
|
import { NextResponse as NextResponse9 } from "next/server";
|
|
8
|
-
import {
|
|
9
|
-
import { SolvaPayError } from "@solvapay/core";
|
|
8
|
+
import { checkPurchaseCore, isErrorResult as isErrorResult9 } from "@solvapay/server";
|
|
10
9
|
|
|
11
10
|
// src/cache.ts
|
|
12
11
|
function createRequestDeduplicator(options = {}) {
|
|
@@ -349,45 +348,23 @@ async function listPlans(request) {
|
|
|
349
348
|
|
|
350
349
|
// src/helpers/usage.ts
|
|
351
350
|
import { NextResponse as NextResponse8 } from "next/server";
|
|
352
|
-
import {
|
|
351
|
+
import { trackUsageCore, isErrorResult as isErrorResult8 } from "@solvapay/server";
|
|
353
352
|
async function trackUsage(request, body, options = {}) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
if (isErrorResult8(userResult)) {
|
|
357
|
-
return NextResponse8.json(
|
|
358
|
-
{ error: userResult.error, details: userResult.details },
|
|
359
|
-
{ status: userResult.status }
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
const { userId, email, name } = userResult;
|
|
363
|
-
const solvaPay = options.solvaPay || createSolvaPay();
|
|
364
|
-
const customerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
365
|
-
email: email || void 0,
|
|
366
|
-
name: name || void 0
|
|
367
|
-
});
|
|
368
|
-
await solvaPay.trackUsage({
|
|
369
|
-
customerRef,
|
|
370
|
-
actionType: body.actionType,
|
|
371
|
-
units: body.units,
|
|
372
|
-
productRef: body.productRef,
|
|
373
|
-
description: body.description,
|
|
374
|
-
metadata: body.metadata
|
|
375
|
-
});
|
|
376
|
-
return NextResponse8.json({ success: true });
|
|
377
|
-
} catch (error) {
|
|
378
|
-
console.error("[trackUsage] Error:", error);
|
|
379
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
353
|
+
const result = await trackUsageCore(request, body, options);
|
|
354
|
+
if (isErrorResult8(result)) {
|
|
380
355
|
return NextResponse8.json(
|
|
381
|
-
{ error:
|
|
382
|
-
{ status:
|
|
356
|
+
{ error: result.error, details: result.details },
|
|
357
|
+
{ status: result.status }
|
|
383
358
|
);
|
|
384
359
|
}
|
|
360
|
+
return NextResponse8.json(result);
|
|
385
361
|
}
|
|
386
362
|
|
|
387
363
|
// src/index.ts
|
|
388
364
|
async function checkPurchase(request, options = {}) {
|
|
365
|
+
const deduplicator = getSharedDeduplicator(options.deduplication);
|
|
389
366
|
try {
|
|
390
|
-
const { requireUserId
|
|
367
|
+
const { requireUserId } = await import("@solvapay/auth");
|
|
391
368
|
const userIdOrError = requireUserId(request);
|
|
392
369
|
if (userIdOrError instanceof Response) {
|
|
393
370
|
const clonedResponse = userIdOrError.clone();
|
|
@@ -395,61 +372,25 @@ async function checkPurchase(request, options = {}) {
|
|
|
395
372
|
return NextResponse9.json(body, { status: userIdOrError.status });
|
|
396
373
|
}
|
|
397
374
|
const userId = userIdOrError;
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (customer && customer.customerRef) {
|
|
406
|
-
if (customer.externalRef && customer.externalRef === userId) {
|
|
407
|
-
const filteredPurchases = (customer.purchases || []).filter(
|
|
408
|
-
(p) => p.status === "active"
|
|
409
|
-
);
|
|
410
|
-
return {
|
|
411
|
-
customerRef: customer.customerRef,
|
|
412
|
-
email: customer.email,
|
|
413
|
-
name: customer.name,
|
|
414
|
-
purchases: filteredPurchases
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
} catch {
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
const deduplicator = getSharedDeduplicator(options.deduplication);
|
|
422
|
-
const response = await deduplicator.deduplicate(userId, async () => {
|
|
423
|
-
try {
|
|
424
|
-
const ensuredCustomerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
425
|
-
email: email || void 0,
|
|
426
|
-
name: name || void 0
|
|
375
|
+
const response = await deduplicator.deduplicate(
|
|
376
|
+
userId,
|
|
377
|
+
async () => {
|
|
378
|
+
return await checkPurchaseCore(request, {
|
|
379
|
+
solvaPay: options.solvaPay,
|
|
380
|
+
includeEmail: options.includeEmail,
|
|
381
|
+
includeName: options.includeName
|
|
427
382
|
});
|
|
428
|
-
const customer = await solvaPay.getCustomer({ customerRef: ensuredCustomerRef });
|
|
429
|
-
const filteredPurchases = (customer.purchases || []).filter(
|
|
430
|
-
(p) => p.status === "active"
|
|
431
|
-
);
|
|
432
|
-
const result = {
|
|
433
|
-
customerRef: customer.customerRef || userId,
|
|
434
|
-
email: customer.email,
|
|
435
|
-
name: customer.name,
|
|
436
|
-
purchases: filteredPurchases
|
|
437
|
-
};
|
|
438
|
-
return result;
|
|
439
|
-
} catch (error) {
|
|
440
|
-
console.error("[checkPurchase] Error fetching customer:", error);
|
|
441
|
-
return {
|
|
442
|
-
customerRef: userId,
|
|
443
|
-
purchases: []
|
|
444
|
-
};
|
|
445
383
|
}
|
|
446
|
-
|
|
384
|
+
);
|
|
385
|
+
if (isErrorResult9(response)) {
|
|
386
|
+
return NextResponse9.json(
|
|
387
|
+
{ error: response.error, details: response.details },
|
|
388
|
+
{ status: response.status }
|
|
389
|
+
);
|
|
390
|
+
}
|
|
447
391
|
return response;
|
|
448
392
|
} catch (error) {
|
|
449
393
|
console.error("Check purchase failed:", error);
|
|
450
|
-
if (error instanceof SolvaPayError) {
|
|
451
|
-
return NextResponse9.json({ error: error.message }, { status: 500 });
|
|
452
|
-
}
|
|
453
394
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
454
395
|
return NextResponse9.json(
|
|
455
396
|
{ error: "Failed to check purchase", details: errorMessage },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solvapay/next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9-preview.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
},
|
|
35
35
|
"sideEffects": false,
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@solvapay/
|
|
38
|
-
"@solvapay/
|
|
39
|
-
"@solvapay/
|
|
37
|
+
"@solvapay/core": "1.0.9-preview.1",
|
|
38
|
+
"@solvapay/server": "1.0.9-preview.1",
|
|
39
|
+
"@solvapay/auth": "1.0.9-preview.1"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"next": ">=13.0.0"
|