hook-fetch
Version:
A lightweight and modern HTTP request library developed based on the native Fetch API of the browser, providing a user-friendly interface similar to Axios and powerful extensibility.
46 lines (45 loc) • 2.11 kB
TypeScript
import { HookFetchPlugin } from '../types';
export interface SSETextDecoderPluginOptions {
splitSeparator: string;
lineSeparator: string | undefined;
trim: boolean;
json: boolean;
prefix: string;
doneSymbol: string;
}
/**
* SSE 文本解码插件 | A SSE (Server-Sent Events) response text decoder plugin.
*
* 用于处理服务端推送事件(SSE)的响应流,提供以下功能:
* - 将二进制Buffer解码为文本
* - 按指定分隔符拆分事件块
* - 去除首尾空白字符
* - 自动JSON解析
* - 移除特定前缀
*
* Processes Server-Sent Events (SSE) response streams with the following features:
* - Decodes binary buffer to text
* - Splits event chunks by specified separator
* - Trims whitespace
* - Automatic JSON parsing
* - Removes specified prefix
*
* @param {Object} [options] 配置选项 | Plugin options
* @param {string} [options.splitSeparator='\n\n'] 分割符,用于拆分事件块(默认 '\n\n') | Separator for splitting events (default '\n\n')
* @param {string} [options.lineSeparator] 行分割符,用于拆分每行(可选) | Line separator for splitting each line (optional)
* @param {boolean} [options.trim=true] 是否去除首尾空白(默认 true) | Whether to trim whitespace (default true)
* @param {boolean} [options.json=false] 是否解析JSON(默认 false) | Whether to parse JSON (default false)
* @param {string} [options.prefix=''] 要移除的前缀,如 "data: "(默认为空) | Prefix to remove, e.g. "data: " (default '')
* @param {string} [options.doneSymbol] 结束标记,收到此标记时结束流(可选) | Symbol indicating stream end (optional)
* @returns {HookFetchPlugin} 返回 HookFetch 插件实例 | Returns a HookFetch plugin instance
* @example
* request.use(sseTextDecoderPlugin({
* json: true,
* prefix: 'data:',
* splitSeparator: '\n\n',
* doneSymbol: '[DONE]'
* }));
*/
export declare const sseTextDecoderPlugin: ({ splitSeparator, lineSeparator, trim, json, prefix, doneSymbol }?: Partial<SSETextDecoderPluginOptions>) => HookFetchPlugin<unknown, {
sseAble: boolean;
}>;