UNPKG

spaps

Version:

Sweet Potato Authentication & Payment Service CLI - Zero-config local development and project scaffolding

273 lines (246 loc) 7.01 kB
/** * SPAPS AI Agent Helper * Provides AI-friendly outputs and quick commands */ const chalk = require('chalk'); function getQuickStartInstructions(port = 3300) { return { success: true, instructions: { step1: { description: "Install SDK", command: "npm install spaps-sdk", verify: "npm list spaps-sdk" }, step2: { description: "Create test file", filename: "test-spaps.js", content: `const { SPAPSClient } = require('spaps-sdk'); async function test() { const spaps = new SPAPSClient({ apiUrl: 'http://localhost:${port}' }); // Test login const { data } = await spaps.login('test@example.com', 'password'); console.log('✅ Login successful:', data.user.email); // Test authenticated request const user = await spaps.getUser(); console.log('✅ Got user:', user.data.email); return { success: true, user: user.data }; } test().then(console.log).catch(console.error);` }, step3: { description: "Run test", command: "node test-spaps.js", expected_output: { success: true, user: { id: "local-user-123", email: "test@example.com" } } } }, endpoints: [ { method: "POST", path: "/api/auth/login", body: { email: "string", password: "string" }, response: { access_token: "string", refresh_token: "string", user: "object" } }, { method: "POST", path: "/api/auth/register", body: { email: "string", password: "string" }, response: { access_token: "string", refresh_token: "string", user: "object" } }, { method: "GET", path: "/api/auth/user", headers: { Authorization: "Bearer TOKEN" }, response: { id: "string", email: "string", role: "string" } }, { method: "POST", path: "/api/stripe/create-checkout-session", body: { price_id: "string", success_url: "string" }, response: { sessionId: "string", url: "string" } }, { method: "GET", path: "/api/usage/balance", headers: { Authorization: "Bearer TOKEN" }, response: { balance: "number", currency: "string" } } ], test_commands: { health_check: `curl http://localhost:${port}/health`, login: `curl -X POST http://localhost:${port}/api/auth/login -H "Content-Type: application/json" -d '{"email":"test@example.com","password":"password"}'`, with_sdk: `node -e "const {SPAPSClient}=require('spaps-sdk');const s=new SPAPSClient();s.login('test@example.com','password').then(r=>console.log(JSON.stringify(r.data))).catch(console.error)"` } }; } function getServerStatus(port = 3300) { const http = require('http'); return new Promise((resolve) => { const options = { hostname: 'localhost', port: port, path: '/health', method: 'GET', timeout: 1000 }; const req = http.request(options, (res) => { let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => { try { const parsed = JSON.parse(data); resolve({ running: true, port: port, health: parsed, url: `http://localhost:${port}`, docs: `http://localhost:${port}/docs` }); } catch { resolve({ running: true, port: port, error: 'Invalid response' }); } }); }); req.on('error', () => { resolve({ running: false, port: port, message: 'Server not running', start_command: `npx spaps local --port ${port}` }); }); req.on('timeout', () => { req.destroy(); resolve({ running: false, port: port, message: 'Server timeout', start_command: `npx spaps local --port ${port}` }); }); req.end(); }); } async function runQuickTest(port = 3300) { const results = []; // Check server const status = await getServerStatus(port); results.push({ test: 'server_status', success: status.running, details: status }); if (!status.running) { return { success: false, message: 'Server not running', fix: `npx spaps local --port ${port}`, results }; } // Try HTTP request try { const http = require('http'); const loginResult = await new Promise((resolve, reject) => { const postData = JSON.stringify({ email: 'test@example.com', password: 'password' }); const options = { hostname: 'localhost', port: port, path: '/api/auth/login', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } }; const req = http.request(options, (res) => { let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => { try { resolve(JSON.parse(data)); } catch { reject(new Error('Invalid JSON response')); } }); }); req.on('error', reject); req.write(postData); req.end(); }); results.push({ test: 'login_endpoint', success: true, response: loginResult }); } catch (error) { results.push({ test: 'login_endpoint', success: false, error: error.message }); } // Check SDK availability try { require.resolve('spaps-sdk'); results.push({ test: 'sdk_installed', success: true, message: 'spaps-sdk is installed' }); // Try SDK login try { const { SPAPSClient } = require('spaps-sdk'); const spaps = new SPAPSClient({ apiUrl: `http://localhost:${port}` }); const { data } = await spaps.login('test@example.com', 'password'); results.push({ test: 'sdk_login', success: true, user: data.user }); } catch (error) { results.push({ test: 'sdk_login', success: false, error: error.message }); } } catch { results.push({ test: 'sdk_installed', success: false, message: 'spaps-sdk not installed', fix: 'npm install spaps-sdk' }); } const allSuccess = results.every(r => r.success); return { success: allSuccess, summary: `${results.filter(r => r.success).length}/${results.length} tests passed`, results, next_steps: allSuccess ? [ 'Server is running and SDK is working', 'You can now use SPAPS in your application', 'See docs at http://localhost:' + port + '/docs' ] : [ 'Fix the failing tests above', 'Run: npx spaps test --json to retry' ] }; } module.exports = { getQuickStartInstructions, getServerStatus, runQuickTest };