@solvapay/server 1.0.0-preview.2 → 1.0.0-preview.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 SolvaPay Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -5,6 +5,7 @@ Universal server SDK for Node.js and edge runtimes. Includes API client, paywall
5
5
  **Works in**: Node.js, Vercel Edge Functions, Cloudflare Workers, Deno, Supabase Edge Functions, and more.
6
6
 
7
7
  ## Install
8
+
8
9
  ```bash
9
10
  pnpm add @solvapay/server
10
11
  ```
@@ -16,39 +17,39 @@ pnpm add @solvapay/server
16
17
  The same imports work in **Node.js and edge runtimes**. The correct implementation is automatically selected:
17
18
 
18
19
  ```ts
19
- import { createSolvaPayClient, verifyWebhook } from '@solvapay/server';
20
+ import { createSolvaPayClient, verifyWebhook } from '@solvapay/server'
20
21
 
21
22
  // Works in Node.js, Edge Functions, Cloudflare Workers, Deno, etc.
22
- const apiClient = createSolvaPayClient({
23
- apiKey: process.env.SOLVAPAY_SECRET_KEY!
24
- });
23
+ const apiClient = createSolvaPayClient({
24
+ apiKey: process.env.SOLVAPAY_SECRET_KEY!,
25
+ })
25
26
 
26
27
  // Auto-selects Node crypto or Web Crypto based on runtime
27
- const event = await verifyWebhook({
28
- body,
29
- signature,
30
- secret: process.env.SOLVAPAY_WEBHOOK_SECRET!
31
- });
28
+ const event = await verifyWebhook({
29
+ body,
30
+ signature,
31
+ secret: process.env.SOLVAPAY_WEBHOOK_SECRET!,
32
+ })
32
33
  ```
33
34
 
34
35
  **Edge Runtime Examples:**
35
36
 
36
37
  ```ts
37
38
  // Supabase Edge Function
38
- import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
39
- import { createSolvaPayClient, verifyWebhook } from 'https://esm.sh/@solvapay/server@latest';
39
+ import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
40
+ import { createSolvaPayClient, verifyWebhook } from 'https://esm.sh/@solvapay/server@latest'
40
41
 
41
42
  const solvapay = createSolvaPayClient({
42
43
  apiKey: Deno.env.get('SOLVAPAY_SECRET_KEY')!,
43
- });
44
+ })
44
45
 
45
46
  // Vercel Edge Function
46
- import { createSolvaPayClient } from '@solvapay/server';
47
+ import { createSolvaPayClient } from '@solvapay/server'
47
48
 
48
- export const runtime = 'edge';
49
+ export const runtime = 'edge'
49
50
  const solvapay = createSolvaPayClient({
50
51
  apiKey: process.env.SOLVAPAY_SECRET_KEY!,
51
- });
52
+ })
52
53
  ```
53
54
 
54
55
  ### Paywall Protection
@@ -59,8 +60,8 @@ Use the unified payable API to protect your endpoints and functions with usage l
59
60
  import { createSolvaPay } from '@solvapay/server';
60
61
 
61
62
  // Create SolvaPay instance with your API key
