@fetchx/fetcher-interceptor-biz 0.1.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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/intercept-biz.js +10 -0
- package/dist/cjs/types/common.js +5 -0
- package/dist/cjs/types/fetcher-config.js +5 -0
- package/dist/cjs/types/index.js +38 -0
- package/dist/cjs/types/intercept-options.js +5 -0
- package/dist/cjs/util/create-interceptor-response-fulfilled.js +40 -0
- package/dist/cjs/util/get-response-code.js +11 -0
- package/dist/cjs/util/get-response-data.js +15 -0
- package/dist/cjs/util/get-response-message.js +11 -0
- package/dist/cjs/util/get-response-title.js +11 -0
- package/dist/cjs/util/get-string-from-response.js +16 -0
- package/dist/cjs/util/index.js +13 -0
- package/dist/cjs/util/is-response-success.js +16 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/intercept-biz.js +5 -0
- package/dist/esm/intercept-biz.js.map +1 -0
- package/dist/esm/types/common.js +2 -0
- package/dist/esm/types/common.js.map +1 -0
- package/dist/esm/types/fetcher-config.js +2 -0
- package/dist/esm/types/fetcher-config.js.map +1 -0
- package/dist/esm/types/index.js +4 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/intercept-options.js +2 -0
- package/dist/esm/types/intercept-options.js.map +1 -0
- package/dist/esm/util/create-interceptor-response-fulfilled.js +35 -0
- package/dist/esm/util/create-interceptor-response-fulfilled.js.map +1 -0
- package/dist/esm/util/get-response-code.js +5 -0
- package/dist/esm/util/get-response-code.js.map +1 -0
- package/dist/esm/util/get-response-data.js +10 -0
- package/dist/esm/util/get-response-data.js.map +1 -0
- package/dist/esm/util/get-response-message.js +5 -0
- package/dist/esm/util/get-response-message.js.map +1 -0
- package/dist/esm/util/get-response-title.js +5 -0
- package/dist/esm/util/get-response-title.js.map +1 -0
- package/dist/esm/util/get-string-from-response.js +11 -0
- package/dist/esm/util/get-string-from-response.js.map +1 -0
- package/dist/esm/util/index.js +2 -0
- package/dist/esm/util/index.js.map +1 -0
- package/dist/esm/util/is-response-success.js +11 -0
- package/dist/esm/util/is-response-success.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/intercept-biz.d.ts +3 -0
- package/dist/types/types/common.d.ts +4 -0
- package/dist/types/types/fetcher-config.d.ts +45 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/intercept-options.d.ts +3 -0
- package/dist/types/util/create-interceptor-response-fulfilled.d.ts +7 -0
- package/dist/types/util/get-response-code.d.ts +2 -0
- package/dist/types/util/get-response-data.d.ts +2 -0
- package/dist/types/util/get-response-message.d.ts +2 -0
- package/dist/types/util/get-response-title.d.ts +2 -0
- package/dist/types/util/get-string-from-response.d.ts +2 -0
- package/dist/types/util/index.d.ts +1 -0
- package/dist/types/util/is-response-success.d.ts +2 -0
- package/package.json +58 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Alibaba Cloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "default", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function get() {
|
|
10
|
+
return _interceptBiz.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _interceptBiz = _interopRequireDefault(require("./intercept-biz"));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = interceptBiz;
|
|
7
|
+
var _util = require("./util");
|
|
8
|
+
function interceptBiz(fetcher, options, priority) {
|
|
9
|
+
return fetcher.interceptResponse((0, _util.createInterceptorResponseFulfilled)(options), undefined, priority);
|
|
10
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _common = require("./common");
|
|
7
|
+
Object.keys(_common).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _common[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function get() {
|
|
13
|
+
return _common[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _fetcherConfig = require("./fetcher-config");
|
|
18
|
+
Object.keys(_fetcherConfig).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _fetcherConfig[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function get() {
|
|
24
|
+
return _fetcherConfig[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _interceptOptions = require("./intercept-options");
|
|
29
|
+
Object.keys(_interceptOptions).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _interceptOptions[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function get() {
|
|
35
|
+
return _interceptOptions[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = createInterceptorResponseFulfilled;
|
|
8
|
+
var _isPlainObject2 = _interopRequireDefault(require("lodash/isPlainObject"));
|
|
9
|
+
var _fetcherCore = require("@fetchx/fetcher-core");
|
|
10
|
+
var _isResponseSuccess = _interopRequireDefault(require("./is-response-success"));
|
|
11
|
+
var _getResponseData = _interopRequireDefault(require("./get-response-data"));
|
|
12
|
+
var _getResponseCode = _interopRequireDefault(require("./get-response-code"));
|
|
13
|
+
var _getResponseTitle = _interopRequireDefault(require("./get-response-title"));
|
|
14
|
+
var _getResponseMessage = _interopRequireDefault(require("./get-response-message"));
|
|
15
|
+
/**
|
|
16
|
+
* 请求到这里,说明服务端有返回,但业务上不一定是成功的。
|
|
17
|
+
* 这里会判断业务是否成功,如果成功则返回从原屎返回中得出的真正的数据,如果失败在抛出 FetchErrorBiz。
|
|
18
|
+
*/
|
|
19
|
+
function createInterceptorResponseFulfilled(options) {
|
|
20
|
+
return function (o, config) {
|
|
21
|
+
var _config$getCode, _config$isSuccess, _config$getMessage, _config$getTitle;
|
|
22
|
+
if (!(0, _isPlainObject2.default)(o)) {
|
|
23
|
+
// 绕过非对象,比如 Blob、ArrayBuffer 等
|
|
24
|
+
return o;
|
|
25
|
+
}
|
|
26
|
+
var result = o;
|
|
27
|
+
var code = (0, _getResponseCode.default)(result, (_config$getCode = config.getCode) !== null && _config$getCode !== void 0 ? _config$getCode : options === null || options === void 0 ? void 0 : options.getCode) || '__UNKNOWN__';
|
|
28
|
+
var success = (0, _isResponseSuccess.default)(result, code, (_config$isSuccess = config.isSuccess) !== null && _config$isSuccess !== void 0 ? _config$isSuccess : options === null || options === void 0 ? void 0 : options.isSuccess);
|
|
29
|
+
if (success) {
|
|
30
|
+
var _config$getData;
|
|
31
|
+
return (0, _getResponseData.default)(result, (_config$getData = config.getData) !== null && _config$getData !== void 0 ? _config$getData : options === null || options === void 0 ? void 0 : options.getData);
|
|
32
|
+
}
|
|
33
|
+
throw (0, _fetcherCore.createFetcherError)(config, {
|
|
34
|
+
code: code,
|
|
35
|
+
name: _fetcherCore.FetcherErrorName.BIZ,
|
|
36
|
+
message: (0, _getResponseMessage.default)(result, (_config$getMessage = config.getMessage) !== null && _config$getMessage !== void 0 ? _config$getMessage : options === null || options === void 0 ? void 0 : options.getMessage),
|
|
37
|
+
title: (0, _getResponseTitle.default)(result, (_config$getTitle = config.getTitle) !== null && _config$getTitle !== void 0 ? _config$getTitle : options === null || options === void 0 ? void 0 : options.getTitle)
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = getResponseCode;
|
|
8
|
+
var _getStringFromResponse = _interopRequireDefault(require("./get-string-from-response"));
|
|
9
|
+
function getResponseCode(o, getter) {
|
|
10
|
+
return (0, _getStringFromResponse.default)(o, getter || 'code');
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getResponseData;
|
|
7
|
+
function getResponseData(o, getter) {
|
|
8
|
+
if (typeof getter === 'function') {
|
|
9
|
+
return getter(o);
|
|
10
|
+
}
|
|
11
|
+
if (typeof getter === 'string') {
|
|
12
|
+
return o[getter];
|
|
13
|
+
}
|
|
14
|
+
return o.data;
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = getResponseMessage;
|
|
8
|
+
var _getStringFromResponse = _interopRequireDefault(require("./get-string-from-response"));
|
|
9
|
+
function getResponseMessage(o, getter) {
|
|
10
|
+
return (0, _getStringFromResponse.default)(o, getter || 'message');
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = getResponseTitle;
|
|
8
|
+
var _getStringFromResponse = _interopRequireDefault(require("./get-string-from-response"));
|
|
9
|
+
function getResponseTitle(o, getter) {
|
|
10
|
+
return (0, _getStringFromResponse.default)(o, getter || 'title');
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getStringFromResponse;
|
|
7
|
+
function getStringFromResponse(o, getter) {
|
|
8
|
+
var value = typeof getter === 'function' ? getter(o) : o[getter];
|
|
9
|
+
if (typeof value === 'string') {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
if (typeof value === 'number') {
|
|
13
|
+
return String(value);
|
|
14
|
+
}
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "createInterceptorResponseFulfilled", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function get() {
|
|
10
|
+
return _createInterceptorResponseFulfilled.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _createInterceptorResponseFulfilled = _interopRequireDefault(require("./create-interceptor-response-fulfilled"));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = isResponseSuccess;
|
|
7
|
+
function isResponseSuccess(o, code) {
|
|
8
|
+
var checker = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '200';
|
|
9
|
+
if (typeof checker === 'boolean') {
|
|
10
|
+
return checker;
|
|
11
|
+
}
|
|
12
|
+
if (typeof checker === 'function') {
|
|
13
|
+
return checker(o);
|
|
14
|
+
}
|
|
15
|
+
return checker === code;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["default"],"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './intercept-biz';\n\nexport type {\n IFetcherConfigBiz as FetcherConfigBiz,\n IFetcherInterceptorBizOptions as FetcherInterceptorBizOptions\n} from './types';\n"],"mappings":"AAAA,SAASA,OAAO,QAAQ,iBAAiB","ignoreList":[]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { createInterceptorResponseFulfilled } from './util';
|
|
2
|
+
export default function interceptBiz(fetcher, options, priority) {
|
|
3
|
+
return fetcher.interceptResponse(createInterceptorResponseFulfilled(options), undefined, priority);
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=intercept-biz.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intercept-biz.js","names":["createInterceptorResponseFulfilled","interceptBiz","fetcher","options","priority","interceptResponse","undefined"],"sources":["../../src/intercept-biz.ts"],"sourcesContent":["import {\n Fetcher\n} from '@fetchx/fetcher-core';\n\nimport {\n IFetcherInterceptorBizOptions\n} from './types';\nimport {\n createInterceptorResponseFulfilled\n} from './util';\n\nexport default function interceptBiz(fetcher: Fetcher, options?: IFetcherInterceptorBizOptions, priority?: number): () => void {\n return fetcher.interceptResponse(createInterceptorResponseFulfilled(options), undefined, priority);\n}\n"],"mappings":"AAOA,SACEA,kCAAkC,QAC7B,QAAQ;AAEf,eAAe,SAASC,YAAYA,CAACC,OAAgB,EAAEC,OAAuC,EAAEC,QAAiB,EAAc;EAC7H,OAAOF,OAAO,CAACG,iBAAiB,CAACL,kCAAkC,CAACG,OAAO,CAAC,EAAEG,SAAS,EAAEF,QAAQ,CAAC;AACpG","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","names":[],"sources":["../../../src/types/common.ts"],"sourcesContent":["export type TResponseResult = Record<string, unknown>;\n\nexport type TIsSuccess = ((o: TResponseResult) => boolean) | string | boolean;\n\nexport type TGetString = ((o: TResponseResult) => string) | string;\n\nexport type TGetData<T = unknown> = ((o: TResponseResult) => T) | string;\n"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetcher-config.js","names":[],"sources":["../../../src/types/fetcher-config.ts"],"sourcesContent":["import {\n FetcherConfig\n} from '@fetchx/fetcher-core';\n\nimport {\n TIsSuccess,\n TGetString,\n TGetData\n} from './common';\n\n/**\n * 可以在调用 Fetcher 请求时,覆盖默认的设置以适应某些特殊的场景,从而避免必须多生 Fetcher 实例的尴尬\n */\nexport interface IFetcherConfigBiz {\n /**\n * 判断请求是否成功,默认 '200'(即 code 为 '200')\n *\n * - `string` 与 `getCode` 得到的结果对比\n * - `boolean` 直接成功或失败\n * - `json => boolean` 根据原始 json 对象进行自定义判断\n */\n isSuccess?: TIsSuccess;\n /**\n * 提取最终需要的数据,默认 'data'\n *\n * - `string` 自定义数据字段,如 'DATA' 则表示获取 `json.DATA`\n * - `json => unknown` 从原始 json 对象进行自定义提取\n */\n getData?: TGetData;\n /**\n * 从数据中提取 code,默认 'code'\n *\n * - `string` 自定义数据字段,如 'CODE' 则表示获取 `json.CODE`\n * - `json => string` 从原始 json 对象进行自定义提取\n */\n getCode?: TGetString;\n /**\n * 从数据中提取错误 title,默认 'title'\n *\n * - `string` 自定义数据字段,如 'TITLE' 则表示获取 `json.TITLE`\n * - `json => string` 从原始 json 对象进行自定义提取\n */\n getTitle?: TGetString;\n /**\n * 从数据中提取错误信息,默认 'message'\n *\n * - `string` 自定义数据字段,如 'MESSAGE' 则表示获取 `json.MESSAGE`\n * - `json => string` 从原始 json 对象进行自定义提取\n */\n getMessage?: TGetString;\n}\n\nexport interface IFetcherConfigAugmentedBiz extends FetcherConfig, IFetcherConfigBiz {}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/types/index.ts"],"sourcesContent":["export * from './common';\nexport * from './fetcher-config';\nexport * from './intercept-options';\n"],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,kBAAkB;AAChC,cAAc,qBAAqB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intercept-options.js","names":[],"sources":["../../../src/types/intercept-options.ts"],"sourcesContent":["import {\n IFetcherConfigBiz\n} from './fetcher-config';\n\nexport interface IFetcherInterceptorBizOptions extends IFetcherConfigBiz {}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import _isPlainObject from 'lodash/isPlainObject';
|
|
2
|
+
import { FetcherErrorName, createFetcherError } from '@fetchx/fetcher-core';
|
|
3
|
+
import isResponseSuccess from './is-response-success';
|
|
4
|
+
import getResponseData from './get-response-data';
|
|
5
|
+
import getResponseCode from './get-response-code';
|
|
6
|
+
import getResponseTitle from './get-response-title';
|
|
7
|
+
import getResponseMessage from './get-response-message';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 请求到这里,说明服务端有返回,但业务上不一定是成功的。
|
|
11
|
+
* 这里会判断业务是否成功,如果成功则返回从原屎返回中得出的真正的数据,如果失败在抛出 FetchErrorBiz。
|
|
12
|
+
*/
|
|
13
|
+
export default function createInterceptorResponseFulfilled(options) {
|
|
14
|
+
return function (o, config) {
|
|
15
|
+
var _config$getCode, _config$isSuccess, _config$getMessage, _config$getTitle;
|
|
16
|
+
if (!_isPlainObject(o)) {
|
|
17
|
+
// 绕过非对象,比如 Blob、ArrayBuffer 等
|
|
18
|
+
return o;
|
|
19
|
+
}
|
|
20
|
+
var result = o;
|
|
21
|
+
var code = getResponseCode(result, (_config$getCode = config.getCode) !== null && _config$getCode !== void 0 ? _config$getCode : options === null || options === void 0 ? void 0 : options.getCode) || '__UNKNOWN__';
|
|
22
|
+
var success = isResponseSuccess(result, code, (_config$isSuccess = config.isSuccess) !== null && _config$isSuccess !== void 0 ? _config$isSuccess : options === null || options === void 0 ? void 0 : options.isSuccess);
|
|
23
|
+
if (success) {
|
|
24
|
+
var _config$getData;
|
|
25
|
+
return getResponseData(result, (_config$getData = config.getData) !== null && _config$getData !== void 0 ? _config$getData : options === null || options === void 0 ? void 0 : options.getData);
|
|
26
|
+
}
|
|
27
|
+
throw createFetcherError(config, {
|
|
28
|
+
code: code,
|
|
29
|
+
name: FetcherErrorName.BIZ,
|
|
30
|
+
message: getResponseMessage(result, (_config$getMessage = config.getMessage) !== null && _config$getMessage !== void 0 ? _config$getMessage : options === null || options === void 0 ? void 0 : options.getMessage),
|
|
31
|
+
title: getResponseTitle(result, (_config$getTitle = config.getTitle) !== null && _config$getTitle !== void 0 ? _config$getTitle : options === null || options === void 0 ? void 0 : options.getTitle)
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=create-interceptor-response-fulfilled.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-interceptor-response-fulfilled.js","names":["_isPlainObject","FetcherErrorName","createFetcherError","isResponseSuccess","getResponseData","getResponseCode","getResponseTitle","getResponseMessage","createInterceptorResponseFulfilled","options","o","config","_config$getCode","_config$isSuccess","_config$getMessage","_config$getTitle","result","code","getCode","success","isSuccess","_config$getData","getData","name","BIZ","message","getMessage","title","getTitle"],"sources":["../../../src/util/create-interceptor-response-fulfilled.ts"],"sourcesContent":["import _isPlainObject from 'lodash/isPlainObject';\n\nimport {\n FetcherErrorName,\n FetcherInterceptResponseFulfilled,\n createFetcherError\n} from '@fetchx/fetcher-core';\n\nimport {\n TResponseResult,\n IFetcherInterceptorBizOptions,\n IFetcherConfigAugmentedBiz\n} from '../types';\n\nimport isResponseSuccess from './is-response-success';\nimport getResponseData from './get-response-data';\nimport getResponseCode from './get-response-code';\nimport getResponseTitle from './get-response-title';\nimport getResponseMessage from './get-response-message';\n\n/**\n * 请求到这里,说明服务端有返回,但业务上不一定是成功的。\n * 这里会判断业务是否成功,如果成功则返回从原屎返回中得出的真正的数据,如果失败在抛出 FetchErrorBiz。\n */\nexport default function createInterceptorResponseFulfilled(options?: IFetcherInterceptorBizOptions): FetcherInterceptResponseFulfilled {\n return (o: unknown, config: IFetcherConfigAugmentedBiz): unknown => {\n if (!_isPlainObject(o)) { // 绕过非对象,比如 Blob、ArrayBuffer 等\n return o;\n }\n \n const result = o as TResponseResult;\n const code = getResponseCode(result, config.getCode ?? options?.getCode) || '__UNKNOWN__';\n const success = isResponseSuccess(result, code, config.isSuccess ?? options?.isSuccess);\n \n if (success) {\n return getResponseData(result, config.getData ?? options?.getData);\n }\n \n throw createFetcherError(config, {\n code,\n name: FetcherErrorName.BIZ,\n message: getResponseMessage(result, config.getMessage ?? options?.getMessage),\n title: getResponseTitle(result, config.getTitle ?? options?.getTitle)\n });\n };\n}\n"],"mappings":"AAAA,OAAOA,cAAc,MAAM,sBAAsB;AAEjD,SACEC,gBAAgB,EAEhBC,kBAAkB,QACb,sBAAsB;AAQ7B,OAAOC,iBAAiB,MAAM,uBAAuB;AACrD,OAAOC,eAAe,MAAM,qBAAqB;AACjD,OAAOC,eAAe,MAAM,qBAAqB;AACjD,OAAOC,gBAAgB,MAAM,sBAAsB;AACnD,OAAOC,kBAAkB,MAAM,wBAAwB;;AAEvD;AACA;AACA;AACA;AACA,eAAe,SAASC,kCAAkCA,CAACC,OAAuC,EAAqC;EACrI,OAAO,UAACC,CAAU,EAAEC,MAAkC,EAAc;IAAA,IAAAC,eAAA,EAAAC,iBAAA,EAAAC,kBAAA,EAAAC,gBAAA;IAClE,IAAI,CAACf,cAAc,CAACU,CAAC,CAAC,EAAE;MAAE;MACxB,OAAOA,CAAC;IACV;IAEA,IAAMM,MAAM,GAAGN,CAAoB;IACnC,IAAMO,IAAI,GAAGZ,eAAe,CAACW,MAAM,GAAAJ,eAAA,GAAED,MAAM,CAACO,OAAO,cAAAN,eAAA,cAAAA,eAAA,GAAIH,OAAO,aAAPA,OAAO,uBAAPA,OAAO,CAAES,OAAO,CAAC,IAAI,aAAa;IACzF,IAAMC,OAAO,GAAGhB,iBAAiB,CAACa,MAAM,EAAEC,IAAI,GAAAJ,iBAAA,GAAEF,MAAM,CAACS,SAAS,cAAAP,iBAAA,cAAAA,iBAAA,GAAIJ,OAAO,aAAPA,OAAO,uBAAPA,OAAO,CAAEW,SAAS,CAAC;IAEvF,IAAID,OAAO,EAAE;MAAA,IAAAE,eAAA;MACX,OAAOjB,eAAe,CAACY,MAAM,GAAAK,eAAA,GAAEV,MAAM,CAACW,OAAO,cAAAD,eAAA,cAAAA,eAAA,GAAIZ,OAAO,aAAPA,OAAO,uBAAPA,OAAO,CAAEa,OAAO,CAAC;IACpE;IAEA,MAAMpB,kBAAkB,CAACS,MAAM,EAAE;MAC/BM,IAAI,EAAJA,IAAI;MACJM,IAAI,EAAEtB,gBAAgB,CAACuB,GAAG;MAC1BC,OAAO,EAAElB,kBAAkB,CAACS,MAAM,GAAAF,kBAAA,GAAEH,MAAM,CAACe,UAAU,cAAAZ,kBAAA,cAAAA,kBAAA,GAAIL,OAAO,aAAPA,OAAO,uBAAPA,OAAO,CAAEiB,UAAU,CAAC;MAC7EC,KAAK,EAAErB,gBAAgB,CAACU,MAAM,GAAAD,gBAAA,GAAEJ,MAAM,CAACiB,QAAQ,cAAAb,gBAAA,cAAAA,gBAAA,GAAIN,OAAO,aAAPA,OAAO,uBAAPA,OAAO,CAAEmB,QAAQ;IACtE,CAAC,CAAC;EACJ,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-response-code.js","names":["getStringFromResponse","getResponseCode","o","getter"],"sources":["../../../src/util/get-response-code.ts"],"sourcesContent":["import {\n TResponseResult,\n TGetString\n} from '../types';\n\nimport getStringFromResponse from './get-string-from-response';\n\nexport default function getResponseCode(o: TResponseResult, getter?: TGetString): string {\n return getStringFromResponse(o, getter || 'code');\n}\n"],"mappings":"AAKA,OAAOA,qBAAqB,MAAM,4BAA4B;AAE9D,eAAe,SAASC,eAAeA,CAACC,CAAkB,EAAEC,MAAmB,EAAU;EACvF,OAAOH,qBAAqB,CAACE,CAAC,EAAEC,MAAM,IAAI,MAAM,CAAC;AACnD","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-response-data.js","names":["getResponseData","o","getter","data"],"sources":["../../../src/util/get-response-data.ts"],"sourcesContent":["import {\n TResponseResult,\n TGetData\n} from '../types';\n\nexport default function getResponseData<T = unknown>(o: TResponseResult, getter?: TGetData<T>): T {\n if (typeof getter === 'function') {\n return getter(o);\n }\n \n if (typeof getter === 'string') {\n return o[getter] as T;\n }\n \n return o.data as T;\n}\n"],"mappings":"AAKA,eAAe,SAASA,eAAeA,CAAcC,CAAkB,EAAEC,MAAoB,EAAK;EAChG,IAAI,OAAOA,MAAM,KAAK,UAAU,EAAE;IAChC,OAAOA,MAAM,CAACD,CAAC,CAAC;EAClB;EAEA,IAAI,OAAOC,MAAM,KAAK,QAAQ,EAAE;IAC9B,OAAOD,CAAC,CAACC,MAAM,CAAC;EAClB;EAEA,OAAOD,CAAC,CAACE,IAAI;AACf","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-response-message.js","names":["getStringFromResponse","getResponseMessage","o","getter"],"sources":["../../../src/util/get-response-message.ts"],"sourcesContent":["import {\n TResponseResult,\n TGetString\n} from '../types';\n\nimport getStringFromResponse from './get-string-from-response';\n\nexport default function getResponseMessage(o: TResponseResult, getter?: TGetString): string {\n return getStringFromResponse(o, getter || 'message');\n}\n"],"mappings":"AAKA,OAAOA,qBAAqB,MAAM,4BAA4B;AAE9D,eAAe,SAASC,kBAAkBA,CAACC,CAAkB,EAAEC,MAAmB,EAAU;EAC1F,OAAOH,qBAAqB,CAACE,CAAC,EAAEC,MAAM,IAAI,SAAS,CAAC;AACtD","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-response-title.js","names":["getStringFromResponse","getResponseTitle","o","getter"],"sources":["../../../src/util/get-response-title.ts"],"sourcesContent":["import {\n TResponseResult,\n TGetString\n} from '../types';\n\nimport getStringFromResponse from './get-string-from-response';\n\nexport default function getResponseTitle(o: TResponseResult, getter?: TGetString): string {\n return getStringFromResponse(o, getter || 'title');\n}\n"],"mappings":"AAKA,OAAOA,qBAAqB,MAAM,4BAA4B;AAE9D,eAAe,SAASC,gBAAgBA,CAACC,CAAkB,EAAEC,MAAmB,EAAU;EACxF,OAAOH,qBAAqB,CAACE,CAAC,EAAEC,MAAM,IAAI,OAAO,CAAC;AACpD","ignoreList":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function getStringFromResponse(o, getter) {
|
|
2
|
+
var value = typeof getter === 'function' ? getter(o) : o[getter];
|
|
3
|
+
if (typeof value === 'string') {
|
|
4
|
+
return value;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === 'number') {
|
|
7
|
+
return String(value);
|
|
8
|
+
}
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=get-string-from-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-string-from-response.js","names":["getStringFromResponse","o","getter","value","String"],"sources":["../../../src/util/get-string-from-response.ts"],"sourcesContent":["import {\n TResponseResult,\n TGetString\n} from '../types';\n\nexport default function getStringFromResponse(o: TResponseResult, getter: TGetString): string {\n const value = typeof getter === 'function' ? getter(o) : o[getter];\n \n if (typeof value === 'string') {\n return value;\n }\n \n if (typeof value === 'number') {\n return String(value);\n }\n \n return '';\n}\n"],"mappings":"AAKA,eAAe,SAASA,qBAAqBA,CAACC,CAAkB,EAAEC,MAAkB,EAAU;EAC5F,IAAMC,KAAK,GAAG,OAAOD,MAAM,KAAK,UAAU,GAAGA,MAAM,CAACD,CAAC,CAAC,GAAGA,CAAC,CAACC,MAAM,CAAC;EAElE,IAAI,OAAOC,KAAK,KAAK,QAAQ,EAAE;IAC7B,OAAOA,KAAK;EACd;EAEA,IAAI,OAAOA,KAAK,KAAK,QAAQ,EAAE;IAC7B,OAAOC,MAAM,CAACD,KAAK,CAAC;EACtB;EAEA,OAAO,EAAE;AACX","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["default","createInterceptorResponseFulfilled"],"sources":["../../../src/util/index.ts"],"sourcesContent":["export { default as createInterceptorResponseFulfilled } from './create-interceptor-response-fulfilled';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,kCAAkC,QAAQ,yCAAyC","ignoreList":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function isResponseSuccess(o, code) {
|
|
2
|
+
var checker = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '200';
|
|
3
|
+
if (typeof checker === 'boolean') {
|
|
4
|
+
return checker;
|
|
5
|
+
}
|
|
6
|
+
if (typeof checker === 'function') {
|
|
7
|
+
return checker(o);
|
|
8
|
+
}
|
|
9
|
+
return checker === code;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=is-response-success.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-response-success.js","names":["isResponseSuccess","o","code","checker","arguments","length","undefined"],"sources":["../../../src/util/is-response-success.ts"],"sourcesContent":["import {\n TResponseResult,\n TIsSuccess\n} from '../types';\n\nexport default function isResponseSuccess(o: TResponseResult, code: string, checker: TIsSuccess = '200'): boolean {\n if (typeof checker === 'boolean') {\n return checker;\n }\n \n if (typeof checker === 'function') {\n return checker(o);\n }\n \n return checker === code;\n}\n"],"mappings":"AAKA,eAAe,SAASA,iBAAiBA,CAACC,CAAkB,EAAEC,IAAY,EAAwC;EAAA,IAAtCC,OAAmB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,KAAK;EACrG,IAAI,OAAOD,OAAO,KAAK,SAAS,EAAE;IAChC,OAAOA,OAAO;EAChB;EAEA,IAAI,OAAOA,OAAO,KAAK,UAAU,EAAE;IACjC,OAAOA,OAAO,CAACF,CAAC,CAAC;EACnB;EAEA,OAAOE,OAAO,KAAKD,IAAI;AACzB","ignoreList":[]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type TResponseResult = Record<string, unknown>;
|
|
2
|
+
export type TIsSuccess = ((o: TResponseResult) => boolean) | string | boolean;
|
|
3
|
+
export type TGetString = ((o: TResponseResult) => string) | string;
|
|
4
|
+
export type TGetData<T = unknown> = ((o: TResponseResult) => T) | string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { FetcherConfig } from '@fetchx/fetcher-core';
|
|
2
|
+
import { TIsSuccess, TGetString, TGetData } from './common';
|
|
3
|
+
/**
|
|
4
|
+
* 可以在调用 Fetcher 请求时,覆盖默认的设置以适应某些特殊的场景,从而避免必须多生 Fetcher 实例的尴尬
|
|
5
|
+
*/
|
|
6
|
+
export interface IFetcherConfigBiz {
|
|
7
|
+
/**
|
|
8
|
+
* 判断请求是否成功,默认 '200'(即 code 为 '200')
|
|
9
|
+
*
|
|
10
|
+
* - `string` 与 `getCode` 得到的结果对比
|
|
11
|
+
* - `boolean` 直接成功或失败
|
|
12
|
+
* - `json => boolean` 根据原始 json 对象进行自定义判断
|
|
13
|
+
*/
|
|
14
|
+
isSuccess?: TIsSuccess;
|
|
15
|
+
/**
|
|
16
|
+
* 提取最终需要的数据,默认 'data'
|
|
17
|
+
*
|
|
18
|
+
* - `string` 自定义数据字段,如 'DATA' 则表示获取 `json.DATA`
|
|
19
|
+
* - `json => unknown` 从原始 json 对象进行自定义提取
|
|
20
|
+
*/
|
|
21
|
+
getData?: TGetData;
|
|
22
|
+
/**
|
|
23
|
+
* 从数据中提取 code,默认 'code'
|
|
24
|
+
*
|
|
25
|
+
* - `string` 自定义数据字段,如 'CODE' 则表示获取 `json.CODE`
|
|
26
|
+
* - `json => string` 从原始 json 对象进行自定义提取
|
|
27
|
+
*/
|
|
28
|
+
getCode?: TGetString;
|
|
29
|
+
/**
|
|
30
|
+
* 从数据中提取错误 title,默认 'title'
|
|
31
|
+
*
|
|
32
|
+
* - `string` 自定义数据字段,如 'TITLE' 则表示获取 `json.TITLE`
|
|
33
|
+
* - `json => string` 从原始 json 对象进行自定义提取
|
|
34
|
+
*/
|
|
35
|
+
getTitle?: TGetString;
|
|
36
|
+
/**
|
|
37
|
+
* 从数据中提取错误信息,默认 'message'
|
|
38
|
+
*
|
|
39
|
+
* - `string` 自定义数据字段,如 'MESSAGE' 则表示获取 `json.MESSAGE`
|
|
40
|
+
* - `json => string` 从原始 json 对象进行自定义提取
|
|
41
|
+
*/
|
|
42
|
+
getMessage?: TGetString;
|
|
43
|
+
}
|
|
44
|
+
export interface IFetcherConfigAugmentedBiz extends FetcherConfig, IFetcherConfigBiz {
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FetcherInterceptResponseFulfilled } from '@fetchx/fetcher-core';
|
|
2
|
+
import { IFetcherInterceptorBizOptions } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* 请求到这里,说明服务端有返回,但业务上不一定是成功的。
|
|
5
|
+
* 这里会判断业务是否成功,如果成功则返回从原屎返回中得出的真正的数据,如果失败在抛出 FetchErrorBiz。
|
|
6
|
+
*/
|
|
7
|
+
export default function createInterceptorResponseFulfilled(options?: IFetcherInterceptorBizOptions): FetcherInterceptResponseFulfilled;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as createInterceptorResponseFulfilled } from './create-interceptor-response-fulfilled';
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fetchx/fetcher-interceptor-biz",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fetcher interceptor to extract biz data and throw error.",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "dist/cjs/index.js",
|
|
9
|
+
"module": "dist/esm/index.js",
|
|
10
|
+
"types": "dist/types/index.d.ts",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/justnewbee/fetcher.git"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/justnewbee/fetcher/tree/main/packages-fetcher/fetcher-interceptor-biz",
|
|
16
|
+
"bugs": "https://github.com/justnewbee/fetcher/issues",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Jianchun Wang",
|
|
19
|
+
"email": "justnewbee@gmail.com"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/cli": "^7.29.7",
|
|
26
|
+
"@babel/core": "^7.29.7",
|
|
27
|
+
"@babel/plugin-transform-runtime": "^7.29.7",
|
|
28
|
+
"@babel/preset-env": "^7.29.7",
|
|
29
|
+
"@babel/preset-typescript": "^7.29.7",
|
|
30
|
+
"@fetchx/fetcher": "^0.1.0",
|
|
31
|
+
"@kcuf/ts-config": "^0.2.0",
|
|
32
|
+
"@types/lodash": "^4.17.25",
|
|
33
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
34
|
+
"fetch-mock": "^12.6.0",
|
|
35
|
+
"jsdom": "^30.0.1",
|
|
36
|
+
"rimraf": "^6.1.3",
|
|
37
|
+
"typescript": "^6.0.3",
|
|
38
|
+
"vitest": "^4.1.11"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@babel/runtime": ">=7.0.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@fetchx/fetcher-core": "^0.1.0",
|
|
45
|
+
"lodash": "^4.18.1"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build:clean": "rimraf dist",
|
|
49
|
+
"build:esm": "ESM=1 babel src -d dist/esm --extensions .ts,.tsx --source-maps",
|
|
50
|
+
"build:cjs": "ESM=0 babel src -d dist/cjs --extensions .ts,.tsx",
|
|
51
|
+
"build:types": "tsc -rootDir src --outDir dist/types --declaration --noEmit false --emitDeclarationOnly --isolatedModules false",
|
|
52
|
+
"build": "pnpm build:esm && pnpm build:cjs && pnpm build:types",
|
|
53
|
+
"watch": "pnpm build:esm -w",
|
|
54
|
+
"test": "vitest --coverage --coverage.include=src",
|
|
55
|
+
"prepublishOnly": "pnpm build:clean && pnpm build"
|
|
56
|
+
},
|
|
57
|
+
"gitHead": "bf2c0669d37eb75df199b4762e5295eef57600aa"
|
|
58
|
+
}
|