UNPKG

autumn-js

Version:
361 lines (357 loc) 8.32 kB
"use client"; import { AutumnError } from "./chunk-QN2ALOVD.mjs"; // src/libraries/react/client/ConvexAutumnClient.tsx var ConvexAutumnClient = class { convexApi; convex; // protected readonly convexClient: ConvexHttpClient; customerData; headers; backendUrl = void 0; prefix = "/api/convex"; getBearerToken; suppressLogs; // Stub implementations for HTTP-specific methods that the interface requires async detectCors() { return { valid: true, includeCredentials: false }; } async shouldIncludeCredentials() { return false; } async getHeaders() { return { "Content-Type": "application/json" }; } async handleFetch() { throw new Error("HTTP methods not supported in Convex mode"); } async post() { throw new Error("HTTP methods not supported in Convex mode"); } async get() { throw new Error("HTTP methods not supported in Convex mode"); } async delete() { throw new Error("HTTP methods not supported in Convex mode"); } constructor({ convexApi, customerData, headers, getBearerToken, convex, suppressLogs }) { this.convex = convex; this.convexApi = convexApi; this.getBearerToken = getBearerToken; this.customerData = customerData; this.headers = headers; this.suppressLogs = suppressLogs ?? false; } async createCustomer(params) { try { const result = await this.convex.action( this.convexApi.createCustomer, params ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } } // Core methods that wrap Convex actions attach = async (args) => { try { const { dialog, openInNewTab, ...backendArgs } = args; const result = await this.convex.action( this.convexApi.attach, backendArgs ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; checkout = async (args) => { try { const { dialog, openInNewTab, ...backendArgs } = args; const result = await this.convex.action( this.convexApi.checkout, backendArgs ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; cancel = async (args) => { try { const result = await this.convex.action(this.convexApi.cancel, args); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; check = async (args) => { try { const result = await this.convex.action(this.convexApi.check, args); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; track = async (args) => { try { const result = await this.convex.action(this.convexApi.track, args); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; openBillingPortal = async (args) => { try { const { openInNewTab, ...backendArgs } = args; const result = await this.convex.action( this.convexApi.billingPortal, backendArgs ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; setupPayment = async (args) => { try { const result = await this.convex.action( this.convexApi.setupPayment, args ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; query = async (args) => { try { const result = await this.convex.action(this.convexApi.query, args); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }; entities = { create: async (args) => { try { if (Array.isArray(args)) { throw new Error( "Passing an array of entities to createEntity() is not supported for Convex" ); } else { const result = await this.convex.action( this.convexApi.createEntity, args ); return result; } } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }, get: async (entityId, args) => { try { const result = await this.convex.action(this.convexApi.getEntity, { entityId, ...args }); return result; } catch (error) { if (!this.suppressLogs) { console.error("Error fetching entity: ", error); } return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }, delete: async (args) => { try { const result = await this.convex.action( this.convexApi.deleteEntity, args ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } } }; referrals = { createCode: async (args) => { try { const result = await this.convex.action( this.convexApi.createReferralCode, args ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }, redeemCode: async (args) => { try { const result = await this.convex.action( this.convexApi.redeemReferralCode, args ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } } }; products = { list: async () => { try { const result = await this.convex.action( this.convexApi.listProducts, {} ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } } }; events = { list: async (params) => { try { const result = await this.convex.action( this.convexApi.listEvents, params ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } }, aggregate: async (params) => { try { const result = await this.convex.action( this.convexApi.aggregateEvents, params ); return result; } catch (error) { return { data: null, error: new AutumnError({ message: error.message, code: "convex_action_failed" }) }; } } }; }; export { ConvexAutumnClient };