@i.un/api-client 1.0.8 → 1.1.0

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/dist/index.d.mts CHANGED
@@ -136,116 +136,85 @@ interface ChainRequestRule {
136
136
  declare function executeRequestChain<T>(requests: ChainRequestRule[], handlers?: NetworkHandler[], getChainRequests?: (context: Record<string, any>) => ChainRequestRule[] | null | undefined, initContext?: Record<string, any>): Promise<T>;
137
137
 
138
138
  /**
139
- * Protobuf 通用编解码模块
139
+ * Protobuf 安全通信模块
140
140
  *
141
- * 特性:
142
- * - 支持加盐混淆(每次编码结果不同)
143
- * - 直接输出二进制格式
144
- * - 内置通用容器 schema(任意 JSON 数据)
145
- * - 支持自定义编解码函数
141
+ * 提供基于 Protobuf 的二进制序列化、XOR 混淆加密以及与 API Client 的无缝集成。
146
142
  */
147
143
 
148
- interface ProtobufCodecOptions {
149
- /** 是否启用加盐 (默认 true) */
150
- useSalt?: boolean;
151
- /** 自定义盐值生成器 */
152
- saltGenerator?: () => string;
153
- }
154
- interface ProtobufConfig {
155
- /** 自定义编码函数 */
156
- encode?: (data: unknown) => Uint8Array | Promise<Uint8Array>;
157
- /** 自定义解码函数 */
158
- decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
159
- /** 编码选项(仅内置编解码器使用) */
160
- options?: ProtobufCodecOptions;
161
- }
162
144
  /**
163
- * 使用内置 SaltedPayload schema 编码数据
164
- *
165
- * @param data - 任意 JSON 可序列化数据
166
- * @param options - 编码选项
167
- * @returns Uint8Array 二进制数据
168
- */
169
- declare function encodeProtobuf<T>(data: T, options?: ProtobufCodecOptions): Uint8Array;
170
- /**
171
- * 使用内置 SaltedPayload schema 解码数据
172
- *
173
- * @param buffer - 二进制数据
174
- * @returns 解码后的原始数据
145
+ * 基础编解码配置
146
+ * 控制如何将数据转换为二进制,以及是否混淆
175
147
  */
176
- declare function decodeProtobuf<T>(buffer: Uint8Array | ArrayBuffer): T;
148
+ interface ProtobufCodecOptions {
149
+ /** 是否启用二进制 XOR 混淆 (默认 true) */
150
+ obfuscate?: boolean;
151
+ /**
152
+ * 混淆密钥
153
+ * 可以是静态字符串/字节数组,也可以是动态获取密钥的同步或异步函数
154
+ */
155
+ obfuscationKey?: string | Uint8Array | (() => string | Uint8Array | Promise<string | Uint8Array>);
156
+ /** 预编译的 Protobuf 类型对象 (推荐,性能最高) */
157
+ protoType?: any;
158
+ /** 数据转换钩子:处理业务模型与 Proto 结构不一致的情况 */
159
+ transform?: {
160
+ beforeEncode?: (data: any) => any;
161
+ afterDecode?: (payload: any) => any;
162
+ };
163
+ }
177
164
  /**
178
- * 创建 Protobuf 二进制响应
179
- *
180
- * @param data - 要编码的数据
181
- * @param options - 编码选项和 CORS headers
182
- * @returns Response 对象
165
+ * 完全自定义编解码接口
166
+ * 用于外部项目完全接管序列化过程,同时复用基础库的混淆外壳
183
167
  */
184
- declare function protobufResponse<T>(data: T, options?: ProtobufCodecOptions & {
185
- corsHeaders?: Record<string, string>;
186
- }): Response;
168
+ interface ProtobufCustomCodec {
169
+ /** 外部定义的编码逻辑 (返回原始二进制) */
170
+ encode?: (data: any) => Uint8Array | Promise<Uint8Array>;
171
+ /** 外部定义的解码逻辑 (返回原始对象) */
172
+ decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
173
+ }
187
174
  /**
188
- * 检查 Content-Type 是否是 Protobuf 格式
175
+ * 集成配置项
176
+ * 用于 createProtobufHooks 或 API Client 全局配置
189
177
  */
190
- declare function isProtobufContentType(contentType: string | null): boolean;
178
+ interface ProtobufHooksOptions extends ProtobufCodecOptions, ProtobufCustomCodec {
179
+ }
191
180
  /**
192
- * 规范化 protobuf 配置
193
- * 将 boolean | ProtobufConfig 统一为 ProtobufConfig 或 null
181
+ * API Client 内部使用的标准化配置
194
182
  */
195
- declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined): ProtobufConfig | null;
183
+ interface ProtobufConfig {
184
+ encode: (data: any) => Uint8Array | Promise<Uint8Array>;
185
+ decode: <T>(buffer: Uint8Array) => T | Promise<T>;
186
+ options?: ProtobufCodecOptions;
187
+ }
188
+ /** 简单的二进制异或混淆转换 */
189
+ declare function xorTransform(data: Uint8Array, key: string | Uint8Array): Uint8Array;
190
+ /** 获取混淆密钥(支持异步/函数) */
191
+ declare function getObfuscationKey(keyOption?: ProtobufCodecOptions["obfuscationKey"]): Promise<string | Uint8Array>;
196
192
  /**
197
- * 解析 Protobuf 请求体 (Worker/Server 端使用)
193
+ * 编码安全载荷
198
194
  *
199
- * @param request - Request 对象
200
- * @param customDecode - 可选的自定义解码函数
201
- * @returns 解码后的数据
195
+ * 流程:业务数据 -> (自定义编码 / Proto 序列化) -> 二进制混淆
202
196
  */
