@onekeyfe/hd-transport-http 1.1.12 → 1.1.14-alpha.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.
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AA4BF,wBAAsB,OAAO,CAAC,OAAO,EAAE,kBAAkB,gBA6BxD"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AA4BF,wBAAsB,OAAO,CAAC,OAAO,EAAE,kBAAkB,gBA+CxD"}
package/dist/index.js CHANGED
@@ -66,7 +66,7 @@ function parseResult(text) {
66
66
  }
67
67
  }
68
68
  function request(options) {
69
- var _a;
69
+ var _a, _b;
70
70
  return __awaiter(this, void 0, void 0, function* () {
71
71
  const fetchOptions = {
72
72
  url: options.url,
@@ -79,31 +79,47 @@ function request(options) {
79
79
  timeout: (_a = options.timeout) !== null && _a !== void 0 ? _a : undefined,
80
80
  transformResponse: data => data,
81
81
  };
82
- const res = yield axios__default["default"].request(fetchOptions);
83
- if (+res.status === 200) {
84
- return parseResult(res.data);
82
+ try {
83
+ const res = yield axios__default["default"].request(fetchOptions);
84
+ if (+res.status === 200) {
85
+ return parseResult(res.data);
86
+ }
87
+ const resJson = parseResult(res.data);
88
+ if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
89
+ throw new hdShared.HardwareError({
90
+ errorCode: hdShared.HardwareErrorCode.NetworkError,
91
+ message: resJson.error,
92
+ });
93
+ }
94
+ else {
95
+ throw new hdShared.HardwareError({ errorCode: hdShared.HardwareErrorCode.NetworkError, message: res.data });
96
+ }
85
97
  }
86
- const resJson = parseResult(res.data);
87
- if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
98
+ catch (err) {
99
+ const axiosErr = err;
100
+ const respData = (_b = axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.response) === null || _b === void 0 ? void 0 : _b.data;
101
+ if (typeof respData === 'string') {
102
+ const parsed = parseResult(respData);
103
+ if (typeof parsed === 'object' && parsed !== null && parsed.error) {
104
+ throw hdShared.CreateHardwareErrorByBridgeError(String(parsed.error));
105
+ }
106
+ throw hdShared.CreateHardwareErrorByBridgeError(respData);
107
+ }
88
108
  throw new hdShared.HardwareError({
89
- errorCode: hdShared.HardwareErrorCode.NetworkError,
90
- message: resJson.error,
109
+ errorCode: hdShared.HardwareErrorCode.BridgeNetworkError,
110
+ message: (axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.message) || 'Bridge network error',
91
111
  });
92
112
  }
93
- else {
94
- throw new hdShared.HardwareError({ errorCode: hdShared.HardwareErrorCode.NetworkError, message: res.data });
95
- }
96
113
  });
97
114
  }
98
- axios__default["default"].interceptors.request.use(config => {
99
- var _a, _b;
115
+ axios__default["default"].interceptors.request.use((config) => {
116
+ var _a;
100
117
  if (typeof window !== 'undefined') {
101
118
  return config;
102
119
  }
103
120
  if ((_a = config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http://localhost:21320')) {
104
- if (!((_b = config === null || config === void 0 ? void 0 : config.headers) === null || _b === void 0 ? void 0 : _b.Origin)) {
105
- console.log('set node request origin');
106
- config.headers = Object.assign(Object.assign({}, config.headers), { Origin: 'https://jssdk.onekey.so' });
121
+ if (!config.headers.get('Origin')) {
122
+ config.headers.set('Origin', 'https://jssdk.onekey.so');
107
123
  }
108
124
  }
109
125
  return config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-http",
3
- "version": "1.1.12",
3
+ "version": "1.1.14-alpha.0",
4
4
  "description": "hardware http transport",
5
5
  "author": "OneKey",
6
6
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
@@ -24,10 +24,10 @@
24
24
  "url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
25
25
  },
26
26
  "dependencies": {
27
- "@onekeyfe/hd-shared": "1.1.12",
28
- "@onekeyfe/hd-transport": "1.1.12",
29
- "axios": "^0.27.2",
27
+ "@onekeyfe/hd-shared": "1.1.14-alpha.0",
28
+ "@onekeyfe/hd-transport": "1.1.14-alpha.0",
29
+ "axios": "1.12.2",
30
30
  "secure-json-parse": "^4.0.0"
31
31
  },
32
- "gitHead": "bd87ededa77c034409a557682dbc78014fcfa803"
32
+ "gitHead": "cde48372296ce37cba3ed627f3fcfd65071d22f2"
33
33
  }
package/src/http.ts CHANGED
@@ -1,5 +1,9 @@
1
- import axios, { AxiosRequestConfig } from 'axios';
2
- import { HardwareError, HardwareErrorCode } from '@onekeyfe/hd-shared';
1
+ import axios, { AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
2
+ import {
3
+ HardwareError,
4
+ HardwareErrorCode,
5
+ CreateHardwareErrorByBridgeError,
6
+ } from '@onekeyfe/hd-shared';
3
7
  import secureJSON from 'secure-json-parse';
4
8
 
5
9
  export type HttpRequestOptions = {
@@ -50,35 +54,49 @@ export async function request(options: HttpRequestOptions) {
50
54
  transformResponse: data => data,
51
55
  };
52
56
 
53
- const res = await axios.request(fetchOptions);
57
+ try {
58
+ const res = await axios.request(fetchOptions);
59
+
60
+ if (+res.status === 200) {
61
+ return parseResult(res.data);
62
+ }
63
+ const resJson = parseResult(res.data);
64
+ if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
65
+ throw new HardwareError({
66
+ errorCode: HardwareErrorCode.NetworkError,
67
+ message: resJson.error,
68
+ });
69
+ } else {
70
+ throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: res.data });
71
+ }
72
+ } catch (err) {
73
+ const axiosErr = err as AxiosError<string>;
74
+ const respData = axiosErr?.response?.data;
75
+
76
+ if (typeof respData === 'string') {
77
+ const parsed = parseResult(respData);
78
+ if (typeof parsed === 'object' && parsed !== null && parsed.error) {
79
+ throw CreateHardwareErrorByBridgeError(String(parsed.error));
80
+ }
81
+ throw CreateHardwareErrorByBridgeError(respData);
82
+ }
54
83
 
55
- if (+res.status === 200) {
56
- return parseResult(res.data);
57
- }
58
- const resJson = parseResult(res.data);
59
- if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
60
84
  throw new HardwareError({
61
- errorCode: HardwareErrorCode.NetworkError,
62
- message: resJson.error,
85
+ errorCode: HardwareErrorCode.BridgeNetworkError,
86
+ message: axiosErr?.message || 'Bridge network error',
63
87
  });
64
- } else {
65
- throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: res.data });
66
88
  }
67
89
  }
68
90
 
69
- axios.interceptors.request.use(config => {
91
+ axios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
70
92
  if (typeof window !== 'undefined') {
71
93
  return config;
72
94
  }
73
95
  // node environment
74
96
  if (config.url?.startsWith('http://localhost:21320')) {
75
- if (!config?.headers?.Origin) {
76
- console.log('set node request origin');
97
+ if (!config.headers.get('Origin')) {
77
98
  // add Origin field for request headers
78
- config.headers = {
79
- ...config.headers,
80
- Origin: 'https://jssdk.onekey.so',
81
- };
99
+ config.headers.set('Origin', 'https://jssdk.onekey.so');
82
100
  }
83
101
  }
84
102
  return config;