@jolibox/implement 1.1.4-beta.10

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 (195) hide show
  1. package/.eslintrc.js +13 -0
  2. package/.rush/temp/package-deps_build.json +105 -0
  3. package/.rush/temp/shrinkwrap-deps.json +79 -0
  4. package/README.md +1 -0
  5. package/dist/common/api-factory/index.d.ts +21 -0
  6. package/dist/common/api-factory/validator/__tests__/validate/any.test.d.ts +1 -0
  7. package/dist/common/api-factory/validator/__tests__/validate/array.test.d.ts +1 -0
  8. package/dist/common/api-factory/validator/__tests__/validate/arraybuffer.test.d.ts +1 -0
  9. package/dist/common/api-factory/validator/__tests__/validate/boolean.test.d.ts +1 -0
  10. package/dist/common/api-factory/validator/__tests__/validate/enum.test.d.ts +1 -0
  11. package/dist/common/api-factory/validator/__tests__/validate/function.test.d.ts +1 -0
  12. package/dist/common/api-factory/validator/__tests__/validate/literal.test.d.ts +1 -0
  13. package/dist/common/api-factory/validator/__tests__/validate/nullish.test.d.ts +1 -0
  14. package/dist/common/api-factory/validator/__tests__/validate/number.test.d.ts +1 -0
  15. package/dist/common/api-factory/validator/__tests__/validate/object.test.d.ts +1 -0
  16. package/dist/common/api-factory/validator/__tests__/validate/or.test.d.ts +1 -0
  17. package/dist/common/api-factory/validator/__tests__/validate/record.test.d.ts +1 -0
  18. package/dist/common/api-factory/validator/__tests__/validate/string.test.d.ts +1 -0
  19. package/dist/common/api-factory/validator/__tests__/validate/symbol.test.d.ts +1 -0
  20. package/dist/common/api-factory/validator/__tests__/validate/tuple.test.d.ts +1 -0
  21. package/dist/common/api-factory/validator/__tests__/validate/type-asserts.test.d.ts +1 -0
  22. package/dist/common/api-factory/validator/__tests__/validate/utils.test.d.ts +1 -0
  23. package/dist/common/api-factory/validator/index.d.ts +29 -0
  24. package/dist/common/api-factory/validator/validate.d.ts +119 -0
  25. package/dist/common/can-i-use.d.ts +2 -0
  26. package/dist/common/context/index.d.ts +16 -0
  27. package/dist/common/context/types.d.ts +5 -0
  28. package/dist/common/context/url-parse.d.ts +22 -0
  29. package/dist/common/http/index.d.ts +13 -0
  30. package/dist/common/http/uuid.d.ts +2 -0
  31. package/dist/common/http/xua.d.ts +17 -0
  32. package/dist/common/report/base-tracker.d.ts +13 -0
  33. package/dist/common/report/create-trace.d.ts +7 -0
  34. package/dist/common/report/errors/error-types.d.ts +122 -0
  35. package/dist/common/report/errors/index.d.ts +13 -0
  36. package/dist/common/report/errors/report/index.d.ts +51 -0
  37. package/dist/common/report/errors/report/listeners.d.ts +1 -0
  38. package/dist/common/report/index.d.ts +3 -0
  39. package/dist/common/report/task-track/index.d.ts +25 -0
  40. package/dist/common/report/track.d.ts +3 -0
  41. package/dist/common/report/types.d.ts +75 -0
  42. package/dist/h5/ads/ads-action-detection.d.ts +6 -0
  43. package/dist/h5/ads/anti-cheating.d.ts +61 -0
  44. package/dist/h5/ads/index.d.ts +275 -0
  45. package/dist/h5/api/base.d.ts +13 -0
  46. package/dist/h5/api/get-system-info.d.ts +1 -0
  47. package/dist/h5/api/index.d.ts +4 -0
  48. package/dist/h5/api/lifecycle.d.ts +1 -0
  49. package/dist/h5/api/storage.d.ts +27 -0
  50. package/dist/h5/api/task.d.ts +1 -0
  51. package/dist/h5/bootstrap/index.d.ts +1 -0
  52. package/dist/h5/http/index.d.ts +33 -0
  53. package/dist/h5/http/utils/__tests__/uuid.test.d.ts +1 -0
  54. package/dist/h5/http/utils/__tests__/xua.test.d.ts +1 -0
  55. package/dist/h5/http/utils/index.d.ts +14 -0
  56. package/dist/h5/http/utils/session.d.ts +1 -0
  57. package/dist/h5/report/errors/index.d.ts +4 -0
  58. package/dist/h5/report/event-tracker.d.ts +8 -0
  59. package/dist/h5/report/index.d.ts +10 -0
  60. package/dist/h5/report/task-tracker.d.ts +18 -0
  61. package/dist/index.d.ts +3 -0
  62. package/dist/index.js +13 -0
  63. package/dist/index.native.d.ts +2 -0
  64. package/dist/index.native.js +3 -0
  65. package/dist/native/api/ads.d.ts +1 -0
  66. package/dist/native/api/base.d.ts +13 -0
  67. package/dist/native/api/get-system-info.d.ts +1 -0
  68. package/dist/native/api/index.d.ts +8 -0
  69. package/dist/native/api/keyboard.d.ts +9 -0
  70. package/dist/native/api/lifecycle.d.ts +1 -0
  71. package/dist/native/api/login.d.ts +1 -0
  72. package/dist/native/api/request.d.ts +1 -0
  73. package/dist/native/api/storage.d.ts +25 -0
  74. package/dist/native/api/task.d.ts +1 -0
  75. package/dist/native/bootstrap/bridge.d.ts +4 -0
  76. package/dist/native/bootstrap/index.d.ts +1 -0
  77. package/dist/native/js-bridge/const.d.ts +5 -0
  78. package/dist/native/js-bridge/index.d.ts +2 -0
  79. package/dist/native/js-bridge/invoke.d.ts +21 -0
  80. package/dist/native/js-bridge/js-bridge.d.ts +6 -0
  81. package/dist/native/js-bridge/report.d.ts +63 -0
  82. package/dist/native/js-bridge/subscribe.d.ts +8 -0
  83. package/dist/native/js-bridge/types.d.ts +14 -0
  84. package/dist/native/js-bridge/utils.d.ts +17 -0
  85. package/dist/native/js-core/index.d.ts +3 -0
  86. package/dist/native/js-core/jolibox-js-core.d.ts +45 -0
  87. package/dist/native/js-core/message-port.d.ts +12 -0
  88. package/dist/native/js-core/utils.d.ts +7 -0
  89. package/dist/native/network/create-fetch.d.ts +27 -0
  90. package/dist/native/network/index.d.ts +11 -0
  91. package/dist/native/network/report.d.ts +15 -0
  92. package/dist/native/network/types.d.ts +61 -0
  93. package/dist/native/network/utils.d.ts +9 -0
  94. package/dist/native/report/errors/index.d.ts +4 -0
  95. package/dist/native/report/index.d.ts +10 -0
  96. package/dist/native/report/task-tracker.d.ts +24 -0
  97. package/dist/utils/index.d.ts +0 -0
  98. package/esbuild.config.js +66 -0
  99. package/implement.build.log +9 -0
  100. package/package.json +30 -0
  101. package/src/common/api-factory/index.ts +188 -0
  102. package/src/common/api-factory/validator/__tests__/validate/any.test.ts +68 -0
  103. package/src/common/api-factory/validator/__tests__/validate/array.test.ts +402 -0
  104. package/src/common/api-factory/validator/__tests__/validate/arraybuffer.test.ts +48 -0
  105. package/src/common/api-factory/validator/__tests__/validate/boolean.test.ts +27 -0
  106. package/src/common/api-factory/validator/__tests__/validate/enum.test.ts +106 -0
  107. package/src/common/api-factory/validator/__tests__/validate/function.test.ts +54 -0
  108. package/src/common/api-factory/validator/__tests__/validate/literal.test.ts +130 -0
  109. package/src/common/api-factory/validator/__tests__/validate/nullish.test.ts +41 -0
  110. package/src/common/api-factory/validator/__tests__/validate/number.test.ts +147 -0
  111. package/src/common/api-factory/validator/__tests__/validate/object.test.ts +131 -0
  112. package/src/common/api-factory/validator/__tests__/validate/or.test.ts +96 -0
  113. package/src/common/api-factory/validator/__tests__/validate/record.test.ts +274 -0
  114. package/src/common/api-factory/validator/__tests__/validate/string.test.ts +187 -0
  115. package/src/common/api-factory/validator/__tests__/validate/symbol.test.ts +23 -0
  116. package/src/common/api-factory/validator/__tests__/validate/tuple.test.ts +86 -0
  117. package/src/common/api-factory/validator/__tests__/validate/type-asserts.test.ts +13 -0
  118. package/src/common/api-factory/validator/__tests__/validate/utils.test.ts +44 -0
  119. package/src/common/api-factory/validator/index.ts +107 -0
  120. package/src/common/api-factory/validator/validate.ts +641 -0
  121. package/src/common/can-i-use.ts +19 -0
  122. package/src/common/context/index.ts +85 -0
  123. package/src/common/context/types.ts +5 -0
  124. package/src/common/context/url-parse.ts +63 -0
  125. package/src/common/http/index.ts +29 -0
  126. package/src/common/http/uuid.ts +11 -0
  127. package/src/common/http/xua.ts +79 -0
  128. package/src/common/report/base-tracker.ts +134 -0
  129. package/src/common/report/create-trace.ts +17 -0
  130. package/src/common/report/errors/error-types.ts +206 -0
  131. package/src/common/report/errors/index.ts +20 -0
  132. package/src/common/report/errors/report/index.ts +63 -0
  133. package/src/common/report/errors/report/listeners.ts +80 -0
  134. package/src/common/report/index.ts +3 -0
  135. package/src/common/report/task-track/index.ts +102 -0
  136. package/src/common/report/track.ts +49 -0
  137. package/src/common/report/types.ts +90 -0
  138. package/src/h5/ads/ads-action-detection.ts +31 -0
  139. package/src/h5/ads/anti-cheating.ts +244 -0
  140. package/src/h5/ads/index.ts +658 -0
  141. package/src/h5/api/base.ts +9 -0
  142. package/src/h5/api/get-system-info.ts +59 -0
  143. package/src/h5/api/index.ts +4 -0
  144. package/src/h5/api/lifecycle.ts +95 -0
  145. package/src/h5/api/storage.ts +173 -0
  146. package/src/h5/api/task.ts +190 -0
  147. package/src/h5/bootstrap/index.ts +16 -0
  148. package/src/h5/http/index.ts +189 -0
  149. package/src/h5/http/utils/__tests__/uuid.test.ts +16 -0
  150. package/src/h5/http/utils/__tests__/xua.test.ts +27 -0
  151. package/src/h5/http/utils/index.ts +19 -0
  152. package/src/h5/http/utils/session.ts +10 -0
  153. package/src/h5/report/errors/index.ts +40 -0
  154. package/src/h5/report/event-tracker.ts +40 -0
  155. package/src/h5/report/index.ts +56 -0
  156. package/src/h5/report/task-tracker.ts +42 -0
  157. package/src/index.native.ts +7 -0
  158. package/src/index.ts +9 -0
  159. package/src/native/api/ads.ts +52 -0
  160. package/src/native/api/base.ts +8 -0
  161. package/src/native/api/get-system-info.ts +44 -0
  162. package/src/native/api/index.ts +8 -0
  163. package/src/native/api/keyboard.ts +75 -0
  164. package/src/native/api/lifecycle.ts +76 -0
  165. package/src/native/api/login.ts +73 -0
  166. package/src/native/api/request.ts +154 -0
  167. package/src/native/api/storage.ts +287 -0
  168. package/src/native/api/task.ts +227 -0
  169. package/src/native/bootstrap/bridge.ts +59 -0
  170. package/src/native/bootstrap/index.ts +59 -0
  171. package/src/native/js-bridge/const.ts +11 -0
  172. package/src/native/js-bridge/index.ts +2 -0
  173. package/src/native/js-bridge/invoke.ts +210 -0
  174. package/src/native/js-bridge/js-bridge.ts +23 -0
  175. package/src/native/js-bridge/report.ts +311 -0
  176. package/src/native/js-bridge/subscribe.ts +50 -0
  177. package/src/native/js-bridge/types.ts +26 -0
  178. package/src/native/js-bridge/utils.ts +116 -0
  179. package/src/native/js-core/index.ts +4 -0
  180. package/src/native/js-core/jolibox-js-core.ts +188 -0
  181. package/src/native/js-core/message-port.ts +52 -0
  182. package/src/native/js-core/utils.ts +9 -0
  183. package/src/native/network/create-fetch.ts +237 -0
  184. package/src/native/network/index.ts +15 -0
  185. package/src/native/network/report.ts +58 -0
  186. package/src/native/network/types.ts +77 -0
  187. package/src/native/network/utils.ts +90 -0
  188. package/src/native/report/errors/index.ts +27 -0
  189. package/src/native/report/index.ts +51 -0
  190. package/src/native/report/task-tracker.ts +72 -0
  191. package/src/native/types/global.d.ts +26 -0
  192. package/src/native/types/native-method-map.d.ts +282 -0
  193. package/src/native/types/native-method.d.ts +30 -0
  194. package/src/utils/index.ts +0 -0
  195. package/tsconfig.json +18 -0
