@windrun-huaiin/backend-core 31.0.0 → 31.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.
Files changed (26) hide show
  1. package/dist/app/api/user/anonymous/init/fingerprint-only-route.d.ts +8 -0
  2. package/dist/app/api/user/anonymous/init/fingerprint-only-route.d.ts.map +1 -0
  3. package/dist/app/api/user/anonymous/init/fingerprint-only-route.js +20 -0
  4. package/dist/app/api/user/anonymous/init/fingerprint-only-route.mjs +18 -0
  5. package/dist/app/api/user/anonymous/init/route-shared.d.ts +10 -0
  6. package/dist/app/api/user/anonymous/init/route-shared.d.ts.map +1 -0
  7. package/dist/app/api/user/anonymous/init/route-shared.js +557 -0
  8. package/dist/app/api/user/anonymous/init/route-shared.mjs +555 -0
  9. package/dist/app/api/user/anonymous/init/route.d.ts +3 -3
  10. package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -1
  11. package/dist/app/api/user/anonymous/init/route.js +6 -553
  12. package/dist/app/api/user/anonymous/init/route.mjs +7 -554
  13. package/dist/app/api/user/credit-overview/route.d.ts +1 -0
  14. package/dist/app/api/user/credit-overview/route.d.ts.map +1 -1
  15. package/dist/app/api/user/credit-overview/route.js +8 -0
  16. package/dist/app/api/user/credit-overview/route.mjs +8 -0
  17. package/dist/lib/credit-overview.d.ts +1 -0
  18. package/dist/lib/credit-overview.d.ts.map +1 -1
  19. package/dist/lib/credit-overview.js +37 -21
  20. package/dist/lib/credit-overview.mjs +37 -21
  21. package/package.json +9 -4
  22. package/src/app/api/user/anonymous/init/fingerprint-only-route.ts +14 -0
  23. package/src/app/api/user/anonymous/init/route-shared.ts +710 -0
  24. package/src/app/api/user/anonymous/init/route.ts +7 -711
  25. package/src/app/api/user/credit-overview/route.ts +9 -0
  26. package/src/lib/credit-overview.ts +48 -18
@@ -20,12 +20,18 @@ export interface CreateGETOptions {
20
20
  pricingPath?: string;
21
21
  checkoutApiEndpoint?: string;
22
22
  customerPortalApiEndpoint?: string;
23
+ enabledBillingTypes?: string[];
23
24
  }
24
25
 
