@i.un/api-client 1.0.9 → 1.1.1

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