@@ -0,0 +1,189 @@
1
+ import { IHttpClient } from '@/common/http';
2
+ import { xUserAgent } from '../../common/http/xua';
3
+ import { context } from '@/common/context';
4
+
5
+ declare global {
6
+ interface Window {
7
+ JoliboxHttpClient: typeof JoliboxHttpClient;
8
+ }
9
+ }
10
+
11
+ interface IHttpClientInitParams {
12
+ baseUrl?: string;
13
+ }
14
+
15
+ const timeoutFn = (ms: number) => {
16
+ const ctrl = new AbortController();
17
+ setTimeout(() => ctrl.abort(), ms);
18
+ return ctrl.signal;
19
+ };
20
+
21
+ (AbortSignal as any).timeout ??= timeoutFn;
22
+
23
+ export class JoliboxHttpClient implements IHttpClient {
24
+ private baseUrl: string;
25
+ private xua = xUserAgent();
26
+
27
+ private getJoliSource = () => {
28
+ const urlParams = new URLSearchParams(window.location.search);
29
+ return urlParams.get('joliSource') ?? null;
30
+ };
31
+
32
+ // private getApiBaseURL = () => {
33
+ // const urlParams = new URLSearchParams(window.location.search);
34
+ // return urlParams.get('apiBaseURL') ?? null;
35
+ // };
36
+
37
+ constructor(config?: IHttpClientInitParams) {
38
+ const defaultUrl = context.testMode ? 'https://stg-api.jolibox.com' : 'https://api.jolibox.com';
39
+ this.baseUrl = config?.baseUrl ?? defaultUrl;
40
+ }
41
+
42
+ async get<T>(
43
+ path: string,
44
+ {
45
+ query,
46
+ headers,
47
+ timeout
48
+ }: {
49
+ query?: Record<string, string>;
50
+ headers?: Record<string, string>;
51
+ timeout?: number;
52
+ }
53
+ ) {
54
+ const searchParams = new URLSearchParams(query);
55
+ const search = searchParams.toString();
56
+ const url = `${this.baseUrl}${path}${search ? `?${search}` : ''}`;
57
+ const xua = this.xua;
58
+ const joliSource = this.getJoliSource();
59
+ headers = Object.assign(
60
+ {},
61
+ headers,
62
+ xua ? { 'x-user-agent': xua } : {},
63
+ joliSource ? { 'x-joli-source': joliSource } : {}
64
+ );
65
+ const response = await fetch(url, {
66
+ method: 'GET',
67
+ headers,
68
+ signal: timeoutFn(timeout ?? 30000)
69
+ });
70
+ return (await response.json()) as T;
71
+ }
72
+
73
+ async post<T = any>(
74
+ path: string,
75
+ {
76
+ data,
77
+ query,
78
+ headers,
79
+ timeout
80
+ }: {
81
+ data?: any;
82
+ query?: Record<string, string>;
83
+ headers?: Record<string, string>;
84
+ timeout?: number;
85
+ }
86
+ ) {
87
+ const searchParams = new URLSearchParams(query);
88
+ const search = searchParams.toString();
89
+ const url = `${this.baseUrl}${path}${search ? `?${search}` : ''}`;
90
+ const xua = this.xua;
91
+ const joliSource = this.getJoliSource();
92
+ headers = Object.assign(
93
+ {},
94
+ headers,
95
+ { 'Content-Type': 'application/json' },
96
+ xua ? { 'x-user-agent': xua } : {},
97
+ joliSource ? { 'x-joli-source': joliSource } : {}
98
+ );
99
+ const response = await fetch(url, {
100
+ method: 'POST',
101
+ headers,
102
+ body: JSON.stringify(data),
103
+ signal: timeoutFn(timeout ?? 30000)
104
+ });
105
+ const responseContentType = response.headers.get('content-type');
106
+
107
+ // return blob
108
+ if (responseContentType?.includes('application/octet-stream')) {
109
+ try {
110
+ return response.blob() as unknown as T;
111
+ } catch (e) {
112
+ return (await response.arrayBuffer()) as unknown as T;
113
+ }
114
+ }
115
+
116
+ // return form data
117
+ if (
118
+ responseContentType?.includes('multipart/form-data') ||
119
+ responseContentType?.includes('application/x-www-form-urlencoded')
120
+ ) {
121
+ try {
122
+ return response.formData() as unknown as T;
123
+ } catch (e) {
124
+ return (await response.text()) as unknown as T;
125
+ }
126
+ }
127
+
128
+ // return json
129
+ if (responseContentType?.includes('application/json')) {
130
+ try {
131
+ return (await response.json()) as T;
132
+ } catch (e) {
133
+ return (await response.text()) as unknown as T;
134
+ }
135
+ }
136
+
137
+ // return text
138
+ return response as unknown as T;
139
+ }
140
+
141
+ async put<T>(
142
+ path: string,
143
+ {
144
+ data,
145
+ query,
146
+ headers
147
+ }: {
148
+ data?: any;
149
+ query?: Record<string, string>;
150
+ headers?: Record<string, string>;
151
+ timeout?: number;
152
+ }
153
+ ) {
154
+ try {
155
+ const searchParams = new URLSearchParams(query);
156
+ const search = searchParams.toString();
157
+ const url = `${this.baseUrl}${path}${search ? `?${search}` : ''}`;
158
+ const xua = this.xua;
159
+ const joliSource = this.getJoliSource();
160
+ headers = Object.assign(
161
+ {},
162
+ headers,
163
+ { 'Content-Type': 'application/json' },
164
+ xua ? { 'x-user-agent': xua } : {},
165
+ joliSource ? { 'x-joli-source': joliSource } : {}
166
+ );
167
+ const response = await fetch(url, {
168
+ method: 'PUT',
169
+ headers,
170
+ body: JSON.stringify(data ?? {}) // 将数据转换为 JSON 字符串
171
+ });
172
+
173
+ // 检查响应状态
174
+ if (!response.ok) {
175
+ throw new Error(`HTTP error! status: ${response.status}`);
176
+ }
177
+
178
+ const result = (await response.json()) as T;
179
+ return result;
180
+ } catch (error) {
181
+ console.error('Error:', error);
182
+ throw error;
183
+ }
184
+ }
185
+ }
186
+
187
+ window.JoliboxHttpClient = JoliboxHttpClient;
188
+
189
+ export default JoliboxHttpClient;
@@ -0,0 +1,16 @@
1
+ // import { expect, test } from "vitest";
2
+ import { isValidUUIDV4, uuidv4 } from '../../../../common/http/uuid';
3
+
4
+ describe('uuid', () => {
5
+ it('test uuid', () => {
6
+ const uuid = uuidv4();
7
+ expect(uuid.length).toBe(36);
8
+ expect(isValidUUIDV4(uuid)).toBe(true);
9
+ });
10
+
11
+ it('isValidUUIDV4', () => {
12
+ const randomUUIDV4 = '5b1e0b7b-3b3b-4b4b-8b8b-9b9b0b1b2b3b';
13
+ expect(isValidUUIDV4(randomUUIDV4)).toBe(true);
14
+ expect(isValidUUIDV4('')).toBe(false);
15
+ });
16
+ });
@@ -0,0 +1,27 @@
1
+ // import { expect, test } from 'jest';
2
+ import { xUserAgent, getAppVersion } from '../../../../common/http/xua';
3
+ import { isValidUUIDV4 } from '../../../../common/http/uuid';
4
+
5
+ describe('xua', () => {
6
+ it('test xua', () => {
7
+ // window.__JOLIBOX_LOCAL_SDK_VERSION__ = '1.0.0';
8
+ const xua = xUserAgent();
9
+ const deviceInfoRegex = /.* (\(.*\) ).*/;
10
+ const deviceInfoMatch = xua.match(deviceInfoRegex);
11
+ const deviceInfo = deviceInfoMatch?.[1] ?? '';
12
+ const rest = xua.replace(deviceInfo, '');
13
+ expect(deviceInfo).toBe('(JoliboxSDKPC; UnknownModel; UnknownSystemVersion; en-US) ');
14
+
15
+ const [appName, uuidPart, adidPart, versionPart] = rest.split(' ');
16
+ const uuid = uuidPart.split('/')[1];
17
+ const adid = adidPart.split('/')[1];
18
+ const version = versionPart.split('/')[1];
19
+ expect(appName).toBe('JoliboxWebSDK');
20
+ expect(uuidPart.startsWith('uuid/')).toBe(true);
21
+ expect(adidPart.startsWith('adid/')).toBe(true);
22
+ expect(versionPart.startsWith('version/')).toBe(true);
23
+ expect(isValidUUIDV4(uuid)).toBe(true);
24
+ expect(isValidUUIDV4(adid)).toBe(true);
25
+ expect(version).toBe(getAppVersion());
26
+ });
27
+ });
@@ -0,0 +1,19 @@
1
+ import * as xua from '@/common/http/xua';
2
+ import * as session from './session';
3
+ import * as uuid from '@/common/http/uuid';
4
+
5
+ declare global {
6
+ interface Window {
7
+ JoliboxUtil: typeof JoliboxUtil;
8
+ }
9
+ }
10
+
11
+ class JoliboxUtil {
12
+ static xua = xua;
13
+ static session = session;
14
+ static uuid = uuid;
15
+ }
16
+
17
+ window.JoliboxUtil = JoliboxUtil;
18
+
19
+ export { JoliboxUtil };
@@ -0,0 +1,10 @@
1
+ import { uuidv4 } from '../../../common/http/uuid';
2
+
3
+ export const getGameSessionId = () => {
4
+ let sessionId = sessionStorage.getItem('gameSessionId');
5
+ if (!sessionId) {
6
+ sessionId = uuidv4();
7
+ sessionStorage.setItem('gameSessionId', sessionId);
8
+ }
9
+ return sessionId;
10
+ };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * h5 错误上报实现
3
+ */
4
+ import '@/common/report/errors/report/listeners';
5
+ import { BaseError, logger } from '@jolibox/common';
6
+ import { errorReportEmitter } from '@/common/report/errors';
7
+ import { ErrorData } from '@/common/report/errors/report';
8
+ import { IEvent, EventType, EProject } from '@/common/report';
9
+ import { tracker } from '../event-tracker';
10
+ import { context } from '@/common/context';
11
+
12
+ const doReport = (eventName: string, error: Error | BaseError) => {
13
+ const extra = {
14
+ message: error.message,
15
+ stack: error.stack ?? '',
16
+ errorType: error.name,
17
+ source: 0
18
+ };
19
+
20
+ const eventBody: IEvent = {
21
+ name: eventName,
22
+ type: EventType.ErrorTrace,
23
+ location: null,
24
+ target: null,
25
+ extra: extra,
26
+ timestamp: Date.now(),
27
+ userId: context.hostUserInfo?.uid ?? null
28
+ };
29
+
30
+ tracker.trackEvent(eventBody, EProject.WebSDK);
31
+ };
32
+
33
+ errorReportEmitter.on('GLOBAL_ERROR', (error, errorData: ErrorData) => {
34
+ doReport(errorData.tag, error);
35
+ });
36
+
37
+ errorReportEmitter.on('GLOBAL_USER_ERROR', (error, errorData: ErrorData) => {
38
+ // TODO: User Error
39
+ logger.log('UserError', error, errorData);
40
+ });
@@ -0,0 +1,40 @@
1
+ import { context } from '@/common/context';
2
+ import { logger } from '@jolibox/common';
3
+ import { EProject, EventTracker, EventType } from '@/common/report';
4
+ import JoliboxHttpClient from '@/h5/http';
5
+
6
+ export class H5EventTracker extends EventTracker {
7
+ private hostToApiMap: Record<string | 'default', { test: string; prod: string }> = {
8
+ default: {
9
+ test: 'https://stg-collect.jolibox.com',
10
+ prod: 'https://collect.jolibox.com'
11
+ },
12
+ 'oss.jolibox.com': {
13
+ test: 'https://stg-collect.jolibox.com',
14
+ prod: 'https://collect.jolibox.com'
15
+ },
16
+ 'oss.pico-game.com': {
17
+ test: 'https://stg-collect.pico-game.com',
18
+ prod: 'https://collect.pico-game.com'
19
+ }
20
+ };
21
+
22
+ private get apiBaseURL() {
23
+ const api = this.hostToApiMap[window.location.host] ?? this.hostToApiMap.default;
24
+ return context.testMode ?? false ? api.test : api.prod;
25
+ }
26
+
27
+ private httpClient = new JoliboxHttpClient({
28
+ baseUrl: this.apiBaseURL
29
+ });
30
+
31
+ doReport(event: unknown[]): void {
32
+ this.httpClient.post('/report', {
33
+ data: event,
34
+ timeout: 5000
35
+ });
36
+ logger.info('Track', event);
37
+ }
38
+ }
39
+
40
+ export const tracker = new H5EventTracker();
@@ -0,0 +1,56 @@
1
+ import './errors';
2
+ import { createCommands, EventEmitter, isNumber, isObject, isString } from '@jolibox/common';
3
+ import { CommonReportConfig, createTracks, EProject, EventType, IEvent } from '../../common/report';
4
+ import { tracker } from './event-tracker';
5
+ import { H5TaskTracker } from './task-tracker';
6
+ import { context } from '@/common/context';
7
+ export * from '../../common/report/types';
8
+
9
+ const commands = createCommands();
10
+
11
+ const commonParams: CommonReportConfig = {
12
+ type: EProject.WebSDK,
13
+ platform: 'h5',
14
+ jssdk_version: context.sdkInfo.jssdkVersion,
15
+ mp_id: context.mpId,
16
+ mp_version: context.mpVersion
17
+ };
18
+
19
+ const { track, trackPerformance } = createTracks((...args) => {
20
+ const [, data] = args;
21
+
22
+ const _data = data as Record<string, unknown>;
23
+
24
+ const originExtra = isObject(_data.extra)
25
+ ? _data.extra
26
+ : isString(_data.extra)
27
+ ? JSON.parse(_data.extra)
28
+ : {};
29
+ const extra = {
30
+ ...originExtra,
31
+ mp_id: (_data.mp_id as string) ?? '',
32
+ mp_version: (_data.mp_version as string) ?? ''
33
+ };
34
+ const eventBody: IEvent = {
35
+ name: data.tag,
36
+ type: EventType.System,
37
+ location: null,
38
+ target: null,
39
+ extra: extra,
40
+ timestamp: Date.now(),
41
+ userId: context.hostUserInfo?.uid ?? null
42
+ };
43
+ tracker.trackEvent(eventBody, EProject.WebSDK);
44
+ }, commonParams);
45
+ export { track, trackPerformance };
46
+
47
+ commands.registerCommand('ReportSDK.traceSystemTimeline', ({ event, duration }) => {
48
+ trackPerformance(event, duration);
49
+ });
50
+
51
+ commands.registerCommand('ReportSDK.traceSystem', ({ event, info }) => {
52
+ track(event, info);
53
+ });
54
+
55
+ export const h5TaskEmitter = new EventEmitter<{ visible: [boolean] }>();
56
+ export const taskTracker = new H5TaskTracker(track, h5TaskEmitter);
@@ -0,0 +1,42 @@
1
+ /**
2
+ * H5任务上报
3
+ */
4
+
5
+ import { context } from '@/common/context';
6
+ import { TaskTracker, TaskPoint } from '@/common/report/task-track';
7
+ import { EventEmitter, logger } from '@jolibox/common';
8
+ import JoliboxHttpClient from '../http';
9
+ import type { Track } from '.';
10
+ import type { TrackEvent } from '@jolibox/types';
11
+
12
+ export class H5TaskTracker extends TaskTracker {
13
+ private gameId: string;
14
+ private sessionId: string;
15
+ private httpClient = new JoliboxHttpClient();
16
+ private track: Track;
17
+
18
+ constructor(track: Track, eventEmitter: EventEmitter<{ visible: [boolean] }>, interval?: number) {
19
+ super(eventEmitter, interval);
20
+ this.gameId = context.mpId;
21
+ this.sessionId = context.sessionId;
22
+ this.track = track;
23
+ }
24
+
25
+ async reporter(point: TaskPoint): Promise<void> {
26
+ const { event, params } = point;
27
+ await this.httpClient.post(`/api/base/app-event`, {
28
+ data: {
29
+ eventType: event,
30
+ gameInfo: {
31
+ gameId: this.gameId,
32
+ sessionId: this.sessionId,
33
+ ...params
34
+ }
35
+ }
36
+ });
37
+ logger.log('-----task tracker-----', event, params);
38
+ }
39
+ tracker(event: TrackEvent, info: Record<string, unknown> | null = null) {
40
+ this.track(event, info);
41
+ }
42
+ }
@@ -0,0 +1,7 @@
1
+ //TODO: entrance for native
2
+ import { config } from './native/bootstrap';
3
+
4
+ import './native/api';
5
+ import './native/report';
6
+
7
+ config();
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ //TODO(DX): H5 entrance
2
+ // internal modules
3
+ import './h5/report';
4
+ //global apis
5
+ import './h5/api';
6
+ import './h5/ads';
7
+ import { config } from './h5/bootstrap';
8
+
9
+ config();
@@ -0,0 +1,52 @@
1
+ import { createCommands, logger } from '@jolibox/common';
2
+ import { createSyncAPI, registerCanIUse } from './base';
3
+
4
+ const commands = createCommands();
5
+
6
+ const adInit = createSyncAPI('adInit', {
7
+ implement: (config) => {
8
+ logger.log('[JoliboxAdsSDK] adInit', config);
9
+ }
10
+ });
11
+
12
+ const adConfig = createSyncAPI('adConfig', {
13
+ implement: (params) => {
14
+ logger.log('[JoliboxAdsSDK] adConfig', params);
15
+ }
16
+ });
17
+
18
+ const adBreak = createSyncAPI('adBreak', {
19
+ implement: (params) => {
20
+ logger.log('[JoliboxAdsSDK] adBreak', params);
21
+ }
22
+ });
23
+
24
+ const adUnit = createSyncAPI('adUnit', {
25
+ implement: (params) => {
26
+ logger.log('[JoliboxAdsSDK] adUnit', params);
27
+ }
28
+ });
29
+
30
+ commands.registerCommand('AdsSDK.init', adInit);
31
+
32
+ commands.registerCommand('AdsSDK.adConfig', adConfig);
33
+
34
+ commands.registerCommand('AdsSDK.adBreak', adBreak);
35
+
36
+ commands.registerCommand('AdsSDK.adUnit', adUnit);
37
+
38
+ registerCanIUse('AdsSDK.init', {
39
+ version: '1.0.0'
40
+ });
41
+
42
+ registerCanIUse('AdsSDK.adConfig', {
43
+ version: '1.0.0'
44
+ });
45
+
46
+ registerCanIUse('AdsSDK.adBreak', {
47
+ version: '1.0.0'
48
+ });
49
+
50
+ registerCanIUse('AdsSDK.adUnit', {
51
+ version: '1.0.0'
52
+ });
@@ -0,0 +1,8 @@
1
+ import { registerCanIUseFactory } from '@/common/can-i-use';
2
+ import { track } from '../report';
3
+ import { createAPIFactory } from '@/common/api-factory';
4
+ export const { createAPI, createSyncAPI } = createAPIFactory(track);
5
+
6
+ export const registerCanIUse = registerCanIUseFactory('native');
7
+ import { t } from '@/common/api-factory/validator';
8
+ export { t };
@@ -0,0 +1,44 @@
1
+ import { createCommands } from '@jolibox/common';
2
+ import { createSyncAPI, registerCanIUse } from './base';
3
+ import { invokeNative } from '../bootstrap/bridge';
4
+
5
+ const commands = createCommands();
6
+
7
+ const API_ENV = 'env';
8
+ const API_GET_SYSTEM_SYNC = 'getSystemInfoSync';
9
+
10
+ const getSystemInfoSync = createSyncAPI(API_GET_SYSTEM_SYNC, {
11
+ implement: () => {
12
+ const res = invokeNative('envSync');
13
+ const { data } = res;
14
+ return {
15
+ system: data.deviceInfo.system,
16
+ platform: data.deviceInfo.platform,
17
+ version: data.sdkInfo.jssdkVersion,
18
+ pixelRatio: data.deviceInfo.pixelRatio,
19
+ language: data.deviceInfo.lang,
20
+ brand: data.deviceInfo.brand,
21
+ appName: data.hostInfo?.appName,
22
+ SDKVersion: data.sdkInfo.jssdkVersion
23
+ };
24
+ }
25
+ });
26
+
27
+ const getEnv = createSyncAPI(API_ENV, {
28
+ implement: () => {
29
+ const res = invokeNative('envSync');
30
+
31
+ return res?.data ?? undefined;
32
+ }
33
+ });
34
+
35
+ commands.registerCommand('API.getSystemInfoSync', getSystemInfoSync);
36
+ commands.registerCommand('API.env', getEnv);
37
+
38
+ registerCanIUse('env', {
39
+ version: '1.0.0'
40
+ });
41
+
42
+ registerCanIUse('getSystemInfoSync', {
43
+ version: '1.0.0'
44
+ });
@@ -0,0 +1,8 @@
1
+ import './get-system-info';
2
+ import './lifecycle';
3
+ import './request';
4
+ import './storage';
5
+ import './keyboard';
6
+ import './task';
7
+ import './login';
8
+ import './ads';
@@ -0,0 +1,75 @@
1
+ import { createCommands } from '@jolibox/common';
2
+ import { invokeNative } from '../bootstrap/bridge';
3
+ import { createSyncAPI, registerCanIUse, t } from './base';
4
+
5
+ export const showKeyboard = createSyncAPI('showKeyboard', {
6
+ paramsSchema: t.tuple(
7
+ t.object({
8
+ defaultValue: t.string().optional().default(''),
9
+ multiple: t.boolean().optional().default(false),
10
+ maxLength: t.unknown().optional().default(100000)
11
+ })
12
+ ),
13
+ implement(params) {
14
+ const maxLength = Math.floor(Number(params.maxLength) ?? 100000);
15
+ let { defaultValue } = params;
16
+
17
+ if (defaultValue && maxLength) {
18
+ defaultValue = defaultValue.slice(0, maxLength);
19
+ }
20
+
21
+ invokeNative('showKeyboardSync', { ...params, defaultValue, maxLength });
22
+ }
23
+ });
24
+
25
+ registerCanIUse('keyboard.showKeyboard', {
26
+ version: '1.0.0',
27
+ properties: {
28
+ params: {
29
+ defaultValue: '1.0.0',
30
+ multiple: '1.0.0',
31
+ confirmHold: '1.0.0',
32
+ confirmType: '1.0.0',
33
+ maxLength: '1.0.0'
34
+ }
35
+ },
36
+ success: {
37
+ errMsg: '1.0.0'
38
+ }
39
+ });
40
+
41
+ export const updateKeyboard = createSyncAPI('updateKeyboard', {
42
+ paramsSchema: t.tuple(
43
+ t.object({
44
+ value: t.string()
45
+ })
46
+ ),
47
+ implement(params) {
48
+ invokeNative('updateKeyboardSync', params);
49
+ }
50
+ });
51
+
52
+ registerCanIUse('keyboard.updateKeyboard', {
53
+ version: '1.0.0',
54
+ properties: {
55
+ params: {
56
+ value: '1.0.0'
57
+ }
58
+ }
59
+ });
60
+
61
+ export const hideKeyboard = createSyncAPI('hideKeyboard', {
62
+ implement() {
63
+ invokeNative('hideKeyboardSync');
64
+ }
65
+ });
66
+
67
+ registerCanIUse('keyboard.hideKeyboard', {
68
+ version: '1.0.0'
69
+ });
70
+
71
+ const commands = createCommands();
72
+
73
+ commands.registerCommand('KeyboardSDK.showKeyboard', showKeyboard);
74
+ commands.registerCommand('KeyboardSDK.updateKeyboard', updateKeyboard);
75
+ commands.registerCommand('KeyboardSDK.hideKeyboard', hideKeyboard);