mezon-js 2.13.78 → 2.13.80
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/client.ts +3279 -2482
- package/dist/client.d.ts +163 -349
- package/dist/gateway.api.d.ts +33 -0
- package/dist/index.d.ts +2 -4
- package/dist/{api.gen.d.ts → mezon-js/api.gen.d.ts} +1 -0
- package/dist/mezon-js/client.d.ts +347 -0
- package/dist/mezon-js/gateway.api.d.ts +33 -0
- package/dist/mezon-js/index.d.ts +23 -0
- package/dist/mezon-js/session.d.ts +61 -0
- package/dist/mezon-js/socket.d.ts +1212 -0
- package/dist/mezon-js/types/index.d.ts +2212 -0
- package/dist/mezon-js/utils.d.ts +6 -0
- package/dist/mezon-js/web_socket_adapter.d.ts +83 -0
- package/dist/mezon-js.cjs.js +9038 -21145
- package/dist/mezon-js.esm.mjs +9038 -21145
- package/dist/mezon-js.esm.mjs.map +1 -0
- package/dist/session.d.ts +11 -11
- package/dist/socket.d.ts +385 -386
- package/dist/types/index.d.ts +2212 -0
- package/dist/utils.d.ts +2 -0
- package/dist/webrpc/index.d.ts +3 -0
- package/gateway.api.ts +577 -0
- package/index.ts +2 -5
- package/package.json +3 -2
- package/rollup.config.js +64 -30
- package/session.ts +22 -22
- package/socket.ts +921 -846
- package/types/index.ts +3883 -0
- package/utils.ts +107 -47
- package/api/api.ts +0 -39185
- package/api.gen.ts +0 -11777
- package/google/protobuf/struct.ts +0 -554
- package/google/protobuf/timestamp.ts +0 -223
- package/google/protobuf/wrappers.ts +0 -670
- /package/dist/{api → mezon-js/api}/api.d.ts +0 -0
- /package/dist/{google → mezon-js/google}/protobuf/struct.d.ts +0 -0
- /package/dist/{google → mezon-js/google}/protobuf/timestamp.d.ts +0 -0
- /package/dist/{google → mezon-js/google}/protobuf/wrappers.d.ts +0 -0
package/utils.ts
CHANGED
|
@@ -1,65 +1,125 @@
|
|
|
1
|
-
import {encode, decode} from "js-base64"
|
|
1
|
+
import { encode, decode } from "js-base64";
|
|
2
2
|
|
|
3
|
-
export function buildFetchOptions(
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export function buildFetchOptions(
|
|
4
|
+
method: string,
|
|
5
|
+
options: any,
|
|
6
|
+
bodyJson: string
|
|
7
|
+
) {
|
|
8
|
+
const fetchOptions = { ...{ method: method }, ...options };
|
|
9
|
+
fetchOptions.headers = { ...options.headers };
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
if (typeof XMLHttpRequest !== "undefined") {
|
|
12
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
13
|
+
XMLHttpRequest.prototype,
|
|
14
|
+
"withCredentials"
|
|
15
|
+
);
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
17
|
+
// in Cocos Creator, XMLHttpRequest.withCredentials is not writable, so make the fetch
|
|
18
|
+
// polyfill avoid writing to it.
|
|
19
|
+
if (!descriptor?.set) {
|
|
20
|
+
fetchOptions.credentials = "cocos-ignore"; // string value is arbitrary, cannot be 'omit' or 'include
|
|
15
21
|
}
|
|
22
|
+
}
|
|
16
23
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
if (!Object.keys(fetchOptions.headers).includes("Accept")) {
|
|
25
|
+
fetchOptions.headers["Accept"] = "application/json";
|
|
26
|
+
}
|
|
20
27
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
if (!Object.keys(fetchOptions.headers).includes("Content-Type")) {
|
|
29
|
+
fetchOptions.headers["Content-Type"] = "application/json";
|
|
30
|
+
}
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
if (bodyJson) {
|
|
32
|
-
fetchOptions.body = bodyJson;
|
|
32
|
+
Object.keys(fetchOptions.headers).forEach((key: string) => {
|
|
33
|
+
if (!fetchOptions.headers[key]) {
|
|
34
|
+
delete fetchOptions.headers[key];
|
|
33
35
|
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (bodyJson) {
|
|
39
|
+
fetchOptions.body = bodyJson;
|
|
40
|
+
}
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
return fetchOptions;
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
export function b64EncodeUnicode(str:string) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
export function b64EncodeUnicode(str: string) {
|
|
46
|
+
return encode(
|
|
47
|
+
encodeURIComponent(str).replace(
|
|
48
|
+
/%([0-9A-F]{2})/g,
|
|
49
|
+
function toSolidBytes(_match: string, p1) {
|
|
50
|
+
return String.fromCharCode(Number("0x" + p1));
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
);
|
|
43
54
|
}
|
|
44
55
|
|
|
45
56
|
export function b64DecodeUnicode(str: string) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
return decodeURIComponent(
|
|
58
|
+
decode(str)
|
|
59
|
+
.split("")
|
|
60
|
+
.map(function (c) {
|
|
61
|
+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
62
|
+
})
|
|
63
|
+
.join("")
|
|
64
|
+
);
|
|
49
65
|
}
|
|
50
66
|
|
|
51
67
|
export function safeJSONParse(jsonStr: string) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(jsonStr);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (jsonStr !== "") {
|
|
72
|
+
const fixedJsonStr = jsonStr.replace(/\n/g, "\\n");
|
|
73
|
+
try {
|
|
74
|
+
return JSON.parse(fixedJsonStr);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.error("Error parsing JSON:", jsonStr, error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { t: jsonStr }; // Handle the error gracefully or throw an exception if necessary
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function mapToSnakeCase(obj: any): any {
|
|
84
|
+
if (typeof obj !== "object" || obj === null) {
|
|
85
|
+
return obj;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (Array.isArray(obj)) {
|
|
89
|
+
return obj.map(mapToSnakeCase);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const newObj: { [key: string]: any } = {};
|
|
93
|
+
for (const key in obj) {
|
|
94
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
95
|
+
const newKey = camelToSnakeCase(key);
|
|
96
|
+
newObj[newKey] = mapToSnakeCase(obj[key]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return newObj;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function mapToCamelCase(obj: any): any {
|
|
103
|
+
if (typeof obj !== "object" || obj === null) {
|
|
104
|
+
return obj;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (Array.isArray(obj)) {
|
|
108
|
+
return obj.map(mapToCamelCase);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const newObj: { [key: string]: any } = {};
|
|
112
|
+
for (const key in obj) {
|
|
113
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
114
|
+
const newKey = snakeToCamelCase(key);
|
|
115
|
+
newObj[newKey] = mapToCamelCase(obj[key]);
|
|
64
116
|
}
|
|
65
|
-
}
|
|
117
|
+
}
|
|
118
|
+
return newObj;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const camelToSnakeCase = (str: string): string =>
|
|
122
|
+
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
123
|
+
|
|
124
|
+
const snakeToCamelCase = (str: string): string =>
|
|
125
|
+
str.replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
|