| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  | import * as types from './types.js'; | 
					
						
							|  |  |  | import { CoreResponse as AbstractCoreResponse } from '../core_base/response.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Fetch-based implementation of Core Response class | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | export class CoreResponse<T = any> | 
					
						
							|  |  |  |   extends AbstractCoreResponse<T> | 
					
						
							|  |  |  |   implements types.IFetchResponse<T> | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  |   private response: Response; | 
					
						
							|  |  |  |   private responseClone: Response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Public properties
 | 
					
						
							|  |  |  |   public readonly ok: boolean; | 
					
						
							|  |  |  |   public readonly status: number; | 
					
						
							|  |  |  |   public readonly statusText: string; | 
					
						
							| 
									
										
										
										
											2025-07-28 22:37:36 +00:00
										 |  |  |   public readonly headers: types.Headers; | 
					
						
							| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  |   public readonly url: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(response: Response) { | 
					
						
							|  |  |  |     super(); | 
					
						
							|  |  |  |     // Clone the response so we can read the body multiple times if needed
 | 
					
						
							|  |  |  |     this.response = response; | 
					
						
							|  |  |  |     this.responseClone = response.clone(); | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  |     this.ok = response.ok; | 
					
						
							|  |  |  |     this.status = response.status; | 
					
						
							|  |  |  |     this.statusText = response.statusText; | 
					
						
							|  |  |  |     this.url = response.url; | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  |     // Convert Headers to plain object
 | 
					
						
							|  |  |  |     this.headers = {}; | 
					
						
							|  |  |  |     response.headers.forEach((value, key) => { | 
					
						
							|  |  |  |       this.headers[key] = value; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Parse response as JSON | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async json(): Promise<T> { | 
					
						
							|  |  |  |     this.ensureNotConsumed(); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       return await this.response.json(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       throw new Error(`Failed to parse JSON: ${error.message}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Get response as text | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async text(): Promise<string> { | 
					
						
							|  |  |  |     this.ensureNotConsumed(); | 
					
						
							|  |  |  |     return await this.response.text(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Get response as ArrayBuffer | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async arrayBuffer(): Promise<ArrayBuffer> { | 
					
						
							|  |  |  |     this.ensureNotConsumed(); | 
					
						
							|  |  |  |     return await this.response.arrayBuffer(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Get response as a readable stream (Web Streams API) | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   stream(): ReadableStream<Uint8Array> | null { | 
					
						
							|  |  |  |     this.ensureNotConsumed(); | 
					
						
							|  |  |  |     return this.response.body; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 22:50:12 +00:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Node.js stream method - not available in browser | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   streamNode(): never { | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  |     throw new Error( | 
					
						
							|  |  |  |       'streamNode() is not available in browser/fetch environment. Use stream() for web-style ReadableStream.', | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2025-07-28 22:50:12 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 16:51:30 +00:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Get the raw Response object | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   raw(): Response { | 
					
						
							|  |  |  |     return this.responseClone; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-08-18 00:21:14 +00:00
										 |  |  | } |