feat: add Chartmetric proxy endpoint with credit deduction

SW
8mo ago
+349-0

Summary

  • Adds GET/POST /api/chartmetric/[...path] proxy that forwards requests to Chartmetric
  • Authenticates callers via validateAuthContext (API key or Bearer token)
  • Deducts 5 credits per call via deductCredits
  • Exchanges server-side CHARTMETRIC_REFRESH_TOKEN for short-lived access token (key never exposed to clients)
  • Adds lib/chartmetric/getChartmetricToken.ts and lib/chartmetric/proxyChartmetricRequest.ts
  • 5 vitest tests covering 401, 402, 500, GET proxy, and POST proxy paths

Test plan

  • pnpm test lib/chartmetric — all 5 tests pass
  • CHARTMETRIC_REFRESH_TOKEN env var set in Vercel/production
  • Proxy call deducts 5 credits from caller account

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a new API endpoint that forwards requests to an external service with dynamic (no-cache) behavior
    • Implements per-request credit deduction to enforce usage limits
    • Performs secure server-side token retrieval for upstream authentication
    • Returns CORS-compatible proxied responses with robust error handling and status propagation