@ahoo-wang/fetcher-eventstream 3.15.1 → 3.15.3
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/README.md +9 -36
- package/README.zh-CN.md +8 -33
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ APIs.
|
|
|
15
15
|
## 🌟 Features
|
|
16
16
|
|
|
17
17
|
- **📡 Event Stream Conversion**: Converts `text/event-stream` responses to async generators of `ServerSentEvent` objects
|
|
18
|
-
- **🔌
|
|
18
|
+
- **🔌 Side-Effect Module Import**: Automatically adds `eventStream()` and `jsonEventStream()` methods to the global `Response.prototype` for responses with
|
|
19
19
|
`text/event-stream` content
|
|
20
20
|
type
|
|
21
21
|
- **📋 SSE Parsing**: Parses Server-Sent Events according to the specification, including data, event, id, and retry
|
|
@@ -312,43 +312,16 @@ try {
|
|
|
312
312
|
|
|
313
313
|
## 📚 API Reference
|
|
314
314
|
|
|
315
|
-
###
|
|
316
|
-
|
|
317
|
-
To use the event stream functionality, you need to import the module for its side effects:
|
|
318
|
-
|
|
319
|
-
```typescript
|
|
320
|
-
import '@ahoo-wang/fetcher-eventstream';
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
This import automatically extends the global `Response` interface with methods for handling Server-Sent Events streams:
|
|
324
|
-
|
|
325
|
-
- `eventStream()` - Converts a Response with `text/event-stream` content type to a `ServerSentEventStream`
|
|
326
|
-
- `jsonEventStream<DATA>()` - Converts a Response with `text/event-stream` content type to a
|
|
327
|
-
`JsonServerSentEventStream<DATA>`
|
|
328
|
-
- `isEventStream` getter - Checks if the Response has a `text/event-stream` content type
|
|
329
|
-
- `requiredEventStream()` - Gets a `ServerSentEventStream`, throwing an error if not available
|
|
330
|
-
- `requiredJsonEventStream<DATA>()` - Gets a `JsonServerSentEventStream<DATA>`, throwing an error if not available
|
|
331
|
-
|
|
332
|
-
This is a common pattern in JavaScript/TypeScript for extending existing types with additional functionality without
|
|
333
|
-
modifying the original type definitions.
|
|
334
|
-
|
|
335
|
-
In integration tests and real applications, this import is essential for working with event streams. For example:
|
|
336
|
-
|
|
337
|
-
```typescript
|
|
338
|
-
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
339
|
-
import '@ahoo-wang/fetcher-eventstream';
|
|
315
|
+
### Response Prototype Extension
|
|
340
316
|
|
|
341
|
-
|
|
342
|
-
baseURL: 'https://api.example.com',
|
|
343
|
-
});
|
|
317
|
+
Importing this module patches the global `Response.prototype` with the following properties:
|
|
344
318
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
```
|
|
319
|
+
- `contentType` - The Content-Type header value
|
|
320
|
+
- `isEventStream` - Boolean getter, true if Content-Type is `text/event-stream`
|
|
321
|
+
- `eventStream()` - Returns `ServerSentEventStream` if available, undefined otherwise
|
|
322
|
+
- `requiredEventStream()` - Returns `ServerSentEventStream` or throws if not available
|
|
323
|
+
- `jsonEventStream<DATA>()` - Returns `JsonServerSentEventStream<DATA>` if available
|
|
324
|
+
- `requiredJsonEventStream<DATA>()` - Returns `JsonServerSentEventStream<DATA>` or throws if not available
|
|
352
325
|
|
|
353
326
|
### toJsonServerSentEventStream
|
|
354
327
|
|
package/README.zh-CN.md
CHANGED
|
@@ -283,41 +283,16 @@ try {
|
|
|
283
283
|
|
|
284
284
|
## 📚 API 参考
|
|
285
285
|
|
|
286
|
-
###
|
|
287
|
-
|
|
288
|
-
要使用事件流功能,您需要导入模块以执行其副作用:
|
|
289
|
-
|
|
290
|
-
```typescript
|
|
291
|
-
import '@ahoo-wang/fetcher-eventstream';
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
此导入会自动扩展全局 `Response` 接口以处理服务器发送事件流:
|
|
295
|
-
|
|
296
|
-
- `eventStream()` - 将带有 `text/event-stream` 内容类型的响应转换为 `ServerSentEventStream`
|
|
297
|
-
- `jsonEventStream<DATA>()` - 将带有 `text/event-stream` 内容类型的响应转换为 `JsonServerSentEventStream<DATA>`
|
|
298
|
-
- `isEventStream` getter - 检查响应是否具有 `text/event-stream` 内容类型
|
|
299
|
-
- `requiredEventStream()` - 获取 `ServerSentEventStream`,如果不可用则抛出错误
|
|
300
|
-
- `requiredJsonEventStream<DATA>()` - 获取 `JsonServerSentEventStream<DATA>`,如果不可用则抛出错误
|
|
301
|
-
|
|
302
|
-
这是 JavaScript/TypeScript 中常见的模式,用于在不修改原始类型定义的情况下扩展现有类型的功能。
|
|
303
|
-
|
|
304
|
-
在集成测试和实际应用中,此导入对于处理事件流至关重要。例如:
|
|
286
|
+
### Response 原型扩展
|
|
305
287
|
|
|
306
|
-
|
|
307
|
-
import { Fetcher } from '@ahoo-wang/fetcher';
|
|
308
|
-
import '@ahoo-wang/fetcher-eventstream';
|
|
288
|
+
导入此模块会通过以下属性对全局 `Response.prototype` 进行补丁:
|
|
309
289
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
// 处理事件流
|
|
317
|
-
for await (const event of response.requiredEventStream()) {
|
|
318
|
-
console.log('收到事件:', event);
|
|
319
|
-
}
|
|
320
|
-
```
|
|
290
|
+
- `contentType` - Content-Type 头部的值
|
|
291
|
+
- `isEventStream` - 布尔值 getter,如果 Content-Type 是 `text/event-stream` 则为 true
|
|
292
|
+
- `eventStream()` - 如果可用则返回 `ServerSentEventStream`,否则返回 undefined
|
|
293
|
+
- `requiredEventStream()` - 返回 `ServerSentEventStream`,如果不可用则抛出错误
|
|
294
|
+
- `jsonEventStream<DATA>()` - 如果可用则返回 `JsonServerSentEventStream<DATA>`
|
|
295
|
+
- `requiredJsonEventStream<DATA>()` - 返回 `JsonServerSentEventStream<DATA>`,如果不可用则抛出错误
|
|
321
296
|
|
|
322
297
|
### toJsonServerSentEventStream
|
|
323
298
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-eventstream",
|
|
3
|
-
"version": "3.15.
|
|
3
|
+
"version": "3.15.3",
|
|
4
4
|
"description": "Server-Sent Events (SSE) support for Fetcher HTTP client with native LLM streaming API support. Enables real-time data streaming and token-by-token LLM response handling.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"@vitest/coverage-v8": "^4.1.4",
|
|
47
47
|
"@vitest/ui": "^4.1.4",
|
|
48
48
|
"eslint": "^9.39.4",
|
|
49
|
-
"globals": "^17.
|
|
49
|
+
"globals": "^17.5.0",
|
|
50
50
|
"prettier": "^3.8.1",
|
|
51
51
|
"typescript": "^6.0.2",
|
|
52
|
-
"typescript-eslint": "^8.58.
|
|
52
|
+
"typescript-eslint": "^8.58.2",
|
|
53
53
|
"unplugin-dts": "1.0.0-beta.6",
|
|
54
54
|
"vite": "^8.0.8",
|
|
55
55
|
"vite-bundle-analyzer": "^1.3.7",
|