UNPKG

autumn-js

Version:

Autumn JS Library

983 lines (949 loc) 27.7 kB
"use client"; "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/libraries/react/AutumnContext.tsx var AutumnContext_exports = {}; __export(AutumnContext_exports, { AutumnContext: () => AutumnContext, useAutumnContext: () => useAutumnContext }); module.exports = __toCommonJS(AutumnContext_exports); // src/libraries/react/errorUtils/logAuthError.ts var logAuthError = async (response) => { if (response.status === 401) { let clonedResponse = response.clone(); let data = await clonedResponse.json(); if (data.message.includes("Missing authorization header")) { console.error(`[Autumn] Missing authorization header. Use the getBearerToken prop in <AutumnProvider /> to set the authorization header. https://docs.useautumn.com/quickstart/quickstart#5-set-up-autumnprovider`); return true; } } return false; }; // src/sdk/error.ts var AutumnError = class _AutumnError extends Error { message; code; constructor(response) { super(response.message); this.message = response.message; this.code = response.code; } static fromError(error) { return new _AutumnError({ message: error.message || "Unknown error", code: error.code || "unknown_error" }); } toString() { return `${this.message} (code: ${this.code})`; } toJSON() { return { message: this.message, code: this.code }; } }; // src/sdk/general/genMethods.ts var handleCheckout = async ({ instance, params }) => { return instance.post("/checkout", params); }; var handleAttach = async ({ instance, params }) => { return instance.post("/attach", params); }; var handleSetupPayment = async ({ instance, params }) => { return instance.post("/setup_payment", params); }; var handleCancel = async ({ instance, params }) => { return instance.post("/cancel", params); }; var handleTrack = async ({ instance, params }) => { return instance.post("/track", params); }; var handleUsage = async ({ instance, params }) => { return instance.post("/usage", params); }; var handleCheck = async ({ instance, params }) => { return instance.post("/check", params); }; // src/libraries/backend/constants.ts var autumnApiUrl = "https://api.useautumn.com/v1"; // src/sdk/utils.ts var staticWrapper = (callback, instance, args) => { if (!instance) { instance = new Autumn(); } return callback({ instance, ...args }); }; // src/sdk/customers/cusMethods.ts var customerMethods = (instance) => { return { get: (id, params) => staticWrapper(getCustomer, instance, { id, params }), create: (params) => staticWrapper(createCustomer, instance, { params }), update: (id, params) => staticWrapper(updateCustomer, instance, { id, params }), delete: (id) => staticWrapper(deleteCustomer, instance, { id }), billingPortal: (id, params) => staticWrapper(billingPortal, instance, { id, params }) }; }; var getExpandStr = (expand) => { if (!expand) { return ""; } return `expand=${expand.join(",")}`; }; var getCustomer = async ({ instance, id, params }) => { if (!id) { return { data: null, error: new AutumnError({ message: "Customer ID is required", code: "CUSTOMER_ID_REQUIRED" }) }; } return instance.get(`/customers/${id}?${getExpandStr(params?.expand)}`); }; var createCustomer = async ({ instance, params }) => { return instance.post(`/customers?${getExpandStr(params?.expand)}`, params); }; var updateCustomer = async ({ instance, id, params }) => { return instance.post(`/customers/${id}`, params); }; var deleteCustomer = async ({ instance, id }) => { return instance.delete(`/customers/${id}`); }; var billingPortal = async ({ instance, id, params }) => { return instance.post(`/customers/${id}/billing_portal`, params); }; // src/sdk/customers/entities/entMethods.ts var entityMethods = (instance) => { return { get: (customer_id, entity_id, params) => staticWrapper(getEntity, instance, { customer_id, entity_id, params }), create: (customer_id, params) => staticWrapper(createEntity, instance, { customer_id, params }), delete: (customer_id, entity_id) => staticWrapper(deleteEntity, instance, { customer_id, entity_id }) }; }; var getExpandStr2 = (expand) => { if (!expand) { return ""; } return `expand=${expand.join(",")}`; }; var getEntity = async ({ instance, customer_id, entity_id, params }) => { return instance.get( `/customers/${customer_id}/entities/${entity_id}?${getExpandStr2( params?.expand )}` ); }; var createEntity = async ({ instance, customer_id, params }) => { return instance.post(`/customers/${customer_id}/entities`, params); }; var deleteEntity = async ({ instance, customer_id, entity_id }) => { return instance.delete(`/customers/${customer_id}/entities/${entity_id}`); }; // src/sdk/products/prodMethods.ts var productMethods = (instance) => { return { get: (id) => staticWrapper(getProduct, instance, { id }), create: (params) => staticWrapper(createProduct, instance, { params }), list: (params) => staticWrapper(listProducts, instance, { params }) }; }; var listProducts = async ({ instance, params }) => { let path = "/products_beta"; if (params) { const queryParams = new URLSearchParams(); for (const [key, value] of Object.entries(params)) { if (value !== void 0) { queryParams.append(key, String(value)); } } const queryString = queryParams.toString(); if (queryString) { path += `?${queryString}`; } } return instance.get(path); }; var getProduct = async ({ instance, id }) => { return instance.get(`/products/${id}`); }; var createProduct = async ({ instance, params }) => { return instance.post("/products", params); }; // src/sdk/referrals/referralMethods.ts var referralMethods = (instance) => { return { createCode: (params) => staticWrapper(createReferralCode, instance, { params }), redeemCode: (params) => staticWrapper(redeemReferralCode, instance, { params }) }; }; var createReferralCode = async ({ instance, params }) => { return instance.post("/referrals/code", params); }; var redeemReferralCode = async ({ instance, params }) => { return instance.post("/referrals/redeem", params); }; // src/sdk/response.ts var toContainerResult = async ({ response, logger: logger2, logError = true }) => { if (response.status < 200 || response.status >= 300) { let error; try { error = await response.json(); if (logError) { logger2.error(`[Autumn] ${error.message}`); } } catch (error2) { throw error2; return { data: null, error: new AutumnError({ message: "Failed to parse JSON response from Autumn", code: "internal_error" }), statusCode: response.status }; } return { data: null, error: new AutumnError({ message: error.message, code: error.code }), statusCode: response.status }; } try { let data = await response.json(); return { data, error: null, statusCode: response?.status }; } catch (error) { throw error; return { data: null, error: new AutumnError({ message: "Failed to parse Autumn API response", code: "internal_error" }), statusCode: response?.status }; } }; // src/utils/logger.ts var import_chalk = __toESM(require("chalk")); var getTime = () => { let timeString = (/* @__PURE__ */ new Date()).toISOString(); return `[${timeString.split("T")[1].split(".")[0]}]`; }; var greaterThanLevel = (level) => { return levels.indexOf(level) >= levels.indexOf(logger.level); }; var levels = ["debug", "info", "warn", "error", "fatal"]; var logger = { ...console, level: "info", debug: (...args) => { if (greaterThanLevel("debug")) { console.log(getTime(), import_chalk.default.gray("DEBUG"), ...args); } }, log: (...args) => { console.log(getTime(), import_chalk.default.blue("INFO"), ...args); }, info: (...args) => { if (greaterThanLevel("info")) { console.log(getTime(), import_chalk.default.blue("INFO"), ...args); } }, warn: (...args) => { if (greaterThanLevel("warn")) { console.log(getTime(), import_chalk.default.yellow("WARN"), ...args); } }, error: (...args) => { if (greaterThanLevel("error")) { console.log(getTime(), import_chalk.default.red("ERROR"), ...args); } } }; // src/sdk/client.ts var LATEST_API_VERSION = "1.2"; var Autumn = class { secretKey; publishableKey; headers; url; logger = console; constructor(options) { try { this.secretKey = options?.secretKey || process.env.AUTUMN_SECRET_KEY; this.publishableKey = options?.publishableKey || process.env.AUTUMN_PUBLISHABLE_KEY; } catch (error) { } if (!this.secretKey && !this.publishableKey && !options?.headers) { throw new Error("Autumn secret key or publishable key is required"); } this.headers = options?.headers || { Authorization: `Bearer ${this.secretKey || this.publishableKey}`, "Content-Type": "application/json" }; let version = options?.version || LATEST_API_VERSION; this.headers["x-api-version"] = version; this.url = options?.url || autumnApiUrl; this.logger = logger; this.logger.level = options?.logLevel || "info"; } async get(path) { const response = await fetch(`${this.url}${path}`, { headers: this.headers }); return toContainerResult({ response, logger: this.logger }); } async post(path, body) { try { const response = await fetch(`${this.url}${path}`, { method: "POST", headers: this.headers, body: JSON.stringify(body) }); return toContainerResult({ response, logger: this.logger }); } catch (error) { console.error("Error sending request:", error); throw error; } } async delete(path) { const response = await fetch(`${this.url}${path}`, { method: "DELETE", headers: this.headers }); return toContainerResult({ response, logger: this.logger }); } static customers = customerMethods(); static products = productMethods(); static entities = entityMethods(); static referrals = referralMethods(); customers = customerMethods(this); products = productMethods(this); entities = entityMethods(this); referrals = referralMethods(this); static checkout = (params) => staticWrapper(handleCheckout, void 0, { params }); async checkout(params) { return handleCheckout({ instance: this, params }); } static attach = (params) => staticWrapper(handleAttach, void 0, { params }); static usage = (params) => staticWrapper(handleUsage, void 0, { params }); async attach(params) { return handleAttach({ instance: this, params }); } static setupPayment = (params) => staticWrapper(handleSetupPayment, void 0, { params }); async setupPayment(params) { return handleSetupPayment({ instance: this, params }); } static cancel = (params) => staticWrapper(handleCancel, void 0, { params }); async cancel(params) { return handleCancel({ instance: this, params }); } static check = (params) => staticWrapper(handleCheck, void 0, { params }); async check(params) { return handleCheck({ instance: this, params }); } static track = (params) => staticWrapper(handleTrack, void 0, { params }); async track(params) { return handleTrack({ instance: this, params }); } async usage(params) { return handleUsage({ instance: this, params }); } }; // src/sdk/customers/entities/entTypes.ts var import_zod = require("zod"); var EntityDataSchema = import_zod.z.object({ name: import_zod.z.string().optional(), feature_id: import_zod.z.string() }); // src/sdk/general/genTypes.ts var import_zod2 = require("zod"); var CancelParamsSchema = import_zod2.z.object({ customer_id: import_zod2.z.string(), product_id: import_zod2.z.string(), entity_id: import_zod2.z.string().optional(), cancel_immediately: import_zod2.z.boolean().optional() }); var CancelResultSchema = import_zod2.z.object({ success: import_zod2.z.boolean(), customer_id: import_zod2.z.string(), product_id: import_zod2.z.string() }); var TrackParamsSchema = import_zod2.z.object({ customer_id: import_zod2.z.string(), value: import_zod2.z.number().optional(), feature_id: import_zod2.z.string().optional(), event_name: import_zod2.z.string().optional(), entity_id: import_zod2.z.string().optional(), customer_data: import_zod2.z.any().optional(), idempotency_key: import_zod2.z.string().optional(), entity_data: import_zod2.z.any().optional() }); var TrackResultSchema = import_zod2.z.object({ id: import_zod2.z.string(), code: import_zod2.z.string(), customer_id: import_zod2.z.string(), feature_id: import_zod2.z.string().optional(), event_name: import_zod2.z.string().optional() }); var CheckParamsSchema = import_zod2.z.object({ customer_id: import_zod2.z.string(), feature_id: import_zod2.z.string().optional(), product_id: import_zod2.z.string().optional(), entity_id: import_zod2.z.string().optional(), customer_data: import_zod2.z.any().optional(), required_balance: import_zod2.z.number().optional(), send_event: import_zod2.z.boolean().optional(), with_preview: import_zod2.z.boolean().optional(), entity_data: EntityDataSchema.optional() }); // src/sdk/customers/cusEnums.ts var import_zod3 = require("zod"); var CustomerExpandEnum = import_zod3.z.enum([ "invoices", "rewards", "trials_used", "entities", "referrals", "payment_method" ]); // src/sdk/customers/cusTypes.ts var import_zod4 = require("zod"); var CustomerDataSchema = import_zod4.z.object({ name: import_zod4.z.string().nullish(), email: import_zod4.z.string().nullish(), fingerprint: import_zod4.z.string().nullish() }); var CreateCustomerParamsSchema = import_zod4.z.object({ id: import_zod4.z.string().nullish(), email: import_zod4.z.string().nullish(), name: import_zod4.z.string().nullish(), fingerprint: import_zod4.z.string().nullish(), metadata: import_zod4.z.record(import_zod4.z.any()).optional(), expand: import_zod4.z.array(CustomerExpandEnum).optional() }); var BillingPortalParamsSchema = import_zod4.z.object({ return_url: import_zod4.z.string().optional() }); // src/sdk/referrals/referralTypes.ts var import_zod5 = require("zod"); var CreateReferralCodeParamsSchema = import_zod5.z.object({ customer_id: import_zod5.z.string(), program_id: import_zod5.z.string() }); var RedeemReferralCodeParamsSchema = import_zod5.z.object({ code: import_zod5.z.string(), customer_id: import_zod5.z.string() }); // src/sdk/general/attachTypes.ts var import_zod6 = require("zod"); var AttachFeatureOptionsSchema = import_zod6.z.object({ feature_id: import_zod6.z.string(), quantity: import_zod6.z.number() }); var AttachParamsSchema = import_zod6.z.object({ customer_id: import_zod6.z.string(), product_id: import_zod6.z.string().optional(), entity_id: import_zod6.z.string().optional(), options: import_zod6.z.array(AttachFeatureOptionsSchema).optional(), product_ids: import_zod6.z.array(import_zod6.z.string()).optional(), free_trial: import_zod6.z.boolean().optional(), success_url: import_zod6.z.string().optional(), metadata: import_zod6.z.record(import_zod6.z.string()).optional(), force_checkout: import_zod6.z.boolean().optional(), customer_data: CustomerDataSchema.optional(), entity_data: import_zod6.z.any().optional(), checkout_session_params: import_zod6.z.record(import_zod6.z.any()).optional(), reward: import_zod6.z.string().optional() }); var AttachResultSchema = import_zod6.z.object({ checkout_url: import_zod6.z.string().optional(), customer_id: import_zod6.z.string(), product_ids: import_zod6.z.array(import_zod6.z.string()), code: import_zod6.z.string(), message: import_zod6.z.string(), customer_data: import_zod6.z.any().optional() }); var CheckoutParamsSchema = import_zod6.z.object({ customer_id: import_zod6.z.string(), product_id: import_zod6.z.string(), entity_id: import_zod6.z.string().optional(), success_url: import_zod6.z.string().optional(), customer_data: CustomerDataSchema.optional(), options: import_zod6.z.array(AttachFeatureOptionsSchema).optional() }); // src/libraries/react/errorUtils/logFetchError.ts var logFetchError = ({ method, backendUrl, path, error }) => { console.error(`[Autumn] Fetch failed: ${method} ${backendUrl}${path} 1. Check that backendUrl in <AutumnProvider/> is correctly set. 2. Check that autumnHandler is correctly registered on your backend.`); }; // src/libraries/react/client/clientCompMethods.ts async function getPricingTableMethod() { const res = await this.get(`${this.prefix}/components/pricing_table`); return res; } // src/libraries/react/client/clientCusMethods.ts var createCustomerMethod = async ({ client, params }) => { let result = await client.post(`${client.prefix}/customers`, params); return result; }; // src/libraries/react/utils/toSnakeCase.ts function stringToSnakeCase(str) { return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase(); } var toSnakeCase = (obj, excludeKeys) => { if (Array.isArray(obj)) { return obj.map((item) => toSnakeCase(item, excludeKeys)); } else if (obj !== null && typeof obj === "object") { return Object.fromEntries( Object.entries(obj).map(([key, value]) => { const snakeKey = stringToSnakeCase(key); if (excludeKeys && excludeKeys.includes(key)) { return [snakeKey, value]; } return [snakeKey, toSnakeCase(value, excludeKeys)]; }) ); } return obj; }; // src/utils/entityUtils.tsx var getEntityExpandStr = (expand) => { if (!expand) { return ""; } return `expand=${expand.join(",")}`; }; // src/libraries/react/client/clientEntityMethods.ts async function createEntityMethod(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/entities`, snakeParams); return res; } async function getEntityMethod(entityId, params) { let snakeParams = toSnakeCase(params); let expand = getEntityExpandStr(params?.expand); const res = await this.get(`${this.prefix}/entities/${entityId}?${expand}`); return res; } async function deleteEntityMethod(entityId) { const res = await this.delete(`${this.prefix}/entities/${entityId}`); return res; } // src/libraries/react/client/clientGenMethods.ts async function checkoutMethod(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/checkout`, snakeParams); return res; } async function attachMethod(params) { let snakeParams = toSnakeCase(params, ["checkoutSessionparams"]); const res = await this.post(`${this.prefix}/attach`, snakeParams); return res; } async function setupPaymentMethod(params) { let snakeParams = toSnakeCase(params, ["checkoutSessionParams"]); const res = await this.post(`${this.prefix}/setup_payment`, snakeParams); return res; } async function cancelMethod(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/cancel`, snakeParams); return res; } async function checkMethod(params) { let { dialog, ...rest } = params; let snakeParams = toSnakeCase(rest); const res = await this.post(`${this.prefix}/check`, snakeParams); return res; } async function trackMethod(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/track`, snakeParams); return res; } async function openBillingPortalMethod(params) { let snakeParams = toSnakeCase(params || {}); const res = await this.post(`${this.prefix}/billing_portal`, snakeParams); return res; } // src/libraries/react/client/clientProdMethods.ts async function listProductsMethod() { const res = await this.get(`${this.prefix}/products`); return res; } // src/libraries/react/client/clientReferralMethods.ts async function createCode(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/referrals/code`, snakeParams); return res; } async function redeemCode(params) { let snakeParams = toSnakeCase(params); const res = await this.post(`${this.prefix}/referrals/redeem`, snakeParams); return res; } // src/libraries/react/client/ReactAutumnClient.tsx var AutumnClient = class { backendUrl; getBearerToken; customerData; includeCredentials; prefix; constructor({ backendUrl, getBearerToken, customerData, includeCredentials, betterAuthUrl }) { this.backendUrl = backendUrl; this.getBearerToken = getBearerToken; this.customerData = customerData; this.includeCredentials = includeCredentials; this.prefix = "/api/autumn"; if (betterAuthUrl) { this.prefix = "/api/auth/autumn"; this.backendUrl = betterAuthUrl; } } /** * Detects if the backend supports CORS credentials by making an OPTIONS request */ async detectCors() { if (this.prefix?.includes("/api/auth")) { return { valid: true, includeCredentials: true }; } const testEndpoint = `${this.backendUrl}/api/autumn/cors`; try { await fetch(testEndpoint, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" } }); return { valid: true, includeCredentials: true }; } catch (error) { try { await fetch(testEndpoint, { method: "POST", credentials: "omit", headers: { "Content-Type": "application/json" } }); return { valid: true, includeCredentials: false }; } catch (error2) { return { valid: false, includeCredentials: void 0 }; } } } /** * Automatically determines whether to include credentials based on CORS detection */ async shouldIncludeCredentials() { if (this.includeCredentials !== void 0) { return this.includeCredentials; } try { const corsResult = await this.detectCors(); if (corsResult.valid) { console.warn( `[Autumn] Detected CORS credentials: ${corsResult.includeCredentials}` ); console.warn( `[Autumn] To disable this warning, you can set includeCredentials={${corsResult.includeCredentials ? "true" : "false"}} in <AutumnProvider />` ); this.includeCredentials = corsResult.includeCredentials; } return corsResult.includeCredentials || false; } catch (error) { console.error(`[Autumn] Error detecting CORS: ${error.message}`); return false; } } async getHeaders() { let headers = { "Content-Type": "application/json" }; if (this.getBearerToken) { try { let token = await this.getBearerToken(); headers.Authorization = `Bearer ${token}`; } catch (error) { console.error(`Failed to call getToken() in AutumnProvider`); } } return headers; } async handleFetch({ path, method, body }) { body = method === "POST" ? JSON.stringify({ ...body, customer_data: this.customerData || void 0 }) : void 0; const includeCredentials = await this.shouldIncludeCredentials(); try { const response = await fetch(`${this.backendUrl}${path}`, { method, body, headers: await this.getHeaders(), credentials: includeCredentials ? "include" : "omit" }); const loggedError = await logAuthError(response); return await toContainerResult({ response, logger: console, logError: !loggedError }); } catch (error) { logFetchError({ method, backendUrl: this.backendUrl || "", path, error }); return { data: null, error: new AutumnError({ message: error.message, code: "fetch_failed" }) }; } } async post(path, body) { return await this.handleFetch({ path, method: "POST", body }); } async get(path) { return await this.handleFetch({ path, method: "GET" }); } async delete(path) { return await this.handleFetch({ path, method: "DELETE" }); } async createCustomer(params) { return await createCustomerMethod({ client: this, params }); } async getPricingTable() { return await getPricingTableMethod.bind(this)(); } attach = attachMethod.bind(this); checkout = checkoutMethod.bind(this); cancel = cancelMethod.bind(this); check = checkMethod.bind(this); track = trackMethod.bind(this); openBillingPortal = openBillingPortalMethod.bind(this); setupPayment = setupPaymentMethod.bind(this); entities = { create: createEntityMethod.bind(this), get: getEntityMethod.bind(this), delete: deleteEntityMethod.bind(this) }; referrals = { createCode: createCode.bind(this), redeemCode: redeemCode.bind(this) }; products = { list: listProductsMethod.bind(this) }; }; // src/libraries/react/AutumnContext.tsx var import_react = require("react"); var AutumnContext = (0, import_react.createContext)({ initialized: false, disableDialogs: false, client: new AutumnClient({ backendUrl: "" }), paywallDialog: { props: null, setProps: () => { }, open: false, setOpen: () => { }, setComponent: () => { } }, attachDialog: { props: null, setProps: () => { }, open: false, setOpen: () => { }, setComponent: () => { } } }); var useAutumnContext = ({ AutumnContext: AutumnContext2, name, errorIfNotInitialized = true }) => { const context = (0, import_react.useContext)(AutumnContext2); if (!context.initialized && errorIfNotInitialized) { throw new Error(`${name} must be used within <AutumnProvider />`); } return context; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AutumnContext, useAutumnContext });