203
- declare function parseProtobufRequest<T>(request: Request, customDecode?: (buffer: Uint8Array) => T | Promise<T>): Promise<T>;
197
+ declare function encodeSecure<T>(data: T, options?: ProtobufHooksOptions): Promise<Uint8Array>;
204
198
  /**
205
- * Protobuf 钩子选项
206
- */
207
- interface CreateProtobufHooksOptions extends ProtobufCodecOptions {
208
- /** 自定义编码函数 */
209
- encode?: (data: unknown) => Uint8Array | Promise<Uint8Array>;
210
- /** 自定义解码函数 */
211
- decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
212
- }
213
- /**
214
- * 创建 Protobuf 编解码钩子
215
- * 用于与 createApiClient 配合使用
216
- *
217
- * 钩子会自动:
218
- * - 设置 responseType 为 arrayBuffer
219
- * - 编码请求体为 Protobuf 格式
220
- * - 根据响应 Content-Type 自动解码(protobuf 或 JSON)
199
+ * 解码安全载荷
221
200
  *
222
- * @example
223
- * ```ts
224
- * import { createApiClient } from '@i.un/api-client';
225
- * import { createProtobufHooks } from '@i.un/api-client/protobuf';
226
- *
227
- * const { onRequest, onResponse } = createProtobufHooks();
228
- *
229
- * const client = createApiClient({
230
- * baseURL: 'https://api.example.com',
231
- * tokenStorage,
232
- * onRequest,
233
- * onResponse,
234
- * });
235
- *
236
- * // 自动处理,无需手动指定 responseType
237
- * const data = await client.post('/api/data', body);
238
- * ```
201
+ * 流程:二进制流 -> 二进制反混淆 -> (自定义解码 / Proto 反序列化) -> 业务数据
239
202
  */
