UNPKG

spaps

Version:

Sweet Potato Authentication & Payment Service CLI - Docker Compose orchestrator for local Python/FastAPI SPAPS server with built-in admin middleware

163 lines (138 loc) 4.96 kB
/** * SPAPS Client - Re-export from spaps-sdk * This allows users to import from 'spaps/client' */ try { // Try to load spaps-sdk if it's installed const sdk = require('spaps-sdk'); // Support both default export and named export const SPAPSClient = sdk.SPAPSClient || sdk.default || sdk; module.exports = SPAPSClient; module.exports.SPAPSClient = SPAPSClient; module.exports.default = SPAPSClient; module.exports.SPAPS = SPAPSClient; module.exports.SweetPotatoSDK = SPAPSClient; } catch (error) { // Fallback to a simple client implementation for local development const axios = require('axios'); class SPAPSClient { constructor(config = {}) { const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || 'http://localhost:3301'; this.isLocalMode = apiUrl.includes('localhost') || apiUrl.includes('127.0.0.1'); this.client = axios.create({ baseURL: apiUrl, timeout: config.timeout || 10000, headers: { 'Content-Type': 'application/json', ...(config.apiKey && { 'X-API-Key': config.apiKey }) } }); this.accessToken = null; this.refreshToken = null; } 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 getUser() { return this.client.get('/api/auth/user', { headers: { Authorization: `Bearer ${this.accessToken}` } }); } async getAuthMethods() { return this.client.get('/api/auth/methods'); } async createCheckoutSession(priceId, successUrl, cancelUrl) { return this.client.post('/api/stripe/checkout-sessions', { mode: 'payment', line_items: [{ price_id: priceId, quantity: 1 }], success_url: successUrl, cancel_url: cancelUrl || successUrl }, { headers: { Authorization: `Bearer ${this.accessToken}` } } ); } async getSubscription(subscriptionId) { const path = subscriptionId ? `/api/stripe/subscription/${encodeURIComponent(subscriptionId)}` : '/api/stripe/subscriptions'; return this.client.get(path, { headers: { Authorization: `Bearer ${this.accessToken}` } }); } async getUsageBalance() { return this.client.get('/api/usage/balance', { headers: { Authorization: `Bearer ${this.accessToken}` } }); } isAuthenticated() { return !!this.accessToken; } getAccessToken() { return this.accessToken; } setAccessToken(token) { this.accessToken = token; } isLocalMode() { return this.isLocalMode; } async health() { return this.client.get('/health'); } async logout() { await this.client.post('/api/auth/logout', {}, { headers: { Authorization: `Bearer ${this.accessToken}` } }); this.accessToken = null; this.refreshToken = null; } 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 recordUsage(feature, amount) { return this.client.post('/api/usage/record', { feature, amount }, { headers: { Authorization: `Bearer ${this.accessToken}` } } ); } async cancelSubscription(subscriptionId, options = {}) { if (!subscriptionId) { throw new Error('subscriptionId is required to cancel a subscription.'); } return this.client.post(`/api/stripe/subscription/${encodeURIComponent(subscriptionId)}/cancel`, options, { headers: { Authorization: `Bearer ${this.accessToken}` } }); } } module.exports = SPAPSClient; module.exports.SPAPSClient = SPAPSClient; module.exports.default = SPAPSClient; module.exports.SPAPS = SPAPSClient; module.exports.SweetPotatoSDK = SPAPSClient; }