From 5a19f73e97b13b6679ee2c75ad10b87480599d9b Mon Sep 17 00:00:00 2001 From: Jelf Date: Mon, 19 Sep 2022 19:13:31 +0800 Subject: [PATCH] feat: support `responseType: 'stream'` as `ReadableStream` (#100) --- src/fetch.ts | 2 ++ src/utils.ts | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/fetch.ts b/src/fetch.ts index dedd9acf..214593ec 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -146,6 +146,8 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch { const data = await ctx.response.text() const parseFn = ctx.options.parseResponse || destr ctx.response._data = parseFn(data) + } else if (responseType === 'stream') { + ctx.response._data = ctx.response.body } else { ctx.response._data = await ctx.response[responseType]() } diff --git a/src/utils.ts b/src/utils.ts index 171045d1..4867d894 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -34,6 +34,7 @@ interface ResponseMap { blob: Blob text: string arrayBuffer: ArrayBuffer + stream: ReadableStream } export type ResponseType = keyof ResponseMap | 'json' @@ -52,6 +53,11 @@ export function detectResponseType (_contentType = ''): ResponseType { return 'json' } + // TODO + // if (contentType === 'application/octet-stream') { + // return 'stream' + // } + if (textTypes.has(contentType) || contentType.startsWith('text/')) { return 'text' }