complex-require
Advanced tools
+2
-2
| { | ||
| "name": "complex-require", | ||
| "version": "3.2.6", | ||
| "version": "3.2.7", | ||
| "description": "a complex require", | ||
@@ -11,3 +11,3 @@ "main": "index.ts", | ||
| "dependencies": { | ||
| "complex-utils": "1.0.17 - 1.0.99", | ||
| "complex-data-next": "3.2.3 - 3.2.99", | ||
| "axios": "^1.3.6" | ||
@@ -14,0 +14,0 @@ }, |
+1
-1
@@ -22,3 +22,3 @@ import { AxiosRequestConfig } from 'axios' | ||
| $fail?: boolean | failType // 错误回调 | ||
| $format?: (options: InstanceInitOption, isRefresh?: boolean) => InstanceInitOption | ||
| $format?: (options: InstanceInitOption, isRefresh?: boolean) => void | ||
| } | ||
@@ -25,0 +25,0 @@ |
+13
-4
| import axios, { AxiosInstance, AxiosRequestConfig } from 'axios' | ||
| import { Data, getType, getEnv } from 'complex-utils' | ||
| import { getType, getEnv } from 'complex-utils' | ||
| import { DefaultData } from 'complex-data-next' | ||
| import { DefaultDataInitOption } from 'complex-data-next/src/data/DefaultData' | ||
| import Rule, { RuleInitOption } from './Rule' | ||
@@ -28,3 +30,3 @@ import Instance, { InstanceInitOption, customParameters } from './Instance' | ||
| export type RequireInitOption = { | ||
| export interface RequireInitOption extends DefaultDataInitOption { | ||
| baseUrl?: string | ||
@@ -50,3 +52,3 @@ option?: AxiosRequestConfig | ||
| class Require extends Data { | ||
| class Require extends DefaultData { | ||
| static $name = 'Require' | ||
@@ -59,3 +61,4 @@ service: AxiosInstance | ||
| constructor(initOption: RequireInitOption) { | ||
| super() | ||
| super(initOption) | ||
| this.$triggerCreateLife('Require', 'beforeCreate', initOption) | ||
| this.service = axios.create(initOption.option) | ||
@@ -71,2 +74,3 @@ this.baseUrl = initOption.baseUrl || '' | ||
| initOption.rule.forEach(ruleOption => { | ||
| ruleOption.parent = this | ||
| this.rule[ruleOption.prop] = new Rule(ruleOption) | ||
@@ -87,2 +91,3 @@ if (defaultProp === undefined) { | ||
| } | ||
| this.$triggerCreateLife('Require', 'created', initOption) | ||
| } | ||
@@ -194,3 +199,5 @@ getFormatUrl(formatUrl?: formatUrlType) { | ||
| if (ruleItem.refreshLogin && !isRefresh) { | ||
| ruleItem.$triggerLife('beforeRefreshLogin', ruleItem) | ||
| ruleItem.refreshLogin().then(() => { | ||
| ruleItem.$triggerLife('refreshLogined', ruleItem) | ||
| this.runInstance(instance, ruleItem, true).then(res => { | ||
@@ -202,5 +209,7 @@ resolve(res) | ||
| }).catch(err => { | ||
| ruleItem.$triggerLife('refreshLoginFail', ruleItem) | ||
| reject(err) | ||
| }) | ||
| } else { | ||
| ruleItem.$triggerLife('login', ruleItem, isRefresh) | ||
| instance.fail(false, responseData.msg, 'error') | ||
@@ -207,0 +216,0 @@ // 此处考虑登录的自动或打断实现方案 |
+32
-13
@@ -1,5 +0,7 @@ | ||
| import { Data } from 'complex-utils' | ||
| import { errorType } from './Require' | ||
| import { DefaultData, LifeData } from 'complex-data-next' | ||
| import { DefaultDataInitOption } from 'complex-data-next/src/data/DefaultData' | ||
| import Require, { errorType } from './Require' | ||
| import Instance from './Instance' | ||
| import Token, { TokenInitOption } from './Token' | ||
| import { upperCaseFirstChar } from 'complex-utils' | ||
@@ -18,2 +20,3 @@ export type tokenType = { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export interface responseType<D = any> { | ||
@@ -27,2 +30,3 @@ status: 'success' | 'fail' | 'login' | ||
| type checkUrlType = (url: string) => boolean | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type formatType = (response: any, instance: Instance) => responseType | ||
@@ -34,3 +38,3 @@ type formatUrlType = (url: string) => string | ||
| export interface RuleInitOption { | ||
| export interface RuleInitOption extends DefaultDataInitOption<Require> { | ||
| prop: string | ||
@@ -56,7 +60,16 @@ name: string | ||
| } | ||
| class Rule extends Data { | ||
| /** | ||
| * 已定义以下事件 | ||
| * create相关事件 | ||
| * login:需要登录 | ||
| * token:需要token | ||
| * beforeRefreshToken:准备刷新token | ||
| * refreshTokened:刷新token完成 | ||
| * refreshTokenFail:刷新token失败 | ||
| * beforeRefreshLogin:准备刷新login | ||
| * refreshLogined:刷新login完成 | ||
| * refreshLoginFail:刷新login失败 | ||
| */ | ||
| class Rule extends DefaultData<Require> { | ||
| static $name = 'Rule' | ||
| prop: string | ||
| name: string | ||
| token: formatTokenType | ||
@@ -70,5 +83,4 @@ checkUrl: checkUrlType | ||
| constructor(initOption: RuleInitOption) { | ||
| super() | ||
| this.prop = initOption.prop | ||
| this.name = initOption.name | ||
| super(initOption) | ||
| this.$triggerCreateLife('Rule', 'beforeCreate', initOption) | ||
| const token = initOption.token || {} | ||
@@ -78,3 +90,3 @@ const tokenData: Record<string, Token> = {} | ||
| for (const tokenName in token.data) { | ||
| tokenData[tokenName] = new Token(token.data[tokenName], tokenName, this.prop, token.time, token.session) | ||
| tokenData[tokenName] = new Token(token.data[tokenName], tokenName, this.$prop, token.time, token.session) | ||
| } | ||
@@ -89,2 +101,3 @@ } | ||
| this.fail = initOption.fail || defaultFail | ||
| this.$triggerCreateLife('Rule', 'beforeCreate', initOption) | ||
| } | ||
@@ -149,3 +162,5 @@ getTokenList() { | ||
| return new Promise((resolve, reject) => { | ||
| this.$triggerLife('beforeRefreshToken', this, tokenName, isRefresh) | ||
| this.refreshToken!(tokenName).then(() => { | ||
| this.$triggerLife('refreshTokened', this, tokenName, isRefresh) | ||
| this.$appendToken(instance, tokenName, true).then(res => { | ||
@@ -157,2 +172,3 @@ resolve(res) | ||
| }).catch(err => { | ||
| this.$triggerLife('refreshTokenFail', this, tokenName, isRefresh) | ||
| reject(err) | ||
@@ -162,5 +178,7 @@ }) | ||
| } else { | ||
| this.$triggerLife('token', this, tokenName, isRefresh) | ||
| return Promise.reject({ status: 'fail', code: 'empty token', msg: `Token:${tokenName}的值不存在` }) | ||
| } | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| setToken(tokenName: string, data: any, noSave?: boolean) { | ||
@@ -229,4 +247,5 @@ if (this.token[tokenName]) { | ||
| } | ||
| $selfName() { | ||
| return `(${super.$selfName()}:[${this.name}/${this.prop}])` | ||
| $triggerLife(...args: Parameters<LifeData['trigger']>) { | ||
| this.$parent!.$triggerLife(...['rule' + upperCaseFirstChar(args[0]), ...args.slice(1)]) | ||
| super.$triggerLife(...args) | ||
| } | ||
@@ -233,0 +252,0 @@ } |
+9
-0
| import { isExist, setLocalData, getLocalData, removeLocalData, setSessionLocalData, getSessionLocalData, removeSessionLocalData } from 'complex-utils' | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type getDataType = () => any | ||
| type removeDataType = getDataType | ||
| type clearDataType = getDataType | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type hasDataType = (data: any) => boolean | ||
@@ -12,2 +14,3 @@ type destroyDataType = clearDataType | ||
| export interface TokenInitOption { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| data?: any | ||
@@ -25,2 +28,3 @@ require?: boolean | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| function hasData(data: any) { | ||
@@ -30,2 +34,3 @@ return isExist(data) | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| function setData(this: Token, data: any, noSave?: boolean) { | ||
@@ -37,2 +42,3 @@ this.data = data | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| function setDataBySession(this: Token, data: any, noSave?: boolean) { | ||
@@ -99,2 +105,3 @@ this.data = data | ||
| require?: boolean | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| data: any | ||
@@ -108,2 +115,3 @@ location: locationType | ||
| $destroyData?: destroyDataType | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| setData: (data: any, noSave?: boolean) => void | ||
@@ -139,2 +147,3 @@ getData: getDataType | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| hasData(data: any) { | ||
@@ -141,0 +150,0 @@ let hasStatus: 'exist' | 'empty' | '' = 'exist' |
44854
5.59%985
4.01%+ Added
+ Added
+ Added
- Removed