better-auth-streampay 3.0.0 → 3.0.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/README.md +67 -650
- package/dist/client.d.cts +3 -3
- package/dist/client.d.ts +3 -3
- package/dist/index.cjs +81 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +81 -40
- package/dist/index.js.map +1 -1
- package/dist/{streampay-Cfl70SR_.d.cts → streampay-uhs67tYl.d.cts} +28 -13
- package/dist/{streampay-Cfl70SR_.d.ts → streampay-uhs67tYl.d.ts} +28 -13
- package/docs/integration.md +716 -0
- package/docs/staging-validation.md +159 -0
- package/docs/subscriptions.md +271 -0
- package/package.json +2 -1
package/dist/client.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { s as streampay, E as ErrorCodeEntry } from './streampay-
|
|
2
|
-
export { $ as $ERROR_CODES, S as StreamPayErrorCode } from './streampay-
|
|
3
|
-
import '@streamsdk/typescript';
|
|
1
|
+
import { s as streampay, E as ErrorCodeEntry } from './streampay-uhs67tYl.cjs';
|
|
2
|
+
export { $ as $ERROR_CODES, S as StreamPayErrorCode } from './streampay-uhs67tYl.cjs';
|
|
4
3
|
import 'better-auth';
|
|
4
|
+
import '@streamsdk/typescript';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import 'zod/v4/core';
|
|
7
7
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { s as streampay, E as ErrorCodeEntry } from './streampay-
|
|
2
|
-
export { $ as $ERROR_CODES, S as StreamPayErrorCode } from './streampay-
|
|
3
|
-
import '@streamsdk/typescript';
|
|
1
|
+
import { s as streampay, E as ErrorCodeEntry } from './streampay-uhs67tYl.js';
|
|
2
|
+
export { $ as $ERROR_CODES, S as StreamPayErrorCode } from './streampay-uhs67tYl.js';
|
|
4
3
|
import 'better-auth';
|
|
4
|
+
import '@streamsdk/typescript';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import 'zod/v4/core';
|
|
7
7
|
|
package/dist/index.cjs
CHANGED
|
@@ -288,6 +288,46 @@ function scopedLogger(logger2) {
|
|
|
288
288
|
function getLogger(ctx) {
|
|
289
289
|
return scopedLogger(ctx.context.logger);
|
|
290
290
|
}
|
|
291
|
+
async function getCurrentConsumerUser(ctx, user) {
|
|
292
|
+
if (!user.streampayConsumerId) return user;
|
|
293
|
+
if (!ctx.context.adapter) {
|
|
294
|
+
throw new api.APIError("INTERNAL_SERVER_ERROR", { message: "Consumer storage is unavailable." });
|
|
295
|
+
}
|
|
296
|
+
const current = await ctx.context.adapter.findOne({
|
|
297
|
+
model: "user",
|
|
298
|
+
where: [{ field: "id", value: user.id }]
|
|
299
|
+
});
|
|
300
|
+
if (!current || current.id !== user.id) {
|
|
301
|
+
throw new api.APIError("UNAUTHORIZED", { message: "The account no longer exists." });
|
|
302
|
+
}
|
|
303
|
+
if (current.streampayConsumerId != null && typeof current.streampayConsumerId !== "string") {
|
|
304
|
+
throw new api.APIError("INTERNAL_SERVER_ERROR", { message: "Invalid stored consumer link." });
|
|
305
|
+
}
|
|
306
|
+
return { ...user, streampayConsumerId: current.streampayConsumerId ?? null };
|
|
307
|
+
}
|
|
308
|
+
function asSessionUser(user) {
|
|
309
|
+
if (user === null || typeof user !== "object") return null;
|
|
310
|
+
if (!("id" in user) || typeof user.id !== "string") return null;
|
|
311
|
+
const result = { id: user.id };
|
|
312
|
+
if ("email" in user && typeof user.email === "string") {
|
|
313
|
+
result.email = user.email;
|
|
314
|
+
}
|
|
315
|
+
if ("name" in user && typeof user.name === "string") {
|
|
316
|
+
result.name = user.name;
|
|
317
|
+
}
|
|
318
|
+
if ("isAnonymous" in user && user.isAnonymous === true) {
|
|
319
|
+
result.isAnonymous = true;
|
|
320
|
+
}
|
|
321
|
+
if ("streampayConsumerId" in user) {
|
|
322
|
+
const value = user.streampayConsumerId;
|
|
323
|
+
if (typeof value === "string") {
|
|
324
|
+
result.streampayConsumerId = value;
|
|
325
|
+
} else if (value === null) {
|
|
326
|
+
result.streampayConsumerId = null;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return result;
|
|
330
|
+
}
|
|
291
331
|
|
|
292
332
|
// src/utils/ensure-consumer.ts
|
|
293
333
|
function canClaimBy(mode, identifier) {
|
|
@@ -330,6 +370,7 @@ async function resolveDuplicateConsumer(options, createPayload, context) {
|
|
|
330
370
|
return existing.id;
|
|
331
371
|
}
|
|
332
372
|
async function ensureConsumerForUser(options, ctx, user) {
|
|
373
|
+
user = await getCurrentConsumerUser(ctx, user);
|
|
333
374
|
if (user.streampayConsumerId) {
|
|
334
375
|
return { consumerId: user.streampayConsumerId, created: false };
|
|
335
376
|
}
|
|
@@ -369,10 +410,25 @@ async function ensureConsumerForUser(options, ctx, user) {
|
|
|
369
410
|
if (isDuplicateConsumerError(err)) {
|
|
370
411
|
const reusedId = await resolveDuplicateConsumer(options, payload, ctx);
|
|
371
412
|
if (reusedId) {
|
|
372
|
-
await
|
|
413
|
+
await persistConsumerId(ctx, user.id, reusedId);
|
|
373
414
|
try {
|
|
374
415
|
await options.client.updateConsumer(reusedId, { external_id: user.id });
|
|
375
416
|
} catch (backfillErr) {
|
|
417
|
+
try {
|
|
418
|
+
if (!ctx.context.adapter.update) throw consumerLinkUnavailable();
|
|
419
|
+
await ctx.context.adapter.update({
|
|
420
|
+
model: "user",
|
|
421
|
+
where: [
|
|
422
|
+
{ field: "id", value: user.id },
|
|
423
|
+
{ field: "streampayConsumerId", value: reusedId }
|
|
424
|
+
],
|
|
425
|
+
update: { streampayConsumerId: null }
|
|
426
|
+
});
|
|
427
|
+
} catch (clearErr) {
|
|
428
|
+
getLogger(ctx).error(
|
|
429
|
+
`consumer claim cleanup failed for user=${user.id} consumer=${reusedId}: ${formatStreamPayError(clearErr)}`
|
|
430
|
+
);
|
|
431
|
+
}
|
|
376
432
|
getLogger(ctx).error(
|
|
377
433
|
`consumer external_id backfill failed for user=${user.id} consumer=${reusedId}: ${formatStreamPayError(backfillErr)}`
|
|
378
434
|
);
|
|
@@ -381,7 +437,6 @@ async function ensureConsumerForUser(options, ctx, user) {
|
|
|
381
437
|
message: "StreamPay consumer linking failed. Please try again."
|
|
382
438
|
});
|
|
383
439
|
}
|
|
384
|
-
await persistConsumerId(ctx, user.id, reusedId);
|
|
385
440
|
return { consumerId: reusedId, created: false };
|
|
386
441
|
}
|
|
387
442
|
}
|
|
@@ -401,9 +456,15 @@ function consumerLinkUnavailable() {
|
|
|
401
456
|
}
|
|
402
457
|
async function findConsumerOwner(ctx, consumerId) {
|
|
403
458
|
try {
|
|
459
|
+
const where = [{ field: "streampayConsumerId", value: consumerId }];
|
|
460
|
+
if (ctx.context.internalAdapter?.listUsers) {
|
|
461
|
+
const owners = await ctx.context.internalAdapter.listUsers(1, 0, void 0, where);
|
|
462
|
+
return owners[0] ?? null;
|
|
463
|
+
}
|
|
464
|
+
if (!ctx.context.adapter) throw consumerLinkUnavailable();
|
|
404
465
|
return await ctx.context.adapter.findOne({
|
|
405
466
|
model: "user",
|
|
406
|
-
where
|
|
467
|
+
where
|
|
407
468
|
});
|
|
408
469
|
} catch (err) {
|
|
409
470
|
getLogger(ctx).error(
|
|
@@ -516,31 +577,6 @@ function rejectUnauthorized(user, anonymousMessage) {
|
|
|
516
577
|
}
|
|
517
578
|
}
|
|
518
579
|
|
|
519
|
-
// src/utils/session.ts
|
|
520
|
-
function asSessionUser(user) {
|
|
521
|
-
if (user === null || typeof user !== "object") return null;
|
|
522
|
-
if (!("id" in user) || typeof user.id !== "string") return null;
|
|
523
|
-
const result = { id: user.id };
|
|
524
|
-
if ("email" in user && typeof user.email === "string") {
|
|
525
|
-
result.email = user.email;
|
|
526
|
-
}
|
|
527
|
-
if ("name" in user && typeof user.name === "string") {
|
|
528
|
-
result.name = user.name;
|
|
529
|
-
}
|
|
530
|
-
if ("isAnonymous" in user && user.isAnonymous === true) {
|
|
531
|
-
result.isAnonymous = true;
|
|
532
|
-
}
|
|
533
|
-
if ("streampayConsumerId" in user) {
|
|
534
|
-
const value = user.streampayConsumerId;
|
|
535
|
-
if (typeof value === "string") {
|
|
536
|
-
result.streampayConsumerId = value;
|
|
537
|
-
} else if (value === null) {
|
|
538
|
-
result.streampayConsumerId = null;
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
return result;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
580
|
// src/plugins/subscriptions/status.ts
|
|
545
581
|
var SUBSCRIPTION_STATUSES = [
|
|
546
582
|
"incomplete",
|
|
@@ -2876,9 +2912,10 @@ var paginationQuery = zod.z.object({
|
|
|
2876
2912
|
page: zod.z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).optional(),
|
|
2877
2913
|
size: zod.z.coerce.number().int().min(1).max(100).optional()
|
|
2878
2914
|
});
|
|
2879
|
-
async function getConsumerIdOrNull(client, user) {
|
|
2915
|
+
async function getConsumerIdOrNull(ctx, client, user) {
|
|
2880
2916
|
rejectUnauthorized(user, "Anonymous users cannot access the billing portal.");
|
|
2881
|
-
|
|
2917
|
+
const current = await getCurrentConsumerUser(ctx, user);
|
|
2918
|
+
if (current.streampayConsumerId) return current.streampayConsumerId;
|
|
2882
2919
|
return findConsumerByExternalId(client, { externalId: user.id });
|
|
2883
2920
|
}
|
|
2884
2921
|
var portal = ({ pageSize, createSession } = {}) => (options) => {
|
|
@@ -2893,6 +2930,7 @@ var portal = ({ pageSize, createSession } = {}) => (options) => {
|
|
|
2893
2930
|
ctx.setHeader("cache-control", "no-store");
|
|
2894
2931
|
try {
|
|
2895
2932
|
const consumerId = await getConsumerIdOrNull(
|
|
2933
|
+
ctx,
|
|
2896
2934
|
client,
|
|
2897
2935
|
asSessionUser(ctx.context.session?.user)
|
|
2898
2936
|
);
|
|
@@ -2920,7 +2958,7 @@ var portal = ({ pageSize, createSession } = {}) => (options) => {
|
|
|
2920
2958
|
async (ctx) => {
|
|
2921
2959
|
const user = asSessionUser(ctx.context.session?.user);
|
|
2922
2960
|
try {
|
|
2923
|
-
const consumerId = await getConsumerIdOrNull(client, user);
|
|
2961
|
+
const consumerId = await getConsumerIdOrNull(ctx, client, user);
|
|
2924
2962
|
if (!consumerId) return ctx.json({ hasConsumer: false, consumer: null });
|
|
2925
2963
|
const consumer = await client.getConsumer(consumerId);
|
|
2926
2964
|
return ctx.json({ hasConsumer: true, consumer });
|
|
@@ -2935,7 +2973,7 @@ var portal = ({ pageSize, createSession } = {}) => (options) => {
|
|
|
2935
2973
|
async (ctx) => {
|
|
2936
2974
|
const user = asSessionUser(ctx.context.session?.user);
|
|
2937
2975
|
try {
|
|
2938
|
-
const consumerId = await getConsumerIdOrNull(client, user);
|
|
2976
|
+
const consumerId = await getConsumerIdOrNull(ctx, client, user);
|
|
2939
2977
|
if (!consumerId) return ctx.json({ hasConsumer: false, data: [] });
|
|
2940
2978
|
const response = await client.listSubscriptions({
|
|
2941
2979
|
organization_consumer_id: consumerId,
|
|
@@ -2958,7 +2996,7 @@ var portal = ({ pageSize, createSession } = {}) => (options) => {
|
|
|
2958
2996
|
async (ctx) => {
|
|
2959
2997
|
const user = asSessionUser(ctx.context.session?.user);
|
|
2960
2998
|
try {
|
|
2961
|
-
const consumerId = await getConsumerIdOrNull(client, user);
|
|
2999
|
+
const consumerId = await getConsumerIdOrNull(ctx, client, user);
|
|
2962
3000
|
if (!consumerId) return ctx.json({ hasConsumer: false, data: [] });
|
|
2963
3001
|
const response = await client.listInvoices({
|
|
2964
3002
|
organization_consumer_id: consumerId,
|
|
@@ -5412,9 +5450,6 @@ var webhooks = (webhooksOptions) => {
|
|
|
5412
5450
|
};
|
|
5413
5451
|
};
|
|
5414
5452
|
var isAnonymous = (user) => "isAnonymous" in user && user.isAnonymous === true;
|
|
5415
|
-
function hasInternalAdapter(ctx) {
|
|
5416
|
-
return "internalAdapter" in ctx.context && ctx.context.internalAdapter !== null && typeof ctx.context.internalAdapter === "object" && "updateUser" in ctx.context.internalAdapter && typeof ctx.context.internalAdapter.updateUser === "function";
|
|
5417
|
-
}
|
|
5418
5453
|
var onBeforeUserCreate = (options) => async (user, context) => {
|
|
5419
5454
|
if (!context || !options.createConsumerOnSignUp) return void 0;
|
|
5420
5455
|
if (isAnonymous(user)) return void 0;
|
|
@@ -5444,6 +5479,7 @@ var onBeforeUserCreate = (options) => async (user, context) => {
|
|
|
5444
5479
|
if (isDuplicateConsumerError(err)) {
|
|
5445
5480
|
const reusedId = await resolveDuplicateConsumer(options, createPayload, context);
|
|
5446
5481
|
if (reusedId) {
|
|
5482
|
+
await assertConsumerUnclaimed(context, reusedId);
|
|
5447
5483
|
return { data: { streampayConsumerId: reusedId } };
|
|
5448
5484
|
}
|
|
5449
5485
|
}
|
|
@@ -5492,10 +5528,15 @@ var onUserUpdate = (options) => async (user, context) => {
|
|
|
5492
5528
|
getLogger(context).error(
|
|
5493
5529
|
`consumer ${sessionUser.streampayConsumerId} not found for user=${sessionUser.id}; clearing stale link for re-provisioning.`
|
|
5494
5530
|
);
|
|
5495
|
-
if (
|
|
5531
|
+
if (typeof context.context.adapter?.update === "function") {
|
|
5496
5532
|
try {
|
|
5497
|
-
await context.context.
|
|
5498
|
-
|
|
5533
|
+
await context.context.adapter.update({
|
|
5534
|
+
model: "user",
|
|
5535
|
+
where: [
|
|
5536
|
+
{ field: "id", value: sessionUser.id },
|
|
5537
|
+
{ field: "streampayConsumerId", value: sessionUser.streampayConsumerId }
|
|
5538
|
+
],
|
|
5539
|
+
update: { streampayConsumerId: null }
|
|
5499
5540
|
});
|
|
5500
5541
|
} catch (clearErr) {
|
|
5501
5542
|
getLogger(context).error(
|
|
@@ -5527,7 +5568,7 @@ var onUserDelete = (options) => async (user, context) => {
|
|
|
5527
5568
|
|
|
5528
5569
|
// package.json
|
|
5529
5570
|
var package_default = {
|
|
5530
|
-
version: "3.0.
|
|
5571
|
+
version: "3.0.1"};
|
|
5531
5572
|
|
|
5532
5573
|
// src/version.ts
|
|
5533
5574
|
var version = package_default.version;
|