25
26
  export function createGET(options: CreateGETOptions) {
26
27
  return async function GET(request: Request) {
27
28
  const { searchParams } = new URL(request.url);
28
29
  const locale = searchParams.get('locale') || options.defaultLocale || 'en';
30
+ const requestEnabledBillingTypes = searchParams
31
+ .getAll('enabledBillingTypes')
32
+ .flatMap((value) => value.split(','))
33
+ .map((value) => value.trim())
34
+ .filter((value) => value.length > 0);
29
35
  const translations = await options.resolveTranslations(locale);
30
36
 
31
37
  const payload = await buildCreditOverviewPayload({
@@ -36,6 +42,9 @@ export function createGET(options: CreateGETOptions) {
36
42
  pricingPath: options.pricingPath,
37
43
  checkoutApiEndpoint: options.checkoutApiEndpoint,
38
44
  customerPortalApiEndpoint: options.customerPortalApiEndpoint,
45
+ enabledBillingTypes: requestEnabledBillingTypes.length
46
+ ? requestEnabledBillingTypes
47
+ : options.enabledBillingTypes,
39
48
  });
40
49
 
41
50
  return NextResponse.json(payload);
@@ -21,6 +21,7 @@ export interface BuildCreditOverviewPayloadOptions {
21
21
  pricingPath?: string;
22
22
  checkoutApiEndpoint?: string;
23
23
  customerPortalApiEndpoint?: string;
24
+ enabledBillingTypes?: string[];
24
25
  }
25
26
 
26
27
  export interface CreditOverviewPayload {
@@ -29,6 +30,21 @@ export interface CreditOverviewPayload {
29
30
  translations: CreditOverviewTranslations;
30
31
  }
31
32
 
33
+ const DEFAULT_ENABLED_BILLING_TYPES = ['monthly', 'yearly', 'onetime'];
34
+ const SUBSCRIPTION_BILLING_TYPES = new Set(['monthly', 'yearly']);
35
+
36
+ function normalizeEnabledBillingTypes(enabledBillingTypes: string[] | undefined) {
37
+ const normalized = Array.from(
38
+ new Set(
39
+ (enabledBillingTypes?.length ? enabledBillingTypes : DEFAULT_ENABLED_BILLING_TYPES)
40
+ .map((billingType) => billingType.trim())
41
+ .filter((billingType) => billingType.length > 0),
42
+ ),
43
+ );
44
+
45
+ return normalized.length ? normalized : DEFAULT_ENABLED_BILLING_TYPES;
46
+ }
47
+
32
48
  export async function buildCreditOverviewPayload(
33
49
  options: BuildCreditOverviewPayloadOptions,
34
50
  ): Promise<CreditOverviewPayload | null> {
@@ -46,10 +62,16 @@ export async function buildCreditOverviewPayload(
46
62
  pricingPath = '/pricing',
47
63
  checkoutApiEndpoint = '/api/stripe/checkout',
48
64
  customerPortalApiEndpoint = '/api/stripe/customer-portal',
65
+ enabledBillingTypes: enabledBillingTypesOption,
49
66
  } = options;
50
67
 
51
68
  const { user } = authUser;
52
69
  const enableSubscriptionUpgrade = process.env.ENABLE_STRIPE_SUBSCRIPTION_UPGRADE !== 'false';
70
+ const enabledBillingTypes = normalizeEnabledBillingTypes(enabledBillingTypesOption);
71
+ const subscriptionBillingEnabled = enabledBillingTypes.some((billingType) =>
72
+ SUBSCRIPTION_BILLING_TYPES.has(billingType),
73
+ );
74
+ const onetimeBillingEnabled = enabledBillingTypes.includes('onetime');
53
75
 
54
76
  const [credit, subscription, moneyPriceData] = await Promise.all([
55
77
  creditService.getCredit(user.userId),
@@ -57,7 +79,7 @@ export async function buildCreditOverviewPayload(
57
79
  buildMoneyPriceData({
58
80
  locale,
59
81
  currency: moneyPriceConfig.display.currency,
60
- enabledBillingTypes: ['monthly', 'yearly', 'onetime'],
82
+ enabledBillingTypes,
61
83
  }),
62
84
  ]);
63
85
 
@@ -73,11 +95,11 @@ export async function buildCreditOverviewPayload(
73
95
 
74
96
  const totalBalance =
75
97
  (credit.balanceFree ?? 0) +
76
- (credit.balancePaid ?? 0) +
77
- (credit.balanceOneTimePaid ?? 0);
98
+ (subscriptionBillingEnabled ? credit.balancePaid ?? 0 : 0) +
99
+ (onetimeBillingEnabled ? credit.balanceOneTimePaid ?? 0 : 0);
78
100
 
79
101
  const buckets = [
80
- ...(credit.balancePaid > 0
102
+ ...(subscriptionBillingEnabled && credit.balancePaid > 0
81
103
  ? [{
82
104
  kind: 'subscription' as const,
83
105
  balance: credit.balancePaid,
@@ -85,7 +107,7 @@ export async function buildCreditOverviewPayload(
85
107
  expiresAt: viewLocalTime(credit.paidEnd),
86
108
  }]
87
109
  : []),
88
- ...(credit.balanceOneTimePaid > 0
110
+ ...(onetimeBillingEnabled && credit.balanceOneTimePaid > 0
89
111
  ? [{
90
112
  kind: 'onetime' as const,
91
113
  balance: credit.balanceOneTimePaid,
@@ -122,22 +144,30 @@ export async function buildCreditOverviewPayload(
122
144
  initUserContext,
123
145
  },
124
146
  ctaBehaviors: {
125
- subscribe: {
126
- desktop: { kind: 'modal', mode: 'subscription' },
127
- mobile: { kind: 'redirect', url: `${pricingPageBaseUrl}?initialBillingType=subscription` },
128
- },
129
- manage: {
130
- desktop: { kind: 'auth' },
131
- mobile: { kind: 'auth' },
132
- },
133
- onetime: {
134
- desktop: { kind: 'modal', mode: 'onetime' },
135
- mobile: { kind: 'redirect', url: `${pricingPageBaseUrl}?initialBillingType=onetime` },
136
- },
147
+ ...(subscriptionBillingEnabled
148
+ ? {
149
+ subscribe: {
150
+ desktop: { kind: 'modal', mode: 'subscription' },
151
+ mobile: { kind: 'redirect', url: `${pricingPageBaseUrl}?initialBillingType=subscription` },
152
+ },
153
+ manage: {
154
+ desktop: { kind: 'auth' },
155
+ mobile: { kind: 'auth' },
156
+ },
157
+ }
158
+ : {}),
159
+ ...(onetimeBillingEnabled
160
+ ? {
161
+ onetime: {
162
+ desktop: { kind: 'modal', mode: 'onetime' },
163
+ mobile: { kind: 'redirect', url: `${pricingPageBaseUrl}?initialBillingType=onetime` },
164
+ },
165
+ }
166
+ : {}),
137
167
  },
138
168
  };
139
169
 
140
- if (subscription) {
170
+ if (subscriptionBillingEnabled && subscription) {
141
171
  data.subscription = {
142
172
  planName: subscription.priceName ?? '',
143
173
  periodStart: viewLocalTime(subscription.subPeriodStart),