zova-module-a-interceptor 5.1.14 → 5.1.15

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.
@@ -1 +1 @@
1
- {"version":3,"file":"interceptor.body.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.body.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,EACV,4BAA4B,EAC5B,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,mBAAmB,EAAkD,MAAM,qBAAqB,CAAC;AAE1G,MAAM,WAAW,uBAAwB,SAAQ,4BAA4B;CAAG;AAEhF,qBACa,eAAgB,SAAQ,mBAAmB,CAAC,uBAAuB,CAAE,YAAW,oBAAoB,EAAE,yBAAyB;IACpI,UAAU,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,EAAE,uBAAuB,EAAE,IAAI,EAAE,uBAAuB,GAAG,OAAO,CAAC,aAAa,CAAC;IAiB7H,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,uBAAuB,EAAE,IAAI,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;CAS7H"}
1
+ {"version":3,"file":"interceptor.body.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.body.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,EACV,4BAA4B,EAC5B,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,mBAAmB,EAGpB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,uBAAwB,SAAQ,4BAA4B;CAAG;AAEhF,qBACa,eACX,SAAQ,mBAAmB,CAAC,uBAAuB,CACnD,YAAW,oBAAoB,EAAE,yBAAyB;IAEpD,UAAU,CACd,QAAQ,EAAE,aAAa,EACvB,QAAQ,EAAE,uBAAuB,EACjC,IAAI,EAAE,uBAAuB,GAC5B,OAAO,CAAC,aAAa,CAAC;IAqBnB,eAAe,CACnB,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,uBAAuB,EACjC,IAAI,EAAE,oBAAoB,GACzB,OAAO,CAAC,UAAU,CAAC;CASvB"}
@@ -1 +1 @@
1
- {"version":3,"file":"interceptor.mock.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,yBAAyB,EAEzB,oBAAoB,EAErB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,uBAAwB,SAAQ,4BAA4B;CAAG;AAKhF,qBACa,eAAgB,SAAQ,mBAAmB,CAAC,uBAAuB,CAAE,YAAW,yBAAyB;IAC9G,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,4BAA4B,EAAE,IAAI,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;CA0BlI"}
1
+ {"version":3,"file":"interceptor.mock.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,yBAAyB,EAEzB,oBAAoB,EAErB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,uBAAwB,SAAQ,4BAA4B;CAAG;AAKhF,qBACa,eACX,SAAQ,mBAAmB,CAAC,uBAAuB,CACnD,YAAW,yBAAyB;IAE9B,eAAe,CACnB,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,4BAA4B,EACtC,IAAI,EAAE,oBAAoB,GACzB,OAAO,CAAC,UAAU,CAAC;CA4BvB"}
@@ -1 +1 @@
1
- {"version":3,"file":"interceptor.performAction.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.performAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAE3C,OAAO,EAAE,mBAAmB,EAAE,4BAA4B,EAAE,mBAAmB,EAAe,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAGlJ,MAAM,WAAW,gCAAiC,SAAQ,4BAA4B;CAAG;AAEzF,qBACa,wBAAyB,SAAQ,mBAAmB,CAAC,gCAAgC,CAAE,YAAW,mBAAmB;IAC1H,SAAS,CAAC,MAAM,EAAE,kBAAkB,EAAE,QAAQ,EAAE,gCAAgC,EAAE,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAoBnJ"}
1
+ {"version":3,"file":"interceptor.performAction.d.ts","sourceRoot":"","sources":["../../src/bean/interceptor.performAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAE3C,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,mBAAmB,EAEnB,sBAAsB,EACvB,MAAM,qBAAqB,CAAC;AAG7B,MAAM,WAAW,gCAAiC,SAAQ,4BAA4B;CAAG;AAEzF,qBACa,wBACX,SAAQ,mBAAmB,CAAC,gCAAgC,CAC5D,YAAW,mBAAmB;IAExB,SAAS,CACb,MAAM,EAAE,kBAAkB,EAC1B,QAAQ,EAAE,gCAAgC,EAC1C,IAAI,EAAE,sBAAsB,GAC3B,OAAO,CAAC,kBAAkB,CAAC;CAoB/B"}
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
- import { BeanInfo, BeanScopeBase, cast } from "zova";
1
+ import { $protocolKey, BeanInfo, BeanScopeBase, cast } from "zova";
2
2
  import { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from "zova-module-a-fetch";
3
- import { $customKey } from "zova-core";
4
3
  import { catchError } from "@cabloy/utils";
5
4
  import { Scope } from "zova-module-a-bean";
6
5
  //#region src/bean/interceptor.body.ts
@@ -40,7 +39,7 @@ var InterceptorHeaders = (_dec$4 = Interceptor({ dependencies: "a-interceptor:mo
40
39
  if (keyLocale && !config.headers[keyLocale]) config.headers[keyLocale] = this.app.meta.locale.current;
41
40
  const keyTz = this.sys.env.APP_TZ_HEADER_KEY;
42
41
  if (keyTz && !config.headers[keyTz]) config.headers[keyTz] = this.app.meta.locale.tz;
43
- if (options.openapiSchema) config.headers[$customKey("x-vona-openapi-schema")] = true;
42
+ if (options.openapiSchema) config.headers[$protocolKey("x-vona-openapi-schema")] = true;
44
43
  return next(config);
45
44
  }
46
45
  }) || _class$4) || _class$4);
