UNPKG

autumn-js

Version:
185 lines (180 loc) 5.19 kB
"use client"; "use strict"; 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/client/index.ts var client_exports = {}; __export(client_exports, { AutumnClientError: () => AutumnClientError, createAutumnClient: () => createAutumnClient }); module.exports = __toCommonJS(client_exports); // src/react/client/AutumnClientError.ts var AutumnClientError = class extends Error { code; statusCode; details; constructor({ message, code, statusCode, details }) { super(message); this.name = "AutumnClientError"; this.code = code; this.statusCode = statusCode; this.details = details; } }; // src/react/client/internal/httpClient.ts var isErrorBody = (body) => { return !!body && typeof body === "object" && "message" in body && "code" in body && "statusCode" in body; }; var createHttpClient = (config) => { const { backendUrl, pathPrefix, includeCredentials, headers: customHeaders } = config; const baseUrl = backendUrl ? `${backendUrl}${pathPrefix}` : pathPrefix; const request = async ({ route, body, method = "POST" }) => { const url = `${baseUrl}/${route}`; const headers = { "Content-Type": "application/json", ...customHeaders }; try { const response = await fetch(url, { method, headers, ...includeCredentials && { credentials: "include" }, ...body !== void 0 && { body: JSON.stringify(body) } }); const statusCode = response.status; if (statusCode === 204) { return null; } const result = await response.json(); if (!response.ok) { const error = isErrorBody(result) ? new AutumnClientError(result) : new AutumnClientError({ message: result?.message || "Request failed", code: result?.code || "request_failed", statusCode }); console.error(`[Autumn] ${error.message}`); throw error; } return result; } catch (error) { if (error instanceof AutumnClientError) { throw error; } const message = error instanceof Error ? error.message : "Network error"; const autumnError = new AutumnClientError({ message, code: "network_error", statusCode: 0 }); console.error(`[Autumn] ${autumnError.code}: ${autumnError.message}`); throw autumnError; } }; return { request }; }; // src/react/client/AutumnClient.ts var createAutumnClient = (config) => { const http = createHttpClient({ backendUrl: config.backendUrl, pathPrefix: config.pathPrefix, includeCredentials: config.includeCredentials, headers: config.headers }); return { getOrCreateCustomer: (params) => http.request({ route: "getOrCreateCustomer", body: params }), attach: (params) => http.request({ route: "attach", body: params }), previewAttach: (params) => http.request({ route: "previewAttach", body: params }), updateSubscription: (params) => http.request({ route: "updateSubscription", body: params }), previewUpdateSubscription: (params) => http.request({ route: "previewUpdateSubscription", body: params }), multiAttach: (params) => http.request({ route: "multiAttach", body: params }), previewMultiAttach: (params) => http.request({ route: "previewMultiAttach", body: params }), setupPayment: (params) => http.request({ route: "setupPayment", body: params }), openCustomerPortal: (params) => http.request({ route: "openCustomerPortal", body: params }), createReferralCode: (params) => http.request({ route: "createReferralCode", body: params }), redeemReferralCode: (params) => http.request({ route: "redeemReferralCode", body: params }), listPlans: (params) => http.request({ route: "listPlans", body: params ?? {} }), listEvents: (params) => http.request({ route: "listEvents", body: params }), aggregateEvents: (params) => http.request({ route: "aggregateEvents", body: params }), getEntity: (params) => http.request({ route: "getEntity", body: params }) }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AutumnClientError, createAutumnClient });