lightning-toll 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 +5 -1
- package/package.json +1 -1
- package/src/client/index.js +19 -9
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# ⚡ lightning-toll
|
|
2
2
|
|
|
3
|
-
**
|
|
3
|
+
**Pay before accessing this endpoint.**
|
|
4
|
+
|
|
5
|
+
You can't get the data without paying. lightning-toll is the gate — drop-in Express middleware that puts any API behind a Lightning paywall. No API keys to manage, no billing system, no Stripe. Send a request, get a 402 with an invoice, pay it, retry with the preimage, get your data. Implements the [L402 protocol](https://docs.lightning.engineering/the-lightning-network/l402) with proper macaroon credentials.
|
|
6
|
+
|
|
7
|
+
Part of the constraint chain: [agent-discovery](https://github.com/jeletor/agent-discovery) (find) → [ai-wot](https://github.com/jeletor/ai-wot) (verify) → [lightning-agent](https://github.com/jeletor/lightning-agent) (pay) → **lightning-toll** (gate).
|
|
4
8
|
|
|
5
9
|
## Installation
|
|
6
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lightning-toll",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Drop-in Express middleware for Lightning-gated API endpoints. L402 protocol, macaroons, auto-pay client — monetize any API with Bitcoin Lightning.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"exports": {
|
package/src/client/index.js
CHANGED
|
@@ -99,16 +99,26 @@ class TollClient {
|
|
|
99
99
|
/**
|
|
100
100
|
* Simple one-shot toll fetch.
|
|
101
101
|
*
|
|
102
|
+
* Supports two calling styles:
|
|
103
|
+
* tollFetch(url, { wallet, maxSats, method, body, headers }) — single opts
|
|
104
|
+
* tollFetch(url, fetchOpts, { wallet, maxSats }) — separate fetch + pay opts
|
|
105
|
+
*
|
|
102
106
|
* @param {string} url - URL to fetch
|
|
103
|
-
* @param {object} [
|
|
104
|
-
* @param {
|
|
105
|
-
* @param {number} [opts.maxSats=50] - Max sats to pay
|
|
106
|
-
* @param {object} [opts.headers] - Additional headers
|
|
107
|
-
* @param {string} [opts.method] - HTTP method
|
|
108
|
-
* @param {*} [opts.body] - Request body
|
|
107
|
+
* @param {object} [optsOrFetchOpts] - Combined options, or standard fetch options
|
|
108
|
+
* @param {object} [payOpts] - Pay options (if using 3-arg form)
|
|
109
109
|
* @returns {Promise<Response>}
|
|
110
110
|
*/
|
|
111
|
-
async function tollFetch(url,
|
|
111
|
+
async function tollFetch(url, optsOrFetchOpts = {}, payOpts) {
|
|
112
|
+
// Support both 2-arg and 3-arg calling styles
|
|
113
|
+
let opts;
|
|
114
|
+
if (payOpts && typeof payOpts === 'object') {
|
|
115
|
+
// 3-arg: tollFetch(url, fetchOpts, payOpts)
|
|
116
|
+
opts = { ...optsOrFetchOpts, ...payOpts };
|
|
117
|
+
} else {
|
|
118
|
+
// 2-arg: tollFetch(url, opts)
|
|
119
|
+
opts = optsOrFetchOpts;
|
|
120
|
+
}
|
|
121
|
+
|
|
112
122
|
if (!opts.wallet) {
|
|
113
123
|
throw new Error('tollFetch: wallet is required');
|
|
114
124
|
}
|
|
@@ -125,14 +135,14 @@ async function tollFetch(url, opts = {}) {
|
|
|
125
135
|
if (opts.body) fetchOpts.body = opts.body;
|
|
126
136
|
if (opts.headers) fetchOpts.headers = opts.headers;
|
|
127
137
|
|
|
128
|
-
const
|
|
138
|
+
const paymentOpts = {
|
|
129
139
|
wallet,
|
|
130
140
|
maxSats: opts.maxSats || 50,
|
|
131
141
|
autoRetry: true,
|
|
132
142
|
headers: opts.headers || {}
|
|
133
143
|
};
|
|
134
144
|
|
|
135
|
-
return autoPay(url, fetchOpts,
|
|
145
|
+
return autoPay(url, fetchOpts, paymentOpts);
|
|
136
146
|
}
|
|
137
147
|
|
|
138
148
|
module.exports = {
|