tools-min-ns 1.5.2

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.
Files changed (162) hide show
  1. package/README.md +85 -0
  2. package/esm/array/ArrayUtil.d.ts +109 -0
  3. package/esm/array/ArrayUtil.js +242 -0
  4. package/esm/array/TreeUtil.d.ts +151 -0
  5. package/esm/array/TreeUtil.js +412 -0
  6. package/esm/base/BaseUtil.d.ts +91 -0
  7. package/esm/base/BaseUtil.js +148 -0
  8. package/esm/browser/BrowserUtil.d.ts +57 -0
  9. package/esm/browser/BrowserUtil.js +337 -0
  10. package/esm/browser/fingerprint.d.ts +120 -0
  11. package/esm/browser/fingerprint.js +1469 -0
  12. package/esm/check/CheckUtil.d.ts +110 -0
  13. package/esm/check/CheckUtil.js +465 -0
  14. package/esm/common.d.ts +43 -0
  15. package/esm/common.js +44 -0
  16. package/esm/config.d.ts +7 -0
  17. package/esm/config.js +7 -0
  18. package/esm/cookie/CookieUtil.d.ts +56 -0
  19. package/esm/cookie/CookieUtil.js +12 -0
  20. package/esm/cookie/cookie.d.ts +2 -0
  21. package/esm/cookie/cookie.js +104 -0
  22. package/esm/date/DateUtil.d.ts +261 -0
  23. package/esm/date/DateUtil.js +763 -0
  24. package/esm/env/EnvUtil.d.ts +27 -0
  25. package/esm/env/EnvUtil.js +29 -0
  26. package/esm/env/env.d.ts +41 -0
  27. package/esm/env/env.js +106 -0
  28. package/esm/func/FunctionUtil.d.ts +77 -0
  29. package/esm/func/FunctionUtil.js +215 -0
  30. package/esm/index.d.ts +19 -0
  31. package/esm/index.js +19 -0
  32. package/esm/number/NumberUtil.d.ts +96 -0
  33. package/esm/number/NumberUtil.js +256 -0
  34. package/esm/object/ObjectUtil.d.ts +41 -0
  35. package/esm/object/ObjectUtil.js +158 -0
  36. package/esm/pwd/PasswordUtil.d.ts +33 -0
  37. package/esm/pwd/PasswordUtil.js +68 -0
  38. package/esm/pwd/crypto.d.ts +6 -0
  39. package/esm/pwd/crypto.js +1824 -0
  40. package/esm/request/RequestUtil.d.ts +52 -0
  41. package/esm/request/RequestUtil.js +53 -0
  42. package/esm/request/jsonp.d.ts +18 -0
  43. package/esm/request/jsonp.js +73 -0
  44. package/esm/request/mgop/env.d.ts +7 -0
  45. package/esm/request/mgop/env.js +15 -0
  46. package/esm/request/mgop/index.d.ts +3 -0
  47. package/esm/request/mgop/index.js +276 -0
  48. package/esm/request/mgop/responseCode.d.ts +20 -0
  49. package/esm/request/mgop/responseCode.js +37 -0
  50. package/esm/request/mgop/type.d.ts +34 -0
  51. package/esm/request/mgop/type.js +1 -0
  52. package/esm/request/uniRquest/index.d.ts +2 -0
  53. package/esm/request/uniRquest/index.js +66 -0
  54. package/esm/request/uniRquest/miniapp/index.d.ts +2 -0
  55. package/esm/request/uniRquest/miniapp/index.js +29 -0
  56. package/esm/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
  57. package/esm/request/uniRquest/miniapp-bytedance/index.js +41 -0
  58. package/esm/request/uniRquest/types.d.ts +65 -0
  59. package/esm/request/uniRquest/types.js +36 -0
  60. package/esm/request/uniRquest/utils.d.ts +9 -0
  61. package/esm/request/uniRquest/utils.js +69 -0
  62. package/esm/request/uniRquest/web/index.d.ts +2 -0
  63. package/esm/request/uniRquest/web/index.js +85 -0
  64. package/esm/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
  65. package/esm/request/uniRquest/wechat-miniprogram/index.js +41 -0
  66. package/esm/request/uniRquest/weex/index.d.ts +14 -0
  67. package/esm/request/uniRquest/weex/index.js +59 -0
  68. package/esm/storage/StorageUtil.d.ts +19 -0
  69. package/esm/storage/StorageUtil.js +75 -0
  70. package/esm/string/StringUtil.d.ts +392 -0
  71. package/esm/string/StringUtil.js +556 -0
  72. package/esm/system/SystemUtil.d.ts +46 -0
  73. package/esm/system/SystemUtil.js +122 -0
  74. package/esm/transfer/MapTransferUtil.d.ts +54 -0
  75. package/esm/transfer/MapTransferUtil.js +160 -0
  76. package/esm/transfer/TransferUtil.d.ts +98 -0
  77. package/esm/transfer/TransferUtil.js +323 -0
  78. package/esm/types.d.ts +25 -0
  79. package/esm/types.js +1 -0
  80. package/esm/url/UrlUtil.d.ts +30 -0
  81. package/esm/url/UrlUtil.js +85 -0
  82. package/lib/array/ArrayUtil.d.ts +109 -0
  83. package/lib/array/ArrayUtil.js +252 -0
  84. package/lib/array/TreeUtil.d.ts +151 -0
  85. package/lib/array/TreeUtil.js +422 -0
  86. package/lib/base/BaseUtil.d.ts +91 -0
  87. package/lib/base/BaseUtil.js +153 -0
  88. package/lib/browser/BrowserUtil.d.ts +57 -0
  89. package/lib/browser/BrowserUtil.js +347 -0
  90. package/lib/browser/fingerprint.d.ts +120 -0
  91. package/lib/browser/fingerprint.js +1500 -0
  92. package/lib/check/CheckUtil.d.ts +110 -0
  93. package/lib/check/CheckUtil.js +475 -0
  94. package/lib/common.d.ts +43 -0
  95. package/lib/common.js +50 -0
  96. package/lib/config.d.ts +7 -0
  97. package/lib/config.js +12 -0
  98. package/lib/cookie/CookieUtil.d.ts +56 -0
  99. package/lib/cookie/CookieUtil.js +22 -0
  100. package/lib/cookie/cookie.d.ts +2 -0
  101. package/lib/cookie/cookie.js +109 -0
  102. package/lib/date/DateUtil.d.ts +261 -0
  103. package/lib/date/DateUtil.js +773 -0
  104. package/lib/env/EnvUtil.d.ts +27 -0
  105. package/lib/env/EnvUtil.js +39 -0
  106. package/lib/env/env.d.ts +41 -0
  107. package/lib/env/env.js +112 -0
  108. package/lib/func/FunctionUtil.d.ts +77 -0
  109. package/lib/func/FunctionUtil.js +225 -0
  110. package/lib/index.d.ts +19 -0
  111. package/lib/index.js +144 -0
  112. package/lib/number/NumberUtil.d.ts +96 -0
  113. package/lib/number/NumberUtil.js +266 -0
  114. package/lib/object/ObjectUtil.d.ts +41 -0
  115. package/lib/object/ObjectUtil.js +168 -0
  116. package/lib/pwd/PasswordUtil.d.ts +33 -0
  117. package/lib/pwd/PasswordUtil.js +78 -0
  118. package/lib/pwd/crypto.d.ts +6 -0
  119. package/lib/pwd/crypto.js +1829 -0
  120. package/lib/request/RequestUtil.d.ts +52 -0
  121. package/lib/request/RequestUtil.js +94 -0
  122. package/lib/request/jsonp.d.ts +18 -0
  123. package/lib/request/jsonp.js +80 -0
  124. package/lib/request/mgop/env.d.ts +7 -0
  125. package/lib/request/mgop/env.js +21 -0
  126. package/lib/request/mgop/index.d.ts +3 -0
  127. package/lib/request/mgop/index.js +289 -0
  128. package/lib/request/mgop/responseCode.d.ts +20 -0
  129. package/lib/request/mgop/responseCode.js +42 -0
  130. package/lib/request/mgop/type.d.ts +34 -0
  131. package/lib/request/mgop/type.js +5 -0
  132. package/lib/request/uniRquest/index.d.ts +2 -0
  133. package/lib/request/uniRquest/index.js +77 -0
  134. package/lib/request/uniRquest/miniapp/index.d.ts +2 -0
  135. package/lib/request/uniRquest/miniapp/index.js +35 -0
  136. package/lib/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
  137. package/lib/request/uniRquest/miniapp-bytedance/index.js +47 -0
  138. package/lib/request/uniRquest/types.d.ts +65 -0
  139. package/lib/request/uniRquest/types.js +42 -0
  140. package/lib/request/uniRquest/utils.d.ts +9 -0
  141. package/lib/request/uniRquest/utils.js +83 -0
  142. package/lib/request/uniRquest/web/index.d.ts +2 -0
  143. package/lib/request/uniRquest/web/index.js +91 -0
  144. package/lib/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
  145. package/lib/request/uniRquest/wechat-miniprogram/index.js +47 -0
  146. package/lib/request/uniRquest/weex/index.d.ts +14 -0
  147. package/lib/request/uniRquest/weex/index.js +65 -0
  148. package/lib/storage/StorageUtil.d.ts +19 -0
  149. package/lib/storage/StorageUtil.js +85 -0
  150. package/lib/string/StringUtil.d.ts +392 -0
  151. package/lib/string/StringUtil.js +566 -0
  152. package/lib/system/SystemUtil.d.ts +46 -0
  153. package/lib/system/SystemUtil.js +132 -0
  154. package/lib/transfer/MapTransferUtil.d.ts +54 -0
  155. package/lib/transfer/MapTransferUtil.js +165 -0
  156. package/lib/transfer/TransferUtil.d.ts +98 -0
  157. package/lib/transfer/TransferUtil.js +333 -0
  158. package/lib/types.d.ts +25 -0
  159. package/lib/types.js +5 -0
  160. package/lib/url/UrlUtil.d.ts +30 -0
  161. package/lib/url/UrlUtil.js +95 -0
  162. package/package.json +56 -0