240
- declare function createProtobufHooks(options?: CreateProtobufHooksOptions): {
241
- /**
242
- * 请求钩子:独立处理请求编码和响应类型设置
243
- */
203
+ declare function decodeSecure<T>(buffer: Uint8Array | ArrayBuffer, options?: ProtobufHooksOptions): Promise<T>;
204
+ /** 创建安全响应 (Worker/Server 端使用) */
205
+ declare function secureResponse<T>(data: T, options?: ProtobufCodecOptions & {
206
+ corsHeaders?: Record<string, string>;
207
+ }): Promise<Response>;
208
+ /** 解析安全请求体 (Worker/Server 端使用) */
209
+ declare function parseSecureRequest<T>(request: Request, customDecode?: ProtobufCustomCodec["decode"]): Promise<T>;
210
+ /** 检查是否为 Protobuf 响应头 */
211
+ declare function isProtobufContentType(contentType: string | null): boolean;
212
+ /** 标准化配置对象 */
213
+ declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined, options?: ProtobufHooksOptions): ProtobufConfig | null;
214
+ /** 创建 Protobuf 编解码钩子 (用于与 createApiClient 配合使用) */
215
+ declare function createProtobufHooks(options?: ProtobufHooksOptions): {
244
216
  onRequest(context: FetchContext): Promise<void>;
245
- /**
246
- * 响应钩子:解码 Protobuf 响应
247
- */
248
217
  onResponse(context: FetchContext): Promise<void>;
249
218
  };
250
219
 
