@djvlc/openapi-user-client 1.0.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.
@@ -0,0 +1,105 @@
1
+ import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse } from '@djvlc/contracts-types';
2
+
3
+ /**
4
+ * @djvlc/openapi-user-client - User API 客户端
5
+ *
6
+ * 自动从 OpenAPI 规范生成,提供类型安全的 User API 访问
7
+ * 用于 Runtime 访问后端服务
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+
12
+ interface UserClientConfig {
13
+ /** API 基础 URL */
14
+ baseUrl: string;
15
+ /** 用户 Token */
16
+ token?: string;
17
+ /** 设备 ID */
18
+ deviceId?: string;
19
+ /** 渠道 */
20
+ channel?: string;
21
+ /** 请求超时(毫秒) */
22
+ timeout?: number;
23
+ /** 自定义请求头 */
24
+ headers?: Record<string, string>;
25
+ /** 请求拦截器 */
26
+ onRequest?: (config: RequestInit) => RequestInit;
27
+ /** 响应拦截器 */
28
+ onResponse?: <T>(response: ApiResponse<T>) => ApiResponse<T>;
29
+ /** 错误处理 */
30
+ onError?: (error: Error) => void;
31
+ }
32
+ /** 页面解析请求 */
33
+ interface ResolvePageRequest {
34
+ pageUid: string;
35
+ channel?: string;
36
+ uid?: string;
37
+ deviceId?: string;
38
+ previewToken?: string;
39
+ }
40
+ /**
41
+ * User API 客户端类
42
+ * 用于 Runtime 访问后端服务
43
+ */
44
+ declare class UserApiClient {
45
+ private config;
46
+ constructor(config: UserClientConfig);
47
+ /**
48
+ * 获取通用请求头
49
+ */
50
+ private getCommonHeaders;
51
+ /**
52
+ * 发送请求
53
+ */
54
+ private request;
55
+ /**
56
+ * 解析页面
57
+ * GET /api/page/resolve
58
+ */
59
+ resolvePage(request: ResolvePageRequest): Promise<PageResolveResult>;
60
+ /**
61
+ * 执行动作
62
+ * POST /api/actions/execute
63
+ */
64
+ executeAction<T = unknown>(request: ActionExecuteRequest): Promise<ActionExecuteResponse<T>>;
65
+ /**
66
+ * 快捷方法:领取
67
+ */
68
+ claim(activityId: string, params?: Record<string, unknown>): Promise<ActionExecuteResponse>;
69
+ /**
70
+ * 快捷方法:签到
71
+ */
72
+ signin(activityId: string): Promise<ActionExecuteResponse>;
73
+ /**
74
+ * 查询数据
75
+ * POST /api/data/query
76
+ */
77
+ queryData<T = unknown>(request: DataQueryRequest): Promise<DataQueryResponse<T>>;
78
+ /**
79
+ * 快捷方法:查询数据
80
+ */
81
+ query<T = unknown>(queryVersionId: string, params?: Record<string, unknown>): Promise<T | undefined>;
82
+ /**
83
+ * 上报埋点
84
+ * POST /api/track
85
+ */
86
+ track(events: Array<{
87
+ eventName: string;
88
+ params?: Record<string, unknown>;
89
+ timestamp?: number;
90
+ }>): Promise<void>;
91
+ /**
92
+ * 健康检查
93
+ * GET /api/health
94
+ */
95
+ health(): Promise<{
96
+ status: string;
97
+ version: string;
98
+ }>;
99
+ }
100
+ /**
101
+ * 创建 User API 客户端实例
102
+ */
103
+ declare function createUserClient(config: UserClientConfig): UserApiClient;
104
+
105
+ export { type ResolvePageRequest, UserApiClient, type UserClientConfig, createUserClient, UserApiClient as default };
@@ -0,0 +1,105 @@
1
+ import { ApiResponse, PageResolveResult, ActionExecuteRequest, ActionExecuteResponse, DataQueryRequest, DataQueryResponse } from '@djvlc/contracts-types';
2
+
3
+ /**
4
+ * @djvlc/openapi-user-client - User API 客户端
5
+ *
6
+ * 自动从 OpenAPI 规范生成,提供类型安全的 User API 访问
7
+ * 用于 Runtime 访问后端服务
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+
12
+ interface UserClientConfig {
13
+ /** API 基础 URL */
14
+ baseUrl: string;
15
+ /** 用户 Token */
16
+ token?: string;
17
+ /** 设备 ID */
18
+ deviceId?: string;
19
+ /** 渠道 */
20
+ channel?: string;
21
+ /** 请求超时(毫秒) */
22
+ timeout?: number;
23
+ /** 自定义请求头 */
24
+ headers?: Record<string, string>;
25
+ /** 请求拦截器 */
26
+ onRequest?: (config: RequestInit) => RequestInit;
27
+ /** 响应拦截器 */
28
+ onResponse?: <T>(response: ApiResponse<T>) => ApiResponse<T>;
29
+ /** 错误处理 */
30
+ onError?: (error: Error) => void;
31
+ }
32
+ /** 页面解析请求 */
33
+ interface ResolvePageRequest {
34
+ pageUid: string;
35
+ channel?: string;
36
+ uid?: string;
37
+ deviceId?: string;
38
+ previewToken?: string;
39
+ }
40
+ /**
41
+ * User API 客户端类
42
+ * 用于 Runtime 访问后端服务
43
+ */
44
+ declare class UserApiClient {
45
+ private config;
46
+ constructor(config: UserClientConfig);
47
+ /**
48
+ * 获取通用请求头
49
+ */
50
+ private getCommonHeaders;
51
+ /**
52
+ * 发送请求
53
+ */
54
+ private request;
55
+ /**
56
+ * 解析页面
57
+ * GET /api/page/resolve
58
+ */
59
+ resolvePage(request: ResolvePageRequest): Promise<PageResolveResult>;
60
+ /**
61
+ * 执行动作
62
+ * POST /api/actions/execute
63
+ */
64
+ executeAction<T = unknown>(request: ActionExecuteRequest): Promise<ActionExecuteResponse<T>>;
65
+ /**
66
+ * 快捷方法:领取
67
+ */
68
+ claim(activityId: string, params?: Record<string, unknown>): Promise<ActionExecuteResponse>;
69
+ /**
70
+ * 快捷方法:签到
71
+ */
72
+ signin(activityId: string): Promise<ActionExecuteResponse>;
73
+ /**
74
+ * 查询数据
75
+ * POST /api/data/query
76
+ */
77
+ queryData<T = unknown>(request: DataQueryRequest): Promise<DataQueryResponse<T>>;
78
+ /**
79
+ * 快捷方法:查询数据
80
+ */
81
+ query<T = unknown>(queryVersionId: string, params?: Record<string, unknown>): Promise<T | undefined>;
82
+ /**
83
+ * 上报埋点
84
+ * POST /api/track
85
+ */
86
+ track(events: Array<{
87
+ eventName: string;
88
+ params?: Record<string, unknown>;
89
+ timestamp?: number;
90
+ }>): Promise<void>;
91
+ /**
92
+ * 健康检查
93
+ * GET /api/health
94
+ */
95
+ health(): Promise<{
96
+ status: string;
97
+ version: string;
98
+ }>;
99
+ }
100
+ /**
101
+ * 创建 User API 客户端实例
102
+ */
103
+ declare function createUserClient(config: UserClientConfig): UserApiClient;
104
+
105
+ export { type ResolvePageRequest, UserApiClient, type UserClientConfig, createUserClient, UserApiClient as default };
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ UserApiClient: () => UserApiClient,
24
+ createUserClient: () => createUserClient,
25
+ default: () => index_default
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+ var UserApiClient = class {
29
+ constructor(config) {
30
+ this.config = {
31
+ timeout: 1e4,
32
+ ...config
33
+ };
34
+ }
35
+ /**
36
+ * 获取通用请求头
37
+ */
38
+ getCommonHeaders() {
39
+ const headers = {
40
+ "Content-Type": "application/json",
41
+ ...this.config.headers
42
+ };
43
+ if (this.config.token) {
44
+ headers["Authorization"] = `Bearer ${this.config.token}`;
45
+ }
46
+ if (this.config.deviceId) {
47
+ headers["x-device-id"] = this.config.deviceId;
48
+ }
49
+ if (this.config.channel) {
50
+ headers["x-channel"] = this.config.channel;
51
+ }
52
+ return headers;
53
+ }
54
+ /**
55
+ * 发送请求
56
+ */
57
+ async request(method, path, options) {
58
+ let url = `${this.config.baseUrl}${path}`;
59
+ if (options?.params) {
60
+ const searchParams = new URLSearchParams();
61
+ Object.entries(options.params).forEach(([key, value]) => {
62
+ if (value !== void 0) {
63
+ searchParams.append(key, value);
64
+ }
65
+ });
66
+ const queryString = searchParams.toString();
67
+ if (queryString) {
68
+ url += `?${queryString}`;
69
+ }
70
+ }
71
+ let requestConfig = {
72
+ method,
73
+ headers: {
74
+ ...this.getCommonHeaders(),
75
+ ...options?.headers
76
+ },
77
+ body: options?.body ? JSON.stringify(options.body) : void 0
78
+ };
79
+ if (this.config.onRequest) {
80
+ requestConfig = this.config.onRequest(requestConfig);
81
+ }
82
+ try {
83
+ const controller = new AbortController();
84
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
85
+ const response = await fetch(url, {
86
+ ...requestConfig,
87
+ signal: controller.signal
88
+ });
89
+ clearTimeout(timeoutId);
90
+ let result = await response.json();
91
+ if (this.config.onResponse) {
92
+ result = this.config.onResponse(result);
93
+ }
94
+ if (!result.success) {
95
+ throw new Error(result.message || "Request failed");
96
+ }
97
+ return result.data;
98
+ } catch (error) {
99
+ if (this.config.onError) {
100
+ this.config.onError(error);
101
+ }
102
+ throw error;
103
+ }
104
+ }
105
+ // ==================== 页面解析 ====================
106
+ /**
107
+ * 解析页面
108
+ * GET /api/page/resolve
109
+ */
110
+ async resolvePage(request) {
111
+ return this.request("GET", "/api/page/resolve", {
112
+ params: {
113
+ pageUid: request.pageUid,
114
+ channel: request.channel,
115
+ uid: request.uid,
116
+ deviceId: request.deviceId,
117
+ previewToken: request.previewToken
118
+ }
119
+ });
120
+ }
121
+ // ==================== Action Gateway ====================
122
+ /**
123
+ * 执行动作
124
+ * POST /api/actions/execute
125
+ */
126
+ async executeAction(request) {
127
+ return this.request("POST", "/api/actions/execute", {
128
+ body: request
129
+ });
130
+ }
131
+ /**
132
+ * 快捷方法:领取
133
+ */
134
+ async claim(activityId, params) {
135
+ return this.executeAction({
136
+ actionType: "claim",
137
+ params: { activityId, ...params },
138
+ context: {
139
+ deviceId: this.config.deviceId,
140
+ channel: this.config.channel
141
+ }
142
+ });
143
+ }
144
+ /**
145
+ * 快捷方法:签到
146
+ */
147
+ async signin(activityId) {
148
+ return this.executeAction({
149
+ actionType: "signin",
150
+ params: { activityId },
151
+ context: {
152
+ deviceId: this.config.deviceId,
153
+ channel: this.config.channel
154
+ }
155
+ });
156
+ }
157
+ // ==================== Data Proxy ====================
158
+ /**
159
+ * 查询数据
160
+ * POST /api/data/query
161
+ */
162
+ async queryData(request) {
163
+ return this.request("POST", "/api/data/query", {
164
+ body: request
165
+ });
166
+ }
167
+ /**
168
+ * 快捷方法:查询数据
169
+ */
170
+ async query(queryVersionId, params) {
171
+ const response = await this.queryData({
172
+ queryVersionId,
173
+ params: params || {}
174
+ });
175
+ return response.data;
176
+ }
177
+ // ==================== 埋点 ====================
178
+ /**
179
+ * 上报埋点
180
+ * POST /api/track
181
+ */
182
+ async track(events) {
183
+ await this.request("POST", "/api/track", {
184
+ body: { events }
185
+ });
186
+ }
187
+ // ==================== 健康检查 ====================
188
+ /**
189
+ * 健康检查
190
+ * GET /api/health
191
+ */
192
+ async health() {
193
+ return this.request("GET", "/api/health");
194
+ }
195
+ };
196
+ function createUserClient(config) {
197
+ return new UserApiClient(config);
198
+ }
199
+ var index_default = UserApiClient;
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ UserApiClient,
203
+ createUserClient
204
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,178 @@
1
+ // src/index.ts
2
+ var UserApiClient = class {
3
+ constructor(config) {
4
+ this.config = {
5
+ timeout: 1e4,
6
+ ...config
7
+ };
8
+ }
9
+ /**
10
+ * 获取通用请求头
11
+ */
12
+ getCommonHeaders() {
13
+ const headers = {
14
+ "Content-Type": "application/json",
15
+ ...this.config.headers
16
+ };
17
+ if (this.config.token) {
18
+ headers["Authorization"] = `Bearer ${this.config.token}`;
19
+ }
20
+ if (this.config.deviceId) {
21
+ headers["x-device-id"] = this.config.deviceId;
22
+ }
23
+ if (this.config.channel) {
24
+ headers["x-channel"] = this.config.channel;
25
+ }
26
+ return headers;
27
+ }
28
+ /**
29
+ * 发送请求
30
+ */
31
+ async request(method, path, options) {
32
+ let url = `${this.config.baseUrl}${path}`;
33
+ if (options?.params) {
34
+ const searchParams = new URLSearchParams();
35
+ Object.entries(options.params).forEach(([key, value]) => {
36
+ if (value !== void 0) {
37
+ searchParams.append(key, value);
38
+ }
39
+ });
40
+ const queryString = searchParams.toString();
41
+ if (queryString) {
42
+ url += `?${queryString}`;
43
+ }
44
+ }
45
+ let requestConfig = {
46
+ method,
47
+ headers: {
48
+ ...this.getCommonHeaders(),
49
+ ...options?.headers
50
+ },
51
+ body: options?.body ? JSON.stringify(options.body) : void 0
52
+ };
53
+ if (this.config.onRequest) {
54
+ requestConfig = this.config.onRequest(requestConfig);
55
+ }
56
+ try {
57
+ const controller = new AbortController();
58
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
59
+ const response = await fetch(url, {
60
+ ...requestConfig,
61
+ signal: controller.signal
62
+ });
63
+ clearTimeout(timeoutId);
64
+ let result = await response.json();
65
+ if (this.config.onResponse) {
66
+ result = this.config.onResponse(result);
67
+ }
68
+ if (!result.success) {
69
+ throw new Error(result.message || "Request failed");
70
+ }
71
+ return result.data;
72
+ } catch (error) {
73
+ if (this.config.onError) {
74
+ this.config.onError(error);
75
+ }
76
+ throw error;
77
+ }
78
+ }
79
+ // ==================== 页面解析 ====================
80
+ /**
81
+ * 解析页面
82
+ * GET /api/page/resolve
83
+ */
84
+ async resolvePage(request) {
85
+ return this.request("GET", "/api/page/resolve", {
86
+ params: {
87
+ pageUid: request.pageUid,
88
+ channel: request.channel,
89
+ uid: request.uid,
90
+ deviceId: request.deviceId,
91
+ previewToken: request.previewToken
92
+ }
93
+ });
94
+ }
95
+ // ==================== Action Gateway ====================
96
+ /**
97
+ * 执行动作
98
+ * POST /api/actions/execute
99
+ */
100
+ async executeAction(request) {
101
+ return this.request("POST", "/api/actions/execute", {
102
+ body: request
103
+ });
104
+ }
105
+ /**
106
+ * 快捷方法:领取
107
+ */
108
+ async claim(activityId, params) {
109
+ return this.executeAction({
110
+ actionType: "claim",
111
+ params: { activityId, ...params },
112
+ context: {
113
+ deviceId: this.config.deviceId,
114
+ channel: this.config.channel
115
+ }
116
+ });
117
+ }
118
+ /**
119
+ * 快捷方法:签到
120
+ */
121
+ async signin(activityId) {
122
+ return this.executeAction({
123
+ actionType: "signin",
124
+ params: { activityId },
125
+ context: {
126
+ deviceId: this.config.deviceId,
127
+ channel: this.config.channel
128
+ }
129
+ });
130
+ }
131
+ // ==================== Data Proxy ====================
132
+ /**
133
+ * 查询数据
134
+ * POST /api/data/query
135
+ */
136
+ async queryData(request) {
137
+ return this.request("POST", "/api/data/query", {
138
+ body: request
139
+ });
140
+ }
141
+ /**
142
+ * 快捷方法:查询数据
143
+ */
144
+ async query(queryVersionId, params) {
145
+ const response = await this.queryData({
146
+ queryVersionId,
147
+ params: params || {}
148
+ });
149
+ return response.data;
150
+ }
151
+ // ==================== 埋点 ====================
152
+ /**
153
+ * 上报埋点
154
+ * POST /api/track
155
+ */
156
+ async track(events) {
157
+ await this.request("POST", "/api/track", {
158
+ body: { events }
159
+ });
160
+ }
161
+ // ==================== 健康检查 ====================
162
+ /**
163
+ * 健康检查
164
+ * GET /api/health
165
+ */
166
+ async health() {
167
+ return this.request("GET", "/api/health");
168
+ }
169
+ };
170
+ function createUserClient(config) {
171
+ return new UserApiClient(config);
172
+ }
173
+ var index_default = UserApiClient;
174
+ export {
175
+ UserApiClient,
176
+ createUserClient,
177
+ index_default as default
178
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@djvlc/openapi-user-client",
3
+ "version": "1.0.0",
4
+ "description": "DJV Low-code Platform - User API 客户端(自动生成)",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
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
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
22
+ "lint": "eslint src/ --ext .ts",
23
+ "typecheck": "tsc --noEmit",
24
+ "clean": "rimraf dist",
25
+ "prepublishOnly": "pnpm run build"
26
+ },
27
+ "dependencies": {
28
+ "@djvlc/contracts-types": "1.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20.0.0",
32
+ "@typescript-eslint/eslint-plugin": "^7.0.0",
33
+ "@typescript-eslint/parser": "^7.0.0",
34
+ "eslint": "^8.56.0",
35
+ "openapi-typescript": "^6.0.0",
36
+ "rimraf": "^5.0.0",
37
+ "tsup": "^8.0.0",
38
+ "typescript": "^5.3.0"
39
+ },
40
+ "keywords": [
41
+ "lowcode",
42
+ "openapi",
43
+ "client",
44
+ "user-api",
45
+ "djvlc"
46
+ ],
47
+ "author": "DJV Team",
48
+ "license": "MIT",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/djvlc/contracts.git",
52
+ "directory": "openapi/user-client"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "registry": "https://registry.npmjs.org/"
57
+ },
58
+ "engines": {
59
+ "node": ">=18.0.0"
60
+ }
61
+ }