@be-link/ecommerce-backend-bff-service-node-sdk 0.0.5 → 0.0.7

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.
Files changed (54) hide show
  1. package/README.md +2 -0
  2. package/{bff → cjs/bff}/request/client.js +4 -3
  3. package/{index.d.ts → cjs/index.d.ts} +2 -0
  4. package/{index.js → cjs/index.js} +2 -0
  5. package/{types → cjs/types}/index.d.ts +5 -1
  6. package/{utils → cjs/utils}/string.d.ts +8 -0
  7. package/cjs/utils/string.js +41 -0
  8. package/esm/bff/modules/BaseService.d.ts +41 -0
  9. package/esm/bff/modules/BaseService.mjs +38 -0
  10. package/esm/bff/modules/demo/service.d.ts +22 -0
  11. package/esm/bff/modules/demo/service.mjs +25 -0
  12. package/esm/bff/modules/demo/types.d.ts +28 -0
  13. package/esm/bff/modules/demo/types.mjs +1 -0
  14. package/esm/bff/modules/example/service.d.ts +46 -0
  15. package/esm/bff/modules/example/service.mjs +53 -0
  16. package/esm/bff/modules/example/types.d.ts +133 -0
  17. package/esm/bff/modules/example/types.mjs +1 -0
  18. package/esm/bff/request/client.d.ts +45 -0
  19. package/esm/bff/request/client.mjs +61 -0
  20. package/esm/bff/request/strategy.d.ts +51 -0
  21. package/esm/bff/request/strategy.mjs +179 -0
  22. package/esm/errors/index.d.ts +27 -0
  23. package/esm/errors/index.mjs +46 -0
  24. package/esm/index.d.ts +56 -0
  25. package/esm/index.mjs +48 -0
  26. package/esm/types/index.d.ts +62 -0
  27. package/esm/types/index.mjs +1 -0
  28. package/esm/utils/env.d.ts +23 -0
  29. package/esm/utils/env.mjs +39 -0
  30. package/esm/utils/http.d.ts +5 -0
  31. package/esm/utils/http.mjs +33 -0
  32. package/{utils/string.js → esm/utils/string.d.ts} +9 -9
  33. package/esm/utils/string.mjs +37 -0
  34. package/package.json +17 -13
  35. /package/{bff → cjs/bff}/modules/BaseService.d.ts +0 -0
  36. /package/{bff → cjs/bff}/modules/BaseService.js +0 -0
  37. /package/{bff → cjs/bff}/modules/demo/service.d.ts +0 -0
  38. /package/{bff → cjs/bff}/modules/demo/service.js +0 -0
  39. /package/{bff → cjs/bff}/modules/demo/types.d.ts +0 -0
  40. /package/{bff → cjs/bff}/modules/demo/types.js +0 -0
  41. /package/{bff → cjs/bff}/modules/example/service.d.ts +0 -0
  42. /package/{bff → cjs/bff}/modules/example/service.js +0 -0
  43. /package/{bff → cjs/bff}/modules/example/types.d.ts +0 -0
  44. /package/{bff → cjs/bff}/modules/example/types.js +0 -0
  45. /package/{bff → cjs/bff}/request/client.d.ts +0 -0
  46. /package/{bff → cjs/bff}/request/strategy.d.ts +0 -0
  47. /package/{bff → cjs/bff}/request/strategy.js +0 -0
  48. /package/{errors → cjs/errors}/index.d.ts +0 -0
  49. /package/{errors → cjs/errors}/index.js +0 -0
  50. /package/{types → cjs/types}/index.js +0 -0
  51. /package/{utils → cjs/utils}/env.d.ts +0 -0
  52. /package/{utils → cjs/utils}/env.js +0 -0
  53. /package/{utils → cjs/utils}/http.d.ts +0 -0
  54. /package/{utils → cjs/utils}/http.js +0 -0
package/README.md CHANGED
@@ -26,6 +26,7 @@ import { EcommerceBackendBffSDK } from '@be-link/ecommerce-backend-bff-service-n
26
26
  const sdk = new EcommerceBackendBffSDK({
27
27
  getToken: async () => localStorage.getItem('token') || '',
28
28
  getUserId: async () => localStorage.getItem('userId') || '',
29
+ getRuleId: async () => localStorage.getItem('ruleId') || '',
29
30
  environment: 'production'
30
31
  })