@@ -0,0 +1,29 @@
1
+ import { ERROR_REQUEST_ABORT } from '../types';
2
+ import { object2json } from '../utils';
3
+ export default function (options) {
4
+ return new Promise(function (resolve, reject) {
5
+ var url = options.url,
6
+ method = options.method,
7
+ data = options.data,
8
+ dataType = options.dataType,
9
+ headers = options.headers,
10
+ timeout = options.timeout;
11
+ my.request({
12
+ url: url,
13
+ headers: headers,
14
+ method: method,
15
+ data: data,
16
+ timeout: timeout,
17
+ dataType: dataType,
18
+ success: function success(res) {
19
+ resolve(res);
20
+ },
21
+ fail: function fail(res) {
22
+ reject({
23
+ code: ERROR_REQUEST_ABORT.code,
24
+ message: object2json(res)
25
+ });
26
+ }
27
+ });
28
+ });
29
+ }
@@ -0,0 +1,2 @@
1
+ import { RequestOptions, ResponseData } from '../types';
2
+ export default function (options: RequestOptions): Promise<ResponseData>;
@@ -0,0 +1,41 @@
1
+ import { ERROR_REQUEST_TIMEOUT, ERROR_REQUEST_ABORT } from '../types';
2
+ import { object2json } from '../utils';
3
+ export default function (options) {
4
+ return new Promise(function (resolve, reject) {
5
+ var url = options.url,
6
+ method = options.method,
7
+ data = options.data,
8
+ dataType = options.dataType,
9
+ headers = options.headers,
10
+ timeout = options.timeout;
11
+ var timeoutTimer;
12
+ var requestTask = tt.request({
13
+ url: url,
14
+ headers: headers,
15
+ method: method,
16
+ data: data,
17
+ timeout: timeout,
18
+ dataType: dataType,
19
+ success: function success(res) {
20
+ resolve(res);
21
+ },
22
+ fail: function fail(res) {
23
+ reject({
24
+ code: ERROR_REQUEST_ABORT.code,
25
+ message: object2json(res)
26
+ });
27
+ },
28
+ complete: function complete() {
29
+ if (timeoutTimer) {
30
+ clearTimeout(timeoutTimer);
31
+ }
32
+ }
33
+ });
34
+ if (timeout) {
35
+ timeoutTimer = setTimeout(function () {
36
+ requestTask.abort();
37
+ reject(ERROR_REQUEST_TIMEOUT);
38
+ }, timeout);
39
+ }
40
+ });
41
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Common
3
+ */
4
+ export declare enum DATA_TYPE {
5
+ json = "json",
6
+ text = "text"
7
+ }
8
+ export declare const DEFAULT_TIMEOUT = 20000;
9
+ export declare const DEFAULT_JSON_CALLBACK = "callback";
10
+ export declare const DEFAULT_JSONP_OPTIONS: {
11
+ timeout: number;
12
+ jsonpCallback: string;
13
+ dataType: DATA_TYPE;
14
+ };
15
+ export interface AsObject {
16
+ [key: string]: string | number;
17
+ }
18
+ /**
19
+ * Request
20
+ */
21
+ export interface JsonpOptions {
22
+ url: string;
23
+ timeout?: number;
24
+ dataType?: DATA_TYPE;
25
+ jsonpCallback?: string;
26
+ jsonpCallbackFunctionName?: string;
27
+ }
28
+ export interface RequestOptions {
29
+ url: string;
30
+ headers?: AsObject;
31
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
32
+ data?: AsObject;
33
+ timeout?: number;
34
+ dataType?: DATA_TYPE;
35
+ }
36
+ export interface InnerRequestOptions extends RequestOptions {
37
+ headers: AsObject;
38
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
39
+ data: AsObject;
40
+ timeout: number;
41
+ dataType: DATA_TYPE;
42
+ }
43
+ export declare const DEFAULT_REQUEST_OPTIONS: RequestOptions;
44
+ /**
45
+ * Response
46
+ */
47
+ export interface ResponseData {
48
+ data: string | object;
49
+ headers?: AsObject;
50
+ status?: number;
51
+ }
52
+ export interface ResponseError extends ErrorData {
53
+ code: number;
54
+ message: string;
55
+ }
56
+ /**
57
+ * Error
58
+ */
59
+ export interface ErrorData {
60
+ code: number;
61
+ message?: string;
62
+ }
63
+ export declare const ERROR_REQUEST_ABORT: ErrorData;
64
+ export declare const ERROR_REQUEST_TIMEOUT: ErrorData;
65
+ export declare const ERROR_REQUEST_NOT_SUPPORT: ErrorData;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Common
3
+ */
4
+ export var DATA_TYPE;
5
+ (function (DATA_TYPE) {
6
+ DATA_TYPE["json"] = "json";
7
+ DATA_TYPE["text"] = "text";
8
+ })(DATA_TYPE || (DATA_TYPE = {}));
9
+ export var DEFAULT_TIMEOUT = 20000;
10
+ export var DEFAULT_JSON_CALLBACK = 'callback';
11
+ export var DEFAULT_JSONP_OPTIONS = {
12
+ timeout: DEFAULT_TIMEOUT,
13
+ jsonpCallback: DEFAULT_JSON_CALLBACK,
14
+ dataType: DATA_TYPE.json
15
+ };
16
+ export var DEFAULT_REQUEST_OPTIONS = {
17
+ url: '',
18
+ headers: {
19
+ 'Content-Type': 'application/json'
20
+ },
21
+ method: 'GET',
22
+ timeout: DEFAULT_TIMEOUT,
23
+ dataType: DATA_TYPE.json
24
+ };
25
+ export var ERROR_REQUEST_ABORT = {
26
+ code: 0,
27
+ message: 'Request Abort'
28
+ };
29
+ export var ERROR_REQUEST_TIMEOUT = {
30
+ code: 1,
31
+ message: 'Request timeout'
32
+ };
33
+ export var ERROR_REQUEST_NOT_SUPPORT = {
34
+ code: 2,
35
+ message: 'Request not support this platform'
36
+ };
@@ -0,0 +1,9 @@
1
+ import { DATA_TYPE, AsObject } from './types';
2
+ export declare function getDataWithType(data: any, type: DATA_TYPE): any;
3
+ export declare function stringifyQS(qs: AsObject): string;
4
+ export declare function applyParamToURL(param: AsObject | void, url: string): string;
5
+ export declare function object2json(obj: any): any;
6
+ export declare function isObject(obj: any): boolean;
7
+ export declare function normalizeHeaders(obj: AsObject): AsObject;
8
+ export declare function checkIsApplyDataToURL(headers: AsObject | undefined): boolean;
9
+ export declare function isPlainObject(obj: any): boolean;
@@ -0,0 +1,69 @@
1
+ import { DATA_TYPE } from './types';
2
+ export function getDataWithType(data, type) {
3
+ if (type === DATA_TYPE.json) {
4
+ try {
5
+ return JSON.parse(data);
6
+ } catch (e) {}
7
+ }
8
+ if (type === DATA_TYPE.text) {
9
+ try {
10
+ return JSON.stringify(data);
11
+ } catch (e) {}
12
+ }
13
+ return data;
14
+ }
15
+ export function stringifyQS(qs) {
16
+ if (!qs) {
17
+ return '';
18
+ }
19
+ var str = [];
20
+ for (var key in qs) {
21
+ if (qs[key] !== undefined && qs[key] !== null) {
22
+ str.push("".concat(key, "=").concat(encodeURIComponent(String(qs[key]))));
23
+ }
24
+ }
25
+ return str.join('&');
26
+ }
27
+ export function applyParamToURL(param, url) {
28
+ if (!param) {
29
+ return url;
30
+ }
31
+ return "".concat(url).concat(url.indexOf('?') === -1 ? '?' : '&').concat(stringifyQS(param));
32
+ }
33
+ export function object2json(obj) {
34
+ try {
35
+ return JSON.stringify(obj);
36
+ } catch (e) {
37
+ return obj || '';
38
+ }
39
+ }
40
+ export function isObject(obj) {
41
+ return typeof obj === 'object' && obj !== null;
42
+ }
43
+ export function normalizeHeaders(obj) {
44
+ if (!isObject(obj)) {
45
+ return obj;
46
+ }
47
+ var keyList = ['Accept', 'Content-Type'];
48
+ keyList.forEach(function (key) {
49
+ Object.keys(obj).some(function (headerKey) {
50
+ if (headerKey.toUpperCase() === key.toUpperCase() && headerKey !== key) {
51
+ obj[key] = obj[headerKey];
52
+ delete obj[headerKey];
53
+ return true;
54
+ }
55
+ return false;
56
+ });
57
+ });
58
+ return obj;
59
+ }
60
+ export function checkIsApplyDataToURL(headers) {
61
+ if (!headers || !headers['Content-Type']) {
62
+ return false;
63
+ }
64
+ return String(headers['Content-Type']).toLowerCase().indexOf('application/x-www-form-urlencoded') > -1;
65
+ }
66
+ var EMPTY_OBJECT = {};
67
+ export function isPlainObject(obj) {
68
+ return EMPTY_OBJECT.toString.call(obj) === '[object Object]';
69
+ }
@@ -0,0 +1,2 @@
1
+ import { InnerRequestOptions, ResponseData } from '../types';
2
+ export default function requestXHR(options: InnerRequestOptions): Promise<ResponseData>;
@@ -0,0 +1,85 @@
1
+ import { ERROR_REQUEST_TIMEOUT, ERROR_REQUEST_ABORT } from '../types';
2
+ import { applyParamToURL, object2json, checkIsApplyDataToURL, isObject, isPlainObject } from '../utils';
3
+ export default function requestXHR(options) {
4
+ options.headers = Object.assign({
5
+ 'Accept': 'application/json, text/plain, */*'
6
+ }, options.headers);
7
+ var _a = Object.assign({
8
+ withCredentials: true,
9
+ method: 'GET',
10
+ validateStatus: function validateStatus(status) {
11
+ return status >= 200 && status < 300 || status === 304;
12
+ }
13
+ }, options),
14
+ validateStatus = _a.validateStatus,
15
+ url = _a.url,
16
+ method = _a.method,
17
+ withCredentials = _a.withCredentials,
18
+ headers = _a.headers,
19
+ data = _a.data,
20
+ timeout = _a.timeout,
21
+ dataType = _a.dataType;
22
+ return new Promise(function (resolve, reject) {
23
+ var timer;
24
+ var requestData;
25
+ var xhr = new XMLHttpRequest();
26
+ var clean = function clean() {
27
+ clearTimeout(timer);
28
+ };
29
+ timer = window.setTimeout(function () {
30
+ clean();
31
+ reject(ERROR_REQUEST_TIMEOUT);
32
+ xhr.abort();
33
+ }, timeout);
34
+ xhr.onreadystatechange = function () {
35
+ if (!xhr || xhr.readyState !== 4) {
36
+ return;
37
+ }
38
+ clean();
39
+ if (!validateStatus(xhr.status)) {
40
+ // TODO Detailed error message
41
+ return reject({
42
+ code: ERROR_REQUEST_ABORT.code,
43
+ message: "".concat(JSON.stringify({
44
+ status: xhr.status
45
+ }))
46
+ });
47
+ }
48
+ var headers = xhr.getAllResponseHeaders();
49
+ var arr = headers.trim().split(/[\r\n]+/);
50
+ var headerMap = {};
51
+ arr.forEach(function (line) {
52
+ var parts = line.split(': ');
53
+ var header = parts.shift();
54
+ var value = parts.join(': ');
55
+ headerMap[header] = value;
56
+ });
57
+ resolve({
58
+ data: xhr.response,
59
+ status: xhr.status,
60
+ headers: headerMap
61
+ });
62
+ };
63
+ xhr.responseType = dataType || '';
64
+ if (method === 'GET' || checkIsApplyDataToURL(headers)) {
65
+ xhr.open(method, applyParamToURL(data, url), true);
66
+ } else {
67
+ xhr.open(method, url, true);
68
+ }
69
+ xhr.withCredentials = withCredentials;
70
+ if (headers) {
71
+ Object.keys(headers || []).forEach(function (key) {
72
+ xhr.setRequestHeader(key, String(headers[key]));
73
+ });
74
+ }
75
+ if (method === 'GET') {
76
+ requestData = null;
77
+ } else {
78
+ requestData = data;
79
+ }
80
+ if (isObject(requestData) && isPlainObject(requestData)) {
81
+ requestData = object2json(requestData);
82
+ }
83
+ xhr.send(requestData);
84
+ });
85
+ }
@@ -0,0 +1,2 @@
1
+ import { RequestOptions, ResponseData } from '../types';
2
+ export default function (options: RequestOptions): Promise<ResponseData>;
@@ -0,0 +1,41 @@
1
+ import { ERROR_REQUEST_ABORT, ERROR_REQUEST_TIMEOUT } from '../types';
2
+ import { object2json } from '../utils';
3
+ export default function (options) {
4
+ return new Promise(function (resolve, reject) {
5
+ var url = options.url,
6
+ method = options.method,
7
+ data = options.data,
8
+ dataType = options.dataType,
9
+ headers = options.headers,
10
+ timeout = options.timeout;
11
+ var timeoutTimer;
12
+ var requestTask = wx.request({
13
+ url: url,
14
+ header: headers,
15
+ method: method,
16
+ data: data,
17
+ timeout: timeout,
18
+ dataType: dataType,
19
+ success: function success(res) {
20
+ resolve(res);
21
+ },
22
+ fail: function fail(res) {
23
+ reject({
24
+ code: ERROR_REQUEST_ABORT.code,
25
+ message: object2json(res)
26
+ });
27
+ },
28
+ complete: function complete() {
29
+ if (timeoutTimer) {
30
+ clearTimeout(timeoutTimer);
31
+ }
32
+ }
33
+ });
34
+ if (timeout) {
35
+ timeoutTimer = setTimeout(function () {
36
+ requestTask.abort();
37
+ reject(ERROR_REQUEST_TIMEOUT);
38
+ }, timeout);
39
+ }
40
+ });
41
+ }
@@ -0,0 +1,14 @@
1
+ import { RequestOptions, AsObject, ResponseData } from '../types';
2
+ export interface WeexRequestOptions extends RequestOptions {
3
+ body?: string;
4
+ type?: 'json' | 'text' | 'jsonp';
5
+ isJSONP?: boolean;
6
+ }
7
+ export interface WeexResponse {
8
+ status: number;
9
+ ok: boolean;
10
+ statusText: string;
11
+ data: string;
12
+ headers: AsObject;
13
+ }
14
+ export default function weexRequest(options: WeexRequestOptions): Promise<ResponseData>;
@@ -0,0 +1,59 @@
1
+ import { DATA_TYPE, ERROR_REQUEST_ABORT } from '../types';
2
+ import { applyParamToURL, object2json, checkIsApplyDataToURL } from '../utils';
3
+ export default function weexRequest(options) {
4
+ var Stream = __weex_require__('@weex-module/stream');
5
+ return new Promise(function (resolve, reject) {
6
+ var requestOptions = Object.assign({}, options);
7
+ var url = requestOptions.url,
8
+ method = requestOptions.method,
9
+ data = requestOptions.data,
10
+ dataType = requestOptions.dataType,
11
+ headers = requestOptions.headers;
12
+ if (method === 'GET' && data || checkIsApplyDataToURL(headers)) {
13
+ requestOptions.url = applyParamToURL(data, url);
14
+ } else if (data) {
15
+ requestOptions.body = object2json(data);
16
+ }
17
+ if (requestOptions.isJSONP) {
18
+ requestOptions.type = 'jsonp';
19
+ delete requestOptions.isJSONP;
20
+ } else if (dataType === DATA_TYPE.json) {
21
+ requestOptions.type = 'json';
22
+ } else {
23
+ requestOptions.type = 'text';
24
+ }
25
+ Stream.fetch(requestOptions, function (response) {
26
+ try {
27
+ if (typeof response === 'string') {
28
+ // parse the response(it is string in weex-android & obj in ios)
29
+ response = JSON.parse(response);
30
+ if (response.data && typeof response.data === 'string' && response.ok) {
31
+ try {
32
+ response.data = JSON.parse(response.data);
33
+ } catch (e) {
34
+ throw new Error('the response.data in not valid json');
35
+ }
36
+ }
37
+ }
38
+ if (response.ok !== true) {
39
+ throw response;
40
+ }
41
+ resolve({
42
+ headers: response.headers,
43
+ status: response.status,
44
+ data: response.data
45
+ });
46
+ } catch (error) {
47
+ reject({
48
+ code: ERROR_REQUEST_ABORT.code,
49
+ message: object2json(error)
50
+ });
51
+ }
52
+ }, function (progress) {
53
+ if (progress.status === 'FAILED') reject({
54
+ code: ERROR_REQUEST_ABORT.code,
55
+ message: object2json(progress)
56
+ });
57
+ });
58
+ });
59
+ }
@@ -0,0 +1,19 @@
1
+ import { SessionOpts } from "../types";
2
+ declare namespace StorageUtil {
3
+ function setItem<T>(name: string, data: T, opts?: SessionOpts): void;
4
+ function getItem<T>(name: string, opts?: SessionOpts): T;
5
+ function removeItem(name: string, opts?: SessionOpts): void;
6
+ function clear(opts?: SessionOpts): void;
7
+ function clearTitle(opts?: SessionOpts): void;
8
+ /**
9
+ * 设置全局缓存前缀
10
+ * @param str 缓存前缀
11
+ */
12
+ const setGlobalTitle: (str: string) => void;
13
+ /**
14
+ * 设置全局缓存类型
15
+ * @param type "session" | "local"
16
+ */
17
+ const setGlobalCacheType: (type: SessionOpts["type"]) => void;
18
+ }
19
+ export default StorageUtil;
@@ -0,0 +1,75 @@
1
+ var __read = this && this.__read || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o),
5
+ r,
6
+ ar = [],
7
+ e;
8
+ try {
9
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
10
+ } catch (error) {
11
+ e = {
12
+ error: error
13
+ };
14
+ } finally {
15
+ try {
16
+ if (r && !r.done && (m = i["return"])) m.call(i);
17
+ } finally {
18
+ if (e) throw e.error;
19
+ }
20
+ }
21
+ return ar;
22
+ };
23
+ import config from "../config";
24
+ var StorageUtil;
25
+ (function (StorageUtil) {
26
+ function setItem(name, data, opts) {
27
+ var cacheType = (opts === null || opts === void 0 ? void 0 : opts.type) || config.cacheType;
28
+ if (name && data !== undefined) window["".concat(cacheType, "Storage")].setItem((opts === null || opts === void 0 ? void 0 : opts.itemName) || "".concat(config.title).concat(name), JSON.stringify(data));
29
+ }
30
+ StorageUtil.setItem = setItem;
31
+ function getItem(name, opts) {
32
+ var cacheType = (opts === null || opts === void 0 ? void 0 : opts.type) || config.cacheType;
33
+ var item = window["".concat(cacheType, "Storage")].getItem((opts === null || opts === void 0 ? void 0 : opts.itemName) || "".concat(config.title).concat(name));
34
+ try {
35
+ return JSON.parse(item);
36
+ } catch (error) {
37
+ return item;
38
+ }
39
+ }
40
+ StorageUtil.getItem = getItem;
41
+ function removeItem(name, opts) {
42
+ var cacheType = (opts === null || opts === void 0 ? void 0 : opts.type) || config.cacheType;
43
+ if (name) window["".concat(cacheType, "Storage")].removeItem((opts === null || opts === void 0 ? void 0 : opts.itemName) || "".concat(config.title).concat(name));
44
+ }
45
+ StorageUtil.removeItem = removeItem;
46
+ function clear(opts) {
47
+ var cacheType = (opts === null || opts === void 0 ? void 0 : opts.type) || config.cacheType;
48
+ window["".concat(cacheType, "Storage")].clear();
49
+ }
50
+ StorageUtil.clear = clear;
51
+ function clearTitle(opts) {
52
+ var cacheType = (opts === null || opts === void 0 ? void 0 : opts.type) || config.cacheType;
53
+ Object.entries(window["".concat(cacheType, "Storage")]).forEach(function (_a) {
54
+ var _b = __read(_a, 1),
55
+ k = _b[0];
56
+ if (k.includes(config.title)) window["".concat(cacheType, "Storage")].removeItem(k);
57
+ });
58
+ }
59
+ StorageUtil.clearTitle = clearTitle;
60
+ /**
61
+ * 设置全局缓存前缀
62
+ * @param str 缓存前缀
63
+ */
64
+ StorageUtil.setGlobalTitle = function (str) {
65
+ config.title = str;
66
+ };
67
+ /**
68
+ * 设置全局缓存类型
69
+ * @param type "session" | "local"
70
+ */
71
+ StorageUtil.setGlobalCacheType = function (type) {
72
+ config.cacheType = type;
73
+ };
74
+ })(StorageUtil || (StorageUtil = {}));
75
+ export default StorageUtil;