Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/mpp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { fetchWithMpp } from "../dist/esm/mpp.js";
import { NWCClient } from "@getalby/sdk";

const url = process.env.URL || "https://api.ppq.ai/v1/data/api/exa/answer";

const nostrWalletConnectUrl = process.env.NWC_URL;

if (!nostrWalletConnectUrl) {
throw new Error("Please set a NWC_URL env variable");
}

const nwc = new NWCClient({ nostrWalletConnectUrl });

fetchWithMpp(
url,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: "What is the Lightning Network?" }),
},
{ wallet: nwc },
)
.then((response) => response.json())
.then((data) => {
console.info(JSON.stringify(data, null, 2));
})
.catch((err) => {
console.error("Error:", err.message);
})
.finally(() => nwc.close());
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"require": "./dist/cjs/x402.cjs",
"types": "./dist/types/x402.d.ts"
},
"./mpp": {
"import": "./dist/esm/mpp.js",
"require": "./dist/cjs/mpp.cjs",
"types": "./dist/types/mpp.d.ts"
},
"./lnurl": {
"import": "./dist/esm/lnurl.js",
"require": "./dist/cjs/lnurl.cjs",
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const entries = [
{ name: "402", input: "src/402/index.ts" },
{ name: "l402", input: "src/402/l402/index.ts" },
{ name: "x402", input: "src/402/x402/index.ts" },
{ name: "mpp", input: "src/402/mpp/index.ts" },
{ name: "lnurl", input: "src/lnurl/index.ts" },
{ name: "podcasting2", input: "src/podcasting2/index.ts" },
];
Expand Down
24 changes: 21 additions & 3 deletions src/402/fetch402.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Wallet } from "./utils";
import { handleL402Payment } from "./l402/l402";
import { handleX402Payment } from "./x402/x402";
import { handleMppChargePayment } from "./mpp/mpp";

export const fetch402 = async (
url: string,
Expand All @@ -20,9 +21,26 @@ export const fetch402 = async (

const initResp = await fetch(url, fetchArgs);

const l402Header = initResp.headers.get("www-authenticate");
if (l402Header) {
return handleL402Payment(l402Header, url, fetchArgs, headers, wallet);
const wwwAuthHeader = initResp.headers.get("www-authenticate");
if (wwwAuthHeader) {
if (wwwAuthHeader.trimStart().toLowerCase().startsWith("payment")) {
return handleMppChargePayment(
wwwAuthHeader,
url,
fetchArgs,
headers,
wallet,
);
}
return handleL402Payment(
wwwAuthHeader,
url,
fetchArgs,
headers,
wallet,
store,
HEADER_KEY,
);
}

const x402Header = initResp.headers.get("PAYMENT-REQUIRED");
Expand Down
2 changes: 2 additions & 0 deletions src/402/mpp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./mpp";
export * from "./utils";
Loading
Loading