UNPKG

autumn-js

Version:

Autumn JS Library

139 lines (138 loc) 3.44 kB
"use server"; import { createAutumnClient } from "../server/cusActions"; import { withAuth } from "./auth/withAuth"; import { toServerResponse } from "./utils"; import { toSnakeCase } from "../../libraries/react/utils/toSnakeCase"; const attachAction = withAuth({ fn: async ({ customerId, customerData, productId, entityId, options, successUrl, forceCheckout, metadata, entityData, reward }) => { const autumn = createAutumnClient(); let res = await autumn.attach({ customer_id: customerId, customer_data: customerData, product_id: productId, success_url: successUrl, options, force_checkout: forceCheckout, metadata, entity_id: entityId, entity_data: entityData ? { ...entityData, feature_id: entityData.featureId } : void 0, reward }); return toServerResponse(res); }, withCustomerData: true }); const cancelAction = withAuth({ fn: async (params) => { const autumn = createAutumnClient(); let res = await autumn.cancel(toSnakeCase(params)); return toServerResponse(res); } }); const checkAction = withAuth({ fn: async ({ customerId, customerData, featureId, productId, entityId, requiredBalance, sendEvent, withPreview, entityData }) => { const autumn = createAutumnClient(); let res = await autumn.check({ customer_id: customerId, feature_id: featureId, product_id: productId, entity_id: entityId, required_balance: requiredBalance, send_event: sendEvent, with_preview: withPreview, customer_data: customerData, entity_data: entityData ? { ...entityData, feature_id: entityData.featureId } : void 0 }); return toServerResponse(res); }, withCustomerData: true }); const trackAction = withAuth({ fn: async ({ customerId, featureId, entityId, value, eventName, idempotencyKey, customerData, entityData }) => { const autumn = createAutumnClient(); let res = await autumn.track({ customer_id: customerId, feature_id: featureId, entity_id: entityId, value, event_name: eventName, idempotency_key: idempotencyKey, customer_data: customerData, entity_data: entityData ? { ...entityData, feature_id: entityData.featureId } : void 0 }); return toServerResponse(res); }, withCustomerData: true }); const getBillingPortalAction = withAuth({ fn: async ({ customerId, params }) => { const autumn = createAutumnClient(); let result = await autumn.customers.billingPortal(customerId, params); return toServerResponse(result); } }); const generateReferralCodeAction = withAuth({ fn: async ({ customerId, programId }) => { const autumn = createAutumnClient(); let result = await autumn.referrals.createCode({ customer_id: customerId, program_id: programId }); return toServerResponse(result); } }); const redeemReferralCodeAction = withAuth({ fn: async ({ code, customerId }) => { const autumn = createAutumnClient(); let result = await autumn.referrals.redeemCode({ code, customer_id: customerId }); return toServerResponse(result); } }); export { attachAction, cancelAction, checkAction, generateReferralCodeAction, getBillingPortalAction, redeemReferralCodeAction, trackAction };