autumn-js
Version:
Autumn JS Library
250 lines (243 loc) • 6.83 kB
JavaScript
"use client";
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/react/hooks/internal/getLocalCheckResponse.ts
var getLocalCheckResponse_exports = {};
__export(getLocalCheckResponse_exports, {
getLocalCheckResponse: () => getLocalCheckResponse
});
module.exports = __toCommonJS(getLocalCheckResponse_exports);
// src/react/hooks/internal/check/balanceToAllowed.ts
var getAvailableOverage = ({
balance
}) => {
const breakdown = balance.breakdown ?? [];
const usageBasedBreakdown = breakdown.filter(
(item) => item.price?.billingMethod === "usage_based"
);
if (usageBasedBreakdown.length === 0) {
return balance.maxPurchase ?? void 0;
}
let maxOverage = 0;
let overageUsage = 0;
for (const item of usageBasedBreakdown) {
if (item.price?.maxPurchase === null) {
return void 0;
}
if (item.price?.maxPurchase !== void 0) {
maxOverage += item.price.maxPurchase;
}
const overage = Math.max(
0,
item.usage - item.includedGrant - item.prepaidGrant
);
overageUsage += overage;
}
return Math.max(0, maxOverage - overageUsage);
};
var balanceToAllowed = ({
balance,
feature,
requiredBalance
}) => {
if (feature.type === "boolean") {
return true;
}
if (balance.unlimited) {
return true;
}
if (requiredBalance < 0) {
return true;
}
if (balance.overageAllowed) {
const availableOverage = getAvailableOverage({ balance });
if (availableOverage === void 0) {
return true;
}
return balance.remaining + availableOverage >= requiredBalance;
}
return balance.remaining >= requiredBalance;
};
// src/react/hooks/internal/check/customerToFeatures.ts
var customerToFeatures = ({
customer
}) => {
const balances = Object.values(customer.balances);
const flags = Object.values(customer.flags);
const customerStates = [...balances, ...flags];
if (customerStates.length === 0) return [];
const firstCustomerState = customerStates[0];
if (!firstCustomerState.feature) {
throw new Error(
"[customerToFeatures] please expand `balances.feature` or `flags.feature` to get features for the customer"
);
}
return customerStates.map((customerState) => customerState.feature).filter((feature) => Boolean(feature));
};
// src/react/hooks/internal/check/findCreditSystemsByFeature.ts
var findCreditSystemsByFeature = ({
featureId,
features
}) => {
return features.filter(
(feature) => feature.id !== featureId && feature.type === "credit_system" && feature.creditSchema?.some(
(schema) => schema.meteredFeatureId === featureId
)
);
};
// src/react/hooks/internal/check/getCreditCost.ts
var getCreditCost = ({
featureId,
creditSystem,
amount = 1
}) => {
if (creditSystem.type !== "credit_system") {
return amount;
}
const schemaItem = creditSystem.creditSchema?.find(
(schema) => schema.meteredFeatureId === featureId
);
return amount * (schemaItem?.creditCost ?? 1);
};
// src/react/hooks/internal/getLocalCheckResponse.ts
var getFeatureToUse = ({
featureId,
features,
feature,
customer,
requiredBalance
}) => {
const creditSystems = findCreditSystemsByFeature({
featureId,
features
});
if (creditSystems.length === 0) return feature;
const mainBalance = customer.balances[feature.id];
if (mainBalance && balanceToAllowed({
balance: mainBalance,
feature,
requiredBalance
})) {
return feature;
}
for (const creditSystem of creditSystems) {
const creditBalance = customer.balances[creditSystem.id];
if (!creditBalance) continue;
const creditRequiredBalance = getCreditCost({
featureId: feature.id,
creditSystem,
amount: requiredBalance
});
if (balanceToAllowed({
balance: creditBalance,
feature: creditSystem,
requiredBalance: creditRequiredBalance
})) {
return creditSystem;
}
}
return creditSystems[0];
};
var getFeatureCheckResponse = ({
customer,
params
}) => {
const { featureId, requiredBalance = 1 } = params;
if (customer.flags[featureId]) {
return {
allowed: true,
customerId: customer.id ?? "",
entityId: params.entityId ?? null,
requiredBalance,
balance: null,
flag: customer.flags[featureId]
};
}
const features = customerToFeatures({ customer });
const feature = features.find((item) => item.id === featureId);
if (!feature) {
return {
allowed: false,
customerId: customer.id ?? "",
entityId: params.entityId ?? null,
requiredBalance,
balance: null,
flag: null
};
}
const featureToUse = getFeatureToUse({
featureId,
features,
feature,
customer,
requiredBalance
});
const balanceToUse = customer.balances[featureToUse.id];
if (!balanceToUse) {
return {
allowed: false,
customerId: customer.id ?? "",
entityId: params.entityId ?? null,
requiredBalance,
balance: null,
flag: null
};
}
const requiredBalanceToUse = featureToUse.type === "credit_system" && featureToUse.id !== feature.id ? getCreditCost({
featureId: feature.id,
creditSystem: featureToUse,
amount: requiredBalance
}) : requiredBalance;
const allowed = balanceToAllowed({
balance: balanceToUse,
feature: featureToUse,
requiredBalance: requiredBalanceToUse
});
return {
allowed,
customerId: customer.id ?? "",
entityId: params.entityId ?? null,
requiredBalance: requiredBalanceToUse,
balance: balanceToUse,
flag: null
};
};
var getLocalCheckResponse = ({
customer,
params
}) => {
if (!customer) {
return {
allowed: false,
customerId: "",
entityId: params.entityId ?? null,
requiredBalance: params.requiredBalance ?? 1,
balance: null,
flag: null
};
}
if (!params.featureId) {
throw new Error("check() requires featureId");
}
return getFeatureCheckResponse({ customer, params });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getLocalCheckResponse
});