Skip to content

Commit 34f01d4

Browse files
committed
fix: use duplex: half for resourceRequest with ReadableStream body input
1 parent 87ce68d commit 34f01d4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,8 @@ async function resourceRequest(
28772877
headers.set('authorization', `${headers.has('dpop') ? 'DPoP' : 'Bearer'} ${accessToken}`)
28782878

28792879
const response = await (options?.[customFetch] || fetch)(url.href, {
2880+
// @ts-ignore
2881+
duplex: looseInstanceOf(body, ReadableStream) ? 'half' : undefined,
28802882
// @ts-ignore
28812883
body,
28822884
headers: Object.fromEntries(headers.entries()),

test/protected_resource.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,26 @@ test('protectedResource()', async (t) => {
2222
const response = await lib.protectedResourceRequest('token', 'GET', url)
2323
t.true(response instanceof Response)
2424
})
25+
26+
test('protectedResource() w/ POST and ReadableStream body', async (t) => {
27+
const payload = 'stream body content'
28+
t.context.mock
29+
.get('https://rs.example.com')
30+
.intercept({
31+
path: '/resource',
32+
method: 'POST',
33+
headers: {
34+
authorization: 'Bearer token',
35+
},
36+
})
37+
.reply(200, '')
38+
const url = new URL('https://rs.example.com/resource')
39+
const body = new ReadableStream({
40+
start(controller) {
41+
controller.enqueue(new TextEncoder().encode(payload))
42+
controller.close()
43+
},
44+
})
45+
const response = await lib.protectedResourceRequest('token', 'POST', url, undefined, body)
46+
t.true(response instanceof Response)
47+
})

0 commit comments

Comments
 (0)