@fetchx/fetcher-interceptor-login 0.1.7 → 0.1.8

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/README.md CHANGED
@@ -18,6 +18,7 @@ import doLogin from './do-login';
18
18
 
19
19
  interceptLogin(fetcher, {
20
20
  needLogin,
21
+ ignore,
21
22
  doLogin
22
23
  });
23
24
  ```
@@ -32,12 +33,16 @@ interceptLogin(fetcher, {
32
33
  needLogin: 'NeedLogin'
33
34
  ```
34
35
 
35
- 也可以通过函数进行自定义判断:
36
+ 也可以通过函数进行自定义判断(非常特殊的情况才需要用这个形式):
36
37
 
37
38
  ```ts
38
39
  function needLogin(code: string, error: FetcherError): boolean;
39
40
  ```
40
41
 
42
+ ### ignore
43
+
44
+ `ignore` 用以忽略某个或某些接口:
45
+
41
46
  ### doLogin
42
47
 
43
48
  ```ts
@@ -46,10 +51,6 @@ function doLogin(): Promise<void>;
46
51
 
47
52
  这是一个无参的返回 `Promise<void>` 的方法,一般来说对弹窗登录的 `Promise` 封装。
48
53
 
49
- ### ~~headerKeys~~
50
-
51
- 已废弃,现在重新请求时的 config 是原初对象,不会受到前面请求的污染。
52
-
53
54
  ## FAQ
54
55
 
55
56
  ### ❓登录窗怎么实现?
@@ -8,14 +8,20 @@ exports.default = createInterceptorResponseRejected;
8
8
  var _fetcherCore = require("@fetchx/fetcher-core");
9
9
  var _singletonPromise = _interopRequireDefault(require("./singleton-promise"));
10
10
  var _isLoginNeeded = _interopRequireDefault(require("./is-login-needed"));
11
+ var _isLoginIgnored = _interopRequireDefault(require("./is-login-ignored"));
11
12
  function createInterceptorResponseRejected(_ref) {
12
13
  var needLogin = _ref.needLogin,
14
+ ignore = _ref.ignore,
13
15
  doLogin = _ref.doLogin;
14
16
  var singletonLogin = (0, _singletonPromise.default)(doLogin);
15
17
  return function (error, config, _, requestByInterceptor) {
16
18
  if (!(0, _isLoginNeeded.default)(error, needLogin)) {
17
19
  throw error;
18
20
  }
21
+ if ((0, _isLoginIgnored.default)(error, ignore)) {
22
+ error.name = _fetcherCore.FetcherErrorName.LOGIN_IGNORED;
23
+ throw error;
24
+ }
19
25
  return singletonLogin().then(function () {
20
26
  var _config$_config;
21
27
  return requestByInterceptor((_config$_config = config._config) !== null && _config$_config !== void 0 ? _config$_config : {}); // 登录完成,利用原初 _config 对象(一定存在)重新发起请求
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = isLoginIgnored;
7
+ function isLoginIgnored(error, ignore) {
8
+ var _error$config;
9
+ var url = (_error$config = error.config) === null || _error$config === void 0 ? void 0 : _error$config.url;
10
+ if (!(ignore !== null && ignore !== void 0 && ignore.length) || !url) {
11
+ return false;
12
+ }
13
+ return (Array.isArray(ignore) ? ignore : [ignore]).includes(url);
14
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../src/types/index.ts"],"sourcesContent":["import {\n FetcherError\n} from '@fetchx/fetcher-core';\n\nexport interface IFetcherInterceptorLoginOptions {\n needLogin: string | ((code: string, err: FetcherError) => boolean);\n doLogin(): Promise<unknown>;\n}\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../src/types/index.ts"],"sourcesContent":["import {\n FetcherError\n} from '@fetchx/fetcher-core';\n\nexport interface IFetcherInterceptorLoginOptions {\n /**\n * 什么情况触发登录拦截器,如果设置的是字符串,则表示 error.code 等于该字符串时触发,\n * 比如 `needLogin: '401'` 表示 error.code 等于 401 时将触发登录拦截器\n *\n * 如果需要忽略特殊某些接口,可以用 ignore 参数,如果有更复杂的场景,再用方法\n */\n needLogin: string | ((code: string, err: FetcherError) => boolean);\n /**\n * 忽略哪个或哪些接口,这里的字符串将与 `config.url` 做比较,必须和调用时写的保持一致\n */\n ignore?: string | string[];\n doLogin(): Promise<unknown>;\n}\n"],"mappings":"","ignoreList":[]}
@@ -1,14 +1,20 @@
1
1
  import { FetcherErrorName } from '@fetchx/fetcher-core';
2
2
  import singletonPromise from './singleton-promise';
3
3
  import isLoginNeeded from './is-login-needed';
4
+ import isLoginIgnored from './is-login-ignored';
4
5
  export default function createInterceptorResponseRejected(_ref) {
5
6
  var needLogin = _ref.needLogin,
7
+ ignore = _ref.ignore,
6
8
  doLogin = _ref.doLogin;
7
9
  var singletonLogin = singletonPromise(doLogin);
8
10
  return function (error, config, _, requestByInterceptor) {
9
11
  if (!isLoginNeeded(error, needLogin)) {
10
12
  throw error;
11
13
  }
14
+ if (isLoginIgnored(error, ignore)) {
15
+ error.name = FetcherErrorName.LOGIN_IGNORED;
16
+ throw error;
17
+ }
12
18
  return singletonLogin().then(function () {
13
19
  var _config$_config;
14
20
  return requestByInterceptor((_config$_config = config._config) !== null && _config$_config !== void 0 ? _config$_config : {}); // 登录完成,利用原初 _config 对象(一定存在)重新发起请求
@@ -1 +1 @@
1
- {"version":3,"file":"create-interceptor-response-rejected.js","names":["FetcherErrorName","singletonPromise","isLoginNeeded","createInterceptorResponseRejected","_ref","needLogin","doLogin","singletonLogin","error","config","_","requestByInterceptor","then","_config$_config","_config","name","LOGIN_CANCELLED"],"sources":["../../../src/util/create-interceptor-response-rejected.ts"],"sourcesContent":["import {\n FetcherErrorName,\n FetcherConfig,\n FetcherError,\n FetcherResponse,\n FetcherCallRequest,\n FetcherInterceptResponseRejected\n} from '@fetchx/fetcher-core';\n\nimport {\n IFetcherInterceptorLoginOptions\n} from '../types';\n\nimport singletonPromise from './singleton-promise';\nimport isLoginNeeded from './is-login-needed';\n\nexport default function createInterceptorResponseRejected({\n needLogin,\n doLogin\n}: IFetcherInterceptorLoginOptions): FetcherInterceptResponseRejected {\n const singletonLogin = singletonPromise(doLogin);\n \n return (error: FetcherError, config: FetcherConfig, _: FetcherResponse | undefined, requestByInterceptor: FetcherCallRequest) => {\n if (!isLoginNeeded(error, needLogin)) {\n throw error;\n }\n \n return singletonLogin().then(() => {\n return requestByInterceptor(config._config ?? {}); // 登录完成,利用原初 _config 对象(一定存在)重新发起请求\n }, () => {\n // 一般登录弹窗在登录成功之前是不应该被关闭或取消的,但若真允许取消,这里需要修改错误为「取消登录」\n error.name = FetcherErrorName.LOGIN_CANCELLED;\n \n throw error; // 以新的 name 继续抛错\n });\n };\n}\n"],"mappings":"AAAA,SACEA,gBAAgB,QAMX,sBAAsB;AAM7B,OAAOC,gBAAgB,MAAM,qBAAqB;AAClD,OAAOC,aAAa,MAAM,mBAAmB;AAE7C,eAAe,SAASC,iCAAiCA,CAAAC,IAAA,EAGa;EAAA,IAFpEC,SAAS,GAAAD,IAAA,CAATC,SAAS;IACTC,OAAO,GAAAF,IAAA,CAAPE,OAAO;EAEP,IAAMC,cAAc,GAAGN,gBAAgB,CAACK,OAAO,CAAC;EAEhD,OAAO,UAACE,KAAmB,EAAEC,MAAqB,EAAEC,CAA8B,EAAEC,oBAAwC,EAAK;IAC/H,IAAI,CAACT,aAAa,CAACM,KAAK,EAAEH,SAAS,CAAC,EAAE;MACpC,MAAMG,KAAK;IACb;IAEA,OAAOD,cAAc,CAAC,CAAC,CAACK,IAAI,CAAC,YAAM;MAAA,IAAAC,eAAA;MACjC,OAAOF,oBAAoB,EAAAE,eAAA,GAACJ,MAAM,CAACK,OAAO,cAAAD,eAAA,cAAAA,eAAA,GAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACrD,CAAC,EAAE,YAAM;MACP;MACAL,KAAK,CAACO,IAAI,GAAGf,gBAAgB,CAACgB,eAAe;MAE7C,MAAMR,KAAK,CAAC,CAAC;IACf,CAAC,CAAC;EACJ,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"file":"create-interceptor-response-rejected.js","names":["FetcherErrorName","singletonPromise","isLoginNeeded","isLoginIgnored","createInterceptorResponseRejected","_ref","needLogin","ignore","doLogin","singletonLogin","error","config","_","requestByInterceptor","name","LOGIN_IGNORED","then","_config$_config","_config","LOGIN_CANCELLED"],"sources":["../../../src/util/create-interceptor-response-rejected.ts"],"sourcesContent":["import {\n FetcherErrorName,\n FetcherConfig,\n FetcherError,\n FetcherResponse,\n FetcherCallRequest,\n FetcherInterceptResponseRejected\n} from '@fetchx/fetcher-core';\n\nimport {\n IFetcherInterceptorLoginOptions\n} from '../types';\n\nimport singletonPromise from './singleton-promise';\nimport isLoginNeeded from './is-login-needed';\nimport isLoginIgnored from './is-login-ignored';\n\nexport default function createInterceptorResponseRejected({\n needLogin,\n ignore,\n doLogin\n}: IFetcherInterceptorLoginOptions): FetcherInterceptResponseRejected {\n const singletonLogin = singletonPromise(doLogin);\n \n return (error: FetcherError, config: FetcherConfig, _: FetcherResponse | undefined, requestByInterceptor: FetcherCallRequest) => {\n if (!isLoginNeeded(error, needLogin)) {\n throw error;\n }\n \n if (isLoginIgnored(error, ignore)) {\n error.name = FetcherErrorName.LOGIN_IGNORED;\n \n throw error;\n }\n \n return singletonLogin().then(() => {\n return requestByInterceptor(config._config ?? {}); // 登录完成,利用原初 _config 对象(一定存在)重新发起请求\n }, () => {\n // 一般登录弹窗在登录成功之前是不应该被关闭或取消的,但若真允许取消,这里需要修改错误为「取消登录」\n error.name = FetcherErrorName.LOGIN_CANCELLED;\n \n throw error; // 以新的 name 继续抛错\n });\n };\n}\n"],"mappings":"AAAA,SACEA,gBAAgB,QAMX,sBAAsB;AAM7B,OAAOC,gBAAgB,MAAM,qBAAqB;AAClD,OAAOC,aAAa,MAAM,mBAAmB;AAC7C,OAAOC,cAAc,MAAM,oBAAoB;AAE/C,eAAe,SAASC,iCAAiCA,CAAAC,IAAA,EAIa;EAAA,IAHpEC,SAAS,GAAAD,IAAA,CAATC,SAAS;IACTC,MAAM,GAAAF,IAAA,CAANE,MAAM;IACNC,OAAO,GAAAH,IAAA,CAAPG,OAAO;EAEP,IAAMC,cAAc,GAAGR,gBAAgB,CAACO,OAAO,CAAC;EAEhD,OAAO,UAACE,KAAmB,EAAEC,MAAqB,EAAEC,CAA8B,EAAEC,oBAAwC,EAAK;IAC/H,IAAI,CAACX,aAAa,CAACQ,KAAK,EAAEJ,SAAS,CAAC,EAAE;MACpC,MAAMI,KAAK;IACb;IAEA,IAAIP,cAAc,CAACO,KAAK,EAAEH,MAAM,CAAC,EAAE;MACjCG,KAAK,CAACI,IAAI,GAAGd,gBAAgB,CAACe,aAAa;MAE3C,MAAML,KAAK;IACb;IAEA,OAAOD,cAAc,CAAC,CAAC,CAACO,IAAI,CAAC,YAAM;MAAA,IAAAC,eAAA;MACjC,OAAOJ,oBAAoB,EAAAI,eAAA,GAACN,MAAM,CAACO,OAAO,cAAAD,eAAA,cAAAA,eAAA,GAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACrD,CAAC,EAAE,YAAM;MACP;MACAP,KAAK,CAACI,IAAI,GAAGd,gBAAgB,CAACmB,eAAe;MAE7C,MAAMT,KAAK,CAAC,CAAC;IACf,CAAC,CAAC;EACJ,CAAC;AACH","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ export default function isLoginIgnored(error, ignore) {
2
+ var _error$config;
3
+ var url = (_error$config = error.config) === null || _error$config === void 0 ? void 0 : _error$config.url;
4
+ if (!(ignore !== null && ignore !== void 0 && ignore.length) || !url) {
5
+ return false;
6
+ }
7
+ return (Array.isArray(ignore) ? ignore : [ignore]).includes(url);
8
+ }
9
+ //# sourceMappingURL=is-login-ignored.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-login-ignored.js","names":["isLoginIgnored","error","ignore","_error$config","url","config","length","Array","isArray","includes"],"sources":["../../../src/util/is-login-ignored.ts"],"sourcesContent":["import {\n FetcherError\n} from '@fetchx/fetcher-core';\n\nexport default function isLoginIgnored(error: FetcherError, ignore?: string | string[]): boolean {\n const url = error.config?.url;\n \n if (!ignore?.length || !url) {\n return false;\n }\n \n return (Array.isArray(ignore) ? ignore : [ignore]).includes(url);\n}\n"],"mappings":"AAIA,eAAe,SAASA,cAAcA,CAACC,KAAmB,EAAEC,MAA0B,EAAW;EAAA,IAAAC,aAAA;EAC/F,IAAMC,GAAG,IAAAD,aAAA,GAAGF,KAAK,CAACI,MAAM,cAAAF,aAAA,uBAAZA,aAAA,CAAcC,GAAG;EAE7B,IAAI,EAACF,MAAM,aAANA,MAAM,eAANA,MAAM,CAAEI,MAAM,KAAI,CAACF,GAAG,EAAE;IAC3B,OAAO,KAAK;EACd;EAEA,OAAO,CAACG,KAAK,CAACC,OAAO,CAACN,MAAM,CAAC,GAAGA,MAAM,GAAG,CAACA,MAAM,CAAC,EAAEO,QAAQ,CAACL,GAAG,CAAC;AAClE","ignoreList":[]}
@@ -1,5 +1,15 @@
1
1
  import { FetcherError } from '@fetchx/fetcher-core';
2
2
  export interface IFetcherInterceptorLoginOptions {
3
+ /**
4
+ * 什么情况触发登录拦截器,如果设置的是字符串,则表示 error.code 等于该字符串时触发,
5
+ * 比如 `needLogin: '401'` 表示 error.code 等于 401 时将触发登录拦截器
6
+ *
7
+ * 如果需要忽略特殊某些接口,可以用 ignore 参数,如果有更复杂的场景,再用方法
8
+ */
3
9
  needLogin: string | ((code: string, err: FetcherError) => boolean);
10
+ /**
11
+ * 忽略哪个或哪些接口,这里的字符串将与 `config.url` 做比较,必须和调用时写的保持一致
12
+ */
13
+ ignore?: string | string[];
4
14
  doLogin(): Promise<unknown>;
5
15
  }
@@ -1,3 +1,3 @@
1
1
  import { FetcherInterceptResponseRejected } from '@fetchx/fetcher-core';
2
2
  import { IFetcherInterceptorLoginOptions } from '../types';
3
- export default function createInterceptorResponseRejected({ needLogin, doLogin }: IFetcherInterceptorLoginOptions): FetcherInterceptResponseRejected;
3
+ export default function createInterceptorResponseRejected({ needLogin, ignore, doLogin }: IFetcherInterceptorLoginOptions): FetcherInterceptResponseRejected;
@@ -0,0 +1,2 @@
1
+ import { FetcherError } from '@fetchx/fetcher-core';
2
+ export default function isLoginIgnored(error: FetcherError, ignore?: string | string[]): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fetchx/fetcher-interceptor-login",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Fetcher interceptor seamless login and redo request.",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -27,20 +27,20 @@
27
27
  "@babel/plugin-transform-runtime": "^7.29.7",
28
28
  "@babel/preset-env": "^7.29.7",
29
29
  "@babel/preset-typescript": "^7.29.7",
30
- "@fetchx/fetcher-adapter-web": "^0.1.7",
30
+ "@fetchx/fetcher-adapter-web": "^0.1.8",
31
31
  "@kcuf/ts-config": "^0.2.0",
32
- "@vitest/coverage-v8": "^5.0.5",
32
+ "@vitest/coverage-v8": "^5.1.0",
33
33
  "fetch-mock": "^12.6.0",
34
34
  "jsdom": "^30.1.3",
35
- "rimraf": "^6.1.3",
35
+ "rimraf": "^6.1.4",
36
36
  "typescript": "^6.0.3",
37
- "vitest": "^5.0.5"
37
+ "vitest": "^5.1.0"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@babel/runtime": ">=7.0.0"
41
41
  },
42
42
  "dependencies": {
43
- "@fetchx/fetcher-core": "^0.1.7",
43
+ "@fetchx/fetcher-core": "^0.1.8",
44
44
  "@kcuf/messenger": "^0.0.3"
45
45
  },
46
46
  "scripts": {
@@ -53,5 +53,5 @@
53
53
  "test": "vitest --coverage --coverage.include=src",
54
54
  "prepublishOnly": "pnpm build:clean && pnpm build"
55
55
  },
56
- "gitHead": "de66c0e5906e750a8e0f2c93ccbc28f2ca19eb8d"
56
+ "gitHead": "1c1a9de5ccee14ee6da99d89be047138312f8518"
57
57
  }