@foldspace-fe/casdoor-next-auth-kit 0.1.14 → 0.1.16
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/billing/index.d.ts +18 -3
- package/dist/billing/index.js +22 -2
- package/dist/{callback-BTzHQK_r.d.ts → callback-rEWxVGyL.d.ts} +1 -1
- package/dist/casdoor/index.d.ts +2 -2
- package/dist/chunk-46V73LSW.js +494 -0
- package/dist/chunk-46V73LSW.js.map +1 -0
- package/dist/{chunk-CULH4UKG.js → chunk-FW4WDHNS.js} +15 -1
- package/dist/chunk-FW4WDHNS.js.map +1 -0
- package/dist/{chunk-QYGC4WNG.js → chunk-GLK4IW22.js} +37 -33
- package/dist/chunk-GLK4IW22.js.map +1 -0
- package/dist/{chunk-5ISF7ZAG.js → chunk-ZCHLJYLN.js} +14 -43
- package/dist/chunk-ZCHLJYLN.js.map +1 -0
- package/dist/cli.js +110 -14
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +24 -4
- package/dist/next/index.d.ts +3 -3
- package/dist/next/index.js +1 -1
- package/dist/{options-JUwZSXu2.d.ts → options-D2YQdRWu.d.ts} +1 -1
- package/dist/react/index.d.ts +26 -6
- package/dist/react/index.js +285 -12
- package/dist/react/index.js.map +1 -1
- package/dist/skills/casdoor-next-auth-kit/SKILL.md +22 -6
- package/dist/{types-DqVXdUge.d.ts → types-BJv6j3NZ.d.ts} +1 -0
- package/dist/types-xgHVGy75.d.ts +895 -0
- package/package.json +4 -3
- package/dist/chunk-5ISF7ZAG.js.map +0 -1
- package/dist/chunk-CULH4UKG.js.map +0 -1
- package/dist/chunk-O3FKI5NT.js +0 -187
- package/dist/chunk-O3FKI5NT.js.map +0 -1
- package/dist/chunk-QYGC4WNG.js.map +0 -1
- package/dist/types-DwThfdu-.d.ts +0 -362
package/dist/billing/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { B as BillingActionPayload, b as BillingRuntimeConfig, m as BillingCreditsState, j as BillingProductState, h as BillingSubscriptionState, n as BillingEntitlementState, c as BillingCatalogConfig, p as BillingPurchaseStatus, k as BillingOrderHistoryItem, l as BillingPaymentHistoryItem,
|
|
2
|
-
export { f as BillingActionExecutor,
|
|
1
|
+
import { B as BillingActionPayload, b as BillingRuntimeConfig, Y as BillingPaymentCallbackContext, af as BillingPurchaseRequest, m as BillingCreditsState, j as BillingProductState, h as BillingSubscriptionState, n as BillingEntitlementState, a as BillingItem, c as BillingCatalogConfig, p as BillingPurchaseStatus, k as BillingOrderHistoryItem, l as BillingPaymentHistoryItem, V as BillingInterval, r as BillingProductSnapshot, a9 as BillingPurchasableEntry, H as BillingCasdoorBuyProductRequest, s as BillingCasdoorProductDetail, I as BillingCasdoorBuyProductResponse, u as BillingActionExecutionResult, a4 as BillingPaymentSuccessRouteOptions, $ as BillingPaymentFinishedRouteOptions } from '../types-xgHVGy75.js';
|
|
2
|
+
export { f as BillingActionExecutor, v as BillingActionKind, d as BillingApiClient, w as BillingCasdoorAccountDetail, x as BillingCasdoorAccountMultiFactorAuthDetail, y as BillingCasdoorAccountResponse, z as BillingCasdoorApiResponse, A as BillingCasdoorApplicationDetail, C as BillingCasdoorApplicationProviderDetail, D as BillingCasdoorApplicationResponse, E as BillingCasdoorApplicationSigninItemDetail, F as BillingCasdoorApplicationSigninMethodDetail, G as BillingCasdoorApplicationSignupItemDetail, J as BillingCasdoorErrorDetail, K as BillingCasdoorErrorPayload, L as BillingCasdoorOrganizationDetail, M as BillingCasdoorOrganizationNamesResponse, N as BillingCasdoorOrganizationOption, O as BillingCasdoorPaymentDetail, P as BillingCasdoorPaymentResponse, Q as BillingCasdoorProductResponse, R as BillingCasdoorProviderDetail, S as BillingCasdoorProviderOption, T as BillingConversionRule, t as BillingCoreContextValue, U as BillingCreditsContextValue, g as BillingDefaults, W as BillingItemKind, e as BillingLoaders, X as BillingOrderCreatedContext, Z as BillingPaymentFinishedContext, _ as BillingPaymentFinishedHandler, a0 as BillingPaymentRouteBaseOptions, a1 as BillingPaymentSuccessContext, a2 as BillingPaymentSuccessHandler, a3 as BillingPaymentSuccessHandlerResult, a5 as BillingProductContextValue, a6 as BillingProductDetailState, a7 as BillingProductPurchasableEntry, a8 as BillingProductPurchaseConfig, aa as BillingPurchasableEntryBase, ab as BillingPurchasableKind, ac as BillingPurchasableWhitelist, ad as BillingPurchaseCompleteContext, ae as BillingPurchaseErrorContext, q as BillingPurchaseHooks, o as BillingStatusState, ag as BillingSubscriptionContextValue, i as BillingSubscriptionHistoryItem, ah as BillingSubscriptionPurchasableEntry, ai as BillingSubscriptionPurchaseConfig } from '../types-xgHVGy75.js';
|
|
3
3
|
|
|
4
4
|
declare function normalizeBillingRuntimeConfig(config?: Partial<BillingRuntimeConfig> | null): BillingRuntimeConfig;
|
|
5
5
|
declare function normalizeBillingCatalogConfig(config?: Partial<BillingCatalogConfig> | null): BillingCatalogConfig;
|
|
6
6
|
declare function resolveBillingItem(items: BillingItem[] | undefined, key?: string | null): BillingItem | undefined;
|
|
7
|
+
declare function resolveBillingPurchasable(runtimeConfig: BillingRuntimeConfig | undefined, key?: string | null): BillingPurchasableEntry | undefined;
|
|
8
|
+
declare function buildBillingPurchaseRequest(payload: BillingActionPayload, runtimeConfig?: BillingRuntimeConfig | null): BillingPurchaseRequest | null;
|
|
9
|
+
declare function filterBillingPurchasableItems(items: BillingItem[] | undefined, runtimeConfig?: BillingRuntimeConfig | null): BillingItem[];
|
|
10
|
+
declare function buildBillingPaymentCallbackContext(request: Request, phase?: 'success' | 'failure' | 'finished'): Promise<BillingPaymentCallbackContext>;
|
|
7
11
|
declare function resolveBillingSubscriptionProduct(subscription: BillingSubscriptionState | undefined, runtimeConfig: BillingRuntimeConfig | undefined): BillingProductSnapshot | undefined;
|
|
8
12
|
declare function resolveBillingProductSnapshot(item?: BillingItem | null): BillingProductSnapshot | undefined;
|
|
9
13
|
declare function deriveBillingCreditsState(credits?: BillingCreditsState | null, products?: BillingProductState[] | null, conversionRules?: BillingRuntimeConfig['conversionRules']): BillingCreditsState;
|
|
@@ -12,10 +16,21 @@ declare function normalizeBillingPurchaseStatus(status?: Partial<BillingPurchase
|
|
|
12
16
|
declare function resolveBillingInterval(interval?: BillingInterval | null): BillingInterval | undefined;
|
|
13
17
|
declare function buildBillingActionPayload(payload: BillingActionPayload, runtimeConfig?: BillingRuntimeConfig | null): BillingActionPayload;
|
|
14
18
|
|
|
19
|
+
interface NormalizedCasdoorProductId {
|
|
20
|
+
owner: string;
|
|
21
|
+
name: string;
|
|
22
|
+
}
|
|
23
|
+
declare function readBuyProductRedirectTo(value: unknown): string | undefined;
|
|
24
|
+
declare function normalizeCasdoorProductId(id: string): NormalizedCasdoorProductId;
|
|
25
|
+
declare function chooseCasdoorProviderName(product: Pick<BillingCasdoorProductDetail, 'providers' | 'providerObjs'>, preferredProviderName?: string): string;
|
|
26
|
+
declare function buildCasdoorBuyProductParams(input: BillingCasdoorBuyProductRequest): URLSearchParams;
|
|
27
|
+
declare function buildCasdoorBuyProductRequest(purchase: BillingPurchaseRequest, product: BillingCasdoorProductDetail, preferredProviderName?: string): BillingCasdoorBuyProductRequest;
|
|
28
|
+
declare function normalizeCasdoorBuyProductResponse(response: BillingCasdoorBuyProductResponse, fallbackRedirectTo?: string): BillingActionExecutionResult;
|
|
29
|
+
|
|
15
30
|
declare function createBillingPaymentSuccessResponse(request: Request, options?: BillingPaymentSuccessRouteOptions): Promise<Response>;
|
|
16
31
|
declare function createBillingPaymentSuccessRouteHandler(options?: BillingPaymentSuccessRouteOptions): (request: Request) => Promise<Response>;
|
|
17
32
|
|
|
18
33
|
declare function createBillingPaymentFinishedResponse(request: Request, options?: BillingPaymentFinishedRouteOptions): Promise<Response>;
|
|
19
34
|
declare function createBillingPaymentFinishedRouteHandler(options?: BillingPaymentFinishedRouteOptions): (request: Request) => Promise<Response>;
|
|
20
35
|
|
|
21
|
-
export { BillingActionPayload, BillingCatalogConfig, BillingCreditsState, BillingEntitlementState, BillingInterval, BillingItem, BillingOrderHistoryItem, BillingPaymentFinishedRouteOptions, BillingPaymentHistoryItem, BillingPaymentSuccessRouteOptions, BillingProductSnapshot, BillingProductState, BillingPurchaseStatus, BillingRuntimeConfig, BillingSubscriptionState, buildBillingActionPayload, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingSubscriptionProduct };
|
|
36
|
+
export { BillingActionExecutionResult, BillingActionPayload, BillingCasdoorBuyProductRequest, BillingCasdoorBuyProductResponse, BillingCasdoorProductDetail, BillingCatalogConfig, BillingCreditsState, BillingEntitlementState, BillingInterval, BillingItem, BillingOrderHistoryItem, BillingPaymentCallbackContext, BillingPaymentFinishedRouteOptions, BillingPaymentHistoryItem, BillingPaymentSuccessRouteOptions, BillingProductSnapshot, BillingProductState, BillingPurchasableEntry, BillingPurchaseRequest, BillingPurchaseStatus, BillingRuntimeConfig, BillingSubscriptionState, type NormalizedCasdoorProductId, buildBillingActionPayload, buildBillingPaymentCallbackContext, buildBillingPurchaseRequest, buildCasdoorBuyProductParams, buildCasdoorBuyProductRequest, chooseCasdoorProviderName, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, filterBillingPurchasableItems, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, normalizeCasdoorBuyProductResponse, normalizeCasdoorProductId, readBuyProductRedirectTo, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingPurchasable, resolveBillingSubscriptionProduct };
|
package/dist/billing/index.js
CHANGED
|
@@ -3,33 +3,53 @@ import {
|
|
|
3
3
|
createBillingPaymentFinishedRouteHandler,
|
|
4
4
|
createBillingPaymentSuccessResponse,
|
|
5
5
|
createBillingPaymentSuccessRouteHandler
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-ZCHLJYLN.js";
|
|
7
7
|
import {
|
|
8
8
|
buildBillingActionPayload,
|
|
9
|
+
buildBillingPaymentCallbackContext,
|
|
10
|
+
buildBillingPurchaseRequest,
|
|
11
|
+
buildCasdoorBuyProductParams,
|
|
12
|
+
buildCasdoorBuyProductRequest,
|
|
13
|
+
chooseCasdoorProviderName,
|
|
9
14
|
deriveBillingCreditsState,
|
|
10
15
|
deriveBillingEntitlements,
|
|
16
|
+
filterBillingPurchasableItems,
|
|
11
17
|
normalizeBillingCatalogConfig,
|
|
12
18
|
normalizeBillingPurchaseStatus,
|
|
13
19
|
normalizeBillingRuntimeConfig,
|
|
20
|
+
normalizeCasdoorBuyProductResponse,
|
|
21
|
+
normalizeCasdoorProductId,
|
|
22
|
+
readBuyProductRedirectTo,
|
|
14
23
|
resolveBillingInterval,
|
|
15
24
|
resolveBillingItem,
|
|
16
25
|
resolveBillingProductSnapshot,
|
|
26
|
+
resolveBillingPurchasable,
|
|
17
27
|
resolveBillingSubscriptionProduct
|
|
18
|
-
} from "../chunk-
|
|
28
|
+
} from "../chunk-46V73LSW.js";
|
|
19
29
|
export {
|
|
20
30
|
buildBillingActionPayload,
|
|
31
|
+
buildBillingPaymentCallbackContext,
|
|
32
|
+
buildBillingPurchaseRequest,
|
|
33
|
+
buildCasdoorBuyProductParams,
|
|
34
|
+
buildCasdoorBuyProductRequest,
|
|
35
|
+
chooseCasdoorProviderName,
|
|
21
36
|
createBillingPaymentFinishedResponse,
|
|
22
37
|
createBillingPaymentFinishedRouteHandler,
|
|
23
38
|
createBillingPaymentSuccessResponse,
|
|
24
39
|
createBillingPaymentSuccessRouteHandler,
|
|
25
40
|
deriveBillingCreditsState,
|
|
26
41
|
deriveBillingEntitlements,
|
|
42
|
+
filterBillingPurchasableItems,
|
|
27
43
|
normalizeBillingCatalogConfig,
|
|
28
44
|
normalizeBillingPurchaseStatus,
|
|
29
45
|
normalizeBillingRuntimeConfig,
|
|
46
|
+
normalizeCasdoorBuyProductResponse,
|
|
47
|
+
normalizeCasdoorProductId,
|
|
48
|
+
readBuyProductRedirectTo,
|
|
30
49
|
resolveBillingInterval,
|
|
31
50
|
resolveBillingItem,
|
|
32
51
|
resolveBillingProductSnapshot,
|
|
52
|
+
resolveBillingPurchasable,
|
|
33
53
|
resolveBillingSubscriptionProduct
|
|
34
54
|
};
|
|
35
55
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { A as AuthKitConfig, a as AuthBusinessAdapter, b as AuthPersistenceAdapter } from './types-
|
|
2
|
+
import { A as AuthKitConfig, a as AuthBusinessAdapter, b as AuthPersistenceAdapter } from './types-BJv6j3NZ.js';
|
|
3
3
|
|
|
4
4
|
interface CallbackHandlerOptions {
|
|
5
5
|
config: AuthKitConfig;
|
package/dist/casdoor/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as AuthKitConfig, O as OAuthTokens, C as CasdoorUserInfo } from '../types-
|
|
1
|
+
import { A as AuthKitConfig, O as OAuthTokens, C as CasdoorUserInfo } from '../types-BJv6j3NZ.js';
|
|
2
2
|
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
-
export { C as CallbackHandlerOptions, c as createCallbackHandler, a as createCallbackResponse } from '../callback-
|
|
3
|
+
export { C as CallbackHandlerOptions, c as createCallbackHandler, a as createCallbackResponse } from '../callback-rEWxVGyL.js';
|
|
4
4
|
|
|
5
5
|
declare function getCasdoorConfig(config: AuthKitConfig): AuthKitConfig;
|
|
6
6
|
declare function getCasdoorAuthorizeUrl(config: AuthKitConfig, params: {
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
// src/billing/runtime.ts
|
|
2
|
+
function normalizeCasdoorProductId(id) {
|
|
3
|
+
const [owner, ...rest] = id.split("/");
|
|
4
|
+
const name = rest.join("/");
|
|
5
|
+
if (!owner || !name) {
|
|
6
|
+
throw new Error(`Invalid Casdoor product id: ${id}`);
|
|
7
|
+
}
|
|
8
|
+
return { owner, name };
|
|
9
|
+
}
|
|
10
|
+
function normalizeBillingRuntimeConfig(config) {
|
|
11
|
+
return {
|
|
12
|
+
catalogKey: config?.catalogKey ?? "default",
|
|
13
|
+
items: config?.items ?? [],
|
|
14
|
+
purchasableIds: config?.purchasableIds ?? [],
|
|
15
|
+
purchasables: config?.purchasables ?? [],
|
|
16
|
+
conversionRules: config?.conversionRules ?? [],
|
|
17
|
+
defaults: config?.defaults ?? {}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function normalizeBillingCatalogConfig(config) {
|
|
21
|
+
return {
|
|
22
|
+
...normalizeBillingRuntimeConfig(config),
|
|
23
|
+
title: config?.title,
|
|
24
|
+
description: config?.description,
|
|
25
|
+
portalPath: config?.portalPath,
|
|
26
|
+
successPath: config?.successPath,
|
|
27
|
+
cancelPath: config?.cancelPath
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function resolveBillingItem(items, key) {
|
|
31
|
+
if (!key) return void 0;
|
|
32
|
+
return items?.find((item) => item.key === key || item.backendRef.productId === key || item.backendRef.planId === key);
|
|
33
|
+
}
|
|
34
|
+
function isBillingPurchasableIdMatch(id, item) {
|
|
35
|
+
return id === item.key || id === item.backendRef.productId || "planId" in item.backendRef && id === item.backendRef.planId;
|
|
36
|
+
}
|
|
37
|
+
function resolveBillingPurchasable(runtimeConfig, key) {
|
|
38
|
+
if (!runtimeConfig || !key) {
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
const explicit = runtimeConfig.purchasables?.find(
|
|
42
|
+
(item2) => item2.key === key || item2.backendRef.productId === key || "planId" in item2.backendRef && item2.backendRef.planId === key
|
|
43
|
+
);
|
|
44
|
+
if (explicit) {
|
|
45
|
+
return explicit;
|
|
46
|
+
}
|
|
47
|
+
if (runtimeConfig.purchasableIds?.length) {
|
|
48
|
+
const matchingItem = resolveBillingItem(runtimeConfig.items, key);
|
|
49
|
+
if (!matchingItem) {
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
const allowed = runtimeConfig.purchasableIds.some((itemKey) => isBillingPurchasableIdMatch(itemKey, matchingItem));
|
|
53
|
+
if (!allowed) {
|
|
54
|
+
return void 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const item = resolveBillingItem(runtimeConfig.items, key);
|
|
58
|
+
if (!item) {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
if (item.kind === "subscription") {
|
|
62
|
+
return {
|
|
63
|
+
key: item.key,
|
|
64
|
+
kind: "subscription",
|
|
65
|
+
title: item.title,
|
|
66
|
+
description: item.description,
|
|
67
|
+
enabled: true,
|
|
68
|
+
backendRef: {
|
|
69
|
+
productId: item.backendRef.productId,
|
|
70
|
+
planId: item.backendRef.planId,
|
|
71
|
+
priceId: item.backendRef.priceId
|
|
72
|
+
},
|
|
73
|
+
interval: item.interval,
|
|
74
|
+
hooks: void 0
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
key: item.key,
|
|
79
|
+
kind: "product",
|
|
80
|
+
title: item.title,
|
|
81
|
+
description: item.description,
|
|
82
|
+
enabled: true,
|
|
83
|
+
backendRef: {
|
|
84
|
+
productId: item.backendRef.productId,
|
|
85
|
+
priceId: item.backendRef.priceId
|
|
86
|
+
},
|
|
87
|
+
quantity: void 0,
|
|
88
|
+
creditGrant: item.creditGrant,
|
|
89
|
+
creditRedeem: item.creditRedeem,
|
|
90
|
+
hooks: void 0
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function buildBillingPurchaseRequest(payload, runtimeConfig) {
|
|
94
|
+
const config = normalizeBillingRuntimeConfig(runtimeConfig);
|
|
95
|
+
const purchasable = resolveBillingPurchasable(config, payload.key) ?? resolveBillingPurchasable(config, payload.productId);
|
|
96
|
+
if (!purchasable) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
let productOwner;
|
|
100
|
+
let productName;
|
|
101
|
+
try {
|
|
102
|
+
const normalized = normalizeCasdoorProductId(purchasable.backendRef.productId);
|
|
103
|
+
productOwner = normalized.owner;
|
|
104
|
+
productName = normalized.name;
|
|
105
|
+
} catch {
|
|
106
|
+
productOwner = void 0;
|
|
107
|
+
productName = void 0;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
kind: purchasable.kind,
|
|
111
|
+
key: purchasable.key,
|
|
112
|
+
productId: purchasable.backendRef.productId,
|
|
113
|
+
productOwner,
|
|
114
|
+
productName,
|
|
115
|
+
providerName: payload.providerName,
|
|
116
|
+
pricingName: payload.pricingName,
|
|
117
|
+
planName: payload.planName,
|
|
118
|
+
userName: payload.userName,
|
|
119
|
+
paymentEnv: payload.paymentEnv,
|
|
120
|
+
customPrice: payload.customPrice,
|
|
121
|
+
quantity: payload.quantity ?? 1,
|
|
122
|
+
returnTo: payload.returnTo,
|
|
123
|
+
metadata: payload.metadata
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function filterBillingPurchasableItems(items, runtimeConfig) {
|
|
127
|
+
if (!runtimeConfig) {
|
|
128
|
+
return items ?? [];
|
|
129
|
+
}
|
|
130
|
+
const config = normalizeBillingRuntimeConfig(runtimeConfig);
|
|
131
|
+
if (!config.purchasableIds?.length && !config.purchasables?.length) {
|
|
132
|
+
return items ?? [];
|
|
133
|
+
}
|
|
134
|
+
return (items ?? []).filter((item) => {
|
|
135
|
+
const purchasable = resolveBillingPurchasable(config, item.key);
|
|
136
|
+
return Boolean(purchasable);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async function readRequestBody(request) {
|
|
140
|
+
if (request.method === "GET" || request.method === "HEAD") {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const rawBody = await request.clone().text();
|
|
144
|
+
if (!rawBody) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
148
|
+
if (contentType.includes("application/json")) {
|
|
149
|
+
try {
|
|
150
|
+
return JSON.parse(rawBody);
|
|
151
|
+
} catch {
|
|
152
|
+
return rawBody;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
156
|
+
return Object.fromEntries(new URLSearchParams(rawBody).entries());
|
|
157
|
+
}
|
|
158
|
+
return rawBody;
|
|
159
|
+
}
|
|
160
|
+
async function buildBillingPaymentCallbackContext(request, phase) {
|
|
161
|
+
const url = new URL(request.url);
|
|
162
|
+
const params = {};
|
|
163
|
+
for (const [key, value] of url.searchParams.entries()) {
|
|
164
|
+
params[key] = value;
|
|
165
|
+
}
|
|
166
|
+
const paymentOwner = url.searchParams.get("paymentOwner") ?? url.searchParams.get("owner");
|
|
167
|
+
const paymentName = url.searchParams.get("paymentName") ?? url.searchParams.get("name");
|
|
168
|
+
const paymentId = url.searchParams.get("paymentId");
|
|
169
|
+
const orderId = url.searchParams.get("orderId");
|
|
170
|
+
const redirectTo = url.searchParams.get("redirect") ?? url.searchParams.get("returnTo");
|
|
171
|
+
const failureSignal = url.searchParams.get("error") || url.searchParams.get("status") === "failed";
|
|
172
|
+
const status = failureSignal ? "failure" : phase === "finished" ? "finished" : phase === "failure" ? "failure" : "success";
|
|
173
|
+
return {
|
|
174
|
+
request,
|
|
175
|
+
url,
|
|
176
|
+
searchParams: url.searchParams,
|
|
177
|
+
params,
|
|
178
|
+
paymentOwner,
|
|
179
|
+
paymentName,
|
|
180
|
+
paymentId,
|
|
181
|
+
orderId,
|
|
182
|
+
redirectTo,
|
|
183
|
+
status,
|
|
184
|
+
body: await readRequestBody(request)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function resolveBillingSubscriptionProduct(subscription, runtimeConfig) {
|
|
188
|
+
if (!subscription) return void 0;
|
|
189
|
+
if (subscription.product) return subscription.product;
|
|
190
|
+
const item = resolveBillingItem(runtimeConfig?.items, subscription.planKey ?? subscription.subscriptionId);
|
|
191
|
+
if (!item) return void 0;
|
|
192
|
+
return {
|
|
193
|
+
productKey: item.key,
|
|
194
|
+
productId: item.backendRef.productId,
|
|
195
|
+
title: item.title,
|
|
196
|
+
kind: item.kind,
|
|
197
|
+
planId: item.backendRef.planId,
|
|
198
|
+
priceId: item.backendRef.priceId,
|
|
199
|
+
interval: item.interval,
|
|
200
|
+
creditGrant: item.creditGrant,
|
|
201
|
+
creditRedeem: item.creditRedeem,
|
|
202
|
+
metadata: item.metadata
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function resolveBillingProductSnapshot(item) {
|
|
206
|
+
if (!item) return void 0;
|
|
207
|
+
return {
|
|
208
|
+
productKey: item.key,
|
|
209
|
+
productId: item.backendRef.productId,
|
|
210
|
+
title: item.title,
|
|
211
|
+
kind: item.kind,
|
|
212
|
+
planId: item.backendRef.planId,
|
|
213
|
+
priceId: item.backendRef.priceId,
|
|
214
|
+
interval: item.interval,
|
|
215
|
+
creditGrant: item.creditGrant,
|
|
216
|
+
creditRedeem: item.creditRedeem,
|
|
217
|
+
metadata: item.metadata
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function deriveBillingCreditsState(credits, products, conversionRules) {
|
|
221
|
+
if (credits) return credits;
|
|
222
|
+
const fromProducts = products?.reduce((total, product) => {
|
|
223
|
+
if (typeof product.creditsBalance === "number") {
|
|
224
|
+
return total + Number(product.creditsBalance);
|
|
225
|
+
}
|
|
226
|
+
if (!product.creditGrant) {
|
|
227
|
+
return total;
|
|
228
|
+
}
|
|
229
|
+
const quantity = Number(product.quantity ?? 1);
|
|
230
|
+
return total + Number(product.creditGrant.creditsPerUnit || 0) * quantity;
|
|
231
|
+
}, 0);
|
|
232
|
+
const fromRules = conversionRules?.reduce((total, rule) => {
|
|
233
|
+
if (rule.kind !== "grant-credits") return total;
|
|
234
|
+
return total + Number(rule.creditsPerUnit || 0);
|
|
235
|
+
}, 0);
|
|
236
|
+
return {
|
|
237
|
+
balance: Number(fromProducts ?? fromRules ?? 0)
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function deriveBillingEntitlements(subscription, products, credits, runtimeConfig) {
|
|
241
|
+
const features = /* @__PURE__ */ new Set();
|
|
242
|
+
const limits = {};
|
|
243
|
+
const flags = {};
|
|
244
|
+
const creditBalance = credits?.balance ?? 0;
|
|
245
|
+
for (const item of runtimeConfig?.items ?? []) {
|
|
246
|
+
for (const feature of item.features ?? []) {
|
|
247
|
+
features.add(feature);
|
|
248
|
+
}
|
|
249
|
+
if (item.metadata?.tier) {
|
|
250
|
+
flags[item.metadata.tier] = true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (subscription?.status === "active" || subscription?.status === "trialing") {
|
|
254
|
+
flags.subscribed = true;
|
|
255
|
+
}
|
|
256
|
+
if (creditBalance > 0) {
|
|
257
|
+
flags.hasCredits = true;
|
|
258
|
+
limits.credits = creditBalance;
|
|
259
|
+
}
|
|
260
|
+
for (const product of products ?? []) {
|
|
261
|
+
if (product.owned) {
|
|
262
|
+
flags[`product:${product.productKey}`] = true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
features: [...features],
|
|
267
|
+
limits,
|
|
268
|
+
flags
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function normalizeBillingPurchaseStatus(status, order, payment) {
|
|
272
|
+
if (status) {
|
|
273
|
+
return {
|
|
274
|
+
actionKey: status.actionKey,
|
|
275
|
+
orderId: status.orderId,
|
|
276
|
+
paymentId: status.paymentId,
|
|
277
|
+
transactionId: status.transactionId,
|
|
278
|
+
status: status.status ?? "idle",
|
|
279
|
+
orderStatus: status.orderStatus,
|
|
280
|
+
paymentStatus: status.paymentStatus,
|
|
281
|
+
transactionStatus: status.transactionStatus,
|
|
282
|
+
redirectTo: status.redirectTo,
|
|
283
|
+
updatedAt: status.updatedAt
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
const orderStatus = order?.status;
|
|
287
|
+
const paymentStatus = payment?.status;
|
|
288
|
+
const normalizedStatus = paymentStatus === "paid" || orderStatus === "paid" ? "paid" : paymentStatus === "pending" || orderStatus === "pending" ? "pending" : paymentStatus === "failed" || orderStatus === "failed" ? "failed" : paymentStatus === "canceled" || orderStatus === "canceled" ? "canceled" : paymentStatus === "refunded" || orderStatus === "refunded" ? "refunded" : paymentStatus === "pending" ? "requires_payment" : "idle";
|
|
289
|
+
return {
|
|
290
|
+
actionKey: order?.orderId ?? payment?.paymentId,
|
|
291
|
+
orderId: order?.orderId,
|
|
292
|
+
paymentId: payment?.paymentId,
|
|
293
|
+
transactionId: payment?.transactionId ?? order?.transactionId,
|
|
294
|
+
status: normalizedStatus,
|
|
295
|
+
orderStatus,
|
|
296
|
+
paymentStatus,
|
|
297
|
+
transactionStatus: payment?.transactionId ? "linked" : void 0,
|
|
298
|
+
updatedAt: payment?.updatedAt ?? order?.updatedAt
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function resolveBillingInterval(interval) {
|
|
302
|
+
return interval === "month" || interval === "year" ? interval : void 0;
|
|
303
|
+
}
|
|
304
|
+
function buildBillingActionPayload(payload, runtimeConfig) {
|
|
305
|
+
const config = normalizeBillingRuntimeConfig(runtimeConfig);
|
|
306
|
+
const item = resolveBillingItem(config.items, payload.key) ?? resolveBillingItem(config.items, payload.productId);
|
|
307
|
+
if (payload.kind === "subscribe" && item) {
|
|
308
|
+
return {
|
|
309
|
+
...payload,
|
|
310
|
+
subscriptionConfig: payload.subscriptionConfig ?? {
|
|
311
|
+
productKey: item.key,
|
|
312
|
+
productId: item.backendRef.productId,
|
|
313
|
+
planId: item.backendRef.planId,
|
|
314
|
+
priceId: item.backendRef.priceId,
|
|
315
|
+
interval: item.interval ?? config.defaults?.defaultInterval,
|
|
316
|
+
quantity: payload.quantity ?? config.defaults?.defaultQuantity,
|
|
317
|
+
metadata: item.metadata
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
if ((payload.kind === "purchase" || payload.kind === "manage" || payload.kind === "upgrade" || payload.kind === "cancel") && item) {
|
|
322
|
+
return {
|
|
323
|
+
...payload,
|
|
324
|
+
productConfig: payload.productConfig ?? {
|
|
325
|
+
productKey: item.key,
|
|
326
|
+
productId: item.backendRef.productId,
|
|
327
|
+
priceId: item.backendRef.priceId,
|
|
328
|
+
quantity: payload.quantity ?? config.defaults?.defaultQuantity,
|
|
329
|
+
creditGrant: item.creditGrant,
|
|
330
|
+
creditRedeem: item.creditRedeem,
|
|
331
|
+
metadata: item.metadata
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
return payload;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/billing/casdoor-purchase.ts
|
|
339
|
+
function isNonEmptyString(value) {
|
|
340
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
341
|
+
}
|
|
342
|
+
function isRecord(value) {
|
|
343
|
+
return typeof value === "object" && value !== null;
|
|
344
|
+
}
|
|
345
|
+
function extractString(value) {
|
|
346
|
+
return isNonEmptyString(value) ? value : void 0;
|
|
347
|
+
}
|
|
348
|
+
function parseCasdoorErrorPayload(message) {
|
|
349
|
+
if (!isNonEmptyString(message)) {
|
|
350
|
+
return void 0;
|
|
351
|
+
}
|
|
352
|
+
const trimmed = message.trim();
|
|
353
|
+
if (!trimmed.startsWith("{")) {
|
|
354
|
+
return { message: trimmed };
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const parsed = JSON.parse(trimmed);
|
|
358
|
+
if (!isRecord(parsed)) {
|
|
359
|
+
return { message: trimmed };
|
|
360
|
+
}
|
|
361
|
+
const detail = isRecord(parsed.detail) ? {
|
|
362
|
+
...parsed.detail,
|
|
363
|
+
field: extractString(parsed.detail.field),
|
|
364
|
+
value: extractString(parsed.detail.value),
|
|
365
|
+
issue: extractString(parsed.detail.issue),
|
|
366
|
+
location: extractString(parsed.detail.location)
|
|
367
|
+
} : void 0;
|
|
368
|
+
return {
|
|
369
|
+
...parsed,
|
|
370
|
+
code: extractString(parsed.code),
|
|
371
|
+
message: extractString(parsed.message) ?? extractString(parsed.msg) ?? extractString(detail?.issue) ?? trimmed,
|
|
372
|
+
detail
|
|
373
|
+
};
|
|
374
|
+
} catch {
|
|
375
|
+
return { message: trimmed };
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function readBuyProductRedirectTo(value) {
|
|
379
|
+
if (!isRecord(value)) {
|
|
380
|
+
return void 0;
|
|
381
|
+
}
|
|
382
|
+
for (const key of ["redirectTo", "redirectUrl", "redirect_url", "url", "href", "location"]) {
|
|
383
|
+
const candidate = value[key];
|
|
384
|
+
if (isNonEmptyString(candidate)) {
|
|
385
|
+
return candidate;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return void 0;
|
|
389
|
+
}
|
|
390
|
+
function normalizeCasdoorProductId2(id) {
|
|
391
|
+
const [owner, ...rest] = id.split("/");
|
|
392
|
+
const name = rest.join("/");
|
|
393
|
+
if (!isNonEmptyString(owner) || !isNonEmptyString(name)) {
|
|
394
|
+
throw new Error(`Invalid Casdoor product id: ${id}`);
|
|
395
|
+
}
|
|
396
|
+
return { owner, name };
|
|
397
|
+
}
|
|
398
|
+
function chooseProviderFromObjects(providerObjs, preferredProviderName) {
|
|
399
|
+
if (isNonEmptyString(preferredProviderName)) {
|
|
400
|
+
return preferredProviderName;
|
|
401
|
+
}
|
|
402
|
+
return providerObjs?.find((provider) => isNonEmptyString(provider.name))?.name;
|
|
403
|
+
}
|
|
404
|
+
function chooseCasdoorProviderName(product, preferredProviderName) {
|
|
405
|
+
if (isNonEmptyString(preferredProviderName)) {
|
|
406
|
+
return preferredProviderName;
|
|
407
|
+
}
|
|
408
|
+
const fromProviders = product.providers?.find(isNonEmptyString);
|
|
409
|
+
if (fromProviders) {
|
|
410
|
+
return fromProviders;
|
|
411
|
+
}
|
|
412
|
+
const fromProviderObjs = chooseProviderFromObjects(product.providerObjs, preferredProviderName);
|
|
413
|
+
if (fromProviderObjs) {
|
|
414
|
+
return fromProviderObjs;
|
|
415
|
+
}
|
|
416
|
+
throw new Error("No providerName available for Casdoor buy-product request.");
|
|
417
|
+
}
|
|
418
|
+
function buildCasdoorBuyProductParams(input) {
|
|
419
|
+
const params = new URLSearchParams();
|
|
420
|
+
params.set("id", input.id);
|
|
421
|
+
params.set("providerName", input.providerName);
|
|
422
|
+
params.set("pricingName", input.pricingName ?? "");
|
|
423
|
+
params.set("planName", input.planName ?? "");
|
|
424
|
+
params.set("userName", input.userName ?? "");
|
|
425
|
+
params.set("paymentEnv", input.paymentEnv ?? "");
|
|
426
|
+
params.set("customPrice", String(input.customPrice ?? 0));
|
|
427
|
+
return params;
|
|
428
|
+
}
|
|
429
|
+
function buildCasdoorBuyProductRequest(purchase, product, preferredProviderName) {
|
|
430
|
+
const providerName = chooseCasdoorProviderName(product, purchase.providerName ?? preferredProviderName);
|
|
431
|
+
const productId = `${product.owner}/${product.name}`;
|
|
432
|
+
return {
|
|
433
|
+
id: productId,
|
|
434
|
+
providerName,
|
|
435
|
+
pricingName: purchase.pricingName ?? "",
|
|
436
|
+
planName: purchase.planName ?? "",
|
|
437
|
+
userName: purchase.userName ?? "",
|
|
438
|
+
paymentEnv: purchase.paymentEnv ?? "",
|
|
439
|
+
customPrice: purchase.customPrice ?? 0
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function normalizeCasdoorBuyProductStatus(status) {
|
|
443
|
+
const statusText = typeof status === "string" ? status.toLowerCase() : "";
|
|
444
|
+
if (statusText.includes("error") || statusText.includes("fail") || statusText.includes("cancel")) {
|
|
445
|
+
return "failed";
|
|
446
|
+
}
|
|
447
|
+
if (statusText.includes("pend") || statusText.includes("require")) {
|
|
448
|
+
return "pending";
|
|
449
|
+
}
|
|
450
|
+
return "succeeded";
|
|
451
|
+
}
|
|
452
|
+
function normalizeCasdoorBuyProductResponse(response, fallbackRedirectTo) {
|
|
453
|
+
const errorPayload = parseCasdoorErrorPayload(response.msg);
|
|
454
|
+
const redirectTo = readBuyProductRedirectTo(response.data) ?? readBuyProductRedirectTo(response.data2) ?? readBuyProductRedirectTo(response.data3) ?? fallbackRedirectTo;
|
|
455
|
+
const status = normalizeCasdoorBuyProductStatus(response.status);
|
|
456
|
+
if (status === "failed") {
|
|
457
|
+
return {
|
|
458
|
+
status,
|
|
459
|
+
redirectTo,
|
|
460
|
+
message: errorPayload?.message ?? response.msg ?? "Casdoor buy-product failed.",
|
|
461
|
+
errorCode: errorPayload?.code,
|
|
462
|
+
rawResult: response
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
status,
|
|
467
|
+
redirectTo,
|
|
468
|
+
rawResult: response
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
export {
|
|
473
|
+
normalizeBillingRuntimeConfig,
|
|
474
|
+
normalizeBillingCatalogConfig,
|
|
475
|
+
resolveBillingItem,
|
|
476
|
+
resolveBillingPurchasable,
|
|
477
|
+
buildBillingPurchaseRequest,
|
|
478
|
+
filterBillingPurchasableItems,
|
|
479
|
+
buildBillingPaymentCallbackContext,
|
|
480
|
+
resolveBillingSubscriptionProduct,
|
|
481
|
+
resolveBillingProductSnapshot,
|
|
482
|
+
deriveBillingCreditsState,
|
|
483
|
+
deriveBillingEntitlements,
|
|
484
|
+
normalizeBillingPurchaseStatus,
|
|
485
|
+
resolveBillingInterval,
|
|
486
|
+
buildBillingActionPayload,
|
|
487
|
+
readBuyProductRedirectTo,
|
|
488
|
+
normalizeCasdoorProductId2 as normalizeCasdoorProductId,
|
|
489
|
+
chooseCasdoorProviderName,
|
|
490
|
+
buildCasdoorBuyProductParams,
|
|
491
|
+
buildCasdoorBuyProductRequest,
|
|
492
|
+
normalizeCasdoorBuyProductResponse
|
|
493
|
+
};
|
|
494
|
+
//# sourceMappingURL=chunk-46V73LSW.js.map
|