better-auth-streampay 3.0.1 → 3.0.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/dist/index.cjs +101 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +101 -82
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -247,6 +247,82 @@ function readValidationDetails(body) {
|
|
|
247
247
|
return entries;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
// src/utils/parse-error.ts
|
|
251
|
+
function parseStreamPayError(err) {
|
|
252
|
+
const { status, requestId, body, message: fallbackMessage } = readSdkErrorFields(err);
|
|
253
|
+
const envelope = readEnvelope(body);
|
|
254
|
+
if (envelope && (envelope.code !== void 0 || envelope.message !== void 0)) {
|
|
255
|
+
return {
|
|
256
|
+
status,
|
|
257
|
+
code: envelope.code,
|
|
258
|
+
requestId,
|
|
259
|
+
message: envelope.message ?? fallbackMessage,
|
|
260
|
+
additionalInfo: envelope.additionalInfo,
|
|
261
|
+
validationErrors: []
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const validationErrors = readValidationDetails(body).map((entry) => ({
|
|
265
|
+
loc: entry.loc,
|
|
266
|
+
message: entry.msg,
|
|
267
|
+
type: entry.type
|
|
268
|
+
}));
|
|
269
|
+
return {
|
|
270
|
+
status,
|
|
271
|
+
code: void 0,
|
|
272
|
+
requestId,
|
|
273
|
+
message: fallbackMessage,
|
|
274
|
+
additionalInfo: void 0,
|
|
275
|
+
validationErrors
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// src/utils/errors.ts
|
|
280
|
+
function statusToAPIErrorStatus(status) {
|
|
281
|
+
switch (status) {
|
|
282
|
+
case 400:
|
|
283
|
+
return "BAD_REQUEST";
|
|
284
|
+
case 401:
|
|
285
|
+
return "UNAUTHORIZED";
|
|
286
|
+
case 403:
|
|
287
|
+
return "FORBIDDEN";
|
|
288
|
+
case 404:
|
|
289
|
+
return "NOT_FOUND";
|
|
290
|
+
case 409:
|
|
291
|
+
return "CONFLICT";
|
|
292
|
+
case 422:
|
|
293
|
+
return "UNPROCESSABLE_ENTITY";
|
|
294
|
+
case 429:
|
|
295
|
+
return "TOO_MANY_REQUESTS";
|
|
296
|
+
default:
|
|
297
|
+
return "INTERNAL_SERVER_ERROR";
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function toAPIError(messageOrConfig, err, logger2) {
|
|
301
|
+
if (err instanceof APIError) throw err;
|
|
302
|
+
const { logPrefix, userMessage } = typeof messageOrConfig === "string" ? { logPrefix: messageOrConfig, userMessage: messageOrConfig } : messageOrConfig;
|
|
303
|
+
const parsed = parseStreamPayError(err);
|
|
304
|
+
const tail = [
|
|
305
|
+
parsed.status !== void 0 ? `status=${parsed.status}` : void 0,
|
|
306
|
+
parsed.code ? `code=${parsed.code}` : void 0,
|
|
307
|
+
parsed.requestId ? `request_id=${parsed.requestId}` : void 0
|
|
308
|
+
].filter((segment) => segment !== void 0);
|
|
309
|
+
const validationSummary = parsed.validationErrors.length > 0 ? ` validation=${parsed.validationErrors.map((issue) => `${issue.loc.join(".")}: ${issue.message}`).join(" | ")}` : "";
|
|
310
|
+
const logDetail = (tail.length > 0 ? `${parsed.message} [${tail.join(" ")}]` : parsed.message) + validationSummary;
|
|
311
|
+
logger2.error(`${logPrefix} ${logDetail}`);
|
|
312
|
+
throw new APIError(statusToAPIErrorStatus(parsed.status), {
|
|
313
|
+
code: mapToErrorCode(parsed.code, parsed.status),
|
|
314
|
+
message: userMessage
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
function rejectUnauthorized(user, anonymousMessage) {
|
|
318
|
+
if (!user) {
|
|
319
|
+
throw new APIError("UNAUTHORIZED", { message: "Session user is missing." });
|
|
320
|
+
}
|
|
321
|
+
if (user.isAnonymous) {
|
|
322
|
+
throw new APIError("UNAUTHORIZED", { message: anonymousMessage });
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
250
326
|
// src/utils/format-error.ts
|
|
251
327
|
function formatStreamPayError(err) {
|
|
252
328
|
const { body, message: fallback } = readSdkErrorFields(err);
|
|
@@ -438,12 +514,31 @@ async function ensureConsumerForUser(options, ctx, user) {
|
|
|
438
514
|
return { consumerId: reusedId, created: false };
|
|
439
515
|
}
|
|
440
516
|
}
|
|
441
|
-
|
|
442
|
-
|
|
517
|
+
const { status } = readSdkErrorFields(err);
|
|
518
|
+
const transient = status !== void 0 && status >= 500 || status === void 0 && err instanceof TypeError;
|
|
519
|
+
let recoveredId = null;
|
|
520
|
+
if (transient) {
|
|
521
|
+
try {
|
|
522
|
+
recoveredId = await findConsumerByExternalId(options.client, { externalId: user.id });
|
|
523
|
+
} catch (searchErr) {
|
|
524
|
+
getLogger(ctx).warn(
|
|
525
|
+
`consumer recovery search failed for user=${user.id}: ${formatStreamPayError(searchErr)}`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (recoveredId) {
|
|
530
|
+
await persistConsumerId(ctx, user.id, recoveredId);
|
|
531
|
+
getLogger(ctx).warn(`consumer recovered after failed creation for user=${user.id}`);
|
|
532
|
+
return { consumerId: recoveredId, created: false };
|
|
533
|
+
}
|
|
534
|
+
toAPIError(
|
|
535
|
+
{
|
|
536
|
+
logPrefix: `consumer creation failed for user=${user.id}:`,
|
|
537
|
+
userMessage: "StreamPay consumer provisioning failed. Please try again."
|
|
538
|
+
},
|
|
539
|
+
err,
|
|
540
|
+
getLogger(ctx)
|
|
443
541
|
);
|
|
444
|
-
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
445
|
-
message: "StreamPay consumer provisioning failed. Please try again."
|
|
446
|
-
});
|
|
447
542
|
}
|
|
448
543
|
}
|
|
449
544
|
function consumerLinkUnavailable() {
|
|
@@ -499,82 +594,6 @@ async function persistConsumerId(ctx, userId, consumerId) {
|
|
|
499
594
|
}
|
|
500
595
|
}
|
|
501
596
|
|
|
502
|
-
// src/utils/parse-error.ts
|
|
503
|
-
function parseStreamPayError(err) {
|
|
504
|
-
const { status, requestId, body, message: fallbackMessage } = readSdkErrorFields(err);
|
|
505
|
-
const envelope = readEnvelope(body);
|
|
506
|
-
if (envelope && (envelope.code !== void 0 || envelope.message !== void 0)) {
|
|
507
|
-
return {
|
|
508
|
-
status,
|
|
509
|
-
code: envelope.code,
|
|
510
|
-
requestId,
|
|
511
|
-
message: envelope.message ?? fallbackMessage,
|
|
512
|
-
additionalInfo: envelope.additionalInfo,
|
|
513
|
-
validationErrors: []
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
const validationErrors = readValidationDetails(body).map((entry) => ({
|
|
517
|
-
loc: entry.loc,
|
|
518
|
-
message: entry.msg,
|
|
519
|
-
type: entry.type
|
|
520
|
-
}));
|
|
521
|
-
return {
|
|
522
|
-
status,
|
|
523
|
-
code: void 0,
|
|
524
|
-
requestId,
|
|
525
|
-
message: fallbackMessage,
|
|
526
|
-
additionalInfo: void 0,
|
|
527
|
-
validationErrors
|
|
528
|
-
};
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
// src/utils/errors.ts
|
|
532
|
-
function statusToAPIErrorStatus(status) {
|
|
533
|
-
switch (status) {
|
|
534
|
-
case 400:
|
|
535
|
-
return "BAD_REQUEST";
|
|
536
|
-
case 401:
|
|
537
|
-
return "UNAUTHORIZED";
|
|
538
|
-
case 403:
|
|
539
|
-
return "FORBIDDEN";
|
|
540
|
-
case 404:
|
|
541
|
-
return "NOT_FOUND";
|
|
542
|
-
case 409:
|
|
543
|
-
return "CONFLICT";
|
|
544
|
-
case 422:
|
|
545
|
-
return "UNPROCESSABLE_ENTITY";
|
|
546
|
-
case 429:
|
|
547
|
-
return "TOO_MANY_REQUESTS";
|
|
548
|
-
default:
|
|
549
|
-
return "INTERNAL_SERVER_ERROR";
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
function toAPIError(messageOrConfig, err, logger2) {
|
|
553
|
-
if (err instanceof APIError) throw err;
|
|
554
|
-
const { logPrefix, userMessage } = typeof messageOrConfig === "string" ? { logPrefix: messageOrConfig, userMessage: messageOrConfig } : messageOrConfig;
|
|
555
|
-
const parsed = parseStreamPayError(err);
|
|
556
|
-
const tail = [
|
|
557
|
-
parsed.status !== void 0 ? `status=${parsed.status}` : void 0,
|
|
558
|
-
parsed.code ? `code=${parsed.code}` : void 0,
|
|
559
|
-
parsed.requestId ? `request_id=${parsed.requestId}` : void 0
|
|
560
|
-
].filter((segment) => segment !== void 0);
|
|
561
|
-
const validationSummary = parsed.validationErrors.length > 0 ? ` validation=${parsed.validationErrors.map((issue) => `${issue.loc.join(".")}: ${issue.message}`).join(" | ")}` : "";
|
|
562
|
-
const logDetail = (tail.length > 0 ? `${parsed.message} [${tail.join(" ")}]` : parsed.message) + validationSummary;
|
|
563
|
-
logger2.error(`${logPrefix} ${logDetail}`);
|
|
564
|
-
throw new APIError(statusToAPIErrorStatus(parsed.status), {
|
|
565
|
-
code: mapToErrorCode(parsed.code, parsed.status),
|
|
566
|
-
message: userMessage
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
function rejectUnauthorized(user, anonymousMessage) {
|
|
570
|
-
if (!user) {
|
|
571
|
-
throw new APIError("UNAUTHORIZED", { message: "Session user is missing." });
|
|
572
|
-
}
|
|
573
|
-
if (user.isAnonymous) {
|
|
574
|
-
throw new APIError("UNAUTHORIZED", { message: anonymousMessage });
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
|
|
578
597
|
// src/plugins/subscriptions/status.ts
|
|
579
598
|
var SUBSCRIPTION_STATUSES = [
|
|
580
599
|
"incomplete",
|
|
@@ -5566,7 +5585,7 @@ var onUserDelete = (options) => async (user, context) => {
|
|
|
5566
5585
|
|
|
5567
5586
|
// package.json
|
|
5568
5587
|
var package_default = {
|
|
5569
|
-
version: "3.0.
|
|
5588
|
+
version: "3.0.2"};
|
|
5570
5589
|
|
|
5571
5590
|
// src/version.ts
|
|
5572
5591
|
var version = package_default.version;
|