@i.un/api-client 1.0.9 → 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 +48 -94
- package/dist/index.d.ts +48 -94
- package/dist/index.js +207 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +204 -86
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/index.d.mts
CHANGED
|
@@ -136,131 +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
|
|
|
144
|
+
/**
|
|
145
|
+
* 基础编解码配置
|
|
146
|
+
* 控制如何将数据转换为二进制,以及是否混淆
|
|
147
|
+
*/
|
|
148
148
|
interface ProtobufCodecOptions {
|
|
149
|
-
/**
|
|
149
|
+
/** 是否启用二进制 XOR 混淆 (默认 true) */
|
|
150
150
|
obfuscate?: boolean;
|
|
151
151
|
/**
|
|
152
152
|
* 混淆密钥
|
|
153
153
|
* 可以是静态字符串/字节数组,也可以是动态获取密钥的同步或异步函数
|
|
154
154
|
*/
|
|
155
155
|
obfuscationKey?: string | Uint8Array | (() => string | Uint8Array | Promise<string | Uint8Array>);
|
|
156
|
-
/**
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
messageName?: string;
|
|
160
|
-
/** 自定义数据转换逻辑 (用于非默认 Schema) */
|
|
156
|
+
/** 预编译的 Protobuf 类型对象 (推荐,性能最高) */
|
|
157
|
+
protoType?: any;
|
|
158
|
+
/** 数据转换钩子:处理业务模型与 Proto 结构不一致的情况 */
|
|
161
159
|
transform?: {
|
|
162
|
-
/** 编码前转换:将原始数据转为符合 Schema 的对象 */
|
|
163
160
|
beforeEncode?: (data: any) => any;
|
|
164
|
-
/** 解码后转换:将 Schema 对象转回原始数据 */
|
|
165
161
|
afterDecode?: (payload: any) => any;
|
|
166
162
|
};
|
|
167
163
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
164
|
+
/**
|
|
165
|
+
* 完全自定义编解码接口
|
|
166
|
+
* 用于外部项目完全接管序列化过程,同时复用基础库的混淆外壳
|
|
167
|
+
*/
|
|
168
|
+
interface ProtobufCustomCodec {
|
|
169
|
+
/** 外部定义的编码逻辑 (返回原始二进制) */
|
|
170
|
+
encode?: (data: any) => Uint8Array | Promise<Uint8Array>;
|
|
171
|
+
/** 外部定义的解码逻辑 (返回原始对象) */
|
|
172
172
|
decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
|
|
173
|
-
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 集成配置项
|
|
176
|
+
* 用于 createProtobufHooks 或 API Client 全局配置
|
|
177
|
+
*/
|
|
178
|
+
interface ProtobufHooksOptions extends ProtobufCodecOptions, ProtobufCustomCodec {
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* API Client 内部使用的标准化配置
|
|
182
|
+
*/
|
|
183
|
+
interface ProtobufConfig {
|
|
184
|
+
encode: (data: any) => Uint8Array | Promise<Uint8Array>;
|
|
185
|
+
decode: <T>(buffer: Uint8Array) => T | Promise<T>;
|
|
174
186
|
options?: ProtobufCodecOptions;
|
|
175
187
|
}
|
|
188
|
+
/** 简单的二进制异或混淆转换 */
|
|
189
|
+
declare function xorTransform(data: Uint8Array, key: string | Uint8Array): Uint8Array;
|
|
190
|
+
/** 获取混淆密钥(支持异步/函数) */
|
|
191
|
+
declare function getObfuscationKey(keyOption?: ProtobufCodecOptions["obfuscationKey"]): Promise<string | Uint8Array>;
|
|
176
192
|
/**
|
|
177
193
|
* 编码安全载荷
|
|
178
194
|
*
|
|
179
|
-
*
|
|
180
|
-
* @param options - 编解码选项
|
|
181
|
-
* @returns Uint8Array 混淆后的二进制数据
|
|
195
|
+
* 流程:业务数据 -> (自定义编码 / Proto 序列化) -> 二进制混淆
|
|
182
196
|
*/
|
|
183
|
-
declare function encodeSecure<T>(data: T, options?:
|
|
197
|
+
declare function encodeSecure<T>(data: T, options?: ProtobufHooksOptions): Promise<Uint8Array>;
|
|
184
198
|
/**
|
|
185
199
|
* 解码安全载荷
|
|
186
200
|
*
|
|
187
|
-
*
|
|
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 对象
|
|
201
|
+
* 流程:二进制流 -> 二进制反混淆 -> (自定义解码 / Proto 反序列化) -> 业务数据
|
|
198
202
|
*/
|
|
203
|
+
declare function decodeSecure<T>(buffer: Uint8Array | ArrayBuffer, options?: ProtobufHooksOptions): Promise<T>;
|
|
204
|
+
/** 创建安全响应 (Worker/Server 端使用) */
|
|
199
205
|
declare function secureResponse<T>(data: T, options?: ProtobufCodecOptions & {
|
|
200
206
|
corsHeaders?: Record<string, string>;
|
|
201
207
|
}): Promise<Response>;
|
|
202
|
-
/**
|
|
203
|
-
|
|
204
|
-
*/
|
|
208
|
+
/** 解析安全请求体 (Worker/Server 端使用) */
|
|
209
|
+
declare function parseSecureRequest<T>(request: Request, customDecode?: ProtobufCustomCodec["decode"]): Promise<T>;
|
|
210
|
+
/** 检查是否为 Protobuf 响应头 */
|
|
205
211
|
declare function isProtobufContentType(contentType: string | null): boolean;
|
|
206
|
-
/**
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
*/
|
|
212
|
+
/** 标准化配置对象 */
|
|
213
|
+
declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined, options?: ProtobufHooksOptions): ProtobufConfig | null;
|
|
214
|
+
/** 创建 Protobuf 编解码钩子 (用于与 createApiClient 配合使用) */
|
|
215
|
+
declare function createProtobufHooks(options?: ProtobufHooksOptions): {
|
|
259
216
|
onRequest(context: FetchContext): Promise<void>;
|
|
260
|
-
/**
|
|
261
|
-
* 响应钩子:解码 Protobuf 响应
|
|
262
|
-
*/
|
|
263
217
|
onResponse(context: FetchContext): Promise<void>;
|
|
264
218
|
};
|
|
265
219
|
|
|
266
|
-
export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type
|
|
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,131 +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
|
|
|
144
|
+
/**
|
|
145
|
+
* 基础编解码配置
|
|
146
|
+
* 控制如何将数据转换为二进制,以及是否混淆
|
|
147
|
+
*/
|
|
148
148
|
interface ProtobufCodecOptions {
|
|
149
|
-
/**
|
|
149
|
+
/** 是否启用二进制 XOR 混淆 (默认 true) */
|
|
150
150
|
obfuscate?: boolean;
|
|
151
151
|
/**
|
|
152
152
|
* 混淆密钥
|
|
153
153
|
* 可以是静态字符串/字节数组,也可以是动态获取密钥的同步或异步函数
|
|
154
154
|
*/
|
|
155
155
|
obfuscationKey?: string | Uint8Array | (() => string | Uint8Array | Promise<string | Uint8Array>);
|
|
156
|
-
/**
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
messageName?: string;
|
|
160
|
-
/** 自定义数据转换逻辑 (用于非默认 Schema) */
|
|
156
|
+
/** 预编译的 Protobuf 类型对象 (推荐,性能最高) */
|
|
157
|
+
protoType?: any;
|
|
158
|
+
/** 数据转换钩子:处理业务模型与 Proto 结构不一致的情况 */
|
|
161
159
|
transform?: {
|
|
162
|
-
/** 编码前转换:将原始数据转为符合 Schema 的对象 */
|
|
163
160
|
beforeEncode?: (data: any) => any;
|
|
164
|
-
/** 解码后转换:将 Schema 对象转回原始数据 */
|
|
165
161
|
afterDecode?: (payload: any) => any;
|
|
166
162
|
};
|
|
167
163
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
164
|
+
/**
|
|
165
|
+
* 完全自定义编解码接口
|
|
166
|
+
* 用于外部项目完全接管序列化过程,同时复用基础库的混淆外壳
|
|
167
|
+
*/
|
|
168
|
+
interface ProtobufCustomCodec {
|
|
169
|
+
/** 外部定义的编码逻辑 (返回原始二进制) */
|
|
170
|
+
encode?: (data: any) => Uint8Array | Promise<Uint8Array>;
|
|
171
|
+
/** 外部定义的解码逻辑 (返回原始对象) */
|
|
172
172
|
decode?: <T>(buffer: Uint8Array) => T | Promise<T>;
|
|
173
|
-
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 集成配置项
|
|
176
|
+
* 用于 createProtobufHooks 或 API Client 全局配置
|
|
177
|
+
*/
|
|
178
|
+
interface ProtobufHooksOptions extends ProtobufCodecOptions, ProtobufCustomCodec {
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* API Client 内部使用的标准化配置
|
|
182
|
+
*/
|
|
183
|
+
interface ProtobufConfig {
|
|
184
|
+
encode: (data: any) => Uint8Array | Promise<Uint8Array>;
|
|
185
|
+
decode: <T>(buffer: Uint8Array) => T | Promise<T>;
|
|
174
186
|
options?: ProtobufCodecOptions;
|
|
175
187
|
}
|
|
188
|
+
/** 简单的二进制异或混淆转换 */
|
|
189
|
+
declare function xorTransform(data: Uint8Array, key: string | Uint8Array): Uint8Array;
|
|
190
|
+
/** 获取混淆密钥(支持异步/函数) */
|
|
191
|
+
declare function getObfuscationKey(keyOption?: ProtobufCodecOptions["obfuscationKey"]): Promise<string | Uint8Array>;
|
|
176
192
|
/**
|
|
177
193
|
* 编码安全载荷
|
|
178
194
|
*
|
|
179
|
-
*
|
|
180
|
-
* @param options - 编解码选项
|
|
181
|
-
* @returns Uint8Array 混淆后的二进制数据
|
|
195
|
+
* 流程:业务数据 -> (自定义编码 / Proto 序列化) -> 二进制混淆
|
|
182
196
|
*/
|
|
183
|
-
declare function encodeSecure<T>(data: T, options?:
|
|
197
|
+
declare function encodeSecure<T>(data: T, options?: ProtobufHooksOptions): Promise<Uint8Array>;
|
|
184
198
|
/**
|
|
185
199
|
* 解码安全载荷
|
|
186
200
|
*
|
|
187
|
-
*
|
|
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 对象
|
|
201
|
+
* 流程:二进制流 -> 二进制反混淆 -> (自定义解码 / Proto 反序列化) -> 业务数据
|
|
198
202
|
*/
|
|
203
|
+
declare function decodeSecure<T>(buffer: Uint8Array | ArrayBuffer, options?: ProtobufHooksOptions): Promise<T>;
|
|
204
|
+
/** 创建安全响应 (Worker/Server 端使用) */
|
|
199
205
|
declare function secureResponse<T>(data: T, options?: ProtobufCodecOptions & {
|
|
200
206
|
corsHeaders?: Record<string, string>;
|
|
201
207
|
}): Promise<Response>;
|
|
202
|
-
/**
|
|
203
|
-
|
|
204
|
-
*/
|
|
208
|
+
/** 解析安全请求体 (Worker/Server 端使用) */
|
|
209
|
+
declare function parseSecureRequest<T>(request: Request, customDecode?: ProtobufCustomCodec["decode"]): Promise<T>;
|
|
210
|
+
/** 检查是否为 Protobuf 响应头 */
|
|
205
211
|
declare function isProtobufContentType(contentType: string | null): boolean;
|
|
206
|
-
/**
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
*/
|
|
212
|
+
/** 标准化配置对象 */
|
|
213
|
+
declare function normalizeProtobufConfig(config: boolean | ProtobufConfig | undefined, options?: ProtobufHooksOptions): ProtobufConfig | null;
|
|
214
|
+
/** 创建 Protobuf 编解码钩子 (用于与 createApiClient 配合使用) */
|
|
215
|
+
declare function createProtobufHooks(options?: ProtobufHooksOptions): {
|
|
259
216
|
onRequest(context: FetchContext): Promise<void>;
|
|
260
|
-
/**
|
|
261
|
-
* 响应钩子:解码 Protobuf 响应
|
|
262
|
-
*/
|
|
263
217
|
onResponse(context: FetchContext): Promise<void>;
|
|
264
218
|
};
|
|
265
219
|
|
|
266
|
-
export { type ApiError, type ApiResult, type ChainRequestRule, type CreateApiClientOptions, type
|
|
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 };
|