@be-link/ecommerce-backend-bff-service-node-sdk 0.0.5 → 0.0.6
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/{bff → cjs/bff}/request/client.js +3 -2
- package/{utils → cjs/utils}/string.d.ts +8 -0
- package/cjs/utils/string.js +41 -0
- package/esm/bff/modules/BaseService.d.ts +41 -0
- package/esm/bff/modules/BaseService.mjs +38 -0
- package/esm/bff/modules/demo/service.d.ts +22 -0
- package/esm/bff/modules/demo/service.mjs +25 -0
- package/esm/bff/modules/demo/types.d.ts +28 -0
- package/esm/bff/modules/demo/types.mjs +1 -0
- package/esm/bff/modules/example/service.d.ts +46 -0
- package/esm/bff/modules/example/service.mjs +53 -0
- package/esm/bff/modules/example/types.d.ts +133 -0
- package/esm/bff/modules/example/types.mjs +1 -0
- package/esm/bff/request/client.d.ts +45 -0
- package/esm/bff/request/client.mjs +61 -0
- package/esm/bff/request/strategy.d.ts +51 -0
- package/esm/bff/request/strategy.mjs +179 -0
- package/esm/errors/index.d.ts +27 -0
- package/esm/errors/index.mjs +46 -0
- package/esm/index.d.ts +54 -0
- package/esm/index.mjs +46 -0
- package/esm/types/index.d.ts +58 -0
- package/esm/types/index.mjs +1 -0
- package/esm/utils/env.d.ts +23 -0
- package/esm/utils/env.mjs +39 -0
- package/esm/utils/http.d.ts +5 -0
- package/esm/utils/http.mjs +33 -0
- package/{utils/string.js → esm/utils/string.d.ts} +9 -9
- package/esm/utils/string.mjs +37 -0
- package/package.json +17 -13
- /package/{bff → cjs/bff}/modules/BaseService.d.ts +0 -0
- /package/{bff → cjs/bff}/modules/BaseService.js +0 -0
- /package/{bff → cjs/bff}/modules/demo/service.d.ts +0 -0
- /package/{bff → cjs/bff}/modules/demo/service.js +0 -0
- /package/{bff → cjs/bff}/modules/demo/types.d.ts +0 -0
- /package/{bff → cjs/bff}/modules/demo/types.js +0 -0
- /package/{bff → cjs/bff}/modules/example/service.d.ts +0 -0
- /package/{bff → cjs/bff}/modules/example/service.js +0 -0
- /package/{bff → cjs/bff}/modules/example/types.d.ts +0 -0
- /package/{bff → cjs/bff}/modules/example/types.js +0 -0
- /package/{bff → cjs/bff}/request/client.d.ts +0 -0
- /package/{bff → cjs/bff}/request/strategy.d.ts +0 -0
- /package/{bff → cjs/bff}/request/strategy.js +0 -0
- /package/{errors → cjs/errors}/index.d.ts +0 -0
- /package/{errors → cjs/errors}/index.js +0 -0
- /package/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/{index.js → cjs/index.js} +0 -0
- /package/{types → cjs/types}/index.d.ts +0 -0
- /package/{types → cjs/types}/index.js +0 -0
- /package/{utils → cjs/utils}/env.d.ts +0 -0
- /package/{utils → cjs/utils}/env.js +0 -0
- /package/{utils → cjs/utils}/http.d.ts +0 -0
- /package/{utils → cjs/utils}/http.js +0 -0
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.HttpClient = void 0;
|
|
4
4
|
const strategy_1 = require("./strategy");
|
|
5
5
|
const env_1 = require("../../utils/env");
|
|
6
|
+
const string_1 = require("../../utils/string");
|
|
6
7
|
/**
|
|
7
8
|
* HttpClient 工厂类,管理平台特定的请求策略
|
|
8
9
|
* 自动检测运行时环境并创建相应的策略
|
|
@@ -42,8 +43,8 @@ class HttpClient {
|
|
|
42
43
|
Promise.resolve(this.options.getUserId()).catch(() => ''),
|
|
43
44
|
Promise.resolve(this.options.getRuleId?.() ?? '').catch(() => ''),
|
|
44
45
|
]);
|
|
45
|
-
// 生成唯一的请求 ID
|
|
46
|
-
const requestId = `b-${
|
|
46
|
+
// 生成唯一的请求 ID(兼容所有环境)
|
|
47
|
+
const requestId = `b-${(0, string_1.generateUUID)()}`;
|
|
47
48
|
// 构造标准请求头
|
|
48
49
|
const standardHeaders = {
|
|
49
50
|
'Content-Type': 'application/json',
|
|
@@ -10,3 +10,11 @@
|
|
|
10
10
|
* camelToKebabCase('myHTTPRequest') // 'my-http-request'
|
|
11
11
|
*/
|
|
12
12
|
export declare function camelToKebabCase(str: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* 生成 UUID v4 格式的唯一标识符
|
|
15
|
+
* 兼容浏览器、Node.js 和微信小程序环境
|
|
16
|
+
* @returns UUID 字符串,格式为 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
17
|
+
* @example
|
|
18
|
+
* generateUUID() // '550e8400-e29b-41d4-a716-446655440000'
|
|
19
|
+
*/
|
|
20
|
+
export declare function generateUUID(): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.camelToKebabCase = camelToKebabCase;
|
|
4
|
+
exports.generateUUID = generateUUID;
|
|
5
|
+
/**
|
|
6
|
+
* 将驼峰命名字符串转换为短横线命名
|
|
7
|
+
* @param str 驼峰命名字符串
|
|
8
|
+
* @returns 短横线命名字符串
|
|
9
|
+
* @example
|
|
10
|
+
* camelToKebabCase('fetchConfig') // 'fetch-config'
|
|
11
|
+
* camelToKebabCase('getCosTempSecret') // 'get-cos-temp-secret'
|
|
12
|
+
* camelToKebabCase('getUserProfile') // 'get-user-profile'
|
|
13
|
+
* camelToKebabCase('API') // 'api'
|
|
14
|
+
* camelToKebabCase('myHTTPRequest') // 'my-http-request'
|
|
15
|
+
*/
|
|
16
|
+
function camelToKebabCase(str) {
|
|
17
|
+
return str
|
|
18
|
+
.replace(/([A-Z])/g, '-$1')
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/^-/, ''); // 如果存在则移除前导连字符
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 生成 UUID v4 格式的唯一标识符
|
|
24
|
+
* 兼容浏览器、Node.js 和微信小程序环境
|
|
25
|
+
* @returns UUID 字符串,格式为 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
26
|
+
* @example
|
|
27
|
+
* generateUUID() // '550e8400-e29b-41d4-a716-446655440000'
|
|
28
|
+
*/
|
|
29
|
+
function generateUUID() {
|
|
30
|
+
// 优先使用原生 crypto.randomUUID(浏览器和 Node.js 18+)
|
|
31
|
+
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
32
|
+
return crypto.randomUUID();
|
|
33
|
+
}
|
|
34
|
+
// 降级方案:使用 Math.random() 生成 UUID v4
|
|
35
|
+
// 格式:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
36
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
37
|
+
const r = (Math.random() * 16) | 0;
|
|
38
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
39
|
+
return v.toString(16);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HttpClient } from '../request/client';
|
|
2
|
+
/**
|
|
3
|
+
* BaseService 基类
|
|
4
|
+
* 所有服务类都应该继承此类
|
|
5
|
+
* 提供通过 HttpClient 发起 API 请求的通用功能
|
|
6
|
+
*/
|
|
7
|
+
export default abstract class BaseService {
|
|
8
|
+
/**
|
|
9
|
+
* 服务的 URL 前缀路径(例如 '/api/v1/user')
|
|
10
|
+
* 必须由子类定义
|
|
11
|
+
*/
|
|
12
|
+
protected abstract prefixUrl: string;
|
|
13
|
+
/**
|
|
14
|
+
* 用于发起请求的 HttpClient 实例
|
|
15
|
+
* 通过构造函数注入
|
|
16
|
+
*/
|
|
17
|
+
protected http: HttpClient;
|
|
18
|
+
/**
|
|
19
|
+
* 使用 HttpClient 初始化 BaseService
|
|
20
|
+
* @param http - 用于发起 API 请求的 HttpClient 实例
|
|
21
|
+
*/
|
|
22
|
+
constructor(http: HttpClient);
|
|
23
|
+
/**
|
|
24
|
+
* 将函数引用转换为短横线命名的 API 路径
|
|
25
|
+
* @param func - 函数引用(通常是服务类的方法)
|
|
26
|
+
* @returns 从函数名派生的短横线命名路径段
|
|
27
|
+
* @example
|
|
28
|
+
* getPath(this.getUserProfile) // 返回 'get-user-profile'
|
|
29
|
+
*/
|
|
30
|
+
protected getPath(func: Function): string;
|
|
31
|
+
/**
|
|
32
|
+
* 通过组合 prefixUrl 和方法路径执行 API 请求
|
|
33
|
+
* @param func - 用于派生 API 路径的函数引用
|
|
34
|
+
* @param data - 请求负载数据(可选)
|
|
35
|
+
* @returns Promise,解析为响应数据
|
|
36
|
+
* @throws SdkError 如果请求失败
|
|
37
|
+
* @example
|
|
38
|
+
* await this.request(this.getUserProfile, { id: '123' })
|
|
39
|
+
*/
|
|
40
|
+
protected request<T>(func: Function, data?: any): Promise<T>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { camelToKebabCase } from '../../utils/string.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* BaseService 基类
|
|
4
|
+
* 所有服务类都应该继承此类
|
|
5
|
+
* 提供通过 HttpClient 发起 API 请求的通用功能
|
|
6
|
+
*/
|
|
7
|
+
export default class BaseService {
|
|
8
|
+
/**
|
|
9
|
+
* 使用 HttpClient 初始化 BaseService
|
|
10
|
+
* @param http - 用于发起 API 请求的 HttpClient 实例
|
|
11
|
+
*/
|
|
12
|
+
constructor(http) {
|
|
13
|
+
this.http = http;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 将函数引用转换为短横线命名的 API 路径
|
|
17
|
+
* @param func - 函数引用(通常是服务类的方法)
|
|
18
|
+
* @returns 从函数名派生的短横线命名路径段
|
|
19
|
+
* @example
|
|
20
|
+
* getPath(this.getUserProfile) // 返回 'get-user-profile'
|
|
21
|
+
*/
|
|
22
|
+
getPath(func) {
|
|
23
|
+
return camelToKebabCase(func.name);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 通过组合 prefixUrl 和方法路径执行 API 请求
|
|
27
|
+
* @param func - 用于派生 API 路径的函数引用
|
|
28
|
+
* @param data - 请求负载数据(可选)
|
|
29
|
+
* @returns Promise,解析为响应数据
|
|
30
|
+
* @throws SdkError 如果请求失败
|
|
31
|
+
* @example
|
|
32
|
+
* await this.request(this.getUserProfile, { id: '123' })
|
|
33
|
+
*/
|
|
34
|
+
async request(func, data) {
|
|
35
|
+
const path = `${this.prefixUrl}/${this.getPath(func)}`;
|
|
36
|
+
return this.http.request({ path, data });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Service } from './types';
|
|
2
|
+
import BaseService from '../BaseService';
|
|
3
|
+
import type { StandardResponse } from '../../../types';
|
|
4
|
+
/**
|
|
5
|
+
* DemoService - Demo 服务模块
|
|
6
|
+
* 提供 Demo 相关的 API 方法
|
|
7
|
+
*/
|
|
8
|
+
declare class DemoService extends BaseService {
|
|
9
|
+
protected prefixUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* Demo 方法
|
|
12
|
+
* @param request - 请求参数
|
|
13
|
+
* @returns Promise,解析为标准响应格式
|
|
14
|
+
* @example
|
|
15
|
+
* const result = await sdk.demo.demoFunc({ demoParam: 'test' })
|
|
16
|
+
* console.log(result.data) // 业务数据
|
|
17
|
+
* console.log(result.message) // 响应消息
|
|
18
|
+
* console.log(result.requestId) // 请求 ID
|
|
19
|
+
*/
|
|
20
|
+
demoFunc(request: Service.Request.demoFunc): Promise<StandardResponse<Service.Response.demoFunc>>;
|
|
21
|
+
}
|
|
22
|
+
export default DemoService;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import BaseService from '../BaseService.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* DemoService - Demo 服务模块
|
|
4
|
+
* 提供 Demo 相关的 API 方法
|
|
5
|
+
*/
|
|
6
|
+
class DemoService extends BaseService {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.prefixUrl = '/demo';
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Demo 方法
|
|
13
|
+
* @param request - 请求参数
|
|
14
|
+
* @returns Promise,解析为标准响应格式
|
|
15
|
+
* @example
|
|
16
|
+
* const result = await sdk.demo.demoFunc({ demoParam: 'test' })
|
|
17
|
+
* console.log(result.data) // 业务数据
|
|
18
|
+
* console.log(result.message) // 响应消息
|
|
19
|
+
* console.log(result.requestId) // 请求 ID
|
|
20
|
+
*/
|
|
21
|
+
demoFunc(request) {
|
|
22
|
+
return this.request(this.demoFunc, request);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export default DemoService;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare namespace Service {
|
|
2
|
+
namespace Entity { }
|
|
3
|
+
namespace Request {
|
|
4
|
+
/**
|
|
5
|
+
* 获取配置请求参数
|
|
6
|
+
*/
|
|
7
|
+
interface demoFunc {
|
|
8
|
+
/** 参数 */
|
|
9
|
+
demoParam: string;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
namespace Response {
|
|
13
|
+
interface demoFunc {
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Config Controller 接口定义
|
|
18
|
+
*/
|
|
19
|
+
interface DemoController {
|
|
20
|
+
/**
|
|
21
|
+
* 示例接口
|
|
22
|
+
* @param request 请求参数
|
|
23
|
+
* @param req 请求上下文
|
|
24
|
+
* @returns 响应数据
|
|
25
|
+
*/
|
|
26
|
+
demoFunc(request: Service.Request.demoFunc, req: any): Promise<Service.Response.demoFunc>;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import BaseService from '../BaseService';
|
|
2
|
+
import type { StandardResponse } from '../../../types';
|
|
3
|
+
import { Service } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* ExampleService - 演示如何创建服务模块
|
|
6
|
+
* 此服务提供用户资料和订单管理的示例方法
|
|
7
|
+
*/
|
|
8
|
+
export declare class ExampleService extends BaseService {
|
|
9
|
+
/**
|
|
10
|
+
* 此服务的 API 路径前缀
|
|
11
|
+
* 此服务中的所有方法都将使用此前缀
|
|
12
|
+
*/
|
|
13
|
+
protected prefixUrl: string;
|
|
14
|
+
/**
|
|
15
|
+
* 根据 ID 获取用户资料
|
|
16
|
+
* @param request - 请求参数
|
|
17
|
+
* @returns Promise,解析为标准响应格式,包含用户资料数据
|
|
18
|
+
* @example
|
|
19
|
+
* const result = await exampleService.getUserProfile({ id: 'user123' })
|
|
20
|
+
* console.log(result.data) // 用户资料数据
|
|
21
|
+
* console.log(result.message) // 响应消息
|
|
22
|
+
* console.log(result.requestId) // 请求 ID
|
|
23
|
+
*/
|
|
24
|
+
getUserProfile(request: Service.Request.getUserProfile): Promise<StandardResponse<Service.Response.getUserProfile>>;
|
|
25
|
+
/**
|
|
26
|
+
* 更新用户资料
|
|
27
|
+
* @param request - 请求参数
|
|
28
|
+
* @returns Promise,解析为标准响应格式
|
|
29
|
+
* @example
|
|
30
|
+
* const result = await exampleService.updateProfile({ name: 'John Doe', email: 'john@example.com' })
|
|
31
|
+
* console.log(result.message) // 'success'
|
|
32
|
+
* console.log(result.success) // true
|
|
33
|
+
*/
|
|
34
|
+
updateProfile(request: Service.Request.updateProfile): Promise<StandardResponse<Service.Response.updateProfile>>;
|
|
35
|
+
/**
|
|
36
|
+
* 获取分页订单列表
|
|
37
|
+
* @param request - 请求参数
|
|
38
|
+
* @returns Promise,解析为标准响应格式,包含订单列表数据
|
|
39
|
+
* @example
|
|
40
|
+
* const result = await exampleService.getOrderList({ page: 1, pageSize: 20 })
|
|
41
|
+
* console.log(result.data.orders) // 订单列表
|
|
42
|
+
* console.log(result.data.total) // 总数
|
|
43
|
+
* console.log(result.requestId) // 请求 ID
|
|
44
|
+
*/
|
|
45
|
+
getOrderList(request: Service.Request.getOrderList): Promise<StandardResponse<Service.Response.getOrderList>>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import BaseService from '../BaseService.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* ExampleService - 演示如何创建服务模块
|
|
4
|
+
* 此服务提供用户资料和订单管理的示例方法
|
|
5
|
+
*/
|
|
6
|
+
export class ExampleService extends BaseService {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
/**
|
|
10
|
+
* 此服务的 API 路径前缀
|
|
11
|
+
* 此服务中的所有方法都将使用此前缀
|
|
12
|
+
*/
|
|
13
|
+
this.prefixUrl = '/api/v1/user';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 根据 ID 获取用户资料
|
|
17
|
+
* @param request - 请求参数
|
|
18
|
+
* @returns Promise,解析为标准响应格式,包含用户资料数据
|
|
19
|
+
* @example
|
|
20
|
+
* const result = await exampleService.getUserProfile({ id: 'user123' })
|
|
21
|
+
* console.log(result.data) // 用户资料数据
|
|
22
|
+
* console.log(result.message) // 响应消息
|
|
23
|
+
* console.log(result.requestId) // 请求 ID
|
|
24
|
+
*/
|
|
25
|
+
async getUserProfile(request) {
|
|
26
|
+
return this.request(this.getUserProfile, request);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 更新用户资料
|
|
30
|
+
* @param request - 请求参数
|
|
31
|
+
* @returns Promise,解析为标准响应格式
|
|
32
|
+
* @example
|
|
33
|
+
* const result = await exampleService.updateProfile({ name: 'John Doe', email: 'john@example.com' })
|
|
34
|
+
* console.log(result.message) // 'success'
|
|
35
|
+
* console.log(result.success) // true
|
|
36
|
+
*/
|
|
37
|
+
async updateProfile(request) {
|
|
38
|
+
return this.request(this.updateProfile, request);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 获取分页订单列表
|
|
42
|
+
* @param request - 请求参数
|
|
43
|
+
* @returns Promise,解析为标准响应格式,包含订单列表数据
|
|
44
|
+
* @example
|
|
45
|
+
* const result = await exampleService.getOrderList({ page: 1, pageSize: 20 })
|
|
46
|
+
* console.log(result.data.orders) // 订单列表
|
|
47
|
+
* console.log(result.data.total) // 总数
|
|
48
|
+
* console.log(result.requestId) // 请求 ID
|
|
49
|
+
*/
|
|
50
|
+
async getOrderList(request) {
|
|
51
|
+
return this.request(this.getOrderList, request);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example Service 的类型定义
|
|
3
|
+
* 使用命名空间组织类型,与后端 SDK 保持一致
|
|
4
|
+
*/
|
|
5
|
+
export declare namespace Service {
|
|
6
|
+
/**
|
|
7
|
+
* 实体定义
|
|
8
|
+
*/
|
|
9
|
+
namespace Entity {
|
|
10
|
+
/**
|
|
11
|
+
* 用户资料数据结构
|
|
12
|
+
*/
|
|
13
|
+
interface UserProfile {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
email: string;
|
|
17
|
+
phone?: string;
|
|
18
|
+
avatar?: string;
|
|
19
|
+
createdAt: string;
|
|
20
|
+
updatedAt: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 订单项结构
|
|
24
|
+
*/
|
|
25
|
+
interface Order {
|
|
26
|
+
id: string;
|
|
27
|
+
userId: string;
|
|
28
|
+
orderNumber: string;
|
|
29
|
+
status: string;
|
|
30
|
+
totalAmount: number;
|
|
31
|
+
items: OrderItem[];
|
|
32
|
+
createdAt: string;
|
|
33
|
+
updatedAt: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 订单项详情
|
|
37
|
+
*/
|
|
38
|
+
interface OrderItem {
|
|
39
|
+
id: string;
|
|
40
|
+
productId: string;
|
|
41
|
+
productName: string;
|
|
42
|
+
quantity: number;
|
|
43
|
+
price: number;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 请求参数定义
|
|
48
|
+
*/
|
|
49
|
+
namespace Request {
|
|
50
|
+
/**
|
|
51
|
+
* 获取用户资料的请求参数
|
|
52
|
+
*/
|
|
53
|
+
interface getUserProfile {
|
|
54
|
+
/** 用户 ID */
|
|
55
|
+
id: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 更新用户资料的请求负载
|
|
59
|
+
*/
|
|
60
|
+
interface updateProfile {
|
|
61
|
+
name?: string;
|
|
62
|
+
email?: string;
|
|
63
|
+
phone?: string;
|
|
64
|
+
avatar?: string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 获取订单列表的请求负载
|
|
68
|
+
*/
|
|
69
|
+
interface getOrderList {
|
|
70
|
+
page: number;
|
|
71
|
+
pageSize?: number;
|
|
72
|
+
status?: string;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 响应数据定义
|
|
77
|
+
*/
|
|
78
|
+
namespace Response {
|
|
79
|
+
/**
|
|
80
|
+
* 获取用户资料的响应
|
|
81
|
+
*/
|
|
82
|
+
interface getUserProfile {
|
|
83
|
+
id: string;
|
|
84
|
+
name: string;
|
|
85
|
+
email: string;
|
|
86
|
+
phone?: string;
|
|
87
|
+
avatar?: string;
|
|
88
|
+
createdAt: string;
|
|
89
|
+
updatedAt: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 更新用户资料的响应
|
|
93
|
+
*/
|
|
94
|
+
interface updateProfile {
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 订单列表的响应结构
|
|
98
|
+
*/
|
|
99
|
+
interface getOrderList {
|
|
100
|
+
orders: Entity.Order[];
|
|
101
|
+
total: number;
|
|
102
|
+
page: number;
|
|
103
|
+
pageSize: number;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Example Controller 接口定义
|
|
108
|
+
* 用于后端 Server 实现
|
|
109
|
+
*/
|
|
110
|
+
interface ExampleController {
|
|
111
|
+
/**
|
|
112
|
+
* 获取用户资料
|
|
113
|
+
* @param request 请求参数
|
|
114
|
+
* @param req 请求上下文
|
|
115
|
+
* @returns 用户资料数据
|
|
116
|
+
*/
|
|
117
|
+
getUserProfile(request: Service.Request.getUserProfile, req: any): Promise<Service.Response.getUserProfile>;
|
|
118
|
+
/**
|
|
119
|
+
* 更新用户资料
|
|
120
|
+
* @param request 请求参数
|
|
121
|
+
* @param req 请求上下文
|
|
122
|
+
* @returns 响应数据
|
|
123
|
+
*/
|
|
124
|
+
updateProfile(request: Service.Request.updateProfile, req: any): Promise<Service.Response.updateProfile>;
|
|
125
|
+
/**
|
|
126
|
+
* 获取订单列表
|
|
127
|
+
* @param request 请求参数
|
|
128
|
+
* @param req 请求上下文
|
|
129
|
+
* @returns 订单列表数据
|
|
130
|
+
*/
|
|
131
|
+
getOrderList(request: Service.Request.getOrderList, req: any): Promise<Service.Response.getOrderList>;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { SdkOptions } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* HttpClient 工厂类,管理平台特定的请求策略
|
|
4
|
+
* 自动检测运行时环境并创建相应的策略
|
|
5
|
+
*/
|
|
6
|
+
export declare class HttpClient {
|
|
7
|
+
/**
|
|
8
|
+
* 请求策略实例(Web 或小程序)
|
|
9
|
+
*/
|
|
10
|
+
private strategy;
|
|
11
|
+
/**
|
|
12
|
+
* 标识 SDK 是否运行在微信小程序环境中
|
|
13
|
+
*/
|
|
14
|
+
readonly isMiniProgram: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* SDK 配置选项,包含凭证回调函数
|
|
17
|
+
*/
|
|
18
|
+
private options;
|
|
19
|
+
/**
|
|
20
|
+
* 运行环境配置
|
|
21
|
+
*/
|
|
22
|
+
private environment;
|
|
23
|
+
/**
|
|
24
|
+
* 使用 SDK 选项初始化 HttpClient
|
|
25
|
+
* 自动检测平台并创建相应的请求策略
|
|
26
|
+
* @param options - SDK 配置,包含 getToken、getUserId 回调函数和 environment
|
|
27
|
+
*/
|
|
28
|
+
constructor(options: SdkOptions);
|
|
29
|
+
/**
|
|
30
|
+
* 工厂方法,根据平台创建相应的请求策略
|
|
31
|
+
* @returns BaseRequestStrategy 实例(WebRequestStrategy 或 MiniProgramRequestStrategy)
|
|
32
|
+
*/
|
|
33
|
+
private createStrategy;
|
|
34
|
+
/**
|
|
35
|
+
* 执行 API 请求,自动获取凭证并构造请求头
|
|
36
|
+
* @param opts - 请求选项,包含路径、数据和可选的自定义请求头
|
|
37
|
+
* @returns Promise,解析为响应数据
|
|
38
|
+
* @throws SdkError 如果请求失败
|
|
39
|
+
*/
|
|
40
|
+
request<T>(opts: {
|
|
41
|
+
path: string;
|
|
42
|
+
data?: any;
|
|
43
|
+
headers?: Record<string, string>;
|
|
44
|
+
}): Promise<T>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { WebRequestStrategy, MiniProgramRequestStrategy } from './strategy.mjs';
|
|
2
|
+
import { isMiniProgram } from '../../utils/env.mjs';
|
|
3
|
+
import { generateUUID } from '../../utils/string.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* HttpClient 工厂类,管理平台特定的请求策略
|
|
6
|
+
* 自动检测运行时环境并创建相应的策略
|
|
7
|
+
*/
|
|
8
|
+
export class HttpClient {
|
|
9
|
+
/**
|
|
10
|
+
* 使用 SDK 选项初始化 HttpClient
|
|
11
|
+
* 自动检测平台并创建相应的请求策略
|
|
12
|
+
* @param options - SDK 配置,包含 getToken、getUserId 回调函数和 environment
|
|
13
|
+
*/
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.environment = options.environment;
|
|
17
|
+
this.isMiniProgram = isMiniProgram();
|
|
18
|
+
this.strategy = this.createStrategy();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 工厂方法,根据平台创建相应的请求策略
|
|
22
|
+
* @returns BaseRequestStrategy 实例(WebRequestStrategy 或 MiniProgramRequestStrategy)
|
|
23
|
+
*/
|
|
24
|
+
createStrategy() {
|
|
25
|
+
if (this.isMiniProgram) {
|
|
26
|
+
return new MiniProgramRequestStrategy(this.environment);
|
|
27
|
+
}
|
|
28
|
+
return new WebRequestStrategy(this.environment);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 执行 API 请求,自动获取凭证并构造请求头
|
|
32
|
+
* @param opts - 请求选项,包含路径、数据和可选的自定义请求头
|
|
33
|
+
* @returns Promise,解析为响应数据
|
|
34
|
+
* @throws SdkError 如果请求失败
|
|
35
|
+
*/
|
|
36
|
+
async request(opts) {
|
|
37
|
+
// 并发获取凭证,带错误处理
|
|
38
|
+
const [token, userId, ruleId] = await Promise.all([
|
|
39
|
+
Promise.resolve(this.options.getToken()).catch(() => ''),
|
|
40
|
+
Promise.resolve(this.options.getUserId()).catch(() => ''),
|
|
41
|
+
Promise.resolve(this.options.getRuleId?.() ?? '').catch(() => ''),
|
|
42
|
+
]);
|
|
43
|
+
// 生成唯一的请求 ID(兼容所有环境)
|
|
44
|
+
const requestId = `b-${generateUUID()}`;
|
|
45
|
+
// 构造标准请求头
|
|
46
|
+
const standardHeaders = {
|
|
47
|
+
'Content-Type': 'application/json',
|
|
48
|
+
'X-Request-Id': requestId,
|
|
49
|
+
'x-belink-authorization': token,
|
|
50
|
+
'x-belink-pandora-userId': userId,
|
|
51
|
+
'x-belink-pandora-ruleId': ruleId,
|
|
52
|
+
};
|
|
53
|
+
// 合并自定义请求头(自定义请求头优先)
|
|
54
|
+
const headers = {
|
|
55
|
+
...standardHeaders,
|
|
56
|
+
...(opts.headers || {}),
|
|
57
|
+
};
|
|
58
|
+
// 委托给平台特定的策略
|
|
59
|
+
return this.strategy.request(opts.path, opts.data, headers);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求策略的抽象基类
|
|
3
|
+
* 定义所有请求策略的公共契约和共享实现
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class BaseRequestStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* 运行环境配置
|
|
8
|
+
*/
|
|
9
|
+
protected environment: 'production' | 'test';
|
|
10
|
+
/**
|
|
11
|
+
* 创建请求策略实例
|
|
12
|
+
* @param environment - 运行环境 ('production' 或 'test')
|
|
13
|
+
*/
|
|
14
|
+
constructor(environment: 'production' | 'test');
|
|
15
|
+
/**
|
|
16
|
+
* 子类必须实现的请求方法
|
|
17
|
+
*/
|
|
18
|
+
abstract request<T>(path: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Web/H5 请求策略,使用 axios 进行 HTTP 请求
|
|
22
|
+
* 为浏览器环境实现请求策略
|
|
23
|
+
*/
|
|
24
|
+
export declare class WebRequestStrategy extends BaseRequestStrategy {
|
|
25
|
+
/**
|
|
26
|
+
* 使用 axios 执行 HTTP POST 请求
|
|
27
|
+
* @param path - API 端点路径
|
|
28
|
+
* @param data - 请求负载(可选)
|
|
29
|
+
* @param headers - 要包含在请求中的额外请求头(可选)
|
|
30
|
+
* @returns Promise,解析为响应数据
|
|
31
|
+
* @throws BizError 如果是 4xx 客户端错误
|
|
32
|
+
* @throws SystemError 如果是 5xx 服务器错误或网络错误
|
|
33
|
+
*/
|
|
34
|
+
request<T>(path: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 微信小程序请求策略,使用 wx.cloud.callContainer
|
|
38
|
+
* 为微信小程序环境实现请求策略
|
|
39
|
+
*/
|
|
40
|
+
export declare class MiniProgramRequestStrategy extends BaseRequestStrategy {
|
|
41
|
+
/**
|
|
42
|
+
* 使用微信云托管容器 API 执行请求
|
|
43
|
+
* @param path - API 端点路径
|
|
44
|
+
* @param data - 请求负载(可选)
|
|
45
|
+
* @param headers - 要包含在请求中的额外请求头(可选)
|
|
46
|
+
* @returns Promise,解析为响应数据
|
|
47
|
+
* @throws BizError 如果是 4xx 客户端错误
|
|
48
|
+
* @throws SystemError 如果是 5xx 服务器错误或微信 API 错误
|
|
49
|
+
*/
|
|
50
|
+
request<T>(path: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
51
|
+
}
|