62
- const solvaPay = createSolvaPay({
63
- apiKey: process.env.SOLVAPAY_SECRET_KEY!
63
+ const solvaPay = createSolvaPay({
64
+ apiKey: process.env.SOLVAPAY_SECRET_KEY!
64
65
  });
65
66
 
66
67
  // Create a payable with your agent configuration
@@ -88,11 +89,41 @@ const protectedHandler = await payable.function(async (args) => {
88
89
  return { result: 'success' };
89
90
  });
90
91
 
91
- const result = await protectedHandler({
92
- auth: { customer_ref: 'customer_123' }
92
+ const result = await protectedHandler({
93
+ auth: { customer_ref: 'customer_123' }
93
94
  });
94
95
  ```
95
96
 
97
+ ### Authentication Integration
98
+
99
+ You can integrate authentication adapters from `@solvapay/auth` with the `getCustomerRef` option:
100
+
101
+ ```ts
102
+ import { createSolvaPay } from '@solvapay/server'
103
+ import { SupabaseAuthAdapter } from '@solvapay/auth/supabase'
104
+
105
+ const auth = new SupabaseAuthAdapter({
106
+ jwtSecret: process.env.SUPABASE_JWT_SECRET!,
107
+ })
108
+
109
+ const solvaPay = createSolvaPay({ apiKey: process.env.SOLVAPAY_SECRET_KEY! })
110
+
111
+ // Use with Next.js adapter
112
+ export const POST = solvaPay.payable({ agent: 'my-api' }).next(
113
+ async args => {
114
+ return { result: 'success' }
115
+ },
116
+ {
117
+ getCustomerRef: async req => {
118
+ const userId = await auth.getUserIdFromRequest(req)
119
+ return userId ?? 'anonymous'
120
+ },
121
+ },
122
+ )
123
+ ```
124
+
125
+ This automatically extracts the user ID from authentication tokens and uses it as the customer reference for paywall checks.
126
+
96
127
  ### When to Use Each Adapter
97
128
 
98
129
  Choose the adapter based on your context:
@@ -145,15 +176,17 @@ This will fetch the OpenAPI spec from `http://localhost:3001/v1/openapi.json` an
145
176
  ### Using Generated Types
146
177
 
147
178
  ```typescript
148
- import type { paths, components } from './types/generated';
179
+ import type { paths, components } from './types/generated'
149
180
 
150
181
  // Use path operation types
151
- type CheckLimitsRequest = paths['/v1/sdk/limits']['post']['requestBody']['content']['application/json'];
152
- type CheckLimitsResponse = paths['/v1/sdk/limits']['post']['responses']['200']['content']['application/json'];
182
+ type CheckLimitsRequest =
183
+ paths['/v1/sdk/limits']['post']['requestBody']['content']['application/json']
184
+ type CheckLimitsResponse =
185
+ paths['/v1/sdk/limits']['post']['responses']['200']['content']['application/json']
153
186
 
154
187
  // Use component schemas
155
- type Agent = components['schemas']['Agent'];
156
- type Plan = components['schemas']['Plan'];
188
+ type Agent = components['schemas']['Agent']
189
+ type Plan = components['schemas']['Plan']
157
190
  ```
158
191
 
159
192
  **Note:** The generated types complement the existing hand-written types in `src/types.ts`. Run `pnpm generate:types` whenever the backend API changes to keep types in sync.
@@ -184,6 +217,7 @@ pnpm test:watch
184
217
  ### Unit Tests
185
218
 
186
219
  Unit tests (`__tests__/paywall.test.ts`) use a mock API client and test:
220
+
187
221
  - Paywall protection logic
188
222
  - Handler creation (HTTP, Next.js, MCP)
189
223
  - Error handling
@@ -195,6 +229,7 @@ Unit tests (`__tests__/paywall.test.ts`) use a mock API client and test:
195
229
  ### Integration Tests
196
230
 
197
231
  Integration tests (`__tests__/backend.integration.test.ts`) connect to a real SolvaPay backend and test:
232
+
198
233
  - SDK API methods with real responses
199
234
  - Actual limit enforcement
200
235
  - Real usage tracking
@@ -221,6 +256,7 @@ USE_REAL_BACKEND=true SOLVAPAY_SECRET_KEY=your_key pnpm test:integration
221
256
  ### Payment Integration Tests (Stripe)
222
257
 
223
258
  Payment tests (`__tests__/payment-stripe.integration.test.ts`) verify the complete payment flow with Stripe:
259
+
224
260
  - Creating payment intents
225
261
  - Confirming payments with test cards
226
262
  - Webhook processing (optional)
@@ -240,24 +276,27 @@ export SOLVAPAY_API_BASE_URL=http://localhost:3001
240
276
  The E2E webhook test is skipped by default because it requires Stripe webhooks to be forwarded to your local backend. To enable webhook testing:
241
277
 
242
278
  1. **Install Stripe CLI:**
279
+
243
280
  ```bash
244
281
  # macOS
245
282
  brew install stripe/stripe-cli/stripe
246
-
283
+
247
284
  # Linux / Windows - see https://stripe.com/docs/stripe-cli
248
285
  ```
249
286
 
250
287
  2. **Login to Stripe:**
288
+
251
289
  ```bash
252
290
  stripe login
253
291
  ```
254
292
 
255
293
  3. **Forward webhooks to your local backend:**
294
+
256
295
  ```bash
257
296
  # Terminal 1: Start your backend
258
297
  cd path/to/solvapay-backend
259
298
  pnpm dev
260
-
299
+
261
300
  # Terminal 2: Forward Stripe webhooks
262
301
  stripe listen --forward-to localhost:3001/webhooks/stripe
263
302
  ```
@@ -324,4 +363,4 @@ By default, both are **disabled** to keep test output clean and readable. Enable
324
363
  run: pnpm test:integration
325
364
  ```
326
365
 
327
- More: docs/architecture.md
366
+ More: [docs/guides/architecture.md](../../docs/guides/architecture.md)
@@ -0,0 +1,9 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ export {
8
+ __export
9
+ };