Skip to content

Commit 0b1bd1e

Browse files
authored
Merge pull request #2 from iamyoki/master
refactor: apply logger to console, fix #1
2 parents 976758a + e38e26c commit 0b1bd1e

File tree

17 files changed

+7776
-7928
lines changed

17 files changed

+7776
-7928
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const provider = new Provider({
5555
});
5656
```
5757

58+
> logger可以传入null来禁用日志打印,或传入自定义的日志工具,默认为`window.console`
59+
5860
### UMD
5961

6062
`anyweb-js-sdk``dist` 目录中进行引用 `umd` 版本.

dist/anyweb-js-sdk.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/anyweb-js-sdk.umd.min.js.map

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@idealight-labs/anyweb-js-sdk",
33
"description": "AnyWeb JavaScript Software Development Kit",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {

dist/src/interface/provider.d.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ import { ConsoleLike } from '../utils/types';
88
*/
99
export interface IRequestArguments {
1010
readonly method: string;
11-
readonly params?: any;
11+
readonly params?: unknown;
1212
readonly chainId?: number;
1313
}
1414
export interface IBaseProviderOptions {
1515
logger?: ConsoleLike;
1616
appId: string;
1717
}
18-
/**
19-
* Provider RPC Error
20-
*/
21-
export interface IProviderRpcError extends Error {
22-
message: string;
23-
code: number;
24-
data?: unknown;
25-
}
2618
/**
2719
* Base Provider
2820
*/

dist/src/provider.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ConsoleLike } from './utils/types';
1313
* const provider = new Provider()
1414
*/
1515
export declare class Provider implements IProvider {
16-
logger: ConsoleLike;
16+
logger?: ConsoleLike;
1717
readonly appId: string;
1818
private chainId;
1919
private static instance;

dist/src/provider.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ const address_1 = require("./utils/address");
2929
* const provider = new Provider()
3030
*/
3131
class Provider {
32-
constructor({ logger, appId }) {
32+
constructor({ logger = console, appId }) {
3333
this.chainId = 1;
3434
this.events = {};
3535
if (Provider.instance) {
3636
return Provider.instance;
3737
}
3838
Provider.instance = this;
39-
if (!logger) {
40-
logger = console;
41-
}
4239
this.logger = logger;
4340
this.appId = appId;
4441
// bind functions (to prevent consumers from making unbound calls)
@@ -52,6 +49,7 @@ class Provider {
5249
window.anyweb = this;
5350
}
5451
const messageHandler = (event) => {
52+
var _a;
5553
if (event.data &&
5654
(0, common_1.isObject)(event.data) &&
5755
'type' in event.data &&
@@ -60,25 +58,25 @@ class Provider {
6058
if (IframeData.type == 'event' &&
6159
IframeData.data == 'ready' &&
6260
IframeData.success) {
63-
console.debug('[AnyWeb] SDK初始化完成');
61+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('[AnyWeb] SDK初始化完成');
6462
Provider.ready = true;
6563
this.events.onReady && this.events.onReady();
6664
window.removeEventListener('message', messageHandler);
6765
}
6866
}
6967
};
7068
window.addEventListener('message', messageHandler);
71-
(0, common_1.createIframe)('pages/index/home')
69+
(0, common_1.createIframe)('pages/index/home', this.logger)
7270
.then()
73-
.catch((e) => console.error('[AnyWeb] createIframe error', e));
71+
.catch((e) => { var _a; return (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error('[AnyWeb] createIframe error', e); });
7472
}
7573
static getInstance(params) {
7674
if (!Provider.instance) {
7775
if (params) {
7876
Provider.instance = new Provider(params);
7977
}
8078
else {
81-
throw new Error('[AnyWeb] Provider is not initialized');
79+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.SDKNotReady, 'Provider is not initialized');
8280
}
8381
}
8482
return Provider.instance;
@@ -88,21 +86,23 @@ class Provider {
8886
* @param arg
8987
*/
9088
send(...arg) {
89+
var _a;
9190
return __awaiter(this, void 0, void 0, function* () {
92-
console.info('[AnyWeb] `send` is deprecated, use `request` instead');
91+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info('[AnyWeb] `send` is deprecated, use `request` instead');
9392
if (arg.length > 1) {
9493
return yield this.request({ method: arg[0], params: arg[1] });
9594
}
96-
throw new Error('Invalid arguments');
95+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.ParamsError, 'Invalid arguments');
9796
});
9897
}
9998
/**
10099
* Deprecated: use `request` instead
101100
* @param arg
102101
*/
103102
call(...arg) {
103+
var _a;
104104
return __awaiter(this, void 0, void 0, function* () {
105-
console.info('[AnyWeb] `call` is deprecated, use `request` instead', arg);
105+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info('[AnyWeb] `call` is deprecated, use `request` instead', arg);
106106
if (arg.length > 1) {
107107
return yield this.request({ method: arg[0], params: arg[1] });
108108
}
@@ -119,17 +119,18 @@ class Provider {
119119
* const result = await provider.request({ method: 'cfx_sendTransaction', params})
120120
*/
121121
request(args) {
122+
var _a, _b;
122123
return __awaiter(this, void 0, void 0, function* () {
123124
if (!args || typeof args !== 'object' || Array.isArray(args)) {
124-
throw new Error('Invalid request arguments');
125+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.ParamsError, 'Invalid request arguments');
125126
}
126127
const { method, params } = args;
127128
if (!method || method.trim().length === 0) {
128-
throw new Error('Method is required');
129+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.ParamsError, 'Invalid request arguments: Method is required');
129130
}
130-
console.debug(`[AnyWeb] request ${method} with`, params);
131+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`[AnyWeb] request ${method} with`, params);
131132
const result = yield this.rawRequest(method, params);
132-
console.debug(`[AnyWeb] request(${method}):`, result);
133+
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`[AnyWeb] request(${method}):`, result);
133134
return result;
134135
});
135136
}
@@ -149,16 +150,18 @@ class Provider {
149150
* @param params
150151
* @protected
151152
*/
153+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152154
rawRequest(method, params) {
155+
var _a, _b, _c, _d, _e, _f;
153156
return __awaiter(this, void 0, void 0, function* () {
154157
if (!Provider.ready) {
155-
throw new Error("[AnyWeb] Provider is not ready, please use on('ready', callback) to listen to ready event");
158+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.SDKNotReady, "Provider is not ready, please use on('ready', callback) to listen to ready event");
156159
}
157160
switch (method) {
158161
case 'cfx_requestAccounts':
159162
return this.rawRequest('cfx_accounts');
160163
case 'cfx_accounts':
161-
console.debug('[AnyWeb]', { params });
164+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('[AnyWeb]', { params });
162165
const scopes = params[0].scopes;
163166
let result;
164167
try {
@@ -170,10 +173,10 @@ class Provider {
170173
scopes: scopes,
171174
silence: true,
172175
}, this));
173-
console.debug('[AnyWeb]', 'silent auth result', result);
176+
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug('[AnyWeb]', 'silent auth result', result);
174177
}
175178
catch (e) {
176-
console.debug('[AnyWeb]', 'need to auth', e);
179+
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.debug('[AnyWeb]', 'need to auth', e);
177180
result = (yield (0, common_1.callIframe)('pages/dapp/auth', {
178181
appId: this.appId,
179182
params: params ? JSON.stringify(params[0]) : '',
@@ -208,7 +211,7 @@ class Provider {
208211
const to = payload.to;
209212
if (to) {
210213
authType =
211-
(0, address_1.getAddressType)(to) === address_1.AddressType.CONTRACT
214+
(0, address_1.getAddressType)(to, this.logger) === address_1.AddressType.CONTRACT
212215
? 'callContract'
213216
: 'createTransaction';
214217
}
@@ -229,8 +232,7 @@ class Provider {
229232
}, this);
230233
}
231234
catch (e) {
232-
console.error('[AnyWeb] Error to sendTransaction', e);
233-
return e;
235+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.SendTransactionError, 'Error to sendTransaction: ' + e);
234236
}
235237
case 'anyweb_importAccount':
236238
try {
@@ -242,8 +244,7 @@ class Provider {
242244
}, this);
243245
}
244246
catch (e) {
245-
console.error('[AnyWeb] Error to import Address', e);
246-
return e;
247+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.ImportAddressError, 'Error to import Address: ' + e);
247248
}
248249
case 'anyweb_version':
249250
return package_json_1.default.version;
@@ -272,10 +273,10 @@ class Provider {
272273
authType: 'check_identify',
273274
silence: true,
274275
}, this);
275-
console.debug('[AnyWeb]', 'Check identify result', identifyResult);
276+
(_d = this.logger) === null || _d === void 0 ? void 0 : _d.debug('[AnyWeb]', 'Check identify result', identifyResult);
276277
}
277278
catch (e) {
278-
console.debug('[AnyWeb]', 'need to identify', e);
279+
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.debug('[AnyWeb]', 'need to identify', e);
279280
identifyResult = yield (0, common_1.callIframe)('pages/user/identify', {
280281
appId: this.appId,
281282
chainId: this.chainId,
@@ -304,11 +305,11 @@ class Provider {
304305
}, this);
305306
}
306307
catch (e) {
307-
console.debug('[AnyWeb]', 'need to login', e);
308+
(_f = this.logger) === null || _f === void 0 ? void 0 : _f.debug('[AnyWeb]', 'need to login', e);
308309
return false;
309310
}
310311
default:
311-
return 'Unsupported method';
312+
throw new common_1.ProviderRpcError(common_1.ProviderErrorCode.UnsupportedMethod, 'Unsupported Method: ' + method);
312313
}
313314
});
314315
}
@@ -320,7 +321,8 @@ class Provider {
320321
* provider.on('connected', listener)
321322
*/
322323
on(type, listener) {
323-
console.debug('[AnyWeb] on', {
324+
var _a;
325+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug('[AnyWeb] on', {
324326
type,
325327
listener,
326328
});

dist/src/utils/address.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConsoleLike } from './types';
12
export declare enum AddressType {
23
USER = "user",
34
CONTRACT = "contract",
@@ -8,4 +9,4 @@ export declare enum AddressType {
89
* Get the type of an address.
910
* @param address
1011
*/
11-
export declare const getAddressType: (address: string) => AddressType;
12+
export declare const getAddressType: (address: string, logger?: ConsoleLike) => AddressType;

dist/src/utils/address.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ var AddressType;
1313
* Get the type of an address.
1414
* @param address
1515
*/
16-
const getAddressType = (address) => {
16+
const getAddressType = (address, logger = console) => {
1717
const decodeResult = (0, conflux_address_js_1.decode)(address);
18-
console.debug('[AnyWeb] decodeResult', decodeResult);
18+
logger.debug('[AnyWeb] decodeResult', decodeResult);
1919
if (Object.keys(decodeResult).includes('type')) {
2020
return decodeResult.type;
2121
}

0 commit comments

Comments
 (0)