@payai/x402-fetch-starter 0.1.0 → 0.1.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.
- package/README.md +30 -12
- package/bin/create.js +36 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,38 +1,56 @@
|
|
|
1
|
-
|
|
1
|
+
# x402 Fetch Starter
|
|
2
2
|
|
|
3
|
-
Starter
|
|
3
|
+
Starter for running an x402 fetch client.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
You can find the upstream example at [coinbase/x402 → examples/typescript/clients/fetch](https://github.com/coinbase/x402/tree/main/examples/typescript/clients/fetch).
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
7
8
|
|
|
8
9
|
### Requirements
|
|
9
10
|
|
|
10
11
|
- **Node.js**: 18 or newer
|
|
11
|
-
- **GitHub Actions** enabled in your fork/repo
|
|
12
12
|
|
|
13
|
-
### Create a new app
|
|
13
|
+
### Create a new app using the template
|
|
14
14
|
|
|
15
|
-
Use
|
|
15
|
+
Use your preferred package manager to scaffold:
|
|
16
16
|
|
|
17
|
+
#### npm (npx)
|
|
17
18
|
```bash
|
|
18
|
-
# npm
|
|
19
19
|
npm exec @payai/x402-fetch-starter -- my-x402-app
|
|
20
|
+
```
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
#### pnpm
|
|
23
|
+
```bash
|
|
22
24
|
pnpm dlx @payai/x402-fetch-starter my-x402-app
|
|
25
|
+
```
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
#### bun
|
|
28
|
+
```bash
|
|
25
29
|
bunx @payai/x402-fetch-starter my-x402-app
|
|
26
30
|
```
|
|
27
31
|
|
|
28
32
|
Then inside your new app:
|
|
29
33
|
|
|
30
34
|
```bash
|
|
31
|
-
cp .env-local .env
|
|
32
|
-
npm i
|
|
33
35
|
npm run dev
|
|
34
36
|
```
|
|
35
37
|
|
|
38
|
+
### How the created client example works
|
|
39
|
+
|
|
40
|
+
When you run the generated app, `index.ts` will:
|
|
41
|
+
|
|
42
|
+
- Load environment variables from `.env` (for example: `RESOURCE_SERVER_URL`, `ENDPOINT_PATH`, `PRIVATE_KEY`).
|
|
43
|
+
- Create a wallet client and wrap the native `fetch` with x402 using `wrapFetchWithPayment`.
|
|
44
|
+
- Call your configured endpoint at `${RESOURCE_SERVER_URL}${ENDPOINT_PATH}`.
|
|
45
|
+
- Log two things to the console:
|
|
46
|
+
- The parsed JSON response body
|
|
47
|
+
- The decoded `x-payment-response` headers (useful for inspecting payment receipts/metadata)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## The Starter Itself
|
|
51
|
+
|
|
52
|
+
Below are notes on the starter itself, which creates the example that devs use to get started.
|
|
53
|
+
|
|
36
54
|
### How sync works
|
|
37
55
|
|
|
38
56
|
- Workflow: `.github/workflows/sync.yml`
|
package/bin/create.js
CHANGED
|
@@ -29,6 +29,35 @@ if (fs.existsSync(rootPkgPath)) {
|
|
|
29
29
|
fs.writeFileSync(rootPkgPath, JSON.stringify(rootPkg, null, 2));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// perform post-scaffold steps for convenience
|
|
33
|
+
console.log(`\nSetting up your project in: ${targetDir}`);
|
|
34
|
+
|
|
35
|
+
// 1) Copy environment template
|
|
36
|
+
try {
|
|
37
|
+
const envLocal = path.join(targetDir, ".env-local");
|
|
38
|
+
const envExample = path.join(targetDir, ".env.example");
|
|
39
|
+
const envTarget = path.join(targetDir, ".env");
|
|
40
|
+
if (fs.existsSync(envLocal)) {
|
|
41
|
+
console.log("- Creating .env from .env-local");
|
|
42
|
+
fs.copyFileSync(envLocal, envTarget);
|
|
43
|
+
} else if (fs.existsSync(envExample)) {
|
|
44
|
+
console.log("- Creating .env from .env.example");
|
|
45
|
+
fs.copyFileSync(envExample, envTarget);
|
|
46
|
+
} else {
|
|
47
|
+
console.log("- No .env-local or .env.example found; please create .env manually");
|
|
48
|
+
}
|
|
49
|
+
} catch (err) {
|
|
50
|
+
console.warn("- Skipped env setup:", err instanceof Error ? err.message : String(err));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 2) Install dependencies
|
|
54
|
+
try {
|
|
55
|
+
console.log("- Installing dependencies (npm install)...");
|
|
56
|
+
execSync("npm install", { stdio: "inherit", cwd: targetDir });
|
|
57
|
+
} catch (err) {
|
|
58
|
+
console.warn("- npm install failed, you may run it manually:", err instanceof Error ? err.message : String(err));
|
|
59
|
+
}
|
|
60
|
+
|
|
32
61
|
// init git
|
|
33
62
|
try {
|
|
34
63
|
execSync("git init", { stdio: "ignore", cwd: targetDir });
|
|
@@ -36,13 +65,17 @@ try {
|
|
|
36
65
|
|
|
37
66
|
// print next steps
|
|
38
67
|
console.log(`
|
|
39
|
-
|
|
68
|
+
Successfully created ${projectName}!
|
|
69
|
+
|
|
70
|
+
You now have a starter example for Fetch wrapped with x402 payment handling.
|
|
40
71
|
|
|
41
72
|
Next steps:
|
|
42
73
|
cd ${projectName}
|
|
43
|
-
|
|
44
|
-
npm i
|
|
74
|
+
# edit values inside .env
|
|
45
75
|
npm run dev
|
|
46
76
|
|
|
77
|
+
After install, open the main file (e.g., index.ts) to see how the example works.
|
|
78
|
+
Docs: https://docs.payai.network
|
|
79
|
+
|
|
47
80
|
Happy building 🏗️
|
|
48
81
|
`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payai/x402-fetch-starter",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Create an x402 Fetch client without cloning the monorepo.",
|
|
6
6
|
"type": "module",
|
|
@@ -35,4 +35,4 @@
|
|
|
35
35
|
"config": {
|
|
36
36
|
"x402FetchVersion": "0.5.1"
|
|
37
37
|
}
|
|
38
|
-
}
|
|
38
|
+
}
|