BREAKING CHANGE(client/streaming): Unify streaming APIs: remove raw()/streamNode() and standardize on web ReadableStream across runtimes

This commit is contained in:
2025-11-17 14:18:58 +00:00
parent 1305b92ebe
commit 0cf48b3688
15 changed files with 1273 additions and 1524 deletions

View File

@@ -8,7 +8,6 @@ import type {
ResponseType,
FormField,
RateLimitConfig,
RawStreamFunction,
} from './types/common.js';
import {
type TPaginationConfig,
@@ -142,12 +141,12 @@ export class SmartRequest<T = any> {
if (!this._options.headers) {
this._options.headers = {};
}
// Set content type if provided
if (contentType) {
this._options.headers['Content-Type'] = contentType;
}
// Check if it's a Node.js stream (has pipe method)
if ('pipe' in stream && typeof (stream as any).pipe === 'function') {
// For Node.js streams, we need to use a custom approach
@@ -157,18 +156,7 @@ export class SmartRequest<T = any> {
// For web ReadableStream, pass directly
this._options.requestBody = stream;
}
return this;
}
/**
* Provide a custom function to handle raw request streaming
* This gives full control over the request body streaming
* Note: Only works in Node.js environment, not supported in browsers
*/
raw(streamFunc: RawStreamFunction): this {
// Store the raw streaming function to be used later
(this._options as any).__rawStreamFunc = streamFunc;
return this;
}
@@ -440,7 +428,7 @@ export class SmartRequest<T = any> {
// Main retry loop
for (let attempt = 0; attempt <= this._retries; attempt++) {
try {
// Check if we have a Node.js stream or raw function that needs special handling
// Check if we have a Node.js stream that needs special handling
let requestDataFunc = null;
if ((this._options as any).__nodeStream) {
const nodeStream = (this._options as any).__nodeStream;
@@ -449,16 +437,12 @@ export class SmartRequest<T = any> {
};
// Don't delete __nodeStream yet - let CoreRequest implementations handle it
// Node.js will use requestDataFunc, Bun/Deno will convert the stream
} else if ((this._options as any).__rawStreamFunc) {
requestDataFunc = (this._options as any).__rawStreamFunc;
// Don't delete __rawStreamFunc yet - let CoreRequest implementations handle it
}
const request = new CoreRequest(this._url, this._options as any, requestDataFunc);
// Clean up temporary properties after CoreRequest has been created
delete (this._options as any).__nodeStream;
delete (this._options as any).__rawStreamFunc;
const response = (await request.fire()) as ICoreResponse<R>;
// Check for 429 status if rate limit handling is enabled