UNPKG

spaps-sdk

Version:

Sweet Potato Authentication & Payment Service SDK - Zero-config client for SPAPS

155 lines (154 loc) 4.6 kB
// src/index.ts import axios from "axios"; var SPAPSClient = class { client; apiKey; accessToken; refreshToken; _isLocalMode = false; constructor(config = {}) { const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || process.env.NEXT_PUBLIC_SPAPS_API_URL; if (!apiUrl || apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1")) { this._isLocalMode = true; this.client = axios.create({ baseURL: apiUrl || "http://localhost:3300", timeout: config.timeout || 1e4, headers: { "Content-Type": "application/json" } }); } else { if (!config.apiKey && !process.env.SPAPS_API_KEY) { console.warn("\u26A0\uFE0F SPAPS: No API key provided. Some features may not work."); } this.apiKey = config.apiKey || process.env.SPAPS_API_KEY; this.client = axios.create({ baseURL: apiUrl, timeout: config.timeout || 1e4, headers: { "Content-Type": "application/json", ...this.apiKey && { "X-API-Key": this.apiKey } } }); } this.client.interceptors.request.use((config2) => { if (this.accessToken && !config2.headers.Authorization) { config2.headers.Authorization = `Bearer ${this.accessToken}`; } return config2; }); this.client.interceptors.response.use( (response) => response, async (error) => { if (error.response?.status === 401 && this.refreshToken) { try { const { data } = await this.refresh(this.refreshToken); this.accessToken = data.access_token; this.refreshToken = data.refresh_token; if (error.config) { error.config.headers.Authorization = `Bearer ${this.accessToken}`; return this.client.request(error.config); } } catch (refreshError) { this.accessToken = void 0; this.refreshToken = void 0; } } return Promise.reject(error); } ); } // Authentication Methods async login(email, password) { const response = await this.client.post("/api/auth/login", { email, password }); this.accessToken = response.data.access_token; this.refreshToken = response.data.refresh_token; return response; } async register(email, password) { const response = await this.client.post("/api/auth/register", { email, password }); this.accessToken = response.data.access_token; this.refreshToken = response.data.refresh_token; return response; } async walletSignIn(walletAddress, signature, message, chainType = "solana") { const response = await this.client.post("/api/auth/wallet-sign-in", { wallet_address: walletAddress, signature, message, chain_type: chainType }); this.accessToken = response.data.access_token; this.refreshToken = response.data.refresh_token; return response; } async refresh(refreshToken) { const response = await this.client.post("/api/auth/refresh", { refresh_token: refreshToken || this.refreshToken }); this.accessToken = response.data.access_token; this.refreshToken = response.data.refresh_token; return response; } async logout() { await this.client.post("/api/auth/logout"); this.accessToken = void 0; this.refreshToken = void 0; } async getUser() { return this.client.get("/api/auth/user"); } // Stripe Methods async createCheckoutSession(priceId, successUrl, cancelUrl) { return this.client.post("/api/stripe/create-checkout-session", { price_id: priceId, success_url: successUrl, cancel_url: cancelUrl }); } async getSubscription() { return this.client.get("/api/stripe/subscription"); } async cancelSubscription() { await this.client.delete("/api/stripe/subscription"); } // Usage Methods async getUsageBalance() { return this.client.get("/api/usage/balance"); } async recordUsage(feature, amount) { await this.client.post("/api/usage/record", { feature, amount }); } // Utility Methods isAuthenticated() { return !!this.accessToken; } getAccessToken() { return this.accessToken; } setAccessToken(token) { this.accessToken = token; } isLocalMode() { return this._isLocalMode; } async health() { return this.client.get("/health"); } }; var index_default = SPAPSClient; export { SPAPSClient as SPAPS, SPAPSClient, SPAPSClient as SweetPotatoSDK, index_default as default };