31
32
 
@@ -44,6 +45,7 @@ import { EcommerceBackendBffSDK } from '@be-link/ecommerce-backend-bff-service-n
44
45
  const sdk = new EcommerceBackendBffSDK({
45
46
  getToken: async () => wx.getStorageSync('token') || '',
46
47
  getUserId: async () => wx.getStorageSync('userId') || '',
48
+ getRuleId: async () => wx.getStorageSync('ruleId') || '',
47
49
  environment: 'test'
48
50
  })
49
51
 
@@ -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
  * 自动检测运行时环境并创建相应的策略
@@ -40,10 +41,10 @@ class HttpClient {
40
41
  const [token, userId, ruleId] = await Promise.all([
41
42
  Promise.resolve(this.options.getToken()).catch(() => ''),
42
43
  Promise.resolve(this.options.getUserId()).catch(() => ''),
43
- Promise.resolve(this.options.getRuleId?.() ?? '').catch(() => ''),
44
+ Promise.resolve(this.options.getRuleId()).catch(() => ''),
44
45
  ]);
45
- // 生成唯一的请求 ID
46
- const requestId = `b-${crypto.randomUUID()}`;
46
+ // 生成唯一的请求 ID(兼容所有环境)
47
+ const requestId = `b-${(0, string_1.generateUUID)()}`;
47
48
  // 构造标准请求头
48
49
  const standardHeaders = {
49
50
  'Content-Type': 'application/json',
@@ -11,6 +11,7 @@ import type { SdkOptions } from './types';
11
11
  * const sdk = new EcommerceBackendBffSDK({
12
12
  * getToken: async () => localStorage.getItem('token') || '',
13
13
  * getUserId: async () => localStorage.getItem('userId') || '',
14
+ * getRuleId: async () => localStorage.getItem('ruleId') || '',
14
15
  * environment: 'production'
15
16
  * })
16
17
  *
@@ -18,6 +19,7 @@ import type { SdkOptions } from './types';
18
19
  * const sdk = new EcommerceBackendBffSDK({
19
20
  * getToken: async () => wx.getStorageSync('token') || '',
20
21
  * getUserId: async () => wx.getStorageSync('userId') || '',
22
+ * getRuleId: async () => wx.getStorageSync('ruleId') || '',
21
23
  * environment: 'test'
22
24
  * })
23
25
  *
@@ -17,6 +17,7 @@ const service_2 = __importDefault(require("./bff/modules/demo/service"));
17
17
  * const sdk = new EcommerceBackendBffSDK({
18
18
  * getToken: async () => localStorage.getItem('token') || '',
19
19
  * getUserId: async () => localStorage.getItem('userId') || '',
20
+ * getRuleId: async () => localStorage.getItem('ruleId') || '',
20
21
  * environment: 'production'
21
22
  * })
22
23
  *
@@ -24,6 +25,7 @@ const service_2 = __importDefault(require("./bff/modules/demo/service"));
24
25
  * const sdk = new EcommerceBackendBffSDK({
25
26
  * getToken: async () => wx.getStorageSync('token') || '',
26
27
  * getUserId: async () => wx.getStorageSync('userId') || '',
28
+ * getRuleId: async () => wx.getStorageSync('ruleId') || '',
27
29
  * environment: 'test'
28
30
  * })
29
31
  *
@@ -12,7 +12,11 @@ export interface SdkOptions {
12
12
  * 可以是同步或异步的
13
13
  */
14
14
  getUserId: () => string | Promise<string>;
15
- getRuleId?: () => string | Promise<string>;
15
+ /**
16
+ * 获取规则 ID 的回调函数
17
+ * 可以是同步或异步的
18
+ */
19
+ getRuleId: () => string | Promise<string>;
16
20
  /**
17
21
  * 运行环境配置(必传)
18
22
  * - 'production': 生产环境
@@ -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
+ }