@zimi/remote 0.1.0 → 0.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/README.md +35 -7
- package/dist/adaptors/http.d.ts +13 -13
- package/dist/adaptors/http.js +25 -25
- package/dist/adaptors/iframe.d.ts +4 -4
- package/dist/adaptors/iframe.js +37 -37
- package/dist/adaptors/iframe.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/remote.d.ts +79 -79
- package/dist/remote.js +196 -191
- package/dist/remote.js.map +1 -1
- package/dist/response.d.ts +104 -104
- package/dist/response.js +154 -154
- package/dist/response.js.map +1 -1
- package/package.json +5 -5
- package/src/remote.ts +4 -1
package/dist/response.js
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* name 和 message 都是 human readable 的字符串,
|
|
3
|
-
* 仅用于调试和展示,
|
|
4
|
-
* 程序逻辑判断请使用 code
|
|
5
|
-
*/
|
|
6
|
-
export class RemoteError extends Error {
|
|
7
|
-
/**
|
|
8
|
-
* name 和 message 都是 human readable 的字符串,
|
|
9
|
-
* 仅用于调试和展示,
|
|
10
|
-
* 程序逻辑判断请使用 code
|
|
11
|
-
*/
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message);
|
|
14
|
-
/**
|
|
15
|
-
* code > 0 才是合法的 Error;
|
|
16
|
-
* name 和 message 都是 human readable 的字符串,
|
|
17
|
-
* 仅用于调试和展示,
|
|
18
|
-
* 程序逻辑判断请使用 code
|
|
19
|
-
*/
|
|
20
|
-
Object.defineProperty(this, "code", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: 1
|
|
25
|
-
});
|
|
26
|
-
this.name = 'RemoteError';
|
|
27
|
-
}
|
|
28
|
-
toJson() {
|
|
29
|
-
return {
|
|
30
|
-
code: this.code,
|
|
31
|
-
name: this.name,
|
|
32
|
-
message: this.message,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
toString() {
|
|
36
|
-
return JSON.stringify(this.toJson());
|
|
37
|
-
}
|
|
38
|
-
valueOf() {
|
|
39
|
-
return `${this.name} [${this.code}]: ${this.message}`;
|
|
40
|
-
}
|
|
41
|
-
static fromError(err) {
|
|
42
|
-
if (typeof err === 'string') {
|
|
43
|
-
return new RemoteError(err);
|
|
44
|
-
}
|
|
45
|
-
if (!err) {
|
|
46
|
-
return new RemoteError('Unknown error');
|
|
47
|
-
}
|
|
48
|
-
const json = err;
|
|
49
|
-
const error = new RemoteError(typeof json.message === 'string' ? json.message : 'Unknown error');
|
|
50
|
-
if (typeof json.code === 'number' && json.code > 0) {
|
|
51
|
-
error.code = json.code;
|
|
52
|
-
}
|
|
53
|
-
if (json.name && typeof json.name === 'string') {
|
|
54
|
-
error.name = json.name;
|
|
55
|
-
}
|
|
56
|
-
return error;
|
|
57
|
-
}
|
|
58
|
-
static isRemoteError(data) {
|
|
59
|
-
const json = data;
|
|
60
|
-
return (!!json &&
|
|
61
|
-
typeof json === 'object' &&
|
|
62
|
-
typeof json.code === 'number' &&
|
|
63
|
-
json.code > 0 &&
|
|
64
|
-
typeof json.name === 'string' &&
|
|
65
|
-
typeof json.message === 'string');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* name 和 message 都是 human readable 的字符串,
|
|
70
|
-
* 仅用于调试和展示,
|
|
71
|
-
* 程序逻辑判断请使用 code
|
|
72
|
-
*/
|
|
73
|
-
export class RemoteTimeoutError extends RemoteError {
|
|
74
|
-
/**
|
|
75
|
-
* name 和 message 都是 human readable 的字符串,
|
|
76
|
-
* 仅用于调试和展示,
|
|
77
|
-
* 程序逻辑判断请使用 code
|
|
78
|
-
*/
|
|
79
|
-
constructor(message) {
|
|
80
|
-
super(message);
|
|
81
|
-
/**
|
|
82
|
-
* name 和 message 都是 human readable 的字符串,
|
|
83
|
-
* 仅用于调试和展示,
|
|
84
|
-
* 程序逻辑判断请使用 code
|
|
85
|
-
*/
|
|
86
|
-
Object.defineProperty(this, "code", {
|
|
87
|
-
enumerable: true,
|
|
88
|
-
configurable: true,
|
|
89
|
-
writable: true,
|
|
90
|
-
value: 2
|
|
91
|
-
});
|
|
92
|
-
this.name = 'RemoteTimeoutError';
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* name 和 message 都是 human readable 的字符串,
|
|
97
|
-
* 仅用于调试和展示,
|
|
98
|
-
* 程序逻辑判断请使用 code
|
|
99
|
-
*/
|
|
100
|
-
Object.defineProperty(RemoteTimeoutError, "code", {
|
|
101
|
-
enumerable: true,
|
|
102
|
-
configurable: true,
|
|
103
|
-
writable: true,
|
|
104
|
-
value: 2
|
|
105
|
-
});
|
|
106
|
-
/**
|
|
107
|
-
* name 和 message 都是 human readable 的字符串,
|
|
108
|
-
* 仅用于调试和展示,
|
|
109
|
-
* 程序逻辑判断请使用 code
|
|
110
|
-
*/
|
|
111
|
-
export class RemoteNotFoundError extends RemoteError {
|
|
112
|
-
/**
|
|
113
|
-
* name 和 message 都是 human readable 的字符串,
|
|
114
|
-
* 仅用于调试和展示,
|
|
115
|
-
* 程序逻辑判断请使用 code
|
|
116
|
-
*/
|
|
117
|
-
constructor(message) {
|
|
118
|
-
super(message);
|
|
119
|
-
/**
|
|
120
|
-
* name 和 message 都是 human readable 的字符串,
|
|
121
|
-
* 仅用于调试和展示,
|
|
122
|
-
* 程序逻辑判断请使用 code
|
|
123
|
-
*/
|
|
124
|
-
Object.defineProperty(this, "code", {
|
|
125
|
-
enumerable: true,
|
|
126
|
-
configurable: true,
|
|
127
|
-
writable: true,
|
|
128
|
-
value: 3
|
|
129
|
-
});
|
|
130
|
-
this.name = 'RemoteNotFoundError';
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* name 和 message 都是 human readable 的字符串,
|
|
135
|
-
* 仅用于调试和展示,
|
|
136
|
-
* 程序逻辑判断请使用 code
|
|
137
|
-
*/
|
|
138
|
-
Object.defineProperty(RemoteNotFoundError, "code", {
|
|
139
|
-
enumerable: true,
|
|
140
|
-
configurable: true,
|
|
141
|
-
writable: true,
|
|
142
|
-
value: 3
|
|
143
|
-
});
|
|
144
|
-
export const response = {
|
|
145
|
-
success(data) {
|
|
146
|
-
return {
|
|
147
|
-
code: 0,
|
|
148
|
-
data,
|
|
149
|
-
};
|
|
150
|
-
},
|
|
151
|
-
error(error) {
|
|
152
|
-
return error.toJson();
|
|
153
|
-
},
|
|
154
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* name 和 message 都是 human readable 的字符串,
|
|
3
|
+
* 仅用于调试和展示,
|
|
4
|
+
* 程序逻辑判断请使用 code
|
|
5
|
+
*/
|
|
6
|
+
export class RemoteError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* name 和 message 都是 human readable 的字符串,
|
|
9
|
+
* 仅用于调试和展示,
|
|
10
|
+
* 程序逻辑判断请使用 code
|
|
11
|
+
*/
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
/**
|
|
15
|
+
* code > 0 才是合法的 Error;
|
|
16
|
+
* name 和 message 都是 human readable 的字符串,
|
|
17
|
+
* 仅用于调试和展示,
|
|
18
|
+
* 程序逻辑判断请使用 code
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(this, "code", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: 1
|
|
25
|
+
});
|
|
26
|
+
this.name = 'RemoteError';
|
|
27
|
+
}
|
|
28
|
+
toJson() {
|
|
29
|
+
return {
|
|
30
|
+
code: this.code,
|
|
31
|
+
name: this.name,
|
|
32
|
+
message: this.message,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
toString() {
|
|
36
|
+
return JSON.stringify(this.toJson());
|
|
37
|
+
}
|
|
38
|
+
valueOf() {
|
|
39
|
+
return `${this.name} [${this.code}]: ${this.message}`;
|
|
40
|
+
}
|
|
41
|
+
static fromError(err) {
|
|
42
|
+
if (typeof err === 'string') {
|
|
43
|
+
return new RemoteError(err);
|
|
44
|
+
}
|
|
45
|
+
if (!err) {
|
|
46
|
+
return new RemoteError('Unknown error');
|
|
47
|
+
}
|
|
48
|
+
const json = err;
|
|
49
|
+
const error = new RemoteError(typeof json.message === 'string' ? json.message : 'Unknown error');
|
|
50
|
+
if (typeof json.code === 'number' && json.code > 0) {
|
|
51
|
+
error.code = json.code;
|
|
52
|
+
}
|
|
53
|
+
if (json.name && typeof json.name === 'string') {
|
|
54
|
+
error.name = json.name;
|
|
55
|
+
}
|
|
56
|
+
return error;
|
|
57
|
+
}
|
|
58
|
+
static isRemoteError(data) {
|
|
59
|
+
const json = data;
|
|
60
|
+
return (!!json &&
|
|
61
|
+
typeof json === 'object' &&
|
|
62
|
+
typeof json.code === 'number' &&
|
|
63
|
+
json.code > 0 &&
|
|
64
|
+
typeof json.name === 'string' &&
|
|
65
|
+
typeof json.message === 'string');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* name 和 message 都是 human readable 的字符串,
|
|
70
|
+
* 仅用于调试和展示,
|
|
71
|
+
* 程序逻辑判断请使用 code
|
|
72
|
+
*/
|
|
73
|
+
export class RemoteTimeoutError extends RemoteError {
|
|
74
|
+
/**
|
|
75
|
+
* name 和 message 都是 human readable 的字符串,
|
|
76
|
+
* 仅用于调试和展示,
|
|
77
|
+
* 程序逻辑判断请使用 code
|
|
78
|
+
*/
|
|
79
|
+
constructor(message) {
|
|
80
|
+
super(message);
|
|
81
|
+
/**
|
|
82
|
+
* name 和 message 都是 human readable 的字符串,
|
|
83
|
+
* 仅用于调试和展示,
|
|
84
|
+
* 程序逻辑判断请使用 code
|
|
85
|
+
*/
|
|
86
|
+
Object.defineProperty(this, "code", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: 2
|
|
91
|
+
});
|
|
92
|
+
this.name = 'RemoteTimeoutError';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* name 和 message 都是 human readable 的字符串,
|
|
97
|
+
* 仅用于调试和展示,
|
|
98
|
+
* 程序逻辑判断请使用 code
|
|
99
|
+
*/
|
|
100
|
+
Object.defineProperty(RemoteTimeoutError, "code", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: 2
|
|
105
|
+
});
|
|
106
|
+
/**
|
|
107
|
+
* name 和 message 都是 human readable 的字符串,
|
|
108
|
+
* 仅用于调试和展示,
|
|
109
|
+
* 程序逻辑判断请使用 code
|
|
110
|
+
*/
|
|
111
|
+
export class RemoteNotFoundError extends RemoteError {
|
|
112
|
+
/**
|
|
113
|
+
* name 和 message 都是 human readable 的字符串,
|
|
114
|
+
* 仅用于调试和展示,
|
|
115
|
+
* 程序逻辑判断请使用 code
|
|
116
|
+
*/
|
|
117
|
+
constructor(message) {
|
|
118
|
+
super(message);
|
|
119
|
+
/**
|
|
120
|
+
* name 和 message 都是 human readable 的字符串,
|
|
121
|
+
* 仅用于调试和展示,
|
|
122
|
+
* 程序逻辑判断请使用 code
|
|
123
|
+
*/
|
|
124
|
+
Object.defineProperty(this, "code", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
configurable: true,
|
|
127
|
+
writable: true,
|
|
128
|
+
value: 3
|
|
129
|
+
});
|
|
130
|
+
this.name = 'RemoteNotFoundError';
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* name 和 message 都是 human readable 的字符串,
|
|
135
|
+
* 仅用于调试和展示,
|
|
136
|
+
* 程序逻辑判断请使用 code
|
|
137
|
+
*/
|
|
138
|
+
Object.defineProperty(RemoteNotFoundError, "code", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: true,
|
|
141
|
+
writable: true,
|
|
142
|
+
value: 3
|
|
143
|
+
});
|
|
144
|
+
export const response = {
|
|
145
|
+
success(data) {
|
|
146
|
+
return {
|
|
147
|
+
code: 0,
|
|
148
|
+
data,
|
|
149
|
+
};
|
|
150
|
+
},
|
|
151
|
+
error(error) {
|
|
152
|
+
return error.toJson();
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
155
|
//# sourceMappingURL=response.js.map
|
package/dist/response.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAuBA;;;;GAIG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IASpC;;;;OAIG;IACH,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QAdhB;;;;;WAKG;QACH;;;;mBAAO,CAAC;WAAA;QASN,IAAI,CAAC,IAAI,GAAG,aAAa,CAAA;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAA;IACH,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAA;IACtC,CAAC;IAED,OAAO;QACL,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;IACvD,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,GAAY;QAC3B,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAuBA;;;;GAIG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IASpC;;;;OAIG;IACH,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QAdhB;;;;;WAKG;QACH;;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"version": "0.2.0",
|
|
5
5
|
"author": "xiaomingTang",
|
|
6
6
|
"description": "call remote functions as local",
|
|
7
7
|
"private": false,
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
"src",
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"eventemitter3": "^5.0.1"
|
|
19
|
+
},
|
|
17
20
|
"scripts": {
|
|
18
21
|
"build": "tsc",
|
|
19
22
|
"build:watch": "tsc --watch"
|
|
20
|
-
},
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"eventemitter3": "^5.0.1"
|
|
23
23
|
}
|
|
24
|
-
}
|
|
24
|
+
}
|
package/src/remote.ts
CHANGED
|
@@ -239,7 +239,10 @@ export class Remote<
|
|
|
239
239
|
})
|
|
240
240
|
|
|
241
241
|
self = new Proxy<MF>({} as MF, {
|
|
242
|
-
get: (_, k) =>
|
|
242
|
+
get: (_, k) => (data: unknown) =>
|
|
243
|
+
this.map[k as string]?.callback(data, {
|
|
244
|
+
deviceId: this.deviceId,
|
|
245
|
+
}),
|
|
243
246
|
})
|
|
244
247
|
}
|
|
245
248
|
|