mppx 0.3.11 → 0.3.13
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/client/Mppx.d.ts +1 -1
- package/dist/client/Mppx.d.ts.map +1 -1
- package/dist/client/internal/Fetch.d.ts +1 -1
- package/dist/client/internal/Fetch.d.ts.map +1 -1
- package/dist/client/internal/Fetch.js +76 -11
- package/dist/client/internal/Fetch.js.map +1 -1
- package/dist/internal/constantTimeEqual.d.ts.map +1 -1
- package/dist/internal/constantTimeEqual.js +7 -4
- package/dist/internal/constantTimeEqual.js.map +1 -1
- package/dist/tempo/client/Charge.d.ts +10 -0
- package/dist/tempo/client/Charge.d.ts.map +1 -1
- package/dist/tempo/client/Charge.js +23 -9
- package/dist/tempo/client/Charge.js.map +1 -1
- package/dist/tempo/client/Methods.d.ts +1 -0
- package/dist/tempo/client/Methods.d.ts.map +1 -1
- package/dist/tempo/internal/auto-swap.d.ts +49 -0
- package/dist/tempo/internal/auto-swap.d.ts.map +1 -0
- package/dist/tempo/internal/auto-swap.js +89 -0
- package/dist/tempo/internal/auto-swap.js.map +1 -0
- package/dist/tempo/internal/fee-payer.d.ts +15 -0
- package/dist/tempo/internal/fee-payer.d.ts.map +1 -0
- package/dist/tempo/internal/fee-payer.js +41 -0
- package/dist/tempo/internal/fee-payer.js.map +1 -0
- package/dist/tempo/internal/selectors.d.ts +5 -0
- package/dist/tempo/internal/selectors.d.ts.map +1 -0
- package/dist/tempo/internal/selectors.js +7 -0
- package/dist/tempo/internal/selectors.js.map +1 -0
- package/dist/tempo/server/Charge.d.ts.map +1 -1
- package/dist/tempo/server/Charge.js +8 -6
- package/dist/tempo/server/Charge.js.map +1 -1
- package/package.json +1 -1
- package/src/client/Mppx.test-d.ts +28 -0
- package/src/client/Mppx.ts +3 -3
- package/src/client/internal/Fetch.test.ts +454 -0
- package/src/client/internal/Fetch.ts +89 -14
- package/src/internal/constantTimeEqual.ts +6 -4
- package/src/tempo/client/Charge.ts +40 -9
- package/src/tempo/internal/auto-swap.test.ts +113 -0
- package/src/tempo/internal/auto-swap.ts +141 -0
- package/src/tempo/internal/fee-payer.test.ts +223 -0
- package/src/tempo/internal/fee-payer.ts +53 -0
- package/src/tempo/internal/selectors.ts +10 -0
- package/src/tempo/server/Charge.test.ts +374 -3
- package/src/tempo/server/Charge.ts +9 -18
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { decodeFunctionData, isAddressEqual, parseEventLogs
|
|
1
|
+
import { decodeFunctionData, isAddressEqual, parseEventLogs } from 'viem';
|
|
2
2
|
import { getTransactionReceipt, sendRawTransaction, sendRawTransactionSync, signTransaction, } from 'viem/actions';
|
|
3
3
|
import { tempo as tempo_chain } from 'viem/chains';
|
|
4
4
|
import { Abis, Transaction } from 'viem/tempo';
|
|
@@ -7,10 +7,10 @@ import * as Method from '../../Method.js';
|
|
|
7
7
|
import * as Client from '../../viem/Client.js';
|
|
8
8
|
import * as Account from '../internal/account.js';
|
|
9
9
|
import * as defaults from '../internal/defaults.js';
|
|
10
|
+
import * as FeePayer from '../internal/fee-payer.js';
|
|
11
|
+
import * as Selectors from '../internal/selectors.js';
|
|
10
12
|
import { simulateTransaction } from '../internal/simulate.js';
|
|
11
13
|
import * as Methods from '../Methods.js';
|
|
12
|
-
const transferSelector = /*#__PURE__*/ toFunctionSelector('function transfer(address to, uint256 amount)');
|
|
13
|
-
const transferWithMemoSelector = /*#__PURE__*/ toFunctionSelector('function transferWithMemo(address to, uint256 amount, bytes32 memo)');
|
|
14
14
|
/**
|
|
15
15
|
* Creates a Tempo charge method intent for usage on the server.
|
|
16
16
|
*
|
|
@@ -144,7 +144,7 @@ export function charge(parameters = {}) {
|
|
|
144
144
|
return false;
|
|
145
145
|
const selector = call.data.slice(0, 10);
|
|
146
146
|
if (memo) {
|
|
147
|
-
if (selector !==
|
|
147
|
+
if (selector !== Selectors.transferWithMemo)
|
|
148
148
|
return false;
|
|
149
149
|
try {
|
|
150
150
|
const { args } = decodeFunctionData({ abi: Abis.tip20, data: call.data });
|
|
@@ -157,7 +157,7 @@ export function charge(parameters = {}) {
|
|
|
157
157
|
return false;
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
if (selector ===
|
|
160
|
+
if (selector === Selectors.transfer) {
|
|
161
161
|
try {
|
|
162
162
|
const { args } = decodeFunctionData({ abi: Abis.tip20, data: call.data });
|
|
163
163
|
const [to, amount_] = args;
|
|
@@ -167,7 +167,7 @@ export function charge(parameters = {}) {
|
|
|
167
167
|
return false;
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
|
-
if (selector ===
|
|
170
|
+
if (selector === Selectors.transferWithMemo) {
|
|
171
171
|
try {
|
|
172
172
|
const { args } = decodeFunctionData({ abi: Abis.tip20, data: call.data });
|
|
173
173
|
const [to, amount_] = args;
|
|
@@ -185,6 +185,8 @@ export function charge(parameters = {}) {
|
|
|
185
185
|
currency,
|
|
186
186
|
recipient,
|
|
187
187
|
});
|
|
188
|
+
if (feePayer && methodDetails?.feePayer !== false)
|
|
189
|
+
FeePayer.validateCalls(calls, { amount, currency, recipient });
|
|
188
190
|
const serializedTransaction_final = await (async () => {
|
|
189
191
|
if (feePayer && methodDetails?.feePayer !== false) {
|
|
190
192
|
return signTransaction(client, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Charge.js","sourceRoot":"","sources":["../../../src/tempo/server/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Charge.js","sourceRoot":"","sources":["../../../src/tempo/server/Charge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,cAAc,EAA2B,MAAM,MAAM,CAAA;AAClG,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,eAAe,GAChB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,I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SAAS,CAAC,OAA2B;IAC5C,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,OAAO,CAAA;IAC3C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,yBAAyB,eAAe,EAAE,CAAC,CAAA;IAC7D,CAAC;IACD,OAAO;QACL,MAAM,EAAE,OAAO;QACf,MAAM,EAAE,SAAS;QACjB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACnC,SAAS,EAAE,eAAe;KAClB,CAAA;AACZ,CAAC;AAED,gBAAgB;AAChB,MAAM,aAAc,SAAQ,KAAK;IAG/B,YAAY,MAAc,EAAE,OAA+B;QACzD,KAAK,CAAC,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;QAHxE,SAAI,GAAG,eAAe,CAAA;IAIxC,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -2,6 +2,8 @@ import type { Account } from 'viem'
|
|
|
2
2
|
import { describe, expectTypeOf, test } from 'vitest'
|
|
3
3
|
import * as Method from '../Method.js'
|
|
4
4
|
import { charge } from '../tempo/client/Charge.js'
|
|
5
|
+
import { tempo } from '../tempo/client/Methods.js'
|
|
6
|
+
import type * as AutoSwap from '../tempo/internal/auto-swap.js'
|
|
5
7
|
import * as Methods from '../tempo/Methods.js'
|
|
6
8
|
import * as z from '../zod.js'
|
|
7
9
|
import * as Mppx from './Mppx.js'
|
|
@@ -88,3 +90,29 @@ describe('Mppx with context', () => {
|
|
|
88
90
|
expectTypeOf(mppx.createCredential).returns.toMatchTypeOf<Promise<string>>()
|
|
89
91
|
})
|
|
90
92
|
})
|
|
93
|
+
|
|
94
|
+
describe('fetch context', () => {
|
|
95
|
+
test('context has typed account and autoSwap for tempo charge', () => {
|
|
96
|
+
const mppx = Mppx.create({ methods: [charge()] })
|
|
97
|
+
|
|
98
|
+
type FetchInit = NonNullable<Parameters<typeof mppx.fetch>[1]>
|
|
99
|
+
type Context = NonNullable<FetchInit['context']>
|
|
100
|
+
|
|
101
|
+
expectTypeOf<Context>().toHaveProperty('account')
|
|
102
|
+
expectTypeOf<Context>().toHaveProperty('autoSwap')
|
|
103
|
+
|
|
104
|
+
expectTypeOf<Context['autoSwap']>().toEqualTypeOf<AutoSwap.resolve.Value | undefined>()
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('context has typed account and autoSwap for tempo()', () => {
|
|
108
|
+
const mppx = Mppx.create({ methods: [tempo()] })
|
|
109
|
+
|
|
110
|
+
type FetchInit = NonNullable<Parameters<typeof mppx.fetch>[1]>
|
|
111
|
+
type Context = NonNullable<FetchInit['context']>
|
|
112
|
+
|
|
113
|
+
// Context is a union of charge and session contexts.
|
|
114
|
+
// `account` exists on both; `autoSwap` only on charge.
|
|
115
|
+
expectTypeOf<Context>().toHaveProperty('account')
|
|
116
|
+
expectTypeOf<Extract<Context, { autoSwap?: unknown }>>().toHaveProperty('autoSwap')
|
|
117
|
+
})
|
|
118
|
+
})
|
package/src/client/Mppx.ts
CHANGED
|
@@ -142,9 +142,9 @@ export declare namespace create {
|
|
|
142
142
|
* @internal
|
|
143
143
|
*/
|
|
144
144
|
type AnyContextFor<methods extends readonly Method.AnyClient[]> = {
|
|
145
|
-
[method in keyof methods]: methods[method] extends
|
|
146
|
-
?
|
|
147
|
-
? z.input<
|
|
145
|
+
[method in keyof methods]: NonNullable<methods[method]['context']> extends infer ctx
|
|
146
|
+
? ctx extends z.ZodMiniType
|
|
147
|
+
? z.input<ctx>
|
|
148
148
|
: undefined
|
|
149
149
|
: undefined
|
|
150
150
|
}[number]
|
|
@@ -268,6 +268,402 @@ describe('Fetch.from', () => {
|
|
|
268
268
|
})
|
|
269
269
|
})
|
|
270
270
|
|
|
271
|
+
// Minimal mock method — createCredential is only invoked on the 402 retry path.
|
|
272
|
+
const noopMethod = {
|
|
273
|
+
name: 'test',
|
|
274
|
+
intent: 'test',
|
|
275
|
+
context: undefined,
|
|
276
|
+
createCredential: async () => 'credential',
|
|
277
|
+
} as any
|
|
278
|
+
|
|
279
|
+
/** Builds a valid 402 response with a WWW-Authenticate header. */
|
|
280
|
+
function make402(overrides?: { method?: string; intent?: string }) {
|
|
281
|
+
const method = overrides?.method ?? 'test'
|
|
282
|
+
const intent = overrides?.intent ?? 'test'
|
|
283
|
+
const request = btoa(JSON.stringify({ amount: '1' }))
|
|
284
|
+
.replace(/\+/g, '-')
|
|
285
|
+
.replace(/\//g, '_')
|
|
286
|
+
.replace(/=+$/, '')
|
|
287
|
+
const header = `Payment id="abc", realm="test", method="${method}", intent="${intent}", request="${request}"`
|
|
288
|
+
return new Response(null, {
|
|
289
|
+
status: 402,
|
|
290
|
+
headers: { 'WWW-Authenticate': header },
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
describe('Fetch.from: init passthrough (non-402)', () => {
|
|
295
|
+
test('passes unmodified init to underlying fetch for non-402 responses', async () => {
|
|
296
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
297
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
298
|
+
receivedInits.push(init)
|
|
299
|
+
return new Response('OK', { status: 200 })
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const fetch = Fetch.from({
|
|
303
|
+
fetch: mockFetch,
|
|
304
|
+
methods: [noopMethod],
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
const customInit = {
|
|
308
|
+
method: 'POST',
|
|
309
|
+
headers: { 'X-Custom': 'value' },
|
|
310
|
+
body: JSON.stringify({ data: 'test' }),
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
await fetch('https://example.com/ws-upgrade', customInit)
|
|
314
|
+
|
|
315
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
test('preserves extra properties on init for non-402 responses', async () => {
|
|
319
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
320
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
321
|
+
receivedInits.push(init)
|
|
322
|
+
return new Response('OK', { status: 200 })
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const fetch = Fetch.from({
|
|
326
|
+
fetch: mockFetch,
|
|
327
|
+
methods: [noopMethod],
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
const customInit = {
|
|
331
|
+
method: 'GET',
|
|
332
|
+
headers: { Authorization: 'Bearer token123' },
|
|
333
|
+
signal: AbortSignal.timeout(5000),
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
await fetch('https://example.com/api', customInit)
|
|
337
|
+
|
|
338
|
+
const received = receivedInits[0]!
|
|
339
|
+
expect(received.method).toBe('GET')
|
|
340
|
+
expect((received.headers as Record<string, string>).Authorization).toBe('Bearer token123')
|
|
341
|
+
expect(received.signal).toBe(customInit.signal)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
test('passes through undefined init', async () => {
|
|
345
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
346
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
347
|
+
receivedInits.push(init)
|
|
348
|
+
return new Response('OK', { status: 200 })
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const fetch = Fetch.from({
|
|
352
|
+
fetch: mockFetch,
|
|
353
|
+
methods: [noopMethod],
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
await fetch('https://example.com/api')
|
|
357
|
+
expect(receivedInits[0]).toBeUndefined()
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
test('passes init with context through untouched', async () => {
|
|
361
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
362
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
363
|
+
receivedInits.push(init)
|
|
364
|
+
return new Response('OK', { status: 200 })
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const fetch = Fetch.from({
|
|
368
|
+
fetch: mockFetch,
|
|
369
|
+
methods: [noopMethod],
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
const customInit = { method: 'POST', context: { account: '0xabc' } }
|
|
373
|
+
await fetch('https://example.com/api', customInit as any)
|
|
374
|
+
|
|
375
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
test('preserves object identity across all non-402 status codes', async () => {
|
|
379
|
+
for (const status of [200, 201, 204, 301, 400, 401, 403, 404, 500, 503]) {
|
|
380
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
381
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
382
|
+
receivedInits.push(init)
|
|
383
|
+
return new Response(null, { status })
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const fetch = Fetch.from({
|
|
387
|
+
fetch: mockFetch,
|
|
388
|
+
methods: [noopMethod],
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
const customInit = { method: 'GET' }
|
|
392
|
+
await fetch('https://example.com/api', customInit)
|
|
393
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
394
|
+
}
|
|
395
|
+
})
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
describe('Fetch.from: 402 retry path', () => {
|
|
399
|
+
test('strips context from init on retry', async () => {
|
|
400
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
401
|
+
let callCount = 0
|
|
402
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
403
|
+
calls.push({ init })
|
|
404
|
+
callCount++
|
|
405
|
+
if (callCount === 1) return make402()
|
|
406
|
+
return new Response('OK', { status: 200 })
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const fetch = Fetch.from({
|
|
410
|
+
fetch: mockFetch,
|
|
411
|
+
methods: [noopMethod],
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
await fetch('https://example.com/api', {
|
|
415
|
+
method: 'POST',
|
|
416
|
+
context: { account: '0xabc' },
|
|
417
|
+
} as any)
|
|
418
|
+
|
|
419
|
+
expect(calls).toHaveLength(2)
|
|
420
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
421
|
+
expect(retryInit).not.toHaveProperty('context')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
test('adds Authorization header on retry', async () => {
|
|
425
|
+
let callCount = 0
|
|
426
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
427
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
428
|
+
calls.push({ init })
|
|
429
|
+
callCount++
|
|
430
|
+
if (callCount === 1) return make402()
|
|
431
|
+
return new Response('OK', { status: 200 })
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const fetch = Fetch.from({
|
|
435
|
+
fetch: mockFetch,
|
|
436
|
+
methods: [noopMethod],
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
await fetch('https://example.com/api')
|
|
440
|
+
|
|
441
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
442
|
+
const headers = retryInit.headers as Record<string, string>
|
|
443
|
+
expect(headers.Authorization).toBe('credential')
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
test('preserves existing headers on retry', async () => {
|
|
447
|
+
let callCount = 0
|
|
448
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
449
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
450
|
+
calls.push({ init })
|
|
451
|
+
callCount++
|
|
452
|
+
if (callCount === 1) return make402()
|
|
453
|
+
return new Response('OK', { status: 200 })
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const fetch = Fetch.from({
|
|
457
|
+
fetch: mockFetch,
|
|
458
|
+
methods: [noopMethod],
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
await fetch('https://example.com/api', {
|
|
462
|
+
headers: { 'X-Custom': 'value', 'Content-Type': 'application/json' },
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
466
|
+
const headers = retryInit.headers as Record<string, string>
|
|
467
|
+
expect(headers['X-Custom']).toBe('value')
|
|
468
|
+
expect(headers['Content-Type']).toBe('application/json')
|
|
469
|
+
expect(headers.Authorization).toBe('credential')
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
test('preserves method and other init properties on retry', async () => {
|
|
473
|
+
let callCount = 0
|
|
474
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
475
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
476
|
+
calls.push({ init })
|
|
477
|
+
callCount++
|
|
478
|
+
if (callCount === 1) return make402()
|
|
479
|
+
return new Response('OK', { status: 200 })
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const fetch = Fetch.from({
|
|
483
|
+
fetch: mockFetch,
|
|
484
|
+
methods: [noopMethod],
|
|
485
|
+
})
|
|
486
|
+
|
|
487
|
+
await fetch('https://example.com/api', {
|
|
488
|
+
method: 'PUT',
|
|
489
|
+
body: JSON.stringify({ data: 'test' }),
|
|
490
|
+
credentials: 'include',
|
|
491
|
+
mode: 'cors',
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
495
|
+
expect(retryInit.method).toBe('PUT')
|
|
496
|
+
expect(retryInit.body).toBe(JSON.stringify({ data: 'test' }))
|
|
497
|
+
expect(retryInit.credentials).toBe('include')
|
|
498
|
+
expect(retryInit.mode).toBe('cors')
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
test('handles undefined init on 402 retry', async () => {
|
|
502
|
+
let callCount = 0
|
|
503
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
504
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
505
|
+
calls.push({ init })
|
|
506
|
+
callCount++
|
|
507
|
+
if (callCount === 1) return make402()
|
|
508
|
+
return new Response('OK', { status: 200 })
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const fetch = Fetch.from({
|
|
512
|
+
fetch: mockFetch,
|
|
513
|
+
methods: [noopMethod],
|
|
514
|
+
})
|
|
515
|
+
|
|
516
|
+
await fetch('https://example.com/api')
|
|
517
|
+
|
|
518
|
+
expect(calls).toHaveLength(2)
|
|
519
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
520
|
+
expect(retryInit.headers).toEqual({ Authorization: 'credential' })
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
test('throws when no matching method for 402 challenge', async () => {
|
|
524
|
+
const mockFetch: typeof globalThis.fetch = async () =>
|
|
525
|
+
make402({ method: 'stripe', intent: 'charge' })
|
|
526
|
+
|
|
527
|
+
const fetch = Fetch.from({
|
|
528
|
+
fetch: mockFetch,
|
|
529
|
+
methods: [noopMethod],
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
await expect(fetch('https://example.com/api')).rejects.toThrow(
|
|
533
|
+
'No method found for "stripe.charge"',
|
|
534
|
+
)
|
|
535
|
+
})
|
|
536
|
+
|
|
537
|
+
test('retries exactly once — does not loop on repeated 402', async () => {
|
|
538
|
+
let callCount = 0
|
|
539
|
+
const mockFetch: typeof globalThis.fetch = async () => {
|
|
540
|
+
callCount++
|
|
541
|
+
return make402()
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const fetch = Fetch.from({
|
|
545
|
+
fetch: mockFetch,
|
|
546
|
+
methods: [noopMethod],
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
const response = await fetch('https://example.com/api')
|
|
550
|
+
expect(callCount).toBe(2)
|
|
551
|
+
expect(response.status).toBe(402)
|
|
552
|
+
})
|
|
553
|
+
})
|
|
554
|
+
|
|
555
|
+
describe('Fetch.from: 402 retry headers normalization', () => {
|
|
556
|
+
test('preserves headers when passed as a Headers instance', async () => {
|
|
557
|
+
let callCount = 0
|
|
558
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
559
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
560
|
+
calls.push({ init })
|
|
561
|
+
callCount++
|
|
562
|
+
if (callCount === 1) return make402()
|
|
563
|
+
return new Response('OK', { status: 200 })
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const fetch = Fetch.from({
|
|
567
|
+
fetch: mockFetch,
|
|
568
|
+
methods: [noopMethod],
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
const headers = new Headers({ 'X-Custom': 'value', 'Content-Type': 'application/json' })
|
|
572
|
+
await fetch('https://example.com/api', { headers })
|
|
573
|
+
|
|
574
|
+
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
575
|
+
string,
|
|
576
|
+
string
|
|
577
|
+
>
|
|
578
|
+
expect(retryHeaders['x-custom']).toBe('value')
|
|
579
|
+
expect(retryHeaders['content-type']).toBe('application/json')
|
|
580
|
+
expect(retryHeaders.Authorization).toBe('credential')
|
|
581
|
+
})
|
|
582
|
+
|
|
583
|
+
test('preserves headers when passed as array of tuples', async () => {
|
|
584
|
+
let callCount = 0
|
|
585
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
586
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
587
|
+
calls.push({ init })
|
|
588
|
+
callCount++
|
|
589
|
+
if (callCount === 1) return make402()
|
|
590
|
+
return new Response('OK', { status: 200 })
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const fetch = Fetch.from({
|
|
594
|
+
fetch: mockFetch,
|
|
595
|
+
methods: [noopMethod],
|
|
596
|
+
})
|
|
597
|
+
|
|
598
|
+
await fetch('https://example.com/api', {
|
|
599
|
+
headers: [
|
|
600
|
+
['X-Custom', 'value'],
|
|
601
|
+
['Accept', 'application/json'],
|
|
602
|
+
],
|
|
603
|
+
})
|
|
604
|
+
|
|
605
|
+
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
606
|
+
string,
|
|
607
|
+
string
|
|
608
|
+
>
|
|
609
|
+
expect(retryHeaders['X-Custom']).toBe('value')
|
|
610
|
+
expect(retryHeaders.Accept).toBe('application/json')
|
|
611
|
+
expect(retryHeaders.Authorization).toBe('credential')
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
test('replaces existing authorization header case-insensitively', async () => {
|
|
615
|
+
let callCount = 0
|
|
616
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
617
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
618
|
+
calls.push({ init })
|
|
619
|
+
callCount++
|
|
620
|
+
if (callCount === 1) return make402()
|
|
621
|
+
return new Response('OK', { status: 200 })
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const fetch = Fetch.from({
|
|
625
|
+
fetch: mockFetch,
|
|
626
|
+
methods: [noopMethod],
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
await fetch('https://example.com/api', {
|
|
630
|
+
headers: { authorization: 'Bearer stale-token', 'X-Custom': 'value' },
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
634
|
+
string,
|
|
635
|
+
string
|
|
636
|
+
>
|
|
637
|
+
expect(retryHeaders.authorization).toBeUndefined()
|
|
638
|
+
expect(retryHeaders.Authorization).toBe('credential')
|
|
639
|
+
expect(retryHeaders['X-Custom']).toBe('value')
|
|
640
|
+
})
|
|
641
|
+
})
|
|
642
|
+
|
|
643
|
+
describe('Fetch.from: input passthrough', () => {
|
|
644
|
+
test('passes URL input through on both initial and retry calls', async () => {
|
|
645
|
+
let callCount = 0
|
|
646
|
+
const receivedInputs: (RequestInfo | URL)[] = []
|
|
647
|
+
const mockFetch: typeof globalThis.fetch = async (input, _init) => {
|
|
648
|
+
receivedInputs.push(input)
|
|
649
|
+
callCount++
|
|
650
|
+
if (callCount === 1) return make402()
|
|
651
|
+
return new Response('OK', { status: 200 })
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const fetch = Fetch.from({
|
|
655
|
+
fetch: mockFetch,
|
|
656
|
+
methods: [noopMethod],
|
|
657
|
+
})
|
|
658
|
+
|
|
659
|
+
const url = new URL('https://example.com/resource')
|
|
660
|
+
await fetch(url)
|
|
661
|
+
|
|
662
|
+
expect(receivedInputs[0]).toBe(url)
|
|
663
|
+
expect(receivedInputs[1]).toBe(url)
|
|
664
|
+
})
|
|
665
|
+
})
|
|
666
|
+
|
|
271
667
|
describe('Fetch.polyfill', () => {
|
|
272
668
|
test('default', async () => {
|
|
273
669
|
Fetch.polyfill({
|
|
@@ -313,3 +709,61 @@ describe('Fetch.polyfill', () => {
|
|
|
313
709
|
Fetch.restore()
|
|
314
710
|
})
|
|
315
711
|
})
|
|
712
|
+
|
|
713
|
+
describe('Fetch.polyfill / restore', () => {
|
|
714
|
+
test('restore is a no-op when polyfill was never called', () => {
|
|
715
|
+
const before = globalThis.fetch
|
|
716
|
+
Fetch.restore()
|
|
717
|
+
expect(globalThis.fetch).toBe(before)
|
|
718
|
+
})
|
|
719
|
+
|
|
720
|
+
test('restore reverts to original fetch', () => {
|
|
721
|
+
const originalFetch = globalThis.fetch
|
|
722
|
+
|
|
723
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
724
|
+
expect(globalThis.fetch).not.toBe(originalFetch)
|
|
725
|
+
|
|
726
|
+
Fetch.restore()
|
|
727
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
728
|
+
})
|
|
729
|
+
|
|
730
|
+
test('stacked polyfill calls preserve the true original fetch', () => {
|
|
731
|
+
const originalFetch = globalThis.fetch
|
|
732
|
+
|
|
733
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
734
|
+
const firstPolyfill = globalThis.fetch
|
|
735
|
+
|
|
736
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
737
|
+
expect(globalThis.fetch).not.toBe(firstPolyfill)
|
|
738
|
+
|
|
739
|
+
Fetch.restore()
|
|
740
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
test('double restore does not clobber fetch', () => {
|
|
744
|
+
const originalFetch = globalThis.fetch
|
|
745
|
+
|
|
746
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
747
|
+
Fetch.restore()
|
|
748
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
749
|
+
|
|
750
|
+
Fetch.restore()
|
|
751
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
test('restore is a no-op when fetch was replaced externally after polyfill', () => {
|
|
755
|
+
const originalFetch = globalThis.fetch
|
|
756
|
+
const externalFetch = vi.fn(
|
|
757
|
+
async (_input: RequestInfo | URL, _init?: RequestInit) =>
|
|
758
|
+
new Response('external', { status: 200 }),
|
|
759
|
+
) as unknown as typeof globalThis.fetch
|
|
760
|
+
|
|
761
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
762
|
+
globalThis.fetch = externalFetch
|
|
763
|
+
|
|
764
|
+
Fetch.restore()
|
|
765
|
+
expect(globalThis.fetch).toBe(externalFetch)
|
|
766
|
+
|
|
767
|
+
globalThis.fetch = originalFetch
|
|
768
|
+
})
|
|
769
|
+
})
|