@t402/fastify 2.3.0 → 2.4.0
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/README.md +161 -0
- package/package.json +6 -6
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# @t402/fastify
|
|
2
|
+
|
|
3
|
+
Fastify plugin integration for the t402 Payment Protocol. This package provides a simple middleware function for adding t402 payment requirements to your Fastify applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @t402/fastify
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import Fastify from 'fastify'
|
|
15
|
+
import { paymentMiddleware, t402ResourceServer } from '@t402/fastify'
|
|
16
|
+
import { ExactEvmScheme } from '@t402/evm/exact/server'
|
|
17
|
+
import { HTTPFacilitatorClient } from '@t402/core/server'
|
|
18
|
+
|
|
19
|
+
const app = Fastify()
|
|
20
|
+
|
|
21
|
+
const facilitatorClient = new HTTPFacilitatorClient({ url: 'https://facilitator.t402.io' })
|
|
22
|
+
const resourceServer = new t402ResourceServer(facilitatorClient).register(
|
|
23
|
+
'eip155:84532',
|
|
24
|
+
new ExactEvmScheme(),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
// Apply the payment middleware with your configuration
|
|
28
|
+
app.addHook(
|
|
29
|
+
'onRequest',
|
|
30
|
+
paymentMiddleware(
|
|
31
|
+
{
|
|
32
|
+
'GET /protected-route': {
|
|
33
|
+
accepts: {
|
|
34
|
+
scheme: 'exact',
|
|
35
|
+
price: '$0.10',
|
|
36
|
+
network: 'eip155:84532',
|
|
37
|
+
payTo: '0xYourAddress',
|
|
38
|
+
},
|
|
39
|
+
description: 'Access to premium content',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
resourceServer,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
// Implement your protected route
|
|
47
|
+
app.get('/protected-route', async (request, reply) => {
|
|
48
|
+
return { message: 'This content is behind a paywall' }
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
app.listen({ port: 3000 })
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
The `paymentMiddleware` function accepts the following parameters:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
paymentMiddleware(
|
|
60
|
+
routes: RoutesConfig,
|
|
61
|
+
server: t402ResourceServer,
|
|
62
|
+
paywallConfig?: PaywallConfig,
|
|
63
|
+
paywall?: PaywallProvider,
|
|
64
|
+
syncFacilitatorOnStart?: boolean
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Parameters
|
|
69
|
+
|
|
70
|
+
1. **`routes`** (required): Route configurations for protected endpoints
|
|
71
|
+
2. **`server`** (required): Pre-configured t402ResourceServer instance
|
|
72
|
+
3. **`paywallConfig`** (optional): Configuration for the built-in paywall UI
|
|
73
|
+
4. **`paywall`** (optional): Custom paywall provider
|
|
74
|
+
5. **`syncFacilitatorOnStart`** (optional): Whether to sync with facilitator on startup (defaults to true)
|
|
75
|
+
|
|
76
|
+
## Route Configuration
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const routes: RoutesConfig = {
|
|
80
|
+
'GET /api/protected': {
|
|
81
|
+
accepts: {
|
|
82
|
+
scheme: 'exact',
|
|
83
|
+
price: '$0.10',
|
|
84
|
+
network: 'eip155:84532',
|
|
85
|
+
payTo: '0xYourAddress',
|
|
86
|
+
maxTimeoutSeconds: 60,
|
|
87
|
+
},
|
|
88
|
+
description: 'Premium API access',
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Multiple Payment Networks
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
app.addHook(
|
|
97
|
+
'onRequest',
|
|
98
|
+
paymentMiddleware(
|
|
99
|
+
{
|
|
100
|
+
'GET /api/data': {
|
|
101
|
+
accepts: [
|
|
102
|
+
{
|
|
103
|
+
scheme: 'exact',
|
|
104
|
+
price: '$0.10',
|
|
105
|
+
network: 'eip155:8453',
|
|
106
|
+
payTo: evmAddress,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
scheme: 'exact',
|
|
110
|
+
price: '$0.10',
|
|
111
|
+
network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
112
|
+
payTo: svmAddress,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
description: 'Data API access',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
new t402ResourceServer(facilitatorClient)
|
|
119
|
+
.register('eip155:8453', new ExactEvmScheme())
|
|
120
|
+
.register('solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', new ExactSvmScheme()),
|
|
121
|
+
),
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Paywall Configuration
|
|
126
|
+
|
|
127
|
+
The middleware automatically displays a paywall UI when browsers request protected endpoints.
|
|
128
|
+
|
|
129
|
+
Install the optional `@t402/paywall` package for a complete wallet connection and payment UI:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
pnpm add @t402/paywall
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
const paywallConfig: PaywallConfig = {
|
|
137
|
+
appName: 'Your App Name',
|
|
138
|
+
appLogo: '/path/to/logo.svg',
|
|
139
|
+
testnet: true,
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
app.addHook('onRequest', paymentMiddleware(routes, resourceServer, paywallConfig))
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Peer Dependencies
|
|
146
|
+
|
|
147
|
+
- `fastify` ^4.0.0 || ^5.0.0
|
|
148
|
+
|
|
149
|
+
## Development
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pnpm build # Build the package
|
|
153
|
+
pnpm test # Run unit tests
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Related Packages
|
|
157
|
+
|
|
158
|
+
- `@t402/core` - Core protocol types and client
|
|
159
|
+
- `@t402/express` - Express.js middleware
|
|
160
|
+
- `@t402/hono` - Hono middleware
|
|
161
|
+
- `@t402/paywall` - Universal paywall UI component
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t402/fastify",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"main": "./dist/cjs/index.js",
|
|
5
5
|
"module": "./dist/esm/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"description": "t402 Payment Protocol middleware for Fastify",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@eslint/js": "^9.24.0",
|
|
22
|
-
"@types/node": "^
|
|
22
|
+
"@types/node": "^25.2.0",
|
|
23
23
|
"@typescript-eslint/eslint-plugin": "^8.29.1",
|
|
24
24
|
"@typescript-eslint/parser": "^8.29.1",
|
|
25
25
|
"eslint": "^9.24.0",
|
|
26
26
|
"eslint-plugin-import": "^2.31.0",
|
|
27
|
-
"eslint-plugin-jsdoc": "^
|
|
27
|
+
"eslint-plugin-jsdoc": "^62.5.0",
|
|
28
28
|
"eslint-plugin-prettier": "^5.2.6",
|
|
29
29
|
"fastify": "^5.7.0",
|
|
30
30
|
"glob": "^13.0.0",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"zod": "^3.24.2",
|
|
41
|
-
"@t402/
|
|
42
|
-
"@t402/
|
|
41
|
+
"@t402/extensions": "2.4.0",
|
|
42
|
+
"@t402/core": "2.4.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"fastify": "^4.0.0 || ^5.0.0",
|
|
46
|
-
"@t402/paywall": "2.
|
|
46
|
+
"@t402/paywall": "2.4.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependenciesMeta": {
|
|
49
49
|
"@t402/paywall": {
|