spaps
Version:
Sweet Potato Authentication & Payment Service CLI - Zero-config local development with built-in admin middleware and permission utilities
147 lines (124 loc) • 4.43 kB
JavaScript
/**
* 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:3300';
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 createCheckoutSession(priceId, successUrl, cancelUrl) {
return this.client.post('/api/stripe/create-checkout-session',
{ price_id: priceId, success_url: successUrl, cancel_url: cancelUrl },
{ headers: { Authorization: `Bearer ${this.accessToken}` } }
);
}
async getSubscription() {
return this.client.get('/api/stripe/subscription', {
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() {
return this.client.delete('/api/stripe/subscription', {
headers: { Authorization: `Bearer ${this.accessToken}` }
});
}
}
module.exports = SPAPSClient;
module.exports.SPAPSClient = SPAPSClient;
module.exports.default = SPAPSClient;
module.exports.SPAPS = SPAPSClient;
module.exports.SweetPotatoSDK = SPAPSClient;
}