@t402/fastify 2.3.0 → 2.3.1

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