Skip to content

Commit

Permalink
feat: use fetch in chrome v3
Browse files Browse the repository at this point in the history
  • Loading branch information
veryhappyok committed May 10, 2024
1 parent 5aaed60 commit 136ba0a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ontology-ts-sdk",
"version": "2.0.2",
"version": "2.0.3",
"description": "Comprehensive TypeScript library for the Ontology blockchain.",
"main": "./lib/index.js",
"types": "./lib/types/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/network/rest/restClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import fetchAdapter from "@vespaiach/axios-fetch-adapter";
import { TEST_ONT_URL } from '../../consts';
import { Address } from '../../crypto/address';
import UrlConsts from './urlConsts';
import { needUseFetch } from '../../utils';

/**
* Wrapper class for restful api.
Expand All @@ -46,7 +47,8 @@ export default class RestClient {

constructor(url ?: string, useFetch?: boolean) {
this.url = url || TEST_ONT_URL.REST_URL;
this.config = useFetch ? { adapter: fetchAdapter } : undefined;
const isUseFetch = useFetch || needUseFetch();
this.config = isUseFetch ? { adapter: fetchAdapter } : undefined;
if (this.url[this.url.length - 1] === '/') {
this.url = this.url.substring(0, this.url.length - 1);
}
Expand Down
4 changes: 3 additions & 1 deletion src/network/rpc/rpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import fetchAdapter from "@vespaiach/axios-fetch-adapter";
import { TEST_ONT_URL } from '../../consts';
import { Address } from '../../crypto/address';
import { ERROR_CODE } from '../../error';
import { needUseFetch } from '../../utils';

/**
* Wrapper class for RPC apis.
Expand All @@ -36,7 +37,8 @@ export default class RpcClient {
constructor( url ?: string, useFetch?: boolean ) {
this.url = url || TEST_ONT_URL.RPC_URL;

this.config = useFetch ? { adapter: fetchAdapter } : undefined;
const isUseFetch = useFetch || needUseFetch();
this.config = isUseFetch ? { adapter: fetchAdapter } : undefined;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,24 @@ export function calcUnboundOng(balance: number, startOffset: number, endOffset:
}
return amount * balance;
}

// 判断是否需要使用fetch方法请求数据,在Chrome V3中只能使用fetch
export function needUseFetch() {
let useFetch = false;
try {
// 不能使用XMLHttpRequest
// @ts-ignore
const tt = new XMLHttpRequest();
} catch (e) {
try {
// 且可以使用fetch
// @ts-ignore
const tmp = fetch;
useFetch = true;
} catch (err) {
//
}
}

return useFetch;
}

0 comments on commit 136ba0a

Please sign in to comment.