@djvlc/openapi-user-client 1.1.1 → 1.2.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 +80 -14
- package/dist/index.d.ts +80 -14
- package/dist/index.js +112 -11
- package/dist/index.mjs +112 -11
- package/package.json +11 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse } from '@djvlc/contracts-types';
|
|
1
|
+
import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse, TrackEvent, HealthCheckResponse } from '@djvlc/contracts-types';
|
|
2
|
+
export { ActionContext, ActionExecuteRequest, ActionExecuteResponse, ApiResponse, BlockedComponent, DataQueryContext, DataQueryRequest, DataQueryResponse, HealthCheckResponse, ManifestItem, PageManifest, PageResolveRequest, PageResolveResult, RuntimeConfig, TrackEvent } from '@djvlc/contracts-types';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* @djvlc/openapi-user-client - User API 客户端
|
|
@@ -9,11 +10,16 @@ import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResp
|
|
|
9
10
|
* @packageDocumentation
|
|
10
11
|
*/
|
|
11
12
|
|
|
13
|
+
/**
|
|
14
|
+
* User API 客户端配置
|
|
15
|
+
*/
|
|
12
16
|
interface UserClientConfig {
|
|
13
17
|
/** API 基础 URL */
|
|
14
18
|
baseUrl: string;
|
|
15
19
|
/** 用户 Token */
|
|
16
20
|
token?: string;
|
|
21
|
+
/** 获取 Token 的函数(支持动态获取) */
|
|
22
|
+
getToken?: () => string | Promise<string>;
|
|
17
23
|
/** 设备 ID */
|
|
18
24
|
deviceId?: string;
|
|
19
25
|
/** 渠道 */
|
|
@@ -22,6 +28,8 @@ interface UserClientConfig {
|
|
|
22
28
|
timeout?: number;
|
|
23
29
|
/** 自定义请求头 */
|
|
24
30
|
headers?: Record<string, string>;
|
|
31
|
+
/** 获取 Trace Headers(用于链路追踪) */
|
|
32
|
+
getTraceHeaders?: () => Record<string, string>;
|
|
25
33
|
/** 请求拦截器 */
|
|
26
34
|
onRequest?: (config: RequestInit) => RequestInit;
|
|
27
35
|
/** 响应拦截器 */
|
|
@@ -29,14 +37,58 @@ interface UserClientConfig {
|
|
|
29
37
|
/** 错误处理 */
|
|
30
38
|
onError?: (error: Error) => void;
|
|
31
39
|
}
|
|
32
|
-
/**
|
|
33
|
-
|
|
40
|
+
/**
|
|
41
|
+
* 页面解析请求参数(Query 参数版本)
|
|
42
|
+
* GET /api/page/resolve
|
|
43
|
+
*/
|
|
44
|
+
interface ResolvePageParams {
|
|
45
|
+
/** 页面 UID */
|
|
34
46
|
pageUid: string;
|
|
47
|
+
/** 渠道 */
|
|
35
48
|
channel?: string;
|
|
49
|
+
/** 用户 ID(用于灰度) */
|
|
36
50
|
uid?: string;
|
|
51
|
+
/** 设备 ID */
|
|
37
52
|
deviceId?: string;
|
|
53
|
+
/** 预览 Token */
|
|
38
54
|
previewToken?: string;
|
|
39
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* 埋点上报请求
|
|
58
|
+
* POST /api/track
|
|
59
|
+
*/
|
|
60
|
+
interface TrackRequest {
|
|
61
|
+
/** 事件列表 */
|
|
62
|
+
events: TrackEvent[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 活动状态响应
|
|
66
|
+
* GET /api/activity/state
|
|
67
|
+
*/
|
|
68
|
+
interface ActivityStateResult {
|
|
69
|
+
/** 活动 ID */
|
|
70
|
+
activityId: string;
|
|
71
|
+
/** 活动整体状态 */
|
|
72
|
+
status: 'active' | 'inactive' | 'ended';
|
|
73
|
+
/** 用户参与状态 */
|
|
74
|
+
userStatus: 'not_participated' | 'participated' | 'claimed' | 'expired';
|
|
75
|
+
/** 剩余参与次数 */
|
|
76
|
+
remainingAttempts?: number;
|
|
77
|
+
/** 下次可参与时间 */
|
|
78
|
+
nextAvailableTime?: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 活动状态查询参数
|
|
82
|
+
* GET /api/activity/state
|
|
83
|
+
*/
|
|
84
|
+
interface ActivityStateParams {
|
|
85
|
+
/** 活动 ID */
|
|
86
|
+
activityId: string;
|
|
87
|
+
/** 用户 ID */
|
|
88
|
+
uid?: string;
|
|
89
|
+
/** 设备 ID */
|
|
90
|
+
deviceId?: string;
|
|
91
|
+
}
|
|
40
92
|
/**
|
|
41
93
|
* User API 客户端类
|
|
42
94
|
* 用于 Runtime 访问后端服务
|
|
@@ -44,6 +96,10 @@ interface ResolvePageRequest {
|
|
|
44
96
|
declare class UserApiClient {
|
|
45
97
|
private config;
|
|
46
98
|
constructor(config: UserClientConfig);
|
|
99
|
+
/**
|
|
100
|
+
* 获取认证 Token
|
|
101
|
+
*/
|
|
102
|
+
private getAuthToken;
|
|
47
103
|
/**
|
|
48
104
|
* 获取通用请求头
|
|
49
105
|
*/
|
|
@@ -52,11 +108,15 @@ declare class UserApiClient {
|
|
|
52
108
|
* 发送请求
|
|
53
109
|
*/
|
|
54
110
|
private request;
|
|
111
|
+
/**
|
|
112
|
+
* 发送请求(返回完整响应)
|
|
113
|
+
*/
|
|
114
|
+
private requestFull;
|
|
55
115
|
/**
|
|
56
116
|
* 解析页面
|
|
57
117
|
* GET /api/page/resolve
|
|
58
118
|
*/
|
|
59
|
-
resolvePage(
|
|
119
|
+
resolvePage(params: ResolvePageParams): Promise<PageResolveResult>;
|
|
60
120
|
/**
|
|
61
121
|
* 执行动作
|
|
62
122
|
* POST /api/actions/execute
|
|
@@ -70,6 +130,10 @@ declare class UserApiClient {
|
|
|
70
130
|
* 快捷方法:签到
|
|
71
131
|
*/
|
|
72
132
|
signin(activityId: string): Promise<ActionExecuteResponse>;
|
|
133
|
+
/**
|
|
134
|
+
* 快捷方法:抽奖
|
|
135
|
+
*/
|
|
136
|
+
lottery(activityId: string, params?: Record<string, unknown>): Promise<ActionExecuteResponse>;
|
|
73
137
|
/**
|
|
74
138
|
* 查询数据
|
|
75
139
|
* POST /api/data/query
|
|
@@ -83,23 +147,25 @@ declare class UserApiClient {
|
|
|
83
147
|
* 上报埋点
|
|
84
148
|
* POST /api/track
|
|
85
149
|
*/
|
|
86
|
-
track(events:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
150
|
+
track(events: TrackEvent[]): Promise<void>;
|
|
151
|
+
/**
|
|
152
|
+
* 快捷方法:上报单个事件
|
|
153
|
+
*/
|
|
154
|
+
trackEvent(eventName: string, params?: Record<string, unknown>): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* 获取活动状态
|
|
157
|
+
* GET /api/activity/state
|
|
158
|
+
*/
|
|
159
|
+
getActivityState(params: ActivityStateParams): Promise<ActivityStateResult>;
|
|
91
160
|
/**
|
|
92
161
|
* 健康检查
|
|
93
162
|
* GET /api/health
|
|
94
163
|
*/
|
|
95
|
-
health(): Promise<
|
|
96
|
-
status: string;
|
|
97
|
-
version: string;
|
|
98
|
-
}>;
|
|
164
|
+
health(): Promise<HealthCheckResponse>;
|
|
99
165
|
}
|
|
100
166
|
/**
|
|
101
167
|
* 创建 User API 客户端实例
|
|
102
168
|
*/
|
|
103
169
|
declare function createUserClient(config: UserClientConfig): UserApiClient;
|
|
104
170
|
|
|
105
|
-
export { type
|
|
171
|
+
export { type ActivityStateParams, type ActivityStateResult, type ResolvePageParams, type TrackRequest, UserApiClient, type UserClientConfig, createUserClient, UserApiClient as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse } from '@djvlc/contracts-types';
|
|
1
|
+
import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse, TrackEvent, HealthCheckResponse } from '@djvlc/contracts-types';
|
|
2
|
+
export { ActionContext, ActionExecuteRequest, ActionExecuteResponse, ApiResponse, BlockedComponent, DataQueryContext, DataQueryRequest, DataQueryResponse, HealthCheckResponse, ManifestItem, PageManifest, PageResolveRequest, PageResolveResult, RuntimeConfig, TrackEvent } from '@djvlc/contracts-types';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* @djvlc/openapi-user-client - User API 客户端
|
|
@@ -9,11 +10,16 @@ import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResp
|
|
|
9
10
|
* @packageDocumentation
|
|
10
11
|
*/
|
|
11
12
|
|
|
13
|
+
/**
|
|
14
|
+
* User API 客户端配置
|
|
15
|
+
*/
|
|
12
16
|
interface UserClientConfig {
|
|
13
17
|
/** API 基础 URL */
|
|
14
18
|
baseUrl: string;
|
|
15
19
|
/** 用户 Token */
|
|
16
20
|
token?: string;
|
|
21
|
+
/** 获取 Token 的函数(支持动态获取) */
|
|
22
|
+
getToken?: () => string | Promise<string>;
|
|
17
23
|
/** 设备 ID */
|
|
18
24
|
deviceId?: string;
|
|
19
25
|
/** 渠道 */
|
|
@@ -22,6 +28,8 @@ interface UserClientConfig {
|
|
|
22
28
|
timeout?: number;
|
|
23
29
|
/** 自定义请求头 */
|
|
24
30
|
headers?: Record<string, string>;
|
|
31
|
+
/** 获取 Trace Headers(用于链路追踪) */
|
|
32
|
+
getTraceHeaders?: () => Record<string, string>;
|
|
25
33
|
/** 请求拦截器 */
|
|
26
34
|
onRequest?: (config: RequestInit) => RequestInit;
|
|
27
35
|
/** 响应拦截器 */
|
|
@@ -29,14 +37,58 @@ interface UserClientConfig {
|
|
|
29
37
|
/** 错误处理 */
|
|
30
38
|
onError?: (error: Error) => void;
|
|
31
39
|
}
|
|
32
|
-
/**
|
|
33
|
-
|
|
40
|
+
/**
|
|
41
|
+
* 页面解析请求参数(Query 参数版本)
|
|
42
|
+
* GET /api/page/resolve
|
|
43
|
+
*/
|
|
44
|
+
interface ResolvePageParams {
|
|
45
|
+
/** 页面 UID */
|
|
34
46
|
pageUid: string;
|
|
47
|
+
/** 渠道 */
|
|
35
48
|
channel?: string;
|
|
49
|
+
/** 用户 ID(用于灰度) */
|
|
36
50
|
uid?: string;
|
|
51
|
+
/** 设备 ID */
|
|
37
52
|
deviceId?: string;
|
|
53
|
+
/** 预览 Token */
|
|
38
54
|
previewToken?: string;
|
|
39
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* 埋点上报请求
|
|
58
|
+
* POST /api/track
|
|
59
|
+
*/
|
|
60
|
+
interface TrackRequest {
|
|
61
|
+
/** 事件列表 */
|
|
62
|
+
events: TrackEvent[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 活动状态响应
|
|
66
|
+
* GET /api/activity/state
|
|
67
|
+
*/
|
|
68
|
+
interface ActivityStateResult {
|
|
69
|
+
/** 活动 ID */
|
|
70
|
+
activityId: string;
|
|
71
|
+
/** 活动整体状态 */
|
|
72
|
+
status: 'active' | 'inactive' | 'ended';
|
|
73
|
+
/** 用户参与状态 */
|
|
74
|
+
userStatus: 'not_participated' | 'participated' | 'claimed' | 'expired';
|
|
75
|
+
/** 剩余参与次数 */
|
|
76
|
+
remainingAttempts?: number;
|
|
77
|
+
/** 下次可参与时间 */
|
|
78
|
+
nextAvailableTime?: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 活动状态查询参数
|
|
82
|
+
* GET /api/activity/state
|
|
83
|
+
*/
|
|
84
|
+
interface ActivityStateParams {
|
|
85
|
+
/** 活动 ID */
|
|
86
|
+
activityId: string;
|
|
87
|
+
/** 用户 ID */
|
|
88
|
+
uid?: string;
|
|
89
|
+
/** 设备 ID */
|
|
90
|
+
deviceId?: string;
|
|
91
|
+
}
|
|
40
92
|
/**
|
|
41
93
|
* User API 客户端类
|
|
42
94
|
* 用于 Runtime 访问后端服务
|
|
@@ -44,6 +96,10 @@ interface ResolvePageRequest {
|
|
|
44
96
|
declare class UserApiClient {
|
|
45
97
|
private config;
|
|
46
98
|
constructor(config: UserClientConfig);
|
|
99
|
+
/**
|
|
100
|
+
* 获取认证 Token
|
|
101
|
+
*/
|
|
102
|
+
private getAuthToken;
|
|
47
103
|
/**
|
|
48
104
|
* 获取通用请求头
|
|
49
105
|
*/
|
|
@@ -52,11 +108,15 @@ declare class UserApiClient {
|
|
|
52
108
|
* 发送请求
|
|
53
109
|
*/
|
|
54
110
|
private request;
|
|
111
|
+
/**
|
|
112
|
+
* 发送请求(返回完整响应)
|
|
113
|
+
*/
|
|
114
|
+
private requestFull;
|
|
55
115
|
/**
|
|
56
116
|
* 解析页面
|
|
57
117
|
* GET /api/page/resolve
|
|
58
118
|
*/
|
|
59
|
-
resolvePage(
|
|
119
|
+
resolvePage(params: ResolvePageParams): Promise<PageResolveResult>;
|
|
60
120
|
/**
|
|
61
121
|
* 执行动作
|
|
62
122
|
* POST /api/actions/execute
|
|
@@ -70,6 +130,10 @@ declare class UserApiClient {
|
|
|
70
130
|
* 快捷方法:签到
|
|
71
131
|
*/
|
|
72
132
|
signin(activityId: string): Promise<ActionExecuteResponse>;
|
|
133
|
+
/**
|
|
134
|
+
* 快捷方法:抽奖
|
|
135
|
+
*/
|
|
136
|
+
lottery(activityId: string, params?: Record<string, unknown>): Promise<ActionExecuteResponse>;
|
|
73
137
|
/**
|
|
74
138
|
* 查询数据
|
|
75
139
|
* POST /api/data/query
|
|
@@ -83,23 +147,25 @@ declare class UserApiClient {
|
|
|
83
147
|
* 上报埋点
|
|
84
148
|
* POST /api/track
|
|
85
149
|
*/
|
|
86
|
-
track(events:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
150
|
+
track(events: TrackEvent[]): Promise<void>;
|
|
151
|
+
/**
|
|
152
|
+
* 快捷方法:上报单个事件
|
|
153
|
+
*/
|
|
154
|
+
trackEvent(eventName: string, params?: Record<string, unknown>): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* 获取活动状态
|
|
157
|
+
* GET /api/activity/state
|
|
158
|
+
*/
|
|
159
|
+
getActivityState(params: ActivityStateParams): Promise<ActivityStateResult>;
|
|
91
160
|
/**
|
|
92
161
|
* 健康检查
|
|
93
162
|
* GET /api/health
|
|
94
163
|
*/
|
|
95
|
-
health(): Promise<
|
|
96
|
-
status: string;
|
|
97
|
-
version: string;
|
|
98
|
-
}>;
|
|
164
|
+
health(): Promise<HealthCheckResponse>;
|
|
99
165
|
}
|
|
100
166
|
/**
|
|
101
167
|
* 创建 User API 客户端实例
|
|
102
168
|
*/
|
|
103
169
|
declare function createUserClient(config: UserClientConfig): UserApiClient;
|
|
104
170
|
|
|
105
|
-
export { type
|
|
171
|
+
export { type ActivityStateParams, type ActivityStateResult, type ResolvePageParams, type TrackRequest, UserApiClient, type UserClientConfig, createUserClient, UserApiClient as default };
|
package/dist/index.js
CHANGED
|
@@ -32,16 +32,26 @@ var UserApiClient = class {
|
|
|
32
32
|
...config
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* 获取认证 Token
|
|
37
|
+
*/
|
|
38
|
+
async getAuthToken() {
|
|
39
|
+
if (this.config.getToken) {
|
|
40
|
+
return this.config.getToken();
|
|
41
|
+
}
|
|
42
|
+
return this.config.token;
|
|
43
|
+
}
|
|
35
44
|
/**
|
|
36
45
|
* 获取通用请求头
|
|
37
46
|
*/
|
|
38
|
-
getCommonHeaders() {
|
|
47
|
+
async getCommonHeaders() {
|
|
39
48
|
const headers = {
|
|
40
49
|
"Content-Type": "application/json",
|
|
41
50
|
...this.config.headers
|
|
42
51
|
};
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
const token = await this.getAuthToken();
|
|
53
|
+
if (token) {
|
|
54
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
45
55
|
}
|
|
46
56
|
if (this.config.deviceId) {
|
|
47
57
|
headers["x-device-id"] = this.config.deviceId;
|
|
@@ -49,6 +59,9 @@ var UserApiClient = class {
|
|
|
49
59
|
if (this.config.channel) {
|
|
50
60
|
headers["x-channel"] = this.config.channel;
|
|
51
61
|
}
|
|
62
|
+
if (this.config.getTraceHeaders) {
|
|
63
|
+
Object.assign(headers, this.config.getTraceHeaders());
|
|
64
|
+
}
|
|
52
65
|
return headers;
|
|
53
66
|
}
|
|
54
67
|
/**
|
|
@@ -68,10 +81,11 @@ var UserApiClient = class {
|
|
|
68
81
|
url += `?${queryString}`;
|
|
69
82
|
}
|
|
70
83
|
}
|
|
84
|
+
const commonHeaders = await this.getCommonHeaders();
|
|
71
85
|
let requestConfig = {
|
|
72
86
|
method,
|
|
73
87
|
headers: {
|
|
74
|
-
...
|
|
88
|
+
...commonHeaders,
|
|
75
89
|
...options?.headers
|
|
76
90
|
},
|
|
77
91
|
body: options?.body ? JSON.stringify(options.body) : void 0
|
|
@@ -92,7 +106,9 @@ var UserApiClient = class {
|
|
|
92
106
|
result = this.config.onResponse(result);
|
|
93
107
|
}
|
|
94
108
|
if (!result.success) {
|
|
95
|
-
|
|
109
|
+
const error = new Error(result.message || "Request failed");
|
|
110
|
+
error.code = result.code;
|
|
111
|
+
throw error;
|
|
96
112
|
}
|
|
97
113
|
return result.data;
|
|
98
114
|
} catch (error) {
|
|
@@ -102,19 +118,65 @@ var UserApiClient = class {
|
|
|
102
118
|
throw error;
|
|
103
119
|
}
|
|
104
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* 发送请求(返回完整响应)
|
|
123
|
+
*/
|
|
124
|
+
async requestFull(method, path, options) {
|
|
125
|
+
let url = `${this.config.baseUrl}${path}`;
|
|
126
|
+
if (options?.params) {
|
|
127
|
+
const searchParams = new URLSearchParams();
|
|
128
|
+
Object.entries(options.params).forEach(([key, value]) => {
|
|
129
|
+
if (value !== void 0) {
|
|
130
|
+
searchParams.append(key, value);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
const queryString = searchParams.toString();
|
|
134
|
+
if (queryString) {
|
|
135
|
+
url += `?${queryString}`;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const commonHeaders = await this.getCommonHeaders();
|
|
139
|
+
let requestConfig = {
|
|
140
|
+
method,
|
|
141
|
+
headers: commonHeaders,
|
|
142
|
+
body: options?.body ? JSON.stringify(options.body) : void 0
|
|
143
|
+
};
|
|
144
|
+
if (this.config.onRequest) {
|
|
145
|
+
requestConfig = this.config.onRequest(requestConfig);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const controller = new AbortController();
|
|
149
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
150
|
+
const response = await fetch(url, {
|
|
151
|
+
...requestConfig,
|
|
152
|
+
signal: controller.signal
|
|
153
|
+
});
|
|
154
|
+
clearTimeout(timeoutId);
|
|
155
|
+
let result = await response.json();
|
|
156
|
+
if (this.config.onResponse) {
|
|
157
|
+
result = this.config.onResponse(result);
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (this.config.onError) {
|
|
162
|
+
this.config.onError(error);
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
105
167
|
// ==================== 页面解析 ====================
|
|
106
168
|
/**
|
|
107
169
|
* 解析页面
|
|
108
170
|
* GET /api/page/resolve
|
|
109
171
|
*/
|
|
110
|
-
async resolvePage(
|
|
172
|
+
async resolvePage(params) {
|
|
111
173
|
return this.request("GET", "/api/page/resolve", {
|
|
112
174
|
params: {
|
|
113
|
-
pageUid:
|
|
114
|
-
channel:
|
|
115
|
-
uid:
|
|
116
|
-
deviceId:
|
|
117
|
-
previewToken:
|
|
175
|
+
pageUid: params.pageUid,
|
|
176
|
+
channel: params.channel,
|
|
177
|
+
uid: params.uid,
|
|
178
|
+
deviceId: params.deviceId,
|
|
179
|
+
previewToken: params.previewToken
|
|
118
180
|
}
|
|
119
181
|
});
|
|
120
182
|
}
|
|
@@ -154,6 +216,19 @@ var UserApiClient = class {
|
|
|
154
216
|
}
|
|
155
217
|
});
|
|
156
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* 快捷方法:抽奖
|
|
221
|
+
*/
|
|
222
|
+
async lottery(activityId, params) {
|
|
223
|
+
return this.executeAction({
|
|
224
|
+
actionType: "lottery",
|
|
225
|
+
params: { activityId, ...params },
|
|
226
|
+
context: {
|
|
227
|
+
deviceId: this.config.deviceId,
|
|
228
|
+
channel: this.config.channel
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
157
232
|
// ==================== Data Proxy ====================
|
|
158
233
|
/**
|
|
159
234
|
* 查询数据
|
|
@@ -184,6 +259,32 @@ var UserApiClient = class {
|
|
|
184
259
|
body: { events }
|
|
185
260
|
});
|
|
186
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* 快捷方法:上报单个事件
|
|
264
|
+
*/
|
|
265
|
+
async trackEvent(eventName, params) {
|
|
266
|
+
await this.track([
|
|
267
|
+
{
|
|
268
|
+
eventName,
|
|
269
|
+
params,
|
|
270
|
+
timestamp: Date.now()
|
|
271
|
+
}
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
// ==================== 活动状态 ====================
|
|
275
|
+
/**
|
|
276
|
+
* 获取活动状态
|
|
277
|
+
* GET /api/activity/state
|
|
278
|
+
*/
|
|
279
|
+
async getActivityState(params) {
|
|
280
|
+
return this.request("GET", "/api/activity/state", {
|
|
281
|
+
params: {
|
|
282
|
+
activityId: params.activityId,
|
|
283
|
+
uid: params.uid,
|
|
284
|
+
deviceId: params.deviceId
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
187
288
|
// ==================== 健康检查 ====================
|
|
188
289
|
/**
|
|
189
290
|
* 健康检查
|
package/dist/index.mjs
CHANGED
|
@@ -6,16 +6,26 @@ var UserApiClient = class {
|
|
|
6
6
|
...config
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* 获取认证 Token
|
|
11
|
+
*/
|
|
12
|
+
async getAuthToken() {
|
|
13
|
+
if (this.config.getToken) {
|
|
14
|
+
return this.config.getToken();
|
|
15
|
+
}
|
|
16
|
+
return this.config.token;
|
|
17
|
+
}
|
|
9
18
|
/**
|
|
10
19
|
* 获取通用请求头
|
|
11
20
|
*/
|
|
12
|
-
getCommonHeaders() {
|
|
21
|
+
async getCommonHeaders() {
|
|
13
22
|
const headers = {
|
|
14
23
|
"Content-Type": "application/json",
|
|
15
24
|
...this.config.headers
|
|
16
25
|
};
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
const token = await this.getAuthToken();
|
|
27
|
+
if (token) {
|
|
28
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
19
29
|
}
|
|
20
30
|
if (this.config.deviceId) {
|
|
21
31
|
headers["x-device-id"] = this.config.deviceId;
|
|
@@ -23,6 +33,9 @@ var UserApiClient = class {
|
|
|
23
33
|
if (this.config.channel) {
|
|
24
34
|
headers["x-channel"] = this.config.channel;
|
|
25
35
|
}
|
|
36
|
+
if (this.config.getTraceHeaders) {
|
|
37
|
+
Object.assign(headers, this.config.getTraceHeaders());
|
|
38
|
+
}
|
|
26
39
|
return headers;
|
|
27
40
|
}
|
|
28
41
|
/**
|
|
@@ -42,10 +55,11 @@ var UserApiClient = class {
|
|
|
42
55
|
url += `?${queryString}`;
|
|
43
56
|
}
|
|
44
57
|
}
|
|
58
|
+
const commonHeaders = await this.getCommonHeaders();
|
|
45
59
|
let requestConfig = {
|
|
46
60
|
method,
|
|
47
61
|
headers: {
|
|
48
|
-
...
|
|
62
|
+
...commonHeaders,
|
|
49
63
|
...options?.headers
|
|
50
64
|
},
|
|
51
65
|
body: options?.body ? JSON.stringify(options.body) : void 0
|
|
@@ -66,7 +80,9 @@ var UserApiClient = class {
|
|
|
66
80
|
result = this.config.onResponse(result);
|
|
67
81
|
}
|
|
68
82
|
if (!result.success) {
|
|
69
|
-
|
|
83
|
+
const error = new Error(result.message || "Request failed");
|
|
84
|
+
error.code = result.code;
|
|
85
|
+
throw error;
|
|
70
86
|
}
|
|
71
87
|
return result.data;
|
|
72
88
|
} catch (error) {
|
|
@@ -76,19 +92,65 @@ var UserApiClient = class {
|
|
|
76
92
|
throw error;
|
|
77
93
|
}
|
|
78
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* 发送请求(返回完整响应)
|
|
97
|
+
*/
|
|
98
|
+
async requestFull(method, path, options) {
|
|
99
|
+
let url = `${this.config.baseUrl}${path}`;
|
|
100
|
+
if (options?.params) {
|
|
101
|
+
const searchParams = new URLSearchParams();
|
|
102
|
+
Object.entries(options.params).forEach(([key, value]) => {
|
|
103
|
+
if (value !== void 0) {
|
|
104
|
+
searchParams.append(key, value);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
const queryString = searchParams.toString();
|
|
108
|
+
if (queryString) {
|
|
109
|
+
url += `?${queryString}`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const commonHeaders = await this.getCommonHeaders();
|
|
113
|
+
let requestConfig = {
|
|
114
|
+
method,
|
|
115
|
+
headers: commonHeaders,
|
|
116
|
+
body: options?.body ? JSON.stringify(options.body) : void 0
|
|
117
|
+
};
|
|
118
|
+
if (this.config.onRequest) {
|
|
119
|
+
requestConfig = this.config.onRequest(requestConfig);
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const controller = new AbortController();
|
|
123
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
124
|
+
const response = await fetch(url, {
|
|
125
|
+
...requestConfig,
|
|
126
|
+
signal: controller.signal
|
|
127
|
+
});
|
|
128
|
+
clearTimeout(timeoutId);
|
|
129
|
+
let result = await response.json();
|
|
130
|
+
if (this.config.onResponse) {
|
|
131
|
+
result = this.config.onResponse(result);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (this.config.onError) {
|
|
136
|
+
this.config.onError(error);
|
|
137
|
+
}
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
79
141
|
// ==================== 页面解析 ====================
|
|
80
142
|
/**
|
|
81
143
|
* 解析页面
|
|
82
144
|
* GET /api/page/resolve
|
|
83
145
|
*/
|
|
84
|
-
async resolvePage(
|
|
146
|
+
async resolvePage(params) {
|
|
85
147
|
return this.request("GET", "/api/page/resolve", {
|
|
86
148
|
params: {
|
|
87
|
-
pageUid:
|
|
88
|
-
channel:
|
|
89
|
-
uid:
|
|
90
|
-
deviceId:
|
|
91
|
-
previewToken:
|
|
149
|
+
pageUid: params.pageUid,
|
|
150
|
+
channel: params.channel,
|
|
151
|
+
uid: params.uid,
|
|
152
|
+
deviceId: params.deviceId,
|
|
153
|
+
previewToken: params.previewToken
|
|
92
154
|
}
|
|
93
155
|
});
|
|
94
156
|
}
|
|
@@ -128,6 +190,19 @@ var UserApiClient = class {
|
|
|
128
190
|
}
|
|
129
191
|
});
|
|
130
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* 快捷方法:抽奖
|
|
195
|
+
*/
|
|
196
|
+
async lottery(activityId, params) {
|
|
197
|
+
return this.executeAction({
|
|
198
|
+
actionType: "lottery",
|
|
199
|
+
params: { activityId, ...params },
|
|
200
|
+
context: {
|
|
201
|
+
deviceId: this.config.deviceId,
|
|
202
|
+
channel: this.config.channel
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
131
206
|
// ==================== Data Proxy ====================
|
|
132
207
|
/**
|
|
133
208
|
* 查询数据
|
|
@@ -158,6 +233,32 @@ var UserApiClient = class {
|
|
|
158
233
|
body: { events }
|
|
159
234
|
});
|
|
160
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* 快捷方法:上报单个事件
|
|
238
|
+
*/
|
|
239
|
+
async trackEvent(eventName, params) {
|
|
240
|
+
await this.track([
|
|
241
|
+
{
|
|
242
|
+
eventName,
|
|
243
|
+
params,
|
|
244
|
+
timestamp: Date.now()
|
|
245
|
+
}
|
|
246
|
+
]);
|
|
247
|
+
}
|
|
248
|
+
// ==================== 活动状态 ====================
|
|
249
|
+
/**
|
|
250
|
+
* 获取活动状态
|
|
251
|
+
* GET /api/activity/state
|
|
252
|
+
*/
|
|
253
|
+
async getActivityState(params) {
|
|
254
|
+
return this.request("GET", "/api/activity/state", {
|
|
255
|
+
params: {
|
|
256
|
+
activityId: params.activityId,
|
|
257
|
+
uid: params.uid,
|
|
258
|
+
deviceId: params.deviceId
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
161
262
|
// ==================== 健康检查 ====================
|
|
162
263
|
/**
|
|
163
264
|
* 健康检查
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djvlc/openapi-user-client",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "DJV Low-code Platform - User API
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "DJV Low-code Platform - User API 客户端",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -17,25 +17,31 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
20
|
-
"generate": "openapi-typescript ../specs/user-api.yaml -o src/types.ts && npm run build",
|
|
21
20
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
22
21
|
"lint": "eslint src/ --ext .ts",
|
|
23
22
|
"typecheck": "tsc --noEmit",
|
|
24
23
|
"clean": "rimraf dist"
|
|
25
24
|
},
|
|
26
25
|
"dependencies": {
|
|
27
|
-
"@djvlc/contracts-types": "1.
|
|
26
|
+
"@djvlc/contracts-types": "1.3.0"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@types/node": "^20.0.0",
|
|
31
30
|
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
32
31
|
"@typescript-eslint/parser": "^7.0.0",
|
|
33
32
|
"eslint": "^8.56.0",
|
|
34
|
-
"openapi-typescript": "^6.0.0",
|
|
35
33
|
"rimraf": "^5.0.0",
|
|
36
34
|
"tsup": "^8.0.0",
|
|
37
35
|
"typescript": "^5.3.0"
|
|
38
36
|
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@djvlc/contracts-types": "1.3.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"@djvlc/contracts-types": {
|
|
42
|
+
"optional": false
|
|
43
|
+
}
|
|
44
|
+
},
|
|
39
45
|
"keywords": [
|
|
40
46
|
"lowcode",
|
|
41
47
|
"openapi",
|