rxtutils 1.0.5-beta.9 → 1.0.6

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.
@@ -0,0 +1,126 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib_es6_js = require('C:\\Users\\ruixutong\\code\\RUtils\\node_modules\\tslib\\tslib.es6.js');
6
+ var moment = require('moment');
7
+ var indexDB = require('./indexDB.cjs');
8
+ var defaultEquals = require('../defaultEquals.cjs');
9
+
10
+ var StorageMap = {
11
+ localStorage: localStorage,
12
+ sessionStorage: sessionStorage,
13
+ };
14
+ var Cache = /** @class */ (function () {
15
+ function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
16
+ if (indexDBName === void 0) { indexDBName = '__apiCacheDatabase__'; }
17
+ if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
18
+ this.cache = [];
19
+ this.cacheOptions = {
20
+ storageType: cacheType,
21
+ cacheKey: cacheKey,
22
+ cacheTime: cacheTime,
23
+ indexDBName: indexDBName,
24
+ cacheKeyEquals: cacheKeyEquals,
25
+ };
26
+ if (cacheType === 'indexedDB') {
27
+ this.storage = new indexDB.IndexedDBStorage(indexDBName, 'cacheStore');
28
+ }
29
+ else if (typeof cacheType === 'string') {
30
+ this.storage = StorageMap[cacheType];
31
+ }
32
+ this._init();
33
+ }
34
+ Cache.prototype._init = function () {
35
+ return tslib_es6_js.__awaiter(this, void 0, void 0, function () {
36
+ var _a, cacheType, cacheKey, _b, _c, _d;
37
+ return tslib_es6_js.__generator(this, function (_e) {
38
+ switch (_e.label) {
39
+ case 0:
40
+ _a = this.cacheOptions, cacheType = _a.storageType, cacheKey = _a.cacheKey;
41
+ if (!(this.storage instanceof indexDB.IndexedDBStorage)) return [3 /*break*/, 2];
42
+ _b = this;
43
+ _d = (_c = JSON).parse;
44
+ return [4 /*yield*/, this.storage.getItem(cacheKey)];
45
+ case 1:
46
+ _b.cache = _d.apply(_c, [(_e.sent()) || '[]']);
47
+ return [3 /*break*/, 3];
48
+ case 2:
49
+ if (this.storage instanceof Storage) {
50
+ this.storage = StorageMap[cacheType];
51
+ if (this.storage) {
52
+ if (typeof cacheKey === 'string') {
53
+ try {
54
+ this.cache = JSON.parse(this.storage.getItem(cacheKey) || '[]');
55
+ }
56
+ catch (e) {
57
+ this.cache = [];
58
+ console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
59
+ }
60
+ }
61
+ }
62
+ }
63
+ _e.label = 3;
64
+ case 3:
65
+ this._filterExpired();
66
+ this._saveToStorage();
67
+ return [2 /*return*/];
68
+ }
69
+ });
70
+ });
71
+ };
72
+ Cache.prototype._filterExpired = function () {
73
+ var newCache = this.cache.filter(function (item) {
74
+ return moment(item.expireTime).isAfter(moment());
75
+ });
76
+ this.cache = newCache;
77
+ };
78
+ Cache.prototype._saveToStorage = function () {
79
+ if (this.storage) {
80
+ if (typeof this.cacheOptions.cacheKey === 'string') {
81
+ this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
82
+ }
83
+ }
84
+ };
85
+ Cache.prototype.setCache = function (params, data, cacheOptions) {
86
+ var _a = tslib_es6_js.__assign(tslib_es6_js.__assign({}, this.cacheOptions), cacheOptions), cacheTime = _a.cacheTime, _b = _a.cacheKeyEquals, cacheKeyEquals = _b === void 0 ? defaultEquals : _b;
87
+ var cacheItemIndex = this.cache.findIndex(function (item) {
88
+ return cacheKeyEquals(item.params, params);
89
+ });
90
+ if (cacheItemIndex > -1) {
91
+ this.cache.splice(cacheItemIndex, 1);
92
+ }
93
+ this.cache.push({
94
+ params: params,
95
+ data: data,
96
+ expireTime: moment().add(cacheTime, 'seconds').toJSON(),
97
+ });
98
+ this._saveToStorage();
99
+ };
100
+ Cache.prototype.getCache = function (params) {
101
+ var _this = this;
102
+ // debugger;
103
+ var itemIndex = this.cache.findIndex(function (item) {
104
+ return _this.cacheOptions.cacheKeyEquals(item.params, params);
105
+ });
106
+ var item = this.cache[itemIndex];
107
+ if (item) {
108
+ if (moment(item.expireTime).isAfter(moment())) {
109
+ return item.data;
110
+ }
111
+ else {
112
+ this.cache.splice(itemIndex, 1);
113
+ this._saveToStorage();
114
+ }
115
+ }
116
+ return null;
117
+ };
118
+ Cache.prototype.clear = function () {
119
+ this.cache = [];
120
+ this._saveToStorage();
121
+ };
122
+ return Cache;
123
+ }());
124
+
125
+ exports.StorageMap = StorageMap;
126
+ exports.default = Cache;
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ // IndexedDB的简易封装类
4
+ var IndexedDBStorage = /** @class */ (function () {
5
+ function IndexedDBStorage(dbName, storeName) {
6
+ this.db = null;
7
+ this.dbName = dbName;
8
+ this.storeName = storeName;
9
+ }
10
+ // 打开数据库连接
11
+ IndexedDBStorage.prototype._open = function () {
12
+ var _this = this;
13
+ return new Promise(function (resolve, reject) {
14
+ if (_this.db) {
15
+ resolve(_this.db);
16
+ return;
17
+ }
18
+ var request = indexedDB.open(_this.dbName);
19
+ request.onupgradeneeded = function (event) {
20
+ var db = event.target.result;
21
+ db.createObjectStore(_this.storeName, { keyPath: 'key' });
22
+ };
23
+ request.onerror = function () {
24
+ var _a;
25
+ reject("IndexedDB open request error: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message));
26
+ };
27
+ request.onsuccess = function (event) {
28
+ _this.db = event.target.result;
29
+ resolve(_this.db);
30
+ };
31
+ });
32
+ };
33
+ // 获取存储对象
34
+ IndexedDBStorage.prototype._getStore = function (mode) {
35
+ var _this = this;
36
+ if (mode === void 0) { mode = 'readonly'; }
37
+ return this._open().then(function (db) {
38
+ var transaction = db.transaction(_this.storeName, mode);
39
+ return transaction.objectStore(_this.storeName);
40
+ });
41
+ };
42
+ // 设置键值对
43
+ IndexedDBStorage.prototype.setItem = function (key, value) {
44
+ return this._getStore('readwrite').then(function (store) {
45
+ return new Promise(function (resolve, reject) {
46
+ var request = store.put({ key: key, value: value });
47
+ request.onsuccess = function () { return resolve(); };
48
+ request.onerror = function () { var _a; return reject("Could not set the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
49
+ });
50
+ });
51
+ };
52
+ // 获取键对应的值
53
+ IndexedDBStorage.prototype.getItem = function (key) {
54
+ return this._getStore().then(function (store) {
55
+ return new Promise(function (resolve, reject) {
56
+ var request = store.get(key);
57
+ request.onsuccess = function () {
58
+ return resolve(request.result ? request.result.value : undefined);
59
+ };
60
+ request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
61
+ });
62
+ });
63
+ };
64
+ return IndexedDBStorage;
65
+ }());
66
+
67
+ exports.IndexedDBStorage = IndexedDBStorage;
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var react = require('react');
6
+
7
+ function resolveHookState(nextState, currentState) {
8
+ if (typeof nextState === 'function') {
9
+ return nextState.length
10
+ ? nextState(currentState)
11
+ : nextState();
12
+ }
13
+ return nextState;
14
+ }
15
+ var isBrowser = typeof window !== 'undefined';
16
+ var useEffectOnce = function (effect) {
17
+ react.useEffect(effect, []);
18
+ };
19
+ var useIsomorphicLayoutEffect = isBrowser ? react.useLayoutEffect : react.useEffect;
20
+ function createStateStore(initialState) {
21
+ var store = {
22
+ state: initialState instanceof Function ? initialState() : initialState,
23
+ setState: function (nextState) {
24
+ store.state = resolveHookState(nextState, store.state);
25
+ store.setters.forEach(function (setter) { return setter(store.state); });
26
+ store.watchers.forEach(function (watcher) { return watcher(store.state); });
27
+ },
28
+ setters: [],
29
+ watchers: [],
30
+ };
31
+ var use = function () {
32
+ var _a = react.useState(store.state), globalState = _a[0], stateSetter = _a[1];
33
+ useEffectOnce(function () { return function () {
34
+ store.setters = store.setters.filter(function (setter) { return setter !== stateSetter; });
35
+ }; });
36
+ useIsomorphicLayoutEffect(function () {
37
+ if (!store.setters.includes(stateSetter)) {
38
+ store.setters.push(stateSetter);
39
+ }
40
+ });
41
+ return [globalState, store.setState];
42
+ };
43
+ var get = function () { return store.state; };
44
+ var set = store.setState;
45
+ var watch = function (callback) {
46
+ store.watchers.push(callback);
47
+ var close = function () {
48
+ store.watchers = store.watchers.filter(function (watcher) { return watcher !== callback; });
49
+ };
50
+ return close;
51
+ };
52
+ return {
53
+ use: use,
54
+ get: get,
55
+ set: set,
56
+ watch: watch,
57
+ };
58
+ }
59
+
60
+ exports.default = createStateStore;
61
+ exports.resolveHookState = resolveHookState;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ function defaultEquals(prev, next) {
4
+ return JSON.stringify(prev) === JSON.stringify(next);
5
+ }
6
+
7
+ module.exports = defaultEquals;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var index = require('./cache/index.cjs');
4
+ var index$1 = require('./request/index.cjs');
5
+ var index$2 = require('./createStateStore/index.cjs');
6
+
7
+
8
+
9
+ exports.Cache = index.default;
10
+ exports.createBaseRequest = index$1;
11
+ exports.createStateStore = index$2.default;
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ function _defaultErrorCodeHandler(defaultMessageShower, code) {
4
+ defaultMessageShower("\u8BF7\u6C42\u51FA\u9519\uFF0C\u9519\u8BEF\u7801\uFF1A".concat(code, "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
5
+ }
6
+ function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
7
+ defaultMessageShower("\u670D\u52A1\u7AEF\u8BF7\u6C42\u51FA\u9519\uFF0CHttp\u9519\u8BEF\u7801\uFF1A".concat(String(code), "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
8
+ }
9
+ function _defaultOtherErrorCodeHandler(defaultMessageShower, error) {
10
+ defaultMessageShower("\u672A\u77E5\u8BF7\u6C42\u51FA\u9519\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5");
11
+ }
12
+
13
+ exports._defaultErrorCodeHandler = _defaultErrorCodeHandler;
14
+ exports._defaultHttpErrorCodeHandler = _defaultHttpErrorCodeHandler;
15
+ exports._defaultOtherErrorCodeHandler = _defaultOtherErrorCodeHandler;
@@ -0,0 +1,115 @@
1
+ 'use strict';
2
+
3
+ var tslib_es6_js = require('C:\\Users\\ruixutong\\code\\RUtils\\node_modules\\tslib\\tslib.es6.js');
4
+ var axios = require('axios');
5
+ var lodash = require('lodash');
6
+ var defaultHandlers = require('./defaultHandlers.cjs');
7
+ var defaultEquals = require('../defaultEquals.cjs');
8
+ var index = require('../cache/index.cjs');
9
+
10
+ function createBaseRequest(baseOptions) {
11
+ var baseURL = Object(baseOptions).baseURL;
12
+ // 创建新的 Axios 实例
13
+ var instance = axios.create({
14
+ baseURL: baseURL,
15
+ withCredentials: true,
16
+ });
17
+ return function createRequest(requestOptions, createOptions) {
18
+ var _a = tslib_es6_js.__assign({}, requestOptions), method = _a.method, url = _a.url;
19
+ var _b = tslib_es6_js.__assign(tslib_es6_js.__assign({}, baseOptions), createOptions), baseURL = _b.baseURL, _c = _b.cacheDataKey, cacheDataKey = _c === void 0 ? "".concat(method, ":").concat(baseURL).concat(url) : _c, cacheDataInStorage = _b.cacheDataInStorage, _d = _b.cacheKeyEquals, cacheKeyEquals = _d === void 0 ? defaultEquals : _d, cacheTime = _b.cacheTime, _e = _b.indexDBName, indexDBName = _e === void 0 ? '__apiCacheDatabase__' : _e;
20
+ var cache = new index.default(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
21
+ function request(requestParam, options) {
22
+ var _a = tslib_es6_js.__assign(tslib_es6_js.__assign({}, requestOptions), requestParam), method = _a.method, url = _a.url, _b = _a.data, data = _b === void 0 ? {} : _b, _c = _a.params, params = _c === void 0 ? {} : _c;
23
+ var _d = tslib_es6_js.__assign(tslib_es6_js.__assign(tslib_es6_js.__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _d === void 0 ? alert : _d;
24
+ var _e = tslib_es6_js.__assign(tslib_es6_js.__assign(tslib_es6_js.__assign({}, baseOptions), createOptions), options), baseURL = _e.baseURL, _f = _e.enableCache, enableCache = _f === void 0 ? false : _f, _g = _e.cacheData, cacheData = _g === void 0 ? false : _g, _h = _e.defaultErrorCodeHandler, defaultErrorCodeHandler = _h === void 0 ? defaultHandlers._defaultErrorCodeHandler.bind(null, defaultMessageShower) : _h, _j = _e.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _j === void 0 ? defaultHandlers._defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _j, _k = _e.otherErrorHandler, otherErrorHandler = _k === void 0 ? defaultHandlers._defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _k, _l = _e.errorCodePath, errorCodePath = _l === void 0 ? 'code' : _l, _m = _e.cacheTime, cacheTime = _m === void 0 ? 60 : _m, _o = _e.errorCodeMap, errorCodeMap = _o === void 0 ? {} : _o, _p = _e.successCodes, successCodes = _p === void 0 ? ['0', '200'] : _p, _q = _e.httpErrorCodeMap, httpErrorCodeMap = _q === void 0 ? {} : _q, _r = _e.axiosOptions, axiosOptions = _r === void 0 ? {} : _r, _s = _e.throwError, throwError = _s === void 0 ? true : _s;
25
+ if (enableCache) {
26
+ var cacheItem = cache.getCache(params);
27
+ if (cacheItem) {
28
+ return Promise.resolve(cacheItem);
29
+ }
30
+ }
31
+ return instance
32
+ .request(tslib_es6_js.__assign({ method: method, url: url, data: data, params: params, baseURL: baseURL }, axiosOptions))
33
+ .then(function (res) {
34
+ var errorCode = String(lodash.at(res.data, errorCodePath));
35
+ if (successCodes.includes(errorCode)) {
36
+ if (cacheData) {
37
+ cache.setCache(params, res.data, { cacheTime: cacheTime });
38
+ }
39
+ return res.data;
40
+ }
41
+ var _a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
42
+ var err = new Error('服务端错误');
43
+ err.type = 'server';
44
+ err.data = res;
45
+ if (typeof customHandler === 'string') {
46
+ defaultMessageShower(customHandler);
47
+ }
48
+ else {
49
+ var _d = (Object(customHandler(errorCode, res.data, res, tslib_es6_js.__assign(tslib_es6_js.__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
50
+ res.data = replaceResData;
51
+ switch (handlerThrowError) {
52
+ case true:
53
+ throw err;
54
+ case false:
55
+ return res.data;
56
+ }
57
+ }
58
+ if (throwError) {
59
+ throw err;
60
+ }
61
+ return res.data;
62
+ }, function (error) {
63
+ if (error.response) {
64
+ // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
65
+ var resData = error;
66
+ var _a = httpErrorCodeMap, _b = error.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
67
+ var err = new Error('服务端错误');
68
+ err.type = 'http';
69
+ err.data = error;
70
+ if (typeof customHandler === 'string') {
71
+ message.error(customHandler);
72
+ }
73
+ else {
74
+ var _d = (Object(customHandler(error.response.status, error, tslib_es6_js.__assign(tslib_es6_js.__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? error : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
75
+ resData = replaceResData;
76
+ switch (handlerThrowError) {
77
+ case true:
78
+ throw err;
79
+ case false:
80
+ return resData;
81
+ }
82
+ }
83
+ if (throwError) {
84
+ throw err;
85
+ }
86
+ return resData;
87
+ }
88
+ else {
89
+ var resData = error;
90
+ var err = new Error('服务端错误');
91
+ err.type = 'http';
92
+ err.data = error;
93
+ var _g = (Object(otherErrorHandler(error))), _h = _g.replaceResData, replaceResData = _h === void 0 ? error : _h, _j = _g.throwError, handlerThrowError = _j === void 0 ? 'default' : _j;
94
+ resData = replaceResData;
95
+ switch (handlerThrowError) {
96
+ case true:
97
+ throw err;
98
+ case false:
99
+ return resData;
100
+ }
101
+ if (throwError) {
102
+ throw err;
103
+ }
104
+ return resData;
105
+ }
106
+ });
107
+ }
108
+ request.clearCache = function () {
109
+ cache.clear();
110
+ };
111
+ return request;
112
+ };
113
+ }
114
+
115
+ module.exports = createBaseRequest;
@@ -0,0 +1,121 @@
1
+ import { __awaiter, __generator, __assign } from 'C:\\Users\\ruixutong\\code\\RUtils\\node_modules\\tslib\\tslib.es6.js';
2
+ import moment from 'moment';
3
+ import { IndexedDBStorage } from './indexDB.mjs';
4
+ import defaultEquals from '../defaultEquals.mjs';
5
+
6
+ var StorageMap = {
7
+ localStorage: localStorage,
8
+ sessionStorage: sessionStorage,
9
+ };
10
+ var Cache = /** @class */ (function () {
11
+ function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
12
+ if (indexDBName === void 0) { indexDBName = '__apiCacheDatabase__'; }
13
+ if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
14
+ this.cache = [];
15
+ this.cacheOptions = {
16
+ storageType: cacheType,
17
+ cacheKey: cacheKey,
18
+ cacheTime: cacheTime,
19
+ indexDBName: indexDBName,
20
+ cacheKeyEquals: cacheKeyEquals,
21
+ };
22
+ if (cacheType === 'indexedDB') {
23
+ this.storage = new IndexedDBStorage(indexDBName, 'cacheStore');
24
+ }
25
+ else if (typeof cacheType === 'string') {
26
+ this.storage = StorageMap[cacheType];
27
+ }
28
+ this._init();
29
+ }
30
+ Cache.prototype._init = function () {
31
+ return __awaiter(this, void 0, void 0, function () {
32
+ var _a, cacheType, cacheKey, _b, _c, _d;
33
+ return __generator(this, function (_e) {
34
+ switch (_e.label) {
35
+ case 0:
36
+ _a = this.cacheOptions, cacheType = _a.storageType, cacheKey = _a.cacheKey;
37
+ if (!(this.storage instanceof IndexedDBStorage)) return [3 /*break*/, 2];
38
+ _b = this;
39
+ _d = (_c = JSON).parse;
40
+ return [4 /*yield*/, this.storage.getItem(cacheKey)];
41
+ case 1:
42
+ _b.cache = _d.apply(_c, [(_e.sent()) || '[]']);
43
+ return [3 /*break*/, 3];
44
+ case 2:
45
+ if (this.storage instanceof Storage) {
46
+ this.storage = StorageMap[cacheType];
47
+ if (this.storage) {
48
+ if (typeof cacheKey === 'string') {
49
+ try {
50
+ this.cache = JSON.parse(this.storage.getItem(cacheKey) || '[]');
51
+ }
52
+ catch (e) {
53
+ this.cache = [];
54
+ console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
55
+ }
56
+ }
57
+ }
58
+ }
59
+ _e.label = 3;
60
+ case 3:
61
+ this._filterExpired();
62
+ this._saveToStorage();
63
+ return [2 /*return*/];
64
+ }
65
+ });
66
+ });
67
+ };
68
+ Cache.prototype._filterExpired = function () {
69
+ var newCache = this.cache.filter(function (item) {
70
+ return moment(item.expireTime).isAfter(moment());
71
+ });
72
+ this.cache = newCache;
73
+ };
74
+ Cache.prototype._saveToStorage = function () {
75
+ if (this.storage) {
76
+ if (typeof this.cacheOptions.cacheKey === 'string') {
77
+ this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
78
+ }
79
+ }
80
+ };
81
+ Cache.prototype.setCache = function (params, data, cacheOptions) {
82
+ var _a = __assign(__assign({}, this.cacheOptions), cacheOptions), cacheTime = _a.cacheTime, _b = _a.cacheKeyEquals, cacheKeyEquals = _b === void 0 ? defaultEquals : _b;
83
+ var cacheItemIndex = this.cache.findIndex(function (item) {
84
+ return cacheKeyEquals(item.params, params);
85
+ });
86
+ if (cacheItemIndex > -1) {
87
+ this.cache.splice(cacheItemIndex, 1);
88
+ }
89
+ this.cache.push({
90
+ params: params,
91
+ data: data,
92
+ expireTime: moment().add(cacheTime, 'seconds').toJSON(),
93
+ });
94
+ this._saveToStorage();
95
+ };
96
+ Cache.prototype.getCache = function (params) {
97
+ var _this = this;
98
+ // debugger;
99
+ var itemIndex = this.cache.findIndex(function (item) {
100
+ return _this.cacheOptions.cacheKeyEquals(item.params, params);
101
+ });
102
+ var item = this.cache[itemIndex];
103
+ if (item) {
104
+ if (moment(item.expireTime).isAfter(moment())) {
105
+ return item.data;
106
+ }
107
+ else {
108
+ this.cache.splice(itemIndex, 1);
109
+ this._saveToStorage();
110
+ }
111
+ }
112
+ return null;
113
+ };
114
+ Cache.prototype.clear = function () {
115
+ this.cache = [];
116
+ this._saveToStorage();
117
+ };
118
+ return Cache;
119
+ }());
120
+
121
+ export { StorageMap, Cache as default };
@@ -0,0 +1,65 @@
1
+ // IndexedDB的简易封装类
2
+ var IndexedDBStorage = /** @class */ (function () {
3
+ function IndexedDBStorage(dbName, storeName) {
4
+ this.db = null;
5
+ this.dbName = dbName;
6
+ this.storeName = storeName;
7
+ }
8
+ // 打开数据库连接
9
+ IndexedDBStorage.prototype._open = function () {
10
+ var _this = this;
11
+ return new Promise(function (resolve, reject) {
12
+ if (_this.db) {
13
+ resolve(_this.db);
14
+ return;
15
+ }
16
+ var request = indexedDB.open(_this.dbName);
17
+ request.onupgradeneeded = function (event) {
18
+ var db = event.target.result;
19
+ db.createObjectStore(_this.storeName, { keyPath: 'key' });
20
+ };
21
+ request.onerror = function () {
22
+ var _a;
23
+ reject("IndexedDB open request error: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message));
24
+ };
25
+ request.onsuccess = function (event) {
26
+ _this.db = event.target.result;
27
+ resolve(_this.db);
28
+ };
29
+ });
30
+ };
31
+ // 获取存储对象
32
+ IndexedDBStorage.prototype._getStore = function (mode) {
33
+ var _this = this;
34
+ if (mode === void 0) { mode = 'readonly'; }
35
+ return this._open().then(function (db) {
36
+ var transaction = db.transaction(_this.storeName, mode);
37
+ return transaction.objectStore(_this.storeName);
38
+ });
39
+ };
40
+ // 设置键值对
41
+ IndexedDBStorage.prototype.setItem = function (key, value) {
42
+ return this._getStore('readwrite').then(function (store) {
43
+ return new Promise(function (resolve, reject) {
44
+ var request = store.put({ key: key, value: value });
45
+ request.onsuccess = function () { return resolve(); };
46
+ request.onerror = function () { var _a; return reject("Could not set the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
47
+ });
48
+ });
49
+ };
50
+ // 获取键对应的值
51
+ IndexedDBStorage.prototype.getItem = function (key) {
52
+ return this._getStore().then(function (store) {
53
+ return new Promise(function (resolve, reject) {
54
+ var request = store.get(key);
55
+ request.onsuccess = function () {
56
+ return resolve(request.result ? request.result.value : undefined);
57
+ };
58
+ request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
59
+ });
60
+ });
61
+ };
62
+ return IndexedDBStorage;
63
+ }());
64
+
65
+ export { IndexedDBStorage };
@@ -0,0 +1,56 @@
1
+ import { useState, useLayoutEffect, useEffect } from 'react';
2
+
3
+ function resolveHookState(nextState, currentState) {
4
+ if (typeof nextState === 'function') {
5
+ return nextState.length
6
+ ? nextState(currentState)
7
+ : nextState();
8
+ }
9
+ return nextState;
10
+ }
11
+ var isBrowser = typeof window !== 'undefined';
12
+ var useEffectOnce = function (effect) {
13
+ useEffect(effect, []);
14
+ };
15
+ var useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
16
+ function createStateStore(initialState) {
17
+ var store = {
18
+ state: initialState instanceof Function ? initialState() : initialState,
19
+ setState: function (nextState) {
20
+ store.state = resolveHookState(nextState, store.state);
21
+ store.setters.forEach(function (setter) { return setter(store.state); });
22
+ store.watchers.forEach(function (watcher) { return watcher(store.state); });
23
+ },
24
+ setters: [],
25
+ watchers: [],
26
+ };
27
+ var use = function () {
28
+ var _a = useState(store.state), globalState = _a[0], stateSetter = _a[1];
29
+ useEffectOnce(function () { return function () {
30
+ store.setters = store.setters.filter(function (setter) { return setter !== stateSetter; });
31
+ }; });
32
+ useIsomorphicLayoutEffect(function () {
33
+ if (!store.setters.includes(stateSetter)) {
34
+ store.setters.push(stateSetter);
35
+ }
36
+ });
37
+ return [globalState, store.setState];
38
+ };
39
+ var get = function () { return store.state; };
40
+ var set = store.setState;
41
+ var watch = function (callback) {
42
+ store.watchers.push(callback);
43
+ var close = function () {
44
+ store.watchers = store.watchers.filter(function (watcher) { return watcher !== callback; });
45
+ };
46
+ return close;
47
+ };
48
+ return {
49
+ use: use,
50
+ get: get,
51
+ set: set,
52
+ watch: watch,
53
+ };
54
+ }
55
+
56
+ export { createStateStore as default, resolveHookState };
@@ -0,0 +1,5 @@
1
+ function defaultEquals(prev, next) {
2
+ return JSON.stringify(prev) === JSON.stringify(next);
3
+ }
4
+
5
+ export { defaultEquals as default };
@@ -0,0 +1,3 @@
1
+ export { default as Cache } from './cache/index.mjs';
2
+ export { default as createBaseRequest } from './request/index.mjs';
3
+ export { default as createStateStore } from './createStateStore/index.mjs';
@@ -0,0 +1,11 @@
1
+ function _defaultErrorCodeHandler(defaultMessageShower, code) {
2
+ defaultMessageShower("\u8BF7\u6C42\u51FA\u9519\uFF0C\u9519\u8BEF\u7801\uFF1A".concat(code, "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
3
+ }
4
+ function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
5
+ defaultMessageShower("\u670D\u52A1\u7AEF\u8BF7\u6C42\u51FA\u9519\uFF0CHttp\u9519\u8BEF\u7801\uFF1A".concat(String(code), "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
6
+ }
7
+ function _defaultOtherErrorCodeHandler(defaultMessageShower, error) {
8
+ defaultMessageShower("\u672A\u77E5\u8BF7\u6C42\u51FA\u9519\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5");
9
+ }
10
+
11
+ export { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler };
@@ -0,0 +1,113 @@
1
+ import { __assign } from 'C:\\Users\\ruixutong\\code\\RUtils\\node_modules\\tslib\\tslib.es6.js';
2
+ import axios from 'axios';
3
+ import { at } from 'lodash';
4
+ import { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler } from './defaultHandlers.mjs';
5
+ import defaultEquals from '../defaultEquals.mjs';
6
+ import Cache from '../cache/index.mjs';
7
+
8
+ function createBaseRequest(baseOptions) {
9
+ var baseURL = Object(baseOptions).baseURL;
10
+ // 创建新的 Axios 实例
11
+ var instance = axios.create({
12
+ baseURL: baseURL,
13
+ withCredentials: true,
14
+ });
15
+ return function createRequest(requestOptions, createOptions) {
16
+ var _a = __assign({}, requestOptions), method = _a.method, url = _a.url;
17
+ var _b = __assign(__assign({}, baseOptions), createOptions), baseURL = _b.baseURL, _c = _b.cacheDataKey, cacheDataKey = _c === void 0 ? "".concat(method, ":").concat(baseURL).concat(url) : _c, cacheDataInStorage = _b.cacheDataInStorage, _d = _b.cacheKeyEquals, cacheKeyEquals = _d === void 0 ? defaultEquals : _d, cacheTime = _b.cacheTime, _e = _b.indexDBName, indexDBName = _e === void 0 ? '__apiCacheDatabase__' : _e;
18
+ var cache = new Cache(cacheDataInStorage, cacheDataKey, cacheTime, indexDBName, cacheKeyEquals);
19
+ function request(requestParam, options) {
20
+ var _a = __assign(__assign({}, requestOptions), requestParam), method = _a.method, url = _a.url, _b = _a.data, data = _b === void 0 ? {} : _b, _c = _a.params, params = _c === void 0 ? {} : _c;
21
+ var _d = __assign(__assign(__assign({}, baseOptions), createOptions), options).defaultMessageShower, defaultMessageShower = _d === void 0 ? alert : _d;
22
+ var _e = __assign(__assign(__assign({}, baseOptions), createOptions), options), baseURL = _e.baseURL, _f = _e.enableCache, enableCache = _f === void 0 ? false : _f, _g = _e.cacheData, cacheData = _g === void 0 ? false : _g, _h = _e.defaultErrorCodeHandler, defaultErrorCodeHandler = _h === void 0 ? _defaultErrorCodeHandler.bind(null, defaultMessageShower) : _h, _j = _e.defaultHttpErrorCodeHandler, defaultHttpErrorCodeHandler = _j === void 0 ? _defaultHttpErrorCodeHandler.bind(null, defaultMessageShower) : _j, _k = _e.otherErrorHandler, otherErrorHandler = _k === void 0 ? _defaultOtherErrorCodeHandler.bind(null, defaultMessageShower) : _k, _l = _e.errorCodePath, errorCodePath = _l === void 0 ? 'code' : _l, _m = _e.cacheTime, cacheTime = _m === void 0 ? 60 : _m, _o = _e.errorCodeMap, errorCodeMap = _o === void 0 ? {} : _o, _p = _e.successCodes, successCodes = _p === void 0 ? ['0', '200'] : _p, _q = _e.httpErrorCodeMap, httpErrorCodeMap = _q === void 0 ? {} : _q, _r = _e.axiosOptions, axiosOptions = _r === void 0 ? {} : _r, _s = _e.throwError, throwError = _s === void 0 ? true : _s;
23
+ if (enableCache) {
24
+ var cacheItem = cache.getCache(params);
25
+ if (cacheItem) {
26
+ return Promise.resolve(cacheItem);
27
+ }
28
+ }
29
+ return instance
30
+ .request(__assign({ method: method, url: url, data: data, params: params, baseURL: baseURL }, axiosOptions))
31
+ .then(function (res) {
32
+ var errorCode = String(at(res.data, errorCodePath));
33
+ if (successCodes.includes(errorCode)) {
34
+ if (cacheData) {
35
+ cache.setCache(params, res.data, { cacheTime: cacheTime });
36
+ }
37
+ return res.data;
38
+ }
39
+ var _a = errorCodeMap, _b = errorCode, _c = _a[_b], customHandler = _c === void 0 ? defaultErrorCodeHandler : _c;
40
+ var err = new Error('服务端错误');
41
+ err.type = 'server';
42
+ err.data = res;
43
+ if (typeof customHandler === 'string') {
44
+ defaultMessageShower(customHandler);
45
+ }
46
+ else {
47
+ var _d = (Object(customHandler(errorCode, res.data, res, __assign(__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? res.data : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
48
+ res.data = replaceResData;
49
+ switch (handlerThrowError) {
50
+ case true:
51
+ throw err;
52
+ case false:
53
+ return res.data;
54
+ }
55
+ }
56
+ if (throwError) {
57
+ throw err;
58
+ }
59
+ return res.data;
60
+ }, function (error) {
61
+ if (error.response) {
62
+ // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
63
+ var resData = error;
64
+ var _a = httpErrorCodeMap, _b = error.response.status, _c = _a[_b], customHandler = _c === void 0 ? defaultHttpErrorCodeHandler : _c;
65
+ var err = new Error('服务端错误');
66
+ err.type = 'http';
67
+ err.data = error;
68
+ if (typeof customHandler === 'string') {
69
+ message.error(customHandler);
70
+ }
71
+ else {
72
+ var _d = (Object(customHandler(error.response.status, error, __assign(__assign({}, requestOptions), requestParam)))), _e = _d.replaceResData, replaceResData = _e === void 0 ? error : _e, _f = _d.throwError, handlerThrowError = _f === void 0 ? 'default' : _f;
73
+ resData = replaceResData;
74
+ switch (handlerThrowError) {
75
+ case true:
76
+ throw err;
77
+ case false:
78
+ return resData;
79
+ }
80
+ }
81
+ if (throwError) {
82
+ throw err;
83
+ }
84
+ return resData;
85
+ }
86
+ else {
87
+ var resData = error;
88
+ var err = new Error('服务端错误');
89
+ err.type = 'http';
90
+ err.data = error;
91
+ var _g = (Object(otherErrorHandler(error))), _h = _g.replaceResData, replaceResData = _h === void 0 ? error : _h, _j = _g.throwError, handlerThrowError = _j === void 0 ? 'default' : _j;
92
+ resData = replaceResData;
93
+ switch (handlerThrowError) {
94
+ case true:
95
+ throw err;
96
+ case false:
97
+ return resData;
98
+ }
99
+ if (throwError) {
100
+ throw err;
101
+ }
102
+ return resData;
103
+ }
104
+ });
105
+ }
106
+ request.clearCache = function () {
107
+ cache.clear();
108
+ };
109
+ return request;
110
+ };
111
+ }
112
+
113
+ export { createBaseRequest as default };
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "rxtutils",
3
- "version": "1.0.5-beta.9",
4
- "main": "cjs/index.cjs",
5
- "module": "es/index.mjs",
6
- "types": "types/index.d.ts",
3
+ "version": "1.0.6",
4
+ "main": "dist/cjs/index.cjs",
5
+ "module": "dist/es/index.mjs",
6
+ "types": "dist/types/index.d.ts",
7
7
  "files": [
8
- "cjs",
9
- "es",
10
- "types"
8
+ "dist"
11
9
  ],
12
10
  "scripts": {
13
11
  "build": "rollup -c",
@@ -18,6 +16,7 @@
18
16
  "@types/node": "^22.15.29",
19
17
  "rollup": "^4.41.1",
20
18
  "rollup-plugin-dts": "^6.2.1",
19
+ "tslib": "^2.8.1",
21
20
  "typescript": "^5.8.3",
22
21
  "vite": "^5.4.19"
23
22
  },
@@ -1,31 +0,0 @@
1
- import { IndexedDBStorage } from './indexDB.cjs';
2
-
3
- type StorageType = 'sessionStorage' | 'localStorage' | 'indexedDB';
4
- interface ICache<Param, Data> {
5
- params: Param;
6
- data: Data;
7
- expireTime: string;
8
- }
9
- interface ICacheOptions<Param> {
10
- storageType?: StorageType;
11
- cacheKey?: string;
12
- cacheTime?: number;
13
- cacheKeyEquals: (prev: Param, next: Param) => boolean;
14
- indexDBName?: string;
15
- }
16
- declare const StorageMap: Record<StorageType | string, Storage>;
17
- declare class Cache<Param, Data> {
18
- cache: ICache<Param, Data>[];
19
- private cacheOptions;
20
- storage?: Storage | IndexedDBStorage;
21
- constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
22
- private _init;
23
- private _filterExpired;
24
- private _saveToStorage;
25
- setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, 'storageType' | 'cacheKey' | 'cacheKeyEquals'>): void;
26
- getCache(params: Param): Data;
27
- clear(): void;
28
- }
29
-
30
- export { StorageMap, Cache as default };
31
- export type { ICache, ICacheOptions, StorageType };
@@ -1,12 +0,0 @@
1
- declare class IndexedDBStorage {
2
- private dbName;
3
- private storeName;
4
- private db;
5
- constructor(dbName: string, storeName: string);
6
- private _open;
7
- private _getStore;
8
- setItem<T>(key: string, value: T): Promise<void>;
9
- getItem<T = any>(key: string): Promise<T>;
10
- }
11
-
12
- export { IndexedDBStorage };
@@ -1,14 +0,0 @@
1
- type IHookStateInitialSetter<S> = () => S;
2
- type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
3
- type IHookStateSetter<S> = ((prevState: S) => S) | (() => S);
4
- type IHookStateSetAction<S> = S | IHookStateSetter<S>;
5
- type IHookStateResolvable<S> = S | IHookStateInitialSetter<S> | IHookStateSetter<S>;
6
- declare function createStateStore<S>(initialState?: S): {
7
- use: () => [S, (state: IHookStateSetAction<S>) => void];
8
- get: () => S;
9
- set: (state: IHookStateSetAction<S>) => void;
10
- watch: (callback: (state: S) => S | void) => () => void;
11
- };
12
-
13
- export { createStateStore as default };
14
- export type { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter };
package/cjs/index.cjs DELETED
@@ -1,3 +0,0 @@
1
- export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.cjs';
2
- export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.cjs';
3
- export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.cjs';
@@ -1,40 +0,0 @@
1
- import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
2
- import { StorageType } from '../cache/index.cjs';
3
-
4
- type ErrorHandlerReturnType<D> = {
5
- replaceResData?: D;
6
- throwError?: boolean | 'default';
7
- };
8
- interface Options<Params = any, Data = any> {
9
- baseURL?: string;
10
- throwError?: boolean;
11
- defaultMessageShower?: (message: string) => void;
12
- enableCache?: boolean;
13
- cacheKeyEquals?: (prev: Params, next: Params) => boolean;
14
- cacheData?: boolean;
15
- cacheTime?: number;
16
- cacheDataInStorage?: StorageType;
17
- cacheDataKey?: string;
18
- indexDBName?: string;
19
- errorCodePath?: string;
20
- errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
21
- defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => ErrorHandlerReturnType<Data> | void;
22
- successCodes?: string[];
23
- httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
24
- defaultHttpErrorCodeHandler?: (code: number, error: any) => ErrorHandlerReturnType<Data> | void;
25
- otherErrorHandler?: (error: any) => ErrorHandlerReturnType<Data> | void;
26
- axiosOptions?: Omit<AxiosRequestConfig<Params>, 'method' | 'url' | 'params' | 'data'>;
27
- }
28
- interface RequestOptions<Param> {
29
- method: Method;
30
- url: string;
31
- data?: Param;
32
- params?: Param;
33
- }
34
- declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
35
- (requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
36
- clearCache(): void;
37
- };
38
-
39
- export { createBaseRequest as default };
40
- export type { ErrorHandlerReturnType, Options, RequestOptions };
@@ -1,31 +0,0 @@
1
- import { IndexedDBStorage } from './indexDB.mjs';
2
-
3
- type StorageType = 'sessionStorage' | 'localStorage' | 'indexedDB';
4
- interface ICache<Param, Data> {
5
- params: Param;
6
- data: Data;
7
- expireTime: string;
8
- }
9
- interface ICacheOptions<Param> {
10
- storageType?: StorageType;
11
- cacheKey?: string;
12
- cacheTime?: number;
13
- cacheKeyEquals: (prev: Param, next: Param) => boolean;
14
- indexDBName?: string;
15
- }
16
- declare const StorageMap: Record<StorageType | string, Storage>;
17
- declare class Cache<Param, Data> {
18
- cache: ICache<Param, Data>[];
19
- private cacheOptions;
20
- storage?: Storage | IndexedDBStorage;
21
- constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
22
- private _init;
23
- private _filterExpired;
24
- private _saveToStorage;
25
- setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, 'storageType' | 'cacheKey' | 'cacheKeyEquals'>): void;
26
- getCache(params: Param): Data;
27
- clear(): void;
28
- }
29
-
30
- export { StorageMap, Cache as default };
31
- export type { ICache, ICacheOptions, StorageType };
@@ -1,12 +0,0 @@
1
- declare class IndexedDBStorage {
2
- private dbName;
3
- private storeName;
4
- private db;
5
- constructor(dbName: string, storeName: string);
6
- private _open;
7
- private _getStore;
8
- setItem<T>(key: string, value: T): Promise<void>;
9
- getItem<T = any>(key: string): Promise<T>;
10
- }
11
-
12
- export { IndexedDBStorage };
@@ -1,14 +0,0 @@
1
- type IHookStateInitialSetter<S> = () => S;
2
- type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
3
- type IHookStateSetter<S> = ((prevState: S) => S) | (() => S);
4
- type IHookStateSetAction<S> = S | IHookStateSetter<S>;
5
- type IHookStateResolvable<S> = S | IHookStateInitialSetter<S> | IHookStateSetter<S>;
6
- declare function createStateStore<S>(initialState?: S): {
7
- use: () => [S, (state: IHookStateSetAction<S>) => void];
8
- get: () => S;
9
- set: (state: IHookStateSetAction<S>) => void;
10
- watch: (callback: (state: S) => S | void) => () => void;
11
- };
12
-
13
- export { createStateStore as default };
14
- export type { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter };
package/es/index.mjs DELETED
@@ -1,3 +0,0 @@
1
- export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.mjs';
2
- export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.mjs';
3
- export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.mjs';
@@ -1,40 +0,0 @@
1
- import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
2
- import { StorageType } from '../cache/index.mjs';
3
-
4
- type ErrorHandlerReturnType<D> = {
5
- replaceResData?: D;
6
- throwError?: boolean | 'default';
7
- };
8
- interface Options<Params = any, Data = any> {
9
- baseURL?: string;
10
- throwError?: boolean;
11
- defaultMessageShower?: (message: string) => void;
12
- enableCache?: boolean;
13
- cacheKeyEquals?: (prev: Params, next: Params) => boolean;
14
- cacheData?: boolean;
15
- cacheTime?: number;
16
- cacheDataInStorage?: StorageType;
17
- cacheDataKey?: string;
18
- indexDBName?: string;
19
- errorCodePath?: string;
20
- errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
21
- defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => ErrorHandlerReturnType<Data> | void;
22
- successCodes?: string[];
23
- httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
24
- defaultHttpErrorCodeHandler?: (code: number, error: any) => ErrorHandlerReturnType<Data> | void;
25
- otherErrorHandler?: (error: any) => ErrorHandlerReturnType<Data> | void;
26
- axiosOptions?: Omit<AxiosRequestConfig<Params>, 'method' | 'url' | 'params' | 'data'>;
27
- }
28
- interface RequestOptions<Param> {
29
- method: Method;
30
- url: string;
31
- data?: Param;
32
- params?: Param;
33
- }
34
- declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
35
- (requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
36
- clearCache(): void;
37
- };
38
-
39
- export { createBaseRequest as default };
40
- export type { ErrorHandlerReturnType, Options, RequestOptions };
File without changes
File without changes
File without changes
File without changes