@@ -73,7 +72,7 @@ var InterceptorJwt = (_dec$3 = Interceptor({ dependencies: "a-interceptor:header
73
72
  if (!this.sys.config.api.jwt) return;
74
73
  if (this.sys.config.ssr.ignoreCookieOnServer) return;
75
74
  const authTokenCurrent = authToken ?? this.scope.config.authToken.default;
76
- if (process.env.SERVER) config.headers[$customKey("x-vona-jwt-authtoken")] = typeof authTokenCurrent === "string" ? true : authTokenCurrent;
75
+ if (process.env.SERVER) config.headers[$protocolKey("x-vona-jwt-authtoken")] = typeof authTokenCurrent === "string" ? true : authTokenCurrent;
77
76
  if (typeof authTokenCurrent === "string") return authTokenCurrent;
78
77
  let jwtInfo = await this._beanJwtAdapter.getJwtInfo();
79
78
  if (!jwtInfo) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/bean/interceptor.body.ts","../src/bean/interceptor.headers.ts","../src/bean/interceptor.jwt.ts","../src/bean/interceptor.mock.ts","../src/bean/interceptor.performAction.ts","../src/config/config.ts","../src/.metadata/index.ts"],"sourcesContent":["import type { AxiosError, AxiosResponse } from 'axios';\nimport type {\n IDecoratorInterceptorOptions,\n IInterceptorResponse,\n IInterceptorResponseError,\n NextInterceptorError,\n NextInterceptorResponse,\n} from 'zova-module-a-fetch';\n\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsBody extends IDecoratorInterceptorOptions {}\n\n@Interceptor<IInterceptorOptionsBody>({ dependencies: 'a-interceptor:performAction' })\nexport class InterceptorBody extends BeanInterceptorBase<IInterceptorOptionsBody> implements IInterceptorResponse, IInterceptorResponseError {\n async onResponse(response: AxiosResponse, _options: IInterceptorOptionsBody, next: NextInterceptorResponse): Promise<AxiosResponse> {\n response = await next();\n const contentType = response.headers['content-type'];\n if (!contentType || typeof contentType !== 'string' || !contentType.includes('application/json')) {\n response[SymbolInterceptorBodyResponseFlag] = true;\n return response;\n }\n if (response.data.code !== 0) {\n const error = new Error();\n error.code = response.data.code;\n error.message = response.data.message;\n throw error;\n }\n // return data\n return response.data.data ?? null;\n }\n\n async onResponseError(error: AxiosError, _options: IInterceptorOptionsBody, next: NextInterceptorError): Promise<AxiosError> {\n error = await next();\n if (!(error instanceof Error)) return error;\n if (error.response) {\n error.code = cast(error.response.data)?.code ?? error.response.status;\n error.message = cast(error.response.data)?.message ?? error.response.statusText;\n }\n return error;\n }\n}\n","import { AxiosRequestConfig } from 'axios';\nimport { $customKey } from 'zova-core';\nimport { BeanInterceptorBase, IDecoratorInterceptorOptions, Interceptor, NextInterceptorRequest } from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsHeaders extends IDecoratorInterceptorOptions {\n openapiSchema?: boolean;\n}\n\n@Interceptor<IInterceptorOptionsHeaders>({ dependencies: 'a-interceptor:mock' })\nexport class InterceptorHeaders extends BeanInterceptorBase<IInterceptorOptionsHeaders> {\n async onRequest(config: AxiosRequestConfig, options: IInterceptorOptionsHeaders, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {\n // locale\n const keyLocale = this.sys.env.APP_LOCALE_HEADER_KEY;\n if (keyLocale && !config.headers![keyLocale]) {\n config.headers![keyLocale] = this.app.meta.locale.current;\n }\n // tz\n const keyTz = this.sys.env.APP_TZ_HEADER_KEY;\n if (keyTz && !config.headers![keyTz]) {\n config.headers![keyTz] = this.app.meta.locale.tz;\n }\n // openapi schema\n if (options.openapiSchema) {\n config.headers![$customKey('x-vona-openapi-schema')] = true;\n }\n // next\n return next(config);\n }\n}\n","import type { AxiosRequestConfig } from 'axios';\nimport type { BeanFetch, IDecoratorInterceptorOptions, IInterceptorRequest, NextInterceptorRequest } from 'zova-module-a-fetch';\n\nimport { $customKey } from 'zova-core';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\n\nimport type { IJwtAdapter, IJwtInfo } from '../types/jwt.js';\n\nexport interface IInterceptorOptionsJwt extends IDecoratorInterceptorOptions {\n jwtAdapter?: string;\n authToken?: boolean | string;\n}\n\n@Interceptor<IInterceptorOptionsJwt>({ dependencies: 'a-interceptor:headers' })\nexport class InterceptorJwt extends BeanInterceptorBase<IInterceptorOptionsJwt> implements IInterceptorRequest {\n private _beanJwtAdapter: IJwtAdapter;\n private _refreshAuthTokenPromise?: Promise<IJwtInfo>;\n\n protected async __init__(_beanFetch: BeanFetch, options: IInterceptorOptionsJwt) {\n const jwtAdapter = options.jwtAdapter || this.scope.config.jwtAdapter;\n const beanFullName = jwtAdapter.replace(':', '.service.');\n // singleton\n this._beanJwtAdapter = await this.app.bean._getBean(beanFullName as any, true);\n }\n\n async onRequest(config: AxiosRequestConfig, options: IInterceptorOptionsJwt, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {\n try {\n const accessToken = await this.prepareAccessToken(config, options.authToken);\n if (accessToken) {\n config.headers!.Authorization = `Bearer ${accessToken}`;\n }\n } catch (error: any) {\n error.config = config;\n error.request = undefined;\n error.response = undefined;\n throw error;\n }\n return next(config);\n }\n\n async prepareAccessToken(config: AxiosRequestConfig, authToken: string | boolean | undefined): Promise<string | undefined> {\n if (!this.sys.config.api.jwt) return;\n if (this.sys.config.ssr.ignoreCookieOnServer) return;\n // use default in scope.config rather than IInterceptorOptionsJwt.options\n const authTokenCurrent = authToken ?? this.scope.config.authToken.default;\n // authToken = authToken ?? this.scope.config.authToken.default;\n if (process.env.SERVER) {\n config.headers![$customKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;\n }\n // // authToken: false\n // if (authTokenCurrent === false) return;\n // authToken: string\n if (typeof authTokenCurrent === 'string') return authTokenCurrent;\n // if (typeof authToken === 'string') return authToken;\n // authToken: true\n let jwtInfo = await this._beanJwtAdapter.getJwtInfo();\n if (!jwtInfo) {\n if (authToken === true) {\n this.app.throw(401); // 401 rather than 403\n }\n return;\n }\n // accessToken\n if (process.env.SERVER || !jwtInfo.expireTime || jwtInfo.expireTime > Date.now()) {\n if (!jwtInfo.accessToken) {\n if (authToken === true) this.app.throw(401);\n return;\n }\n return jwtInfo.accessToken;\n }\n // refreshToken\n if (!jwtInfo.refreshToken) {\n if (authToken === true) this.app.throw(401);\n return;\n } else {\n if (authToken === false) {\n // need not refreshAuthToken, such as captcha.create\n return;\n }\n }\n jwtInfo = await this._refreshAuthToken(jwtInfo.refreshToken);\n return jwtInfo.accessToken;\n }\n\n private async _refreshAuthToken(refreshToken: string) {\n if (!this._refreshAuthTokenPromise) {\n this._refreshAuthTokenPromise = this._refreshAuthTokenInner(refreshToken);\n }\n return await this._refreshAuthTokenPromise;\n }\n\n private async _refreshAuthTokenInner(refreshToken: string) {\n try {\n return await this._beanJwtAdapter.refreshAuthToken(refreshToken);\n } finally {\n this._refreshAuthTokenPromise = undefined;\n }\n }\n}\n","import { AxiosError } from 'axios';\nimport {\n BeanInterceptorBase,\n IDecoratorInterceptorOptions,\n IInterceptorResponseError,\n Interceptor,\n NextInterceptorError,\n SymbolInterceptorBodyResponseFlag,\n} from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsMock extends IDecoratorInterceptorOptions {}\n\n// not include: ERR_BAD_RESPONSE\nconst __ErrorsShouldBeMocked = ['ECONNREFUSED', 'ERR_NETWORK', '404'];\n\n@Interceptor<IInterceptorOptionsMock>()\nexport class InterceptorMock extends BeanInterceptorBase<IInterceptorOptionsMock> implements IInterceptorResponseError {\n async onResponseError(error: AxiosError, _options: IDecoratorInterceptorOptions, next: NextInterceptorError): Promise<AxiosError> {\n if (!(error instanceof Error)) return next();\n if (this.sys.env.MOCK_ENABLED === 'true') {\n if (process.env.DEV || (process.env.PROD && this.sys.env.MOCK_BUILD === 'true')) {\n if (__ErrorsShouldBeMocked.includes(String(error.code)) || error.status === 404) {\n const config = error.config!;\n if (config.baseURL) {\n const port = process.env.DEV ? this.sys.env.DEV_SERVER_PORT : this.sys.env.MOCK_BUILD_PORT;\n let baseURL = `http://localhost:${port}`;\n if (config.baseURL.endsWith(this.sys.env.API_PREFIX!)) {\n baseURL = `${baseURL}${this.sys.env.API_PREFIX}`;\n }\n if (config.baseURL !== baseURL && config.baseURL !== this.sys.env.API_PREFIX) {\n const response = await this.$fetch.request(Object.assign({}, config, { baseURL }));\n if (response && response[SymbolInterceptorBodyResponseFlag]) {\n return response.data;\n } else {\n return response as any;\n }\n }\n }\n }\n }\n }\n return next();\n }\n}\n","import { catchError } from '@cabloy/utils';\nimport { AxiosRequestConfig } from 'axios';\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, IDecoratorInterceptorOptions, IInterceptorRequest, Interceptor, NextInterceptorRequest } from 'zova-module-a-fetch';\nimport { ISsrSitePerformActionOptions } from 'zova-module-a-ssr';\n\nexport interface IInterceptorOptionsPerformAction extends IDecoratorInterceptorOptions {}\n\n@Interceptor<IInterceptorOptionsPerformAction>({ dependencies: 'a-interceptor:jwt' })\nexport class InterceptorPerformAction extends BeanInterceptorBase<IInterceptorOptionsPerformAction> implements IInterceptorRequest {\n async onRequest(config: AxiosRequestConfig, _options: IInterceptorOptionsPerformAction, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {\n if (process.env.CLIENT) return next();\n const performAction = this.ctx.meta.$ssr.getPerformAction(config.baseURL);\n if (!performAction) return next();\n const data: ISsrSitePerformActionOptions = {\n method: config.method as any,\n path: config.url!,\n query: config.params,\n body: config.data,\n headers: config.headers,\n };\n const [result, error] = await catchError(() => {\n return performAction(data);\n });\n if (error) {\n cast(error).config = config;\n throw error;\n }\n throw result;\n }\n}\n","import type { ZovaSys } from 'zova';\nimport type { IServiceRecord } from 'zova-module-a-bean';\n\nexport const config = (_sys: ZovaSys) => {\n return {\n jwtAdapter: 'home-api:jwtAdapter' as keyof IServiceRecord,\n authToken: {\n default: true,\n },\n };\n};\n","// eslint-disable\n/** interceptor: begin */\nexport * from '../bean/interceptor.body.js';\nexport * from '../bean/interceptor.headers.js';\nexport * from '../bean/interceptor.jwt.js';\nexport * from '../bean/interceptor.mock.js';\nexport * from '../bean/interceptor.performAction.js';\nimport { IInterceptorOptionsBody } from '../bean/interceptor.body.js';\nimport { IInterceptorOptionsHeaders } from '../bean/interceptor.headers.js';\nimport { IInterceptorOptionsJwt } from '../bean/interceptor.jwt.js';\nimport { IInterceptorOptionsMock } from '../bean/interceptor.mock.js';\nimport { IInterceptorOptionsPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova-module-a-fetch';\ndeclare module 'zova-module-a-fetch' {\n \n export interface IInterceptorRecord {\n 'a-interceptor:body': IInterceptorOptionsBody;\n'a-interceptor:headers': IInterceptorOptionsHeaders;\n'a-interceptor:jwt': IInterceptorOptionsJwt;\n'a-interceptor:mock': IInterceptorOptionsMock;\n'a-interceptor:performAction': IInterceptorOptionsPerformAction;\n }\n\n \n}\ndeclare module 'zova-module-a-interceptor' {\n \n export interface InterceptorBody {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorBody {\n get $beanFullName(): 'a-interceptor.interceptor.body';\n get $onionName(): 'a-interceptor:body';\n get $onionOptions(): IInterceptorOptionsBody;\n }\n\n export interface InterceptorHeaders {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorHeaders {\n get $beanFullName(): 'a-interceptor.interceptor.headers';\n get $onionName(): 'a-interceptor:headers';\n get $onionOptions(): IInterceptorOptionsHeaders;\n }\n\n export interface InterceptorJwt {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorJwt {\n get $beanFullName(): 'a-interceptor.interceptor.jwt';\n get $onionName(): 'a-interceptor:jwt';\n get $onionOptions(): IInterceptorOptionsJwt;\n }\n\n export interface InterceptorMock {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorMock {\n get $beanFullName(): 'a-interceptor.interceptor.mock';\n get $onionName(): 'a-interceptor:mock';\n get $onionOptions(): IInterceptorOptionsMock;\n }\n\n export interface InterceptorPerformAction {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorPerformAction {\n get $beanFullName(): 'a-interceptor.interceptor.performAction';\n get $onionName(): 'a-interceptor:performAction';\n get $onionOptions(): IInterceptorOptionsPerformAction;\n } \n}\n/** interceptor: end */\n/** interceptor: begin */\nimport { InterceptorBody } from '../bean/interceptor.body.js';\nimport { InterceptorHeaders } from '../bean/interceptor.headers.js';\nimport { InterceptorJwt } from '../bean/interceptor.jwt.js';\nimport { InterceptorMock } from '../bean/interceptor.mock.js';\nimport { InterceptorPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordLocal {\n 'a-interceptor.interceptor.body': InterceptorBody;\n'a-interceptor.interceptor.headers': InterceptorHeaders;\n'a-interceptor.interceptor.jwt': InterceptorJwt;\n'a-interceptor.interceptor.mock': InterceptorMock;\n'a-interceptor.interceptor.performAction': InterceptorPerformAction;\n }\n}\n/** interceptor: end */\n/** config: begin */\nexport * from '../config/config.js';\nimport { config } from '../config/config.js';\n/** config: end */\n/** scope: begin */\nimport { BeanScopeBase, type BeanScopeUtil, TypeModuleConfig } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\n\n@Scope()\nexport class ScopeModuleAInterceptor extends BeanScopeBase {}\n\nexport interface ScopeModuleAInterceptor {\n util: BeanScopeUtil;\nconfig: TypeModuleConfig<typeof config>;\n}\n\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanScopeRecord {\n 'a-interceptor': ScopeModuleAInterceptor;\n }\n \n export interface IBeanScopeConfig {\n 'a-interceptor': ReturnType<typeof config>;\n }\n\n \n\n \n}\n \n/** scope: end */\n"],"mappings":";;;;;;AAAA,IAAA,QAAO,SAAO;AAId,IAAE,mBAAyB,SAAA,YAAA,EACzB,cAAA,+BACD,CAAC,EAAA,UAAA,SAAuB,EACvB,QAAM,iBAAA,CAAA,EAAA,OAAA,WAAA,QAAA,WAAA,MAAA,wBAAA,oBAAA;CAER,MAAQ,WAAQ,UAAW,UAAA,MAAA;AAC3B,aAAS,MAAA,MAAA;;AAET,MAAO,CAAA,eAAU,OAAA,gBAAwB,YAAQ,CAAA,YAAA,SAA8B,mBAAA,EAAA;;AAE9E,UAAY;;AAEX,MAAM,SAAA,KAAW,SAAU,GAAA;GACzB,MAAQ,wBAAQ,IAAC,OAAM;AACvB,SAAM,OAAA,SAAc,KAAS;AAC3B,SAAG,UAAa,SAAS,KAAA;AACzB,SAAA;;AAGF,SAAI,SAAS,KAAK,QAAW;;CAE/B,MAAI,gBAAa,OAAS,UAAS,MAAA;AACjC,UAAQ,MAAA,MAAU;AAClB,MAAE,EAAA,iBAAW,OAAA,QAAA;AACb,MAAA,MAAA,UAAA;AACE,SAAC,OAAO,KAAA,MAAA,SAAA,KAAA,EAAA,QAAA,MAAA,SAAA;AACV,SAAO,UAAS,KAAK,MAAQ,SAAI,KAAA,EAAA,WAAA,MAAA,SAAA;;;;EAInC,IAAE,SAAQ,IAAK;;;AClCjB,IAAA,QAAS,SAAA;AAIT,IAAO,sBAAU,SAAA,YAAmC,EAClD,cAAc,sBAChB,CAAA,EAAA,UAAA,SAAA,EAAA,QAAA,iBAEC,CAAA,EAAA,OAAA,WAAY,QAAA,WAAA,MAA0B,2BAAqB,oBA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CAAC,EAAE,KAAI,SAAO,MAAS,SAAC,MAAA,gCAAgB,cAAA,GAAA,IAAA,OAAA,IAAA"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/bean/interceptor.body.ts","../src/bean/interceptor.headers.ts","../src/bean/interceptor.jwt.ts","../src/bean/interceptor.mock.ts","../src/bean/interceptor.performAction.ts","../src/config/config.ts","../src/.metadata/index.ts"],"sourcesContent":["import type { AxiosError, AxiosResponse } from 'axios';\nimport type {\n IDecoratorInterceptorOptions,\n IInterceptorResponse,\n IInterceptorResponseError,\n NextInterceptorError,\n NextInterceptorResponse,\n} from 'zova-module-a-fetch';\n\nimport { cast } from 'zova';\nimport {\n BeanInterceptorBase,\n Interceptor,\n SymbolInterceptorBodyResponseFlag,\n} from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsBody extends IDecoratorInterceptorOptions {}\n\n@Interceptor<IInterceptorOptionsBody>({ dependencies: 'a-interceptor:performAction' })\nexport class InterceptorBody\n extends BeanInterceptorBase<IInterceptorOptionsBody>\n implements IInterceptorResponse, IInterceptorResponseError\n{\n async onResponse(\n response: AxiosResponse,\n _options: IInterceptorOptionsBody,\n next: NextInterceptorResponse,\n ): Promise<AxiosResponse> {\n response = await next();\n const contentType = response.headers['content-type'];\n if (\n !contentType ||\n typeof contentType !== 'string' ||\n !contentType.includes('application/json')\n ) {\n response[SymbolInterceptorBodyResponseFlag] = true;\n return response;\n }\n if (response.data.code !== 0) {\n const error = new Error();\n error.code = response.data.code;\n error.message = response.data.message;\n throw error;\n }\n // return data\n return response.data.data ?? null;\n }\n\n async onResponseError(\n error: AxiosError,\n _options: IInterceptorOptionsBody,\n next: NextInterceptorError,\n ): Promise<AxiosError> {\n error = await next();\n if (!(error instanceof Error)) return error;\n if (error.response) {\n error.code = cast(error.response.data)?.code ?? error.response.status;\n error.message = cast(error.response.data)?.message ?? error.response.statusText;\n }\n return error;\n }\n}\n","import { AxiosRequestConfig } from 'axios';\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, IDecoratorInterceptorOptions, Interceptor, NextInterceptorRequest } from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsHeaders extends IDecoratorInterceptorOptions {\n openapiSchema?: boolean;\n}\n\n@Interceptor<IInterceptorOptionsHeaders>({ dependencies: 'a-interceptor:mock' })\nexport class InterceptorHeaders extends BeanInterceptorBase<IInterceptorOptionsHeaders> {\n async onRequest(config: AxiosRequestConfig, options: IInterceptorOptionsHeaders, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {\n // locale\n const keyLocale = this.sys.env.APP_LOCALE_HEADER_KEY;\n if (keyLocale && !config.headers![keyLocale]) {\n config.headers![keyLocale] = this.app.meta.locale.current;\n }\n // tz\n const keyTz = this.sys.env.APP_TZ_HEADER_KEY;\n if (keyTz && !config.headers![keyTz]) {\n config.headers![keyTz] = this.app.meta.locale.tz;\n }\n // openapi schema\n if (options.openapiSchema) {\n config.headers![$protocolKey('x-vona-openapi-schema')] = true;\n }\n // next\n return next(config);\n }\n}\n","import type { AxiosRequestConfig } from 'axios';\nimport type { BeanFetch, IDecoratorInterceptorOptions, IInterceptorRequest, NextInterceptorRequest } from 'zova-module-a-fetch';\n\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\n\nimport type { IJwtAdapter, IJwtInfo } from '../types/jwt.js';\n\nexport interface IInterceptorOptionsJwt extends IDecoratorInterceptorOptions {\n jwtAdapter?: string;\n authToken?: boolean | string;\n}\n\n@Interceptor<IInterceptorOptionsJwt>({ dependencies: 'a-interceptor:headers' })\nexport class InterceptorJwt extends BeanInterceptorBase<IInterceptorOptionsJwt> implements IInterceptorRequest {\n private _beanJwtAdapter: IJwtAdapter;\n private _refreshAuthTokenPromise?: Promise<IJwtInfo>;\n\n protected async __init__(_beanFetch: BeanFetch, options: IInterceptorOptionsJwt) {\n const jwtAdapter = options.jwtAdapter || this.scope.config.jwtAdapter;\n const beanFullName = jwtAdapter.replace(':', '.service.');\n // singleton\n this._beanJwtAdapter = await this.app.bean._getBean(beanFullName as any, true);\n }\n\n async onRequest(config: AxiosRequestConfig, options: IInterceptorOptionsJwt, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {\n try {\n const accessToken = await this.prepareAccessToken(config, options.authToken);\n if (accessToken) {\n config.headers!.Authorization = `Bearer ${accessToken}`;\n }\n } catch (error: any) {\n error.config = config;\n error.request = undefined;\n error.response = undefined;\n throw error;\n }\n return next(config);\n }\n\n async prepareAccessToken(config: AxiosRequestConfig, authToken: string | boolean | undefined): Promise<string | undefined> {\n if (!this.sys.config.api.jwt) return;\n if (this.sys.config.ssr.ignoreCookieOnServer) return;\n // use default in scope.config rather than IInterceptorOptionsJwt.options\n const authTokenCurrent = authToken ?? this.scope.config.authToken.default;\n // authToken = authToken ?? this.scope.config.authToken.default;\n if (process.env.SERVER) {\n config.headers![$protocolKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;\n }\n // // authToken: false\n // if (authTokenCurrent === false) return;\n // authToken: string\n if (typeof authTokenCurrent === 'string') return authTokenCurrent;\n // if (typeof authToken === 'string') return authToken;\n // authToken: true\n let jwtInfo = await this._beanJwtAdapter.getJwtInfo();\n if (!jwtInfo) {\n if (authToken === true) {\n this.app.throw(401); // 401 rather than 403\n }\n return;\n }\n // accessToken\n if (process.env.SERVER || !jwtInfo.expireTime || jwtInfo.expireTime > Date.now()) {\n if (!jwtInfo.accessToken) {\n if (authToken === true) this.app.throw(401);\n return;\n }\n return jwtInfo.accessToken;\n }\n // refreshToken\n if (!jwtInfo.refreshToken) {\n if (authToken === true) this.app.throw(401);\n return;\n } else {\n if (authToken === false) {\n // need not refreshAuthToken, such as captcha.create\n return;\n }\n }\n jwtInfo = await this._refreshAuthToken(jwtInfo.refreshToken);\n return jwtInfo.accessToken;\n }\n\n private async _refreshAuthToken(refreshToken: string) {\n if (!this._refreshAuthTokenPromise) {\n this._refreshAuthTokenPromise = this._refreshAuthTokenInner(refreshToken);\n }\n return await this._refreshAuthTokenPromise;\n }\n\n private async _refreshAuthTokenInner(refreshToken: string) {\n try {\n return await this._beanJwtAdapter.refreshAuthToken(refreshToken);\n } finally {\n this._refreshAuthTokenPromise = undefined;\n }\n }\n}\n","import { AxiosError } from 'axios';\nimport {\n BeanInterceptorBase,\n IDecoratorInterceptorOptions,\n IInterceptorResponseError,\n Interceptor,\n NextInterceptorError,\n SymbolInterceptorBodyResponseFlag,\n} from 'zova-module-a-fetch';\n\nexport interface IInterceptorOptionsMock extends IDecoratorInterceptorOptions {}\n\n// not include: ERR_BAD_RESPONSE\nconst __ErrorsShouldBeMocked = ['ECONNREFUSED', 'ERR_NETWORK', '404'];\n\n@Interceptor<IInterceptorOptionsMock>()\nexport class InterceptorMock\n extends BeanInterceptorBase<IInterceptorOptionsMock>\n implements IInterceptorResponseError\n{\n async onResponseError(\n error: AxiosError,\n _options: IDecoratorInterceptorOptions,\n next: NextInterceptorError,\n ): Promise<AxiosError> {\n if (!(error instanceof Error)) return next();\n if (this.sys.env.MOCK_ENABLED === 'true') {\n if (process.env.DEV || (process.env.PROD && this.sys.env.MOCK_BUILD === 'true')) {\n if (__ErrorsShouldBeMocked.includes(String(error.code)) || error.status === 404) {\n const config = error.config!;\n if (config.baseURL) {\n const port = process.env.DEV\n ? this.sys.env.DEV_SERVER_PORT\n : this.sys.env.MOCK_BUILD_PORT;\n let baseURL = `http://localhost:${port}`;\n if (config.baseURL.endsWith(this.sys.env.API_PREFIX!)) {\n baseURL = `${baseURL}${this.sys.env.API_PREFIX}`;\n }\n if (config.baseURL !== baseURL && config.baseURL !== this.sys.env.API_PREFIX) {\n const response = await this.$fetch.request(Object.assign({}, config, { baseURL }));\n if (response && response[SymbolInterceptorBodyResponseFlag]) {\n return response.data;\n } else {\n return response as any;\n }\n }\n }\n }\n }\n }\n return next();\n }\n}\n","import { catchError } from '@cabloy/utils';\nimport { AxiosRequestConfig } from 'axios';\nimport { cast } from 'zova';\nimport {\n BeanInterceptorBase,\n IDecoratorInterceptorOptions,\n IInterceptorRequest,\n Interceptor,\n NextInterceptorRequest,\n} from 'zova-module-a-fetch';\nimport { ISsrSitePerformActionOptions } from 'zova-module-a-ssr';\n\nexport interface IInterceptorOptionsPerformAction extends IDecoratorInterceptorOptions {}\n\n@Interceptor<IInterceptorOptionsPerformAction>({ dependencies: 'a-interceptor:jwt' })\nexport class InterceptorPerformAction\n extends BeanInterceptorBase<IInterceptorOptionsPerformAction>\n implements IInterceptorRequest\n{\n async onRequest(\n config: AxiosRequestConfig,\n _options: IInterceptorOptionsPerformAction,\n next: NextInterceptorRequest,\n ): Promise<AxiosRequestConfig> {\n if (process.env.CLIENT) return next();\n const performAction = this.ctx.meta.$ssr.getPerformAction(config.baseURL);\n if (!performAction) return next();\n const data: ISsrSitePerformActionOptions = {\n method: config.method as any,\n path: config.url!,\n query: config.params,\n body: config.data,\n headers: config.headers,\n };\n const [result, error] = await catchError(() => {\n return performAction(data);\n });\n if (error) {\n cast(error).config = config;\n throw error;\n }\n throw result;\n }\n}\n","import type { ZovaSys } from 'zova';\nimport type { IServiceRecord } from 'zova-module-a-bean';\n\nexport const config = (_sys: ZovaSys) => {\n return {\n jwtAdapter: 'home-api:jwtAdapter' as keyof IServiceRecord,\n authToken: {\n default: true,\n },\n };\n};\n","// eslint-disable\n/** interceptor: begin */\nexport * from '../bean/interceptor.body.js';\nexport * from '../bean/interceptor.headers.js';\nexport * from '../bean/interceptor.jwt.js';\nexport * from '../bean/interceptor.mock.js';\nexport * from '../bean/interceptor.performAction.js';\nimport { IInterceptorOptionsBody } from '../bean/interceptor.body.js';\nimport { IInterceptorOptionsHeaders } from '../bean/interceptor.headers.js';\nimport { IInterceptorOptionsJwt } from '../bean/interceptor.jwt.js';\nimport { IInterceptorOptionsMock } from '../bean/interceptor.mock.js';\nimport { IInterceptorOptionsPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova-module-a-fetch';\ndeclare module 'zova-module-a-fetch' {\n \n export interface IInterceptorRecord {\n 'a-interceptor:body': IInterceptorOptionsBody;\n'a-interceptor:headers': IInterceptorOptionsHeaders;\n'a-interceptor:jwt': IInterceptorOptionsJwt;\n'a-interceptor:mock': IInterceptorOptionsMock;\n'a-interceptor:performAction': IInterceptorOptionsPerformAction;\n }\n\n \n}\ndeclare module 'zova-module-a-interceptor' {\n \n export interface InterceptorBody {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorBody {\n get $beanFullName(): 'a-interceptor.interceptor.body';\n get $onionName(): 'a-interceptor:body';\n get $onionOptions(): IInterceptorOptionsBody;\n }\n\n export interface InterceptorHeaders {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorHeaders {\n get $beanFullName(): 'a-interceptor.interceptor.headers';\n get $onionName(): 'a-interceptor:headers';\n get $onionOptions(): IInterceptorOptionsHeaders;\n }\n\n export interface InterceptorJwt {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorJwt {\n get $beanFullName(): 'a-interceptor.interceptor.jwt';\n get $onionName(): 'a-interceptor:jwt';\n get $onionOptions(): IInterceptorOptionsJwt;\n }\n\n export interface InterceptorMock {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorMock {\n get $beanFullName(): 'a-interceptor.interceptor.mock';\n get $onionName(): 'a-interceptor:mock';\n get $onionOptions(): IInterceptorOptionsMock;\n }\n\n export interface InterceptorPerformAction {\n /** @internal */\n get scope(): ScopeModuleAInterceptor;\n }\n\n export interface InterceptorPerformAction {\n get $beanFullName(): 'a-interceptor.interceptor.performAction';\n get $onionName(): 'a-interceptor:performAction';\n get $onionOptions(): IInterceptorOptionsPerformAction;\n } \n}\n/** interceptor: end */\n/** interceptor: begin */\nimport { InterceptorBody } from '../bean/interceptor.body.js';\nimport { InterceptorHeaders } from '../bean/interceptor.headers.js';\nimport { InterceptorJwt } from '../bean/interceptor.jwt.js';\nimport { InterceptorMock } from '../bean/interceptor.mock.js';\nimport { InterceptorPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordLocal {\n 'a-interceptor.interceptor.body': InterceptorBody;\n'a-interceptor.interceptor.headers': InterceptorHeaders;\n'a-interceptor.interceptor.jwt': InterceptorJwt;\n'a-interceptor.interceptor.mock': InterceptorMock;\n'a-interceptor.interceptor.performAction': InterceptorPerformAction;\n }\n}\n/** interceptor: end */\n/** config: begin */\nexport * from '../config/config.js';\nimport { config } from '../config/config.js';\n/** config: end */\n/** scope: begin */\nimport { BeanScopeBase, type BeanScopeUtil, TypeModuleConfig } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\n\n@Scope()\nexport class ScopeModuleAInterceptor extends BeanScopeBase {}\n\nexport interface ScopeModuleAInterceptor {\n util: BeanScopeUtil;\nconfig: TypeModuleConfig<typeof config>;\n}\n\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanScopeRecord {\n 'a-interceptor': ScopeModuleAInterceptor;\n }\n \n export interface IBeanScopeConfig {\n 'a-interceptor': ReturnType<typeof config>;\n }\n\n \n\n \n}\n \n/** scope: end */\n"],"mappings":";;;;;AAAA,IAAA,QAAO,SAAO;AAId,IAAE,mBAAyB,SAAA,YAAA,EACzB,cAAA,+BACD,CAAC,EAAA,UAAA,SAAuB,EACvB,QAAM,iBAAA,CAAA,EAAA,OAAA,WAAA,QAAA,WAAA,MAAA,wBAAA,oBAAA;CAER,MAAQ,WAAQ,UAAW,UAAA,MAAA;AAC3B,aAAO,MAAA,MAAA;EACL,MAAA,cAAmB,SAAA,QAAA;AACnB,MAAA,CAAA,eAAW,OAAA,gBAAA,YAAA,CAAA,YAAA,SAAA,mBAAA,EAAA;AACX,YAAA,qCAAiC;AAC7B,UAAO;;AAEb,MAAO,SAAU,KAAA,SAAA,GAAA;;AAEhB,SAAW,OAAC,SAAA,KAAA;AACP,SAAM,UAAC,SAAA,KAAA;AACX,SAAQ;;AAGR,SAAM,SAAU,KAAA,QA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,MAAQ,OAAA;GACR,OAAW,OAAA;GACb,MAAA,OAAA;GACE,SAAM,OAAS;GACb;EACA,MAAA,CAAA,QAAU,SAAA,MAAA,iBAAgC;AAC1C,UAAM,cAAA,KAAsB;IAC3B;AACD,MAAI,OAAO;AACX,QAAM,MAAA,CAAA,SAAgB;AACpB,SAAG;;AAEL,QAAE;;EAEJ,IAAI,SAAO,IAAA;;;AC9Bb,IAAa,UAAS,SAAQ;AAC9B,QAAO;;EAEP,WAAa,EACX,SAAO,MACL;EACD;;;;;;ACNH,IAAG,MAAO,OAAA;AAyBV,IAAQ,2BAAsB,OAAa,OAAA,EAAA,QAAA,SAAA,EAC1C,QAAA,iBACA,CAAC,EAAE,KAAI,SAAO,MAAS,SAAC,MAAA,gCAAgB,cAAA,GAAA,IAAA,OAAA,IAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zova-module-a-interceptor",
3
- "version": "5.1.14",
4
- "gitHead": "105e4dce1e85740f1f8a7ba9add2029beeaf9316",
3
+ "version": "5.1.15",
4
+ "gitHead": "a247f88a55c663d313296039f874560ee5a13e01",
5
5
  "description": "",
6
6
  "keywords": [
7
7
  "Zova Module"
@@ -8,16 +8,31 @@ import type {
8
8
  } from 'zova-module-a-fetch';
9
9
 
10
10
  import { cast } from 'zova';
11
- import { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';
11
+ import {
12
+ BeanInterceptorBase,
13
+ Interceptor,
14
+ SymbolInterceptorBodyResponseFlag,
15
+ } from 'zova-module-a-fetch';
12
16
 
13
17
  export interface IInterceptorOptionsBody extends IDecoratorInterceptorOptions {}
14
18
 
15
19
  @Interceptor<IInterceptorOptionsBody>({ dependencies: 'a-interceptor:performAction' })
16
- export class InterceptorBody extends BeanInterceptorBase<IInterceptorOptionsBody> implements IInterceptorResponse, IInterceptorResponseError {
17
- async onResponse(response: AxiosResponse, _options: IInterceptorOptionsBody, next: NextInterceptorResponse): Promise<AxiosResponse> {
20
+ export class InterceptorBody
21
+ extends BeanInterceptorBase<IInterceptorOptionsBody>
22
+ implements IInterceptorResponse, IInterceptorResponseError
23
+ {
24
+ async onResponse(
25
+ response: AxiosResponse,
26
+ _options: IInterceptorOptionsBody,
27
+ next: NextInterceptorResponse,
28
+ ): Promise<AxiosResponse> {
18
29
  response = await next();
19
30
  const contentType = response.headers['content-type'];
20
- if (!contentType || typeof contentType !== 'string' || !contentType.includes('application/json')) {
31
+ if (
32
+ !contentType ||
33
+ typeof contentType !== 'string' ||
34
+ !contentType.includes('application/json')
35
+ ) {
21
36
  response[SymbolInterceptorBodyResponseFlag] = true;
22
37
  return response;
23
38
  }
@@ -31,7 +46,11 @@ export class InterceptorBody extends BeanInterceptorBase<IInterceptorOptionsBody
31
46
  return response.data.data ?? null;
32
47
  }
33
48
 
34
- async onResponseError(error: AxiosError, _options: IInterceptorOptionsBody, next: NextInterceptorError): Promise<AxiosError> {
49
+ async onResponseError(
50
+ error: AxiosError,
51
+ _options: IInterceptorOptionsBody,
52
+ next: NextInterceptorError,
53
+ ): Promise<AxiosError> {
35
54
  error = await next();
36
55
  if (!(error instanceof Error)) return error;
37
56
  if (error.response) {
@@ -1,5 +1,5 @@
1
1
  import { AxiosRequestConfig } from 'axios';
2
- import { $customKey } from 'zova-core';
2
+ import { $protocolKey } from 'zova';
3
3
  import { BeanInterceptorBase, IDecoratorInterceptorOptions, Interceptor, NextInterceptorRequest } from 'zova-module-a-fetch';
4
4
 
5
5
  export interface IInterceptorOptionsHeaders extends IDecoratorInterceptorOptions {
@@ -21,7 +21,7 @@ export class InterceptorHeaders extends BeanInterceptorBase<IInterceptorOptionsH
21
21
  }
22
22
  // openapi schema
23
23
  if (options.openapiSchema) {
24
- config.headers![$customKey('x-vona-openapi-schema')] = true;
24
+ config.headers![$protocolKey('x-vona-openapi-schema')] = true;
25
25
  }
26
26
  // next
27
27
  return next(config);
@@ -1,7 +1,7 @@
1
1
  import type { AxiosRequestConfig } from 'axios';
2
2
  import type { BeanFetch, IDecoratorInterceptorOptions, IInterceptorRequest, NextInterceptorRequest } from 'zova-module-a-fetch';
3
3
 
4
- import { $customKey } from 'zova-core';
4
+ import { $protocolKey } from 'zova';
5
5
  import { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';
6
6
 
7
7
  import type { IJwtAdapter, IJwtInfo } from '../types/jwt.js';
@@ -45,7 +45,7 @@ export class InterceptorJwt extends BeanInterceptorBase<IInterceptorOptionsJwt>
45
45
  const authTokenCurrent = authToken ?? this.scope.config.authToken.default;
46
46
  // authToken = authToken ?? this.scope.config.authToken.default;
47
47
  if (process.env.SERVER) {
48
- config.headers![$customKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;
48
+ config.headers![$protocolKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;
49
49
  }
50
50
  // // authToken: false
51
51
  // if (authTokenCurrent === false) return;
@@ -14,15 +14,24 @@ export interface IInterceptorOptionsMock extends IDecoratorInterceptorOptions {}
14
14
  const __ErrorsShouldBeMocked = ['ECONNREFUSED', 'ERR_NETWORK', '404'];
15
15
 
16
16
  @Interceptor<IInterceptorOptionsMock>()
17
- export class InterceptorMock extends BeanInterceptorBase<IInterceptorOptionsMock> implements IInterceptorResponseError {
18
- async onResponseError(error: AxiosError, _options: IDecoratorInterceptorOptions, next: NextInterceptorError): Promise<AxiosError> {
17
+ export class InterceptorMock
18
+ extends BeanInterceptorBase<IInterceptorOptionsMock>
19
+ implements IInterceptorResponseError
20
+ {
21
+ async onResponseError(
22
+ error: AxiosError,
23
+ _options: IDecoratorInterceptorOptions,
24
+ next: NextInterceptorError,
25
+ ): Promise<AxiosError> {
19
26
  if (!(error instanceof Error)) return next();
20
27
  if (this.sys.env.MOCK_ENABLED === 'true') {
21
28
  if (process.env.DEV || (process.env.PROD && this.sys.env.MOCK_BUILD === 'true')) {
22
29
  if (__ErrorsShouldBeMocked.includes(String(error.code)) || error.status === 404) {
23
30
  const config = error.config!;
24
31
  if (config.baseURL) {
25
- const port = process.env.DEV ? this.sys.env.DEV_SERVER_PORT : this.sys.env.MOCK_BUILD_PORT;
32
+ const port = process.env.DEV
33
+ ? this.sys.env.DEV_SERVER_PORT
34
+ : this.sys.env.MOCK_BUILD_PORT;
26
35
  let baseURL = `http://localhost:${port}`;
27
36
  if (config.baseURL.endsWith(this.sys.env.API_PREFIX!)) {
28
37
  baseURL = `${baseURL}${this.sys.env.API_PREFIX}`;
@@ -1,14 +1,27 @@
1
1
  import { catchError } from '@cabloy/utils';
2
2
  import { AxiosRequestConfig } from 'axios';
3
3
  import { cast } from 'zova';
4
- import { BeanInterceptorBase, IDecoratorInterceptorOptions, IInterceptorRequest, Interceptor, NextInterceptorRequest } from 'zova-module-a-fetch';
4
+ import {
5
+ BeanInterceptorBase,
6
+ IDecoratorInterceptorOptions,
7
+ IInterceptorRequest,
8
+ Interceptor,
9
+ NextInterceptorRequest,
10
+ } from 'zova-module-a-fetch';
5
11
  import { ISsrSitePerformActionOptions } from 'zova-module-a-ssr';
6
12
 
7
13
  export interface IInterceptorOptionsPerformAction extends IDecoratorInterceptorOptions {}
8
14
 
9
15
  @Interceptor<IInterceptorOptionsPerformAction>({ dependencies: 'a-interceptor:jwt' })
10
- export class InterceptorPerformAction extends BeanInterceptorBase<IInterceptorOptionsPerformAction> implements IInterceptorRequest {
11
- async onRequest(config: AxiosRequestConfig, _options: IInterceptorOptionsPerformAction, next: NextInterceptorRequest): Promise<AxiosRequestConfig> {
16
+ export class InterceptorPerformAction
17
+ extends BeanInterceptorBase<IInterceptorOptionsPerformAction>
18
+ implements IInterceptorRequest
19
+ {
20
+ async onRequest(
21
+ config: AxiosRequestConfig,
22
+ _options: IInterceptorOptionsPerformAction,
23
+ next: NextInterceptorRequest,
24
+ ): Promise<AxiosRequestConfig> {
12
25
  if (process.env.CLIENT) return next();
13
26
  const performAction = this.ctx.meta.$ssr.getPerformAction(config.baseURL);
14
27
  if (!performAction) return next();