Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/wrapper/mppx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { probe402, wsToHttp } from "./util.js";

export interface MppxClient {
fetch: typeof globalThis.fetch;
rawFetch: typeof globalThis.fetch;
transport: {
setCredential(request: Request, credential: string): RequestInit;
};
createCredential(response: Response): Promise<string>;
}

export async function getPaymentCredentials(wsUrl: string, mppx: MppxClient): Promise<Record<string, string>> {
const response = await probe402(wsUrl);
const response = await probe402(wsUrl, mppx.rawFetch);

const credential = await mppx.createCredential(response);
const signed = mppx.transport.setCredential(new Request(wsToHttp(wsUrl)), credential);
Expand Down
4 changes: 2 additions & 2 deletions src/wrapper/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export function wsToHttp(wsUrl: string): string {
return wsUrl.replace(/^wss:\/\//, "https://").replace(/^ws:\/\//, "http://");
}

export async function probe402(wsUrl: string): Promise<Response> {
export async function probe402(wsUrl: string, fetchFn: typeof fetch = fetch): Promise<Response> {
const httpUrl = wsToHttp(wsUrl);

const response = await fetch(httpUrl);
const response = await fetchFn(httpUrl);
if (response.status !== 402) {
throw new Error(`Expected 402 from ${httpUrl} but got ${response.status}`);
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/wrapper/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe("AgentMailClient environment selection", () => {
const mockMppFetch = vi.fn().mockResolvedValue(new Response());
const mockMppClient = {
fetch: mockMppFetch,
rawFetch: vi.fn().mockResolvedValue(new Response()),
transport: { setCredential: vi.fn() },
createCredential: vi.fn(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/unit/wrapper/WebsocketsClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe("WebsocketsClient wrapper", () => {
describe("with mppx", () => {
const mockMppClient = {
fetch: vi.fn(),
rawFetch: vi.fn(),
transport: { setCredential: vi.fn() },
createCredential: vi.fn(),
};
Expand Down