mppx 0.3.14 → 0.3.15
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/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.d.ts +26 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1478 -915
- package/dist/cli.js.map +1 -1
- package/dist/client/Mppx.d.ts +2 -0
- package/dist/client/Mppx.d.ts.map +1 -1
- package/dist/client/Mppx.js +2 -0
- package/dist/client/Mppx.js.map +1 -1
- package/package.json +4 -4
- package/src/bin.ts +4 -0
- package/src/cli.test.ts +180 -252
- package/src/cli.ts +1085 -485
- package/src/client/Mppx.test-d.ts +9 -0
- package/src/client/Mppx.test.ts +78 -0
- package/src/client/Mppx.ts +5 -0
|
@@ -28,6 +28,15 @@ describe('Mppx', () => {
|
|
|
28
28
|
expectTypeOf(mppx.createCredential).toBeFunction()
|
|
29
29
|
expectTypeOf(mppx.createCredential).returns.toMatchTypeOf<Promise<string>>()
|
|
30
30
|
})
|
|
31
|
+
|
|
32
|
+
test('has rawFetch with standard fetch signature', () => {
|
|
33
|
+
const method = charge({
|
|
34
|
+
account: {} as Account,
|
|
35
|
+
})
|
|
36
|
+
const mppx = Mppx.create({ methods: [method] })
|
|
37
|
+
|
|
38
|
+
expectTypeOf(mppx.rawFetch).toEqualTypeOf<typeof globalThis.fetch>()
|
|
39
|
+
})
|
|
31
40
|
})
|
|
32
41
|
|
|
33
42
|
describe('create.Config', () => {
|
package/src/client/Mppx.test.ts
CHANGED
|
@@ -28,6 +28,7 @@ describe('Mppx.create', () => {
|
|
|
28
28
|
expect(mppx.transport.name).toBe('http')
|
|
29
29
|
expect(typeof mppx.createCredential).toBe('function')
|
|
30
30
|
expect(typeof mppx.fetch).toBe('function')
|
|
31
|
+
expect(typeof mppx.rawFetch).toBe('function')
|
|
31
32
|
})
|
|
32
33
|
|
|
33
34
|
test('behavior: with mcp transport', () => {
|
|
@@ -471,3 +472,80 @@ describe('restore', () => {
|
|
|
471
472
|
expect(globalThis.fetch).toBe(originalFetch)
|
|
472
473
|
})
|
|
473
474
|
})
|
|
475
|
+
|
|
476
|
+
describe('rawFetch', () => {
|
|
477
|
+
test('default: returns the original fetch when polyfill is enabled', () => {
|
|
478
|
+
const originalFetch = globalThis.fetch
|
|
479
|
+
|
|
480
|
+
const mppx = Mppx.create({
|
|
481
|
+
methods: [
|
|
482
|
+
tempo({
|
|
483
|
+
account: accounts[1],
|
|
484
|
+
getClient: () => client,
|
|
485
|
+
}),
|
|
486
|
+
],
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
expect(globalThis.fetch).not.toBe(originalFetch)
|
|
490
|
+
expect(mppx.rawFetch).toBe(originalFetch)
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
test('behavior: returns the original fetch when polyfill is disabled', () => {
|
|
494
|
+
const originalFetch = globalThis.fetch
|
|
495
|
+
|
|
496
|
+
const mppx = Mppx.create({
|
|
497
|
+
polyfill: false,
|
|
498
|
+
methods: [
|
|
499
|
+
tempo({
|
|
500
|
+
account: accounts[1],
|
|
501
|
+
getClient: () => client,
|
|
502
|
+
}),
|
|
503
|
+
],
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
expect(mppx.rawFetch).toBe(originalFetch)
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
test('behavior: returns custom fetch when provided', () => {
|
|
510
|
+
const customFetch = async () => new Response('custom')
|
|
511
|
+
|
|
512
|
+
const mppx = Mppx.create({
|
|
513
|
+
polyfill: false,
|
|
514
|
+
fetch: customFetch as typeof globalThis.fetch,
|
|
515
|
+
methods: [
|
|
516
|
+
tempo({
|
|
517
|
+
account: accounts[1],
|
|
518
|
+
getClient: () => client,
|
|
519
|
+
}),
|
|
520
|
+
],
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
expect(mppx.rawFetch).toBe(customFetch)
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
test('behavior: rawFetch does not intercept 402 responses', async () => {
|
|
527
|
+
const mppx = Mppx.create({
|
|
528
|
+
methods: [
|
|
529
|
+
tempo({
|
|
530
|
+
account: accounts[1],
|
|
531
|
+
getClient: () => client,
|
|
532
|
+
}),
|
|
533
|
+
],
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
const httpServer = await Http.createServer(async (req, res) => {
|
|
537
|
+
const result = await Mppx_server.toNodeListener(
|
|
538
|
+
server.charge({
|
|
539
|
+
amount: '1',
|
|
540
|
+
}),
|
|
541
|
+
)(req, res)
|
|
542
|
+
if (result.status === 402) return
|
|
543
|
+
res.end('OK')
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
const response = await mppx.rawFetch(httpServer.url)
|
|
547
|
+
expect(response.status).toBe(402)
|
|
548
|
+
|
|
549
|
+
httpServer.close()
|
|
550
|
+
})
|
|
551
|
+
})
|
package/src/client/Mppx.ts
CHANGED
|
@@ -15,6 +15,8 @@ export type Mppx<
|
|
|
15
15
|
> = {
|
|
16
16
|
/** Payment-aware fetch function that automatically handles 402 responses. */
|
|
17
17
|
fetch: Fetch.from.Fetch<FlattenMethods<methods>>
|
|
18
|
+
/** The original, unwrapped fetch function (pre-polyfill). Useful when you need to make requests that should not be intercepted (e.g. 402 probes for websocket auth). */
|
|
19
|
+
rawFetch: typeof globalThis.fetch
|
|
18
20
|
/** Methods to configure. */
|
|
19
21
|
methods: FlattenMethods<methods>
|
|
20
22
|
/** The transport used. */
|
|
@@ -56,6 +58,8 @@ export function create<
|
|
|
56
58
|
>(config: create.Config<methods, transport>): Mppx<methods, transport> {
|
|
57
59
|
const { onChallenge, polyfill = true, transport = Transport.http() as transport } = config
|
|
58
60
|
|
|
61
|
+
const rawFetch = config.fetch ?? globalThis.fetch
|
|
62
|
+
|
|
59
63
|
const methods = config.methods.flat() as unknown as FlattenMethods<methods>
|
|
60
64
|
|
|
61
65
|
const resolvedOnChallenge = onChallenge as Fetch.from.Config<
|
|
@@ -71,6 +75,7 @@ export function create<
|
|
|
71
75
|
if (polyfill) Fetch.polyfill(config_fetch)
|
|
72
76
|
return {
|
|
73
77
|
fetch,
|
|
78
|
+
rawFetch,
|
|
74
79
|
methods,
|
|
75
80
|
transport,
|
|
76
81
|
async createCredential(response: Transport.ResponseOf<transport>, context?: unknown) {
|