251
- export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type CreateProtobufHooksOptions, type HttpRequestSpec, type NetworkAdapter, type NetworkHandler, type ProtobufCodecOptions, type ProtobufConfig, type TokenStorage, createApiClient, createProtobufHooks, decodeProtobuf, encodeProtobuf, executeRequestChain, isApiError, isProtobufContentType, normalizeProtobufConfig, parseProtobufRequest, protobufResponse };
220
+ export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type HttpRequestSpec, type NetworkAdapter, type NetworkHandler, type ProtobufCodecOptions, type ProtobufConfig, type ProtobufCustomCodec, type ProtobufHooksOptions, type TokenStorage, createApiClient, createProtobufHooks, decodeSecure, encodeSecure, executeRequestChain, getObfuscationKey, isApiError, isProtobufContentType, normalizeProtobufConfig, parseSecureRequest, secureResponse, xorTransform };
package/dist/index.d.ts CHANGED
@@ -136,116 +136,85 @@ interface ChainRequestRule {
136
136
  declare function executeRequestChain<T>(requests: ChainRequestRule[], handlers?: NetworkHandler[], getChainRequests?: (context: Record<string, any>) => ChainRequestRule[] | null | undefined, initContext?: Record<string, any>): Promise<T>;
137
137
 
138
138
  /**
139
- * Protobuf 通用编解码模块
139
+ * Protobuf 安全通信模块
140
140
  *
141
- * 特性:
142
- * - 支持加盐混淆(每次编码结果不同)
143
- * - 直接输出二进制格式
144
- * - 内置通用容器 schema(任意 JSON 数据)
145
- * - 支持自定义编解码函数
141
+ * 提供基于 Protobuf 的二进制序列化、XOR 混淆加密以及与 API Client 的无缝集成。
146
142
  */
147
143
 
148
- interface ProtobufCodecOptions {
149
- /** 是否启用加盐 (默认 true) */
150
- useSalt?: boolean;
151
- /** 自定义盐值生成器 */
152
- saltGenerator?: () => string;
153
- }
154
- interface ProtobufConfig {
155
- /** 自定义编码函数 */
156
- encode?: (data: unknown) => Uint8Array | Promise<Uint8Array>;
157
- /** 自定义解码函数 */
158
- decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
159
- /** 编码选项(仅内置编解码器使用) */
160
- options?: ProtobufCodecOptions;
161
- }
162
144
  /**
163
- * 使用内置 SaltedPayload schema 编码数据
164
- *
165
- * @param data - 任意 JSON 可序列化数据
166
- * @param options - 编码选项
167
- * @returns Uint8Array 二进制数据
168
- */
169
- declare function encodeProtobuf<T>(data: T, options?: ProtobufCodecOptions): Uint8Array;
170
- /**
171
- * 使用内置 SaltedPayload schema 解码数据
172
- *
173
- * @param buffer - 二进制数据
174
- * @returns 解码后的原始数据
145
+ * 基础编解码配置
146
+ * 控制如何将数据转换为二进制,以及是否混淆
175
147
  */
176
- declare function decodeProtobuf<T>(buffer: Uint8Array | ArrayBuffer): T;
148
+ interface ProtobufCodecOptions {
149
+ /** 是否启用二进制 XOR 混淆 (默认 true) */
150
+ obfuscate?: boolean;
151
+ /**
152
+ * 混淆密钥
153
+ * 可以是静态字符串/字节数组,也可以是动态获取密钥的同步或异步函数
154
+ */
155
+ obfuscationKey?: string | Uint8Array | (() => string | Uint8Array | Promise<string | Uint8Array>);
156
+ /** 预编译的 Protobuf 类型对象 (推荐,性能最高) */
157
+ protoType?: any;
158
+ /** 数据转换钩子:处理业务模型与 Proto 结构不一致的情况 */
159
+ transform?: {
160
+ beforeEncode?: (data: any) => any;
161
+ afterDecode?: (payload: any) => any;
162
+ };
163
+ }
177
164
  /**
178
- * 创建 Protobuf 二进制响应
179
- *
180
- * @param data - 要编码的数据
181
- * @param options - 编码选项和 CORS headers
182
- * @returns Response 对象
165
+ * 完全自定义编解码接口
166
+ * 用于外部项目完全接管序列化过程,同时复用基础库的混淆外壳
183
167
  */
184
- declare function protobufResponse<T>(data: T, options?: ProtobufCodecOptions & {
185
- corsHeaders?: Record<string, string>;
186
- }): Response;
168
+ interface ProtobufCustomCodec {
169
+ /** 外部定义的编码逻辑 (返回原始二进制) */
170
+ encode?: (data: any) => Uint8Array | Promise<Uint8Array>;
171
+ /** 外部定义的解码逻辑 (返回原始对象) */
172
+ decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
173
+ }
187
174
  /**
188
- * 检查 Content-Type 是否是 Protobuf 格式
175
+ * 集成配置项
176
+ * 用于 createProtobufHooks 或 API Client 全局配置
189
177
  */
190
- declare function isProtobufContentType(contentType: string | null): boolean;
178
+ interface ProtobufHooksOptions extends ProtobufCodecOptions, ProtobufCustomCodec {
179
+ }
191
180
  /**
192
- * 规范化 protobuf 配置
193
- * 将 boolean | ProtobufConfig 统一为 ProtobufConfig 或 null
181
+ * API Client 内部使用的标准化配置
194
182
  */
195
- declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined): ProtobufConfig | null;
183
+ interface ProtobufConfig {
184
+ encode: (data: any) => Uint8Array | Promise<Uint8Array>;
185
+ decode: <T>(buffer: Uint8Array) => T | Promise<T>;
186
+ options?: ProtobufCodecOptions;
187
+ }
188
+ /** 简单的二进制异或混淆转换 */
189
+ declare function xorTransform(data: Uint8Array, key: string | Uint8Array): Uint8Array;
190
+ /** 获取混淆密钥(支持异步/函数) */
191
+ declare function getObfuscationKey(keyOption?: ProtobufCodecOptions["obfuscationKey"]): Promise<string | Uint8Array>;
196
192
  /**
197
- * 解析 Protobuf 请求体 (Worker/Server 端使用)
193
+ * 编码安全载荷
198
194
  *
199
- * @param request - Request 对象
200
- * @param customDecode - 可选的自定义解码函数
201
- * @returns 解码后的数据
195
+ * 流程:业务数据 -> (自定义编码 / Proto 序列化) -> 二进制混淆
202
196
  */
