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