@windrun-huaiin/backend-core 14.2.0 → 14.4.0
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/app/api/stripe/checkout/route.js +1 -1
- package/dist/app/api/stripe/checkout/route.mjs +1 -1
- package/dist/app/api/stripe/customer-portal/route.js +1 -1
- package/dist/app/api/stripe/customer-portal/route.mjs +1 -1
- package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -1
- package/dist/app/api/user/anonymous/init/route.js +2 -1
- package/dist/app/api/user/anonymous/init/route.mjs +3 -2
- package/dist/auth/auth-middleware.d.ts +13 -0
- package/dist/auth/auth-middleware.d.ts.map +1 -0
- package/dist/auth/auth-middleware.js +86 -0
- package/dist/auth/auth-middleware.mjs +82 -0
- package/dist/auth/auth-shared.d.ts +14 -0
- package/dist/auth/auth-shared.d.ts.map +1 -0
- package/dist/auth/auth-shared.js +13 -0
- package/dist/auth/auth-shared.mjs +10 -0
- package/dist/auth/auth-utils.d.ts +48 -0
- package/dist/auth/auth-utils.d.ts.map +1 -0
- package/dist/auth/auth-utils.js +110 -0
- package/dist/auth/auth-utils.mjs +105 -0
- package/dist/index.js +2 -6
- package/dist/index.mjs +2 -2
- package/dist/lib/index.d.ts +0 -1
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +0 -5
- package/dist/lib/index.mjs +0 -1
- package/dist/lib/money-price-helper.js +2 -1
- package/dist/lib/money-price-helper.mjs +3 -2
- package/dist/services/context/index.d.ts +1 -0
- package/dist/services/context/index.d.ts.map +1 -1
- package/dist/services/context/index.js +2 -1
- package/dist/services/context/index.mjs +2 -1
- package/dist/services/context/user-context-finalizer.d.ts +13 -0
- package/dist/services/context/user-context-finalizer.d.ts.map +1 -0
- package/dist/services/context/user-context-finalizer.js +74 -0
- package/dist/services/context/user-context-finalizer.mjs +72 -0
- package/dist/services/context/user-context-service.d.ts +0 -6
- package/dist/services/context/user-context-service.d.ts.map +1 -1
- package/dist/services/context/user-context-service.js +0 -64
- package/dist/services/context/user-context-service.mjs +1 -64
- package/package.json +22 -7
- package/src/app/api/stripe/checkout/route.ts +1 -1
- package/src/app/api/stripe/customer-portal/route.ts +1 -1
- package/src/app/api/user/anonymous/init/route.ts +3 -2
- package/src/auth/auth-middleware.ts +109 -0
- package/src/auth/auth-shared.ts +16 -0
- package/src/{lib → auth}/auth-utils.ts +14 -10
- package/src/lib/index.ts +0 -1
- package/src/lib/money-price-helper.ts +3 -3
- package/src/services/context/index.ts +2 -1
- package/src/services/context/user-context-finalizer.ts +84 -0
- package/src/services/context/user-context-service.ts +0 -77
package/dist/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC"}
|
package/dist/lib/index.js
CHANGED
|
@@ -5,7 +5,6 @@ var moneyPriceHelper = require('./money-price-helper.js');
|
|
|
5
5
|
var fingerprintConfig = require('./fingerprint-config.js');
|
|
6
6
|
var creditInit = require('./credit-init.js');
|
|
7
7
|
var stripeConfig = require('./stripe-config.js');
|
|
8
|
-
var authUtils = require('./auth-utils.js');
|
|
9
8
|
var upstashConfig = require('./upstash-config.js');
|
|
10
9
|
var redisLock = require('./upstash/redis-lock.js');
|
|
11
10
|
var redisLike = require('./upstash/redis-like.js');
|
|
@@ -36,10 +35,6 @@ exports.fetchPaymentId = stripeConfig.fetchPaymentId;
|
|
|
36
35
|
exports.getStripe = stripeConfig.getStripe;
|
|
37
36
|
exports.updateSubscription = stripeConfig.updateSubscription;
|
|
38
37
|
exports.validateStripeWebhook = stripeConfig.validateStripeWebhook;
|
|
39
|
-
exports.ApiAuthUtils = authUtils.ApiAuthUtils;
|
|
40
|
-
exports.getAuthenticatedUser = authUtils.getAuthenticatedUser;
|
|
41
|
-
exports.requireAuth = authUtils.requireAuth;
|
|
42
|
-
exports.requireAuthWithUser = authUtils.requireAuthWithUser;
|
|
43
38
|
exports.getQstash = upstashConfig.getQstash;
|
|
44
39
|
exports.getRedis = upstashConfig.getRedis;
|
|
45
40
|
exports.withQstash = upstashConfig.withQstash;
|
package/dist/lib/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ export { getMoneyPriceInitUserContext } from './money-price-helper.mjs';
|
|
|
3
3
|
export { fingerprintConfig } from './fingerprint-config.mjs';
|
|
4
4
|
export { creditsConfig, freeAmount, freeExpiredDays, freeRegisterAmount, oneTimeExpiredDays } from './credit-init.mjs';
|
|
5
5
|
export { ActiveSubscriptionExistsError, cancelSubscription, createCheckoutSession, createCustomerPortalSession, createOrGetCustomer, fetchPaymentId, getStripe, updateSubscription, validateStripeWebhook } from './stripe-config.mjs';
|
|
6
|
-
export { ApiAuthUtils, getAuthenticatedUser, requireAuth, requireAuthWithUser } from './auth-utils.mjs';
|
|
7
6
|
export { getQstash, getRedis, withQstash, withRedis } from './upstash-config.mjs';
|
|
8
7
|
export { acquireLock, releaseLock, withLock } from './upstash/redis-lock.mjs';
|
|
9
8
|
export { getTargetLikeCount, getUserLikedTargets, isTargetLiked, likeTarget, unlikeTarget } from './upstash/redis-like.mjs';
|
|
@@ -5,6 +5,7 @@ var server = require('@clerk/nextjs/server');
|
|
|
5
5
|
var headers = require('next/headers');
|
|
6
6
|
var server$1 = require('@windrun-huaiin/third-ui/fingerprint/server');
|
|
7
7
|
var userContextService = require('../services/context/user-context-service.js');
|
|
8
|
+
var userContextFinalizer = require('../services/context/user-context-finalizer.js');
|
|
8
9
|
|
|
9
10
|
function readFingerprintIdFromRequest() {
|
|
10
11
|
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -36,7 +37,7 @@ function getMoneyPriceInitUserContext() {
|
|
|
36
37
|
subscription: userContext.subscription,
|
|
37
38
|
isClerkAuthenticated: true,
|
|
38
39
|
});
|
|
39
|
-
return
|
|
40
|
+
return userContextFinalizer.finalizeUserContext(initUserContext);
|
|
40
41
|
}
|
|
41
42
|
const fingerprintId = yield readFingerprintIdFromRequest();
|
|
42
43
|
if (!fingerprintId) {
|
|
@@ -2,7 +2,8 @@ import { __awaiter } from '../node_modules/.pnpm/@rollup_plugin-typescript@12.1.
|
|
|
2
2
|
import { auth } from '@clerk/nextjs/server';
|
|
3
3
|
import { cookies, headers } from 'next/headers';
|
|
4
4
|
import { extractFingerprintFromNextStores } from '@windrun-huaiin/third-ui/fingerprint/server';
|
|
5
|
-
import { fetchUserContextByClerkUserId, buildInitUserContextFromEntities
|
|
5
|
+
import { fetchUserContextByClerkUserId, buildInitUserContextFromEntities } from '../services/context/user-context-service.mjs';
|
|
6
|
+
import { finalizeUserContext } from '../services/context/user-context-finalizer.mjs';
|
|
6
7
|
|
|
7
8
|
function readFingerprintIdFromRequest() {
|
|
8
9
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -34,7 +35,7 @@ function getMoneyPriceInitUserContext() {
|
|
|
34
35
|
subscription: userContext.subscription,
|
|
35
36
|
isClerkAuthenticated: true,
|
|
36
37
|
});
|
|
37
|
-
return
|
|
38
|
+
return finalizeUserContext(initUserContext);
|
|
38
39
|
}
|
|
39
40
|
const fingerprintId = yield readFingerprintIdFromRequest();
|
|
40
41
|
if (!fingerprintId) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/context/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC"}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var userContextService = require('./user-context-service.js');
|
|
4
|
+
var userContextFinalizer = require('./user-context-finalizer.js');
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
exports.applyUserMockContext = userContextService.applyUserMockContext;
|
|
8
8
|
exports.buildInitUserContextFromEntities = userContextService.buildInitUserContextFromEntities;
|
|
9
9
|
exports.fetchLatestUserContextByFingerprintId = userContextService.fetchLatestUserContextByFingerprintId;
|
|
10
10
|
exports.fetchUserContextByClerkUserId = userContextService.fetchUserContextByClerkUserId;
|
|
11
11
|
exports.mapCreditToXCredit = userContextService.mapCreditToXCredit;
|
|
12
12
|
exports.mapSubscriptionToXSubscription = userContextService.mapSubscriptionToXSubscription;
|
|
13
13
|
exports.mapUserToXUser = userContextService.mapUserToXUser;
|
|
14
|
+
exports.finalizeUserContext = userContextFinalizer.finalizeUserContext;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser } from './user-context-service.mjs';
|
|
2
|
+
export { finalizeUserContext } from './user-context-finalizer.mjs';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
|
|
2
|
+
type FinalizableUserContext = {
|
|
3
|
+
xUser: XUser | null;
|
|
4
|
+
xSubscription: XSubscription | null;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Output finalizer for user-context payloads.
|
|
8
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
9
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
10
|
+
*/
|
|
11
|
+
export declare function finalizeUserContext<T extends FinalizableUserContext>(context: T): T;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=user-context-finalizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-context-finalizer.d.ts","sourceRoot":"","sources":["../../../src/services/context/user-context-finalizer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,sCAAsC,CAAC;AAEjF,KAAK,sBAAsB,GAAG;IAC5B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;CACrC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,sBAAsB,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAsEnF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@windrun-huaiin/lib/utils');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Output finalizer for user-context payloads.
|
|
7
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
8
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
9
|
+
*/
|
|
10
|
+
function finalizeUserContext(context) {
|
|
11
|
+
var _a;
|
|
12
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
13
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
14
|
+
if (!context.xUser ||
|
|
15
|
+
!mockEnabled ||
|
|
16
|
+
!Number.isInteger(mockType) ||
|
|
17
|
+
mockType < 0 ||
|
|
18
|
+
mockType > 4) {
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
const ensureSubscription = () => {
|
|
22
|
+
if (!context.xSubscription) {
|
|
23
|
+
const now = new Date();
|
|
24
|
+
context.xSubscription = {
|
|
25
|
+
id: BigInt(99999),
|
|
26
|
+
userId: context.xUser.userId,
|
|
27
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
28
|
+
orderId: '',
|
|
29
|
+
priceId: '',
|
|
30
|
+
priceName: 'MOCK-TEST',
|
|
31
|
+
status: 'active',
|
|
32
|
+
creditsAllocated: 0,
|
|
33
|
+
subPeriodStart: utils.viewLocalTime(now),
|
|
34
|
+
subPeriodEnd: utils.viewLocalTime(now),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return context.xSubscription;
|
|
38
|
+
};
|
|
39
|
+
switch (mockType) {
|
|
40
|
+
case 0: {
|
|
41
|
+
const subscription = ensureSubscription();
|
|
42
|
+
subscription.status = '';
|
|
43
|
+
subscription.priceId = '';
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 1: {
|
|
47
|
+
const subscription = ensureSubscription();
|
|
48
|
+
subscription.priceId =
|
|
49
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 2: {
|
|
53
|
+
const subscription = ensureSubscription();
|
|
54
|
+
subscription.priceId =
|
|
55
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 3: {
|
|
59
|
+
const subscription = ensureSubscription();
|
|
60
|
+
subscription.priceId =
|
|
61
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case 4: {
|
|
65
|
+
const subscription = ensureSubscription();
|
|
66
|
+
subscription.priceId =
|
|
67
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return context;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
exports.finalizeUserContext = finalizeUserContext;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { viewLocalTime } from '@windrun-huaiin/lib/utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Output finalizer for user-context payloads.
|
|
5
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
6
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
7
|
+
*/
|
|
8
|
+
function finalizeUserContext(context) {
|
|
9
|
+
var _a;
|
|
10
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
11
|
+
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
12
|
+
if (!context.xUser ||
|
|
13
|
+
!mockEnabled ||
|
|
14
|
+
!Number.isInteger(mockType) ||
|
|
15
|
+
mockType < 0 ||
|
|
16
|
+
mockType > 4) {
|
|
17
|
+
return context;
|
|
18
|
+
}
|
|
19
|
+
const ensureSubscription = () => {
|
|
20
|
+
if (!context.xSubscription) {
|
|
21
|
+
const now = new Date();
|
|
22
|
+
context.xSubscription = {
|
|
23
|
+
id: BigInt(99999),
|
|
24
|
+
userId: context.xUser.userId,
|
|
25
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
26
|
+
orderId: '',
|
|
27
|
+
priceId: '',
|
|
28
|
+
priceName: 'MOCK-TEST',
|
|
29
|
+
status: 'active',
|
|
30
|
+
creditsAllocated: 0,
|
|
31
|
+
subPeriodStart: viewLocalTime(now),
|
|
32
|
+
subPeriodEnd: viewLocalTime(now),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return context.xSubscription;
|
|
36
|
+
};
|
|
37
|
+
switch (mockType) {
|
|
38
|
+
case 0: {
|
|
39
|
+
const subscription = ensureSubscription();
|
|
40
|
+
subscription.status = '';
|
|
41
|
+
subscription.priceId = '';
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case 1: {
|
|
45
|
+
const subscription = ensureSubscription();
|
|
46
|
+
subscription.priceId =
|
|
47
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 2: {
|
|
51
|
+
const subscription = ensureSubscription();
|
|
52
|
+
subscription.priceId =
|
|
53
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case 3: {
|
|
57
|
+
const subscription = ensureSubscription();
|
|
58
|
+
subscription.priceId =
|
|
59
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 4: {
|
|
63
|
+
const subscription = ensureSubscription();
|
|
64
|
+
subscription.priceId =
|
|
65
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return context;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { finalizeUserContext };
|
|
@@ -21,10 +21,4 @@ export declare function buildInitUserContextFromEntities(params: {
|
|
|
21
21
|
}): InitUserContext;
|
|
22
22
|
export declare function fetchUserContextByClerkUserId(clerkUserId: string): Promise<UserContextEntities | null>;
|
|
23
23
|
export declare function fetchLatestUserContextByFingerprintId(fingerprintId: string): Promise<FingerprintUserContext | null>;
|
|
24
|
-
type MockableContext = {
|
|
25
|
-
xUser: XUser | null;
|
|
26
|
-
xSubscription: XSubscription | null;
|
|
27
|
-
};
|
|
28
|
-
export declare function applyUserMockContext<T extends MockableContext>(context: T): T;
|
|
29
|
-
export {};
|
|
30
24
|
//# sourceMappingURL=user-context-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-context-service.d.ts","sourceRoot":"","sources":["../../../src/services/context/user-context-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAEhF,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAE5E,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,sBAAuB,SAAQ,mBAAmB;IACjE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,CAWhD;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAgB1D;AAED,wBAAgB,8BAA8B,CAC5C,YAAY,EAAE,YAAY,GAAG,IAAI,GAChC,aAAa,GAAG,IAAI,CAiBtB;AAED,wBAAgB,gCAAgC,CAAC,MAAM,EAAE;IACvD,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAClC,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,GAAG,eAAe,CAQlB;AAED,wBAAsB,6BAA6B,CACjD,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAarC;AAED,wBAAsB,qCAAqC,CACzD,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAmBxC
|
|
1
|
+
{"version":3,"file":"user-context-service.d.ts","sourceRoot":"","sources":["../../../src/services/context/user-context-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAEhF,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAE5E,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,sBAAuB,SAAQ,mBAAmB;IACjE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,CAWhD;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAgB1D;AAED,wBAAgB,8BAA8B,CAC5C,YAAY,EAAE,YAAY,GAAG,IAAI,GAChC,aAAa,GAAG,IAAI,CAiBtB;AAED,wBAAgB,gCAAgC,CAAC,MAAM,EAAE;IACvD,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;IAClC,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,GAAG,eAAe,CAQlB;AAED,wBAAsB,6BAA6B,CACjD,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAarC;AAED,wBAAsB,qCAAqC,CACzD,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAmBxC"}
|
|
@@ -97,71 +97,7 @@ function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
|
97
97
|
};
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
|
-
function applyUserMockContext(context) {
|
|
101
|
-
var _a;
|
|
102
|
-
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
103
|
-
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
104
|
-
if (!context.xUser ||
|
|
105
|
-
!mockEnabled ||
|
|
106
|
-
!Number.isInteger(mockType) ||
|
|
107
|
-
mockType < 0 ||
|
|
108
|
-
mockType > 4) {
|
|
109
|
-
return context;
|
|
110
|
-
}
|
|
111
|
-
const ensureSubscription = () => {
|
|
112
|
-
if (!context.xSubscription) {
|
|
113
|
-
const now = new Date();
|
|
114
|
-
context.xSubscription = {
|
|
115
|
-
id: BigInt(99999),
|
|
116
|
-
userId: context.xUser.userId,
|
|
117
|
-
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
118
|
-
orderId: '',
|
|
119
|
-
priceId: '',
|
|
120
|
-
priceName: 'MOCK-TEST',
|
|
121
|
-
status: 'active',
|
|
122
|
-
creditsAllocated: 0,
|
|
123
|
-
subPeriodStart: utils.viewLocalTime(now),
|
|
124
|
-
subPeriodEnd: utils.viewLocalTime(now),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
return context.xSubscription;
|
|
128
|
-
};
|
|
129
|
-
switch (mockType) {
|
|
130
|
-
case 0: {
|
|
131
|
-
const subscription = ensureSubscription();
|
|
132
|
-
subscription.status = '';
|
|
133
|
-
subscription.priceId = '';
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
case 1: {
|
|
137
|
-
const subscription = ensureSubscription();
|
|
138
|
-
subscription.priceId =
|
|
139
|
-
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
case 2: {
|
|
143
|
-
const subscription = ensureSubscription();
|
|
144
|
-
subscription.priceId =
|
|
145
|
-
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
case 3: {
|
|
149
|
-
const subscription = ensureSubscription();
|
|
150
|
-
subscription.priceId =
|
|
151
|
-
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
case 4: {
|
|
155
|
-
const subscription = ensureSubscription();
|
|
156
|
-
subscription.priceId =
|
|
157
|
-
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return context;
|
|
162
|
-
}
|
|
163
100
|
|
|
164
|
-
exports.applyUserMockContext = applyUserMockContext;
|
|
165
101
|
exports.buildInitUserContextFromEntities = buildInitUserContextFromEntities;
|
|
166
102
|
exports.fetchLatestUserContextByFingerprintId = fetchLatestUserContextByFingerprintId;
|
|
167
103
|
exports.fetchUserContextByClerkUserId = fetchUserContextByClerkUserId;
|
|
@@ -95,68 +95,5 @@ function fetchLatestUserContextByFingerprintId(fingerprintId) {
|
|
|
95
95
|
};
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
|
-
function applyUserMockContext(context) {
|
|
99
|
-
var _a;
|
|
100
|
-
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
101
|
-
const mockType = Number((_a = process.env.MONEY_PRICE_MOCK_USER_TYPE) !== null && _a !== void 0 ? _a : NaN);
|
|
102
|
-
if (!context.xUser ||
|
|
103
|
-
!mockEnabled ||
|
|
104
|
-
!Number.isInteger(mockType) ||
|
|
105
|
-
mockType < 0 ||
|
|
106
|
-
mockType > 4) {
|
|
107
|
-
return context;
|
|
108
|
-
}
|
|
109
|
-
const ensureSubscription = () => {
|
|
110
|
-
if (!context.xSubscription) {
|
|
111
|
-
const now = new Date();
|
|
112
|
-
context.xSubscription = {
|
|
113
|
-
id: BigInt(99999),
|
|
114
|
-
userId: context.xUser.userId,
|
|
115
|
-
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
116
|
-
orderId: '',
|
|
117
|
-
priceId: '',
|
|
118
|
-
priceName: 'MOCK-TEST',
|
|
119
|
-
status: 'active',
|
|
120
|
-
creditsAllocated: 0,
|
|
121
|
-
subPeriodStart: viewLocalTime(now),
|
|
122
|
-
subPeriodEnd: viewLocalTime(now),
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
return context.xSubscription;
|
|
126
|
-
};
|
|
127
|
-
switch (mockType) {
|
|
128
|
-
case 0: {
|
|
129
|
-
const subscription = ensureSubscription();
|
|
130
|
-
subscription.status = '';
|
|
131
|
-
subscription.priceId = '';
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
case 1: {
|
|
135
|
-
const subscription = ensureSubscription();
|
|
136
|
-
subscription.priceId =
|
|
137
|
-
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
case 2: {
|
|
141
|
-
const subscription = ensureSubscription();
|
|
142
|
-
subscription.priceId =
|
|
143
|
-
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
case 3: {
|
|
147
|
-
const subscription = ensureSubscription();
|
|
148
|
-
subscription.priceId =
|
|
149
|
-
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
case 4: {
|
|
153
|
-
const subscription = ensureSubscription();
|
|
154
|
-
subscription.priceId =
|
|
155
|
-
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
156
|
-
break;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return context;
|
|
160
|
-
}
|
|
161
98
|
|
|
162
|
-
export {
|
|
99
|
+
export { buildInitUserContextFromEntities, fetchLatestUserContextByFingerprintId, fetchUserContextByClerkUserId, mapCreditToXCredit, mapSubscriptionToXSubscription, mapUserToXUser };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@windrun-huaiin/backend-core",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.4.0",
|
|
4
4
|
"description": "Shared backend primitives: Prisma schema/client, database services, routing helpers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -47,6 +47,21 @@
|
|
|
47
47
|
"import": "./dist/lib/index.mjs",
|
|
48
48
|
"require": "./dist/lib/index.js"
|
|
49
49
|
},
|
|
50
|
+
"./auth/server": {
|
|
51
|
+
"types": "./dist/auth/auth-utils.d.ts",
|
|
52
|
+
"import": "./dist/auth/auth-utils.mjs",
|
|
53
|
+
"require": "./dist/auth/auth-utils.js"
|
|
54
|
+
},
|
|
55
|
+
"./auth/shared": {
|
|
56
|
+
"types": "./dist/auth/auth-shared.d.ts",
|
|
57
|
+
"import": "./dist/auth/auth-shared.mjs",
|
|
58
|
+
"require": "./dist/auth/auth-shared.js"
|
|
59
|
+
},
|
|
60
|
+
"./auth/middleware": {
|
|
61
|
+
"types": "./dist/auth/auth-middleware.d.ts",
|
|
62
|
+
"import": "./dist/auth/auth-middleware.mjs",
|
|
63
|
+
"require": "./dist/auth/auth-middleware.js"
|
|
64
|
+
},
|
|
50
65
|
"./app/api/webhook/stripe/route": {
|
|
51
66
|
"types": "./dist/app/api/webhook/stripe/route.d.ts",
|
|
52
67
|
"import": "./dist/app/api/webhook/stripe/route.mjs",
|
|
@@ -85,17 +100,17 @@
|
|
|
85
100
|
],
|
|
86
101
|
"dependencies": {
|
|
87
102
|
"@clerk/nextjs": "^7.0.5",
|
|
88
|
-
"@prisma/client": "^6.
|
|
103
|
+
"@prisma/client": "^6.19.0",
|
|
89
104
|
"@upstash/redis": "^1.34.0",
|
|
90
105
|
"@upstash/qstash": "^2.7.0",
|
|
91
106
|
"@upstash/lock": "^0.2.1",
|
|
92
107
|
"next": "16.1.6",
|
|
93
|
-
"prisma": "^6.
|
|
108
|
+
"prisma": "^6.19.0",
|
|
94
109
|
"stripe": "20.0.0",
|
|
95
110
|
"svix": "^1.86.0",
|
|
96
111
|
"zod": "^4.3.6",
|
|
97
|
-
"@windrun-huaiin/lib": "^14.0.
|
|
98
|
-
"@windrun-huaiin/third-ui": "^14.
|
|
112
|
+
"@windrun-huaiin/lib": "^14.0.1",
|
|
113
|
+
"@windrun-huaiin/third-ui": "^14.4.3"
|
|
99
114
|
},
|
|
100
115
|
"devDependencies": {
|
|
101
116
|
"@rollup/plugin-alias": "^5.1.1",
|
|
@@ -108,9 +123,9 @@
|
|
|
108
123
|
},
|
|
109
124
|
"peerDependencies": {
|
|
110
125
|
"@clerk/nextjs": "^7.0.5",
|
|
111
|
-
"@prisma/client": "^6.
|
|
126
|
+
"@prisma/client": "^6.19.0",
|
|
112
127
|
"next": "16.1.6",
|
|
113
|
-
"prisma": "^6.
|
|
128
|
+
"prisma": "^6.19.0",
|
|
114
129
|
"stripe": "20.0.0",
|
|
115
130
|
"svix": "^1.86.0"
|
|
116
131
|
},
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
PaySupplier,
|
|
13
13
|
PaymentStatus
|
|
14
14
|
} from '@/db/index';
|
|
15
|
-
import { ApiAuthUtils } from '@/
|
|
15
|
+
import { ApiAuthUtils } from '@/auth/auth-utils';
|
|
16
16
|
import { getPriceConfig } from '@/lib/money-price-config';
|
|
17
17
|
|
|
18
18
|
// Request validation schema
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
createCustomerPortalSession,
|
|
5
5
|
createOrGetCustomer,
|
|
6
6
|
} from '@/lib/stripe-config';
|
|
7
|
-
import { ApiAuthUtils } from '@/
|
|
7
|
+
import { ApiAuthUtils } from '@/auth/auth-utils';
|
|
8
8
|
import { subscriptionService } from '@/db/index';
|
|
9
9
|
|
|
10
10
|
const createCustomerPortalSchema = z
|
|
@@ -11,7 +11,6 @@ import { extractFingerprintFromNextRequest } from '@windrun-huaiin/third-ui/fing
|
|
|
11
11
|
import { auth } from '@clerk/nextjs/server';
|
|
12
12
|
import { NextRequest, NextResponse } from 'next/server';
|
|
13
13
|
import {
|
|
14
|
-
applyUserMockContext,
|
|
15
14
|
fetchLatestUserContextByFingerprintId,
|
|
16
15
|
fetchUserContextByClerkUserId,
|
|
17
16
|
mapCreditToXCredit,
|
|
@@ -19,6 +18,8 @@ import {
|
|
|
19
18
|
mapUserToXUser,
|
|
20
19
|
type UserContextEntities,
|
|
21
20
|
} from '@/context/user-context-service';
|
|
21
|
+
import { finalizeUserContext } from '@/context/user-context-finalizer';
|
|
22
|
+
|
|
22
23
|
import type { Prisma } from '@/db/prisma-model-type';
|
|
23
24
|
|
|
24
25
|
|
|
@@ -60,7 +61,7 @@ function createSuccessResponse(params: {
|
|
|
60
61
|
...params.options,
|
|
61
62
|
};
|
|
62
63
|
|
|
63
|
-
return
|
|
64
|
+
return finalizeUserContext(response);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
/** 创建错误响应 */
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { ClerkMiddlewareAuth } from '@clerk/nextjs/server';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
import { AUTH_HEADERS, type ProviderIdentity } from './auth-shared';
|
|
4
|
+
|
|
5
|
+
export function buildProtectedPageRoutePatterns(
|
|
6
|
+
protectedRoots: readonly string[],
|
|
7
|
+
locales: readonly string[]
|
|
8
|
+
): string[] {
|
|
9
|
+
const uniqueRoots = [...new Set(protectedRoots)]
|
|
10
|
+
.map((root) => root.trim())
|
|
11
|
+
.filter(Boolean)
|
|
12
|
+
.map((root) => (root.startsWith('/') ? root : `/${root}`))
|
|
13
|
+
.map((root) => root.replace(/\/+$/, ''));
|
|
14
|
+
|
|
15
|
+
const uniqueLocales = [...new Set(locales)]
|
|
16
|
+
.map((locale) => locale.trim())
|
|
17
|
+
.filter(Boolean);
|
|
18
|
+
|
|
19
|
+
const patterns = new Set<string>();
|
|
20
|
+
|
|
21
|
+
for (const root of uniqueRoots) {
|
|
22
|
+
patterns.add(`${root}(.*)`);
|
|
23
|
+
|
|
24
|
+
for (const locale of uniqueLocales) {
|
|
25
|
+
patterns.add(`/${locale}${root}(.*)`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [...patterns];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AuthMiddlewareOptions {
|
|
33
|
+
protectedPageRoutes: (req: NextRequest) => boolean;
|
|
34
|
+
protectedApiRoutes: (req: NextRequest) => boolean;
|
|
35
|
+
publicApiRoutes: (req: NextRequest) => boolean;
|
|
36
|
+
intlMiddleware: (req: NextRequest) => ReturnType<typeof NextResponse.next> | Response | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function authenticateWithClerk(auth: ClerkMiddlewareAuth): Promise<ProviderIdentity | null> {
|
|
40
|
+
const { userId } = await auth();
|
|
41
|
+
if (!userId) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
provider: 'clerk',
|
|
47
|
+
providerUserId: userId,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function buildAuthenticatedRequestHeaders(
|
|
52
|
+
req: NextRequest,
|
|
53
|
+
auth: ProviderIdentity
|
|
54
|
+
): Headers {
|
|
55
|
+
const headers = new Headers(req.headers);
|
|
56
|
+
headers.set(AUTH_HEADERS.provider, auth.provider);
|
|
57
|
+
headers.set(AUTH_HEADERS.providerUserId, auth.providerUserId);
|
|
58
|
+
return headers;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function handleAuthMiddleware(
|
|
62
|
+
auth: ClerkMiddlewareAuth,
|
|
63
|
+
req: NextRequest,
|
|
64
|
+
options: AuthMiddlewareOptions
|
|
65
|
+
) {
|
|
66
|
+
const { protectedPageRoutes, protectedApiRoutes, publicApiRoutes, intlMiddleware } = options;
|
|
67
|
+
|
|
68
|
+
if (protectedPageRoutes(req)) {
|
|
69
|
+
const identity = await authenticateWithClerk(auth);
|
|
70
|
+
if (!identity) {
|
|
71
|
+
return (await auth()).redirectToSignIn();
|
|
72
|
+
}
|
|
73
|
+
const requestHeaders = buildAuthenticatedRequestHeaders(req, identity);
|
|
74
|
+
console.log('Forward auth context for protected page:', identity.provider, identity.providerUserId);
|
|
75
|
+
return intlMiddleware(
|
|
76
|
+
new NextRequest(req.url, {
|
|
77
|
+
headers: requestHeaders,
|
|
78
|
+
method: req.method,
|
|
79
|
+
body: req.body,
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (protectedApiRoutes(req)) {
|
|
85
|
+
const identity = await authenticateWithClerk(auth);
|
|
86
|
+
if (!identity) {
|
|
87
|
+
return (await auth()).redirectToSignIn();
|
|
88
|
+
}
|
|
89
|
+
const requestHeaders = buildAuthenticatedRequestHeaders(req, identity);
|
|
90
|
+
console.log('Forward auth context for protected API:', identity.provider, identity.providerUserId);
|
|
91
|
+
return NextResponse.next({
|
|
92
|
+
request: {
|
|
93
|
+
headers: requestHeaders,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (publicApiRoutes(req)) {
|
|
99
|
+
console.log('Public API route, no auth required:', req.nextUrl.pathname);
|
|
100
|
+
return NextResponse.next();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (req.nextUrl.pathname.startsWith('/api/')) {
|
|
104
|
+
console.log('Other API route, no internationalization:', req.nextUrl.pathname);
|
|
105
|
+
return NextResponse.next();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return null;
|
|
109
|
+
}
|