203
- declare function parseProtobufRequest<T>(request: Request, customDecode?: (buffer: Uint8Array) => T | Promise<T>): Promise<T>;
197
+ declare function encodeSecure<T>(data: T, options?: ProtobufHooksOptions): Promise<Uint8Array>;
204
198
  /**
205
- * Protobuf 钩子选项
206
- */
207
- interface CreateProtobufHooksOptions extends ProtobufCodecOptions {
208
- /** 自定义编码函数 */
209
- encode?: (data: unknown) => Uint8Array | Promise<Uint8Array>;
210
- /** 自定义解码函数 */
211
- decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
212
- }
213
- /**
214
- * 创建 Protobuf 编解码钩子
215
- * 用于与 createApiClient 配合使用
216
- *
217
- * 钩子会自动:
218
- * - 设置 responseType 为 arrayBuffer
219
- * - 编码请求体为 Protobuf 格式
220
- * - 根据响应 Content-Type 自动解码(protobuf 或 JSON)
199
+ * 解码安全载荷
221
200
  *
222
- * @example
223
- * ```ts
224
- * import { createApiClient } from '@i.un/api-client';
225
- * import { createProtobufHooks } from '@i.un/api-client/protobuf';
226
- *
227
- * const { onRequest, onResponse } = createProtobufHooks();
228
- *
229
- * const client = createApiClient({
230
- * baseURL: 'https://api.example.com',
231
- * tokenStorage,
232
- * onRequest,
233
- * onResponse,
234
- * });
235
- *
236
- * // 自动处理,无需手动指定 responseType
237
- * const data = await client.post('/api/data', body);
238
- * ```
201
+ * 流程:二进制流 -> 二进制反混淆 -> (自定义解码 / Proto 反序列化) -> 业务数据
239
202
  */
240
- declare function createProtobufHooks(options?: CreateProtobufHooksOptions): {
241
- /**
242
- * 请求钩子:独立处理请求编码和响应类型设置
243
- */
203
+ declare function decodeSecure<T>(buffer: Uint8Array | ArrayBuffer, options?: ProtobufHooksOptions): Promise<T>;
204
+ /** 创建安全响应 (Worker/Server 端使用) */
205
+ declare function secureResponse<T>(data: T, options?: ProtobufCodecOptions & {
206
+ corsHeaders?: Record<string, string>;
207
+ }): Promise<Response>;
208
+ /** 解析安全请求体 (Worker/Server 端使用) */
209
+ declare function parseSecureRequest<T>(request: Request, customDecode?: ProtobufCustomCodec["decode"]): Promise<T>;
210
+ /** 检查是否为 Protobuf 响应头 */
211
+ declare function isProtobufContentType(contentType: string | null): boolean;
212
+ /** 标准化配置对象 */
213
+ declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined, options?: ProtobufHooksOptions): ProtobufConfig | null;
214
+ /** 创建 Protobuf 编解码钩子 (用于与 createApiClient 配合使用) */
215
+ declare function createProtobufHooks(options?: ProtobufHooksOptions): {
244
216
  onRequest(context: FetchContext): Promise<void>;
245
- /**
246
- * 响应钩子:解码 Protobuf 响应
247
- */
248
217
  onResponse(context: FetchContext): Promise<void>;
249
218
  };
250
219
 
251
- export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type CreateProtobufHooksOptions, type HttpRequestSpec, type NetworkAdapter, type NetworkHandler, type ProtobufCodecOptions, type ProtobufConfig, type TokenStorage, createApiClient, createProtobufHooks, decodeProtobuf, encodeProtobuf, executeRequestChain, isApiError, isProtobufContentType, normalizeProtobufConfig, parseProtobufRequest, protobufResponse };
220
+ export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type HttpRequestSpec, type NetworkAdapter, type NetworkHandler, type ProtobufCodecOptions, type ProtobufConfig, type ProtobufCustomCodec, type ProtobufHooksOptions, type TokenStorage, createApiClient, createProtobufHooks, decodeSecure, encodeSecure, executeRequestChain, getObfuscationKey, isApiError, isProtobufContentType, normalizeProtobufConfig, parseSecureRequest, secureResponse, xorTransform };