zova-module-a-fetch 5.0.0 → 5.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/LICENSE +21 -0
- package/cli/interceptor/boilerplate/{{sceneName}}.{{beanName}}.ts_ +7 -0
- package/dist/index.js +204 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -23
- package/src/.metadata/index.ts +105 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/bean.fetch.ts +75 -0
- package/src/bean/bean.interceptorBase.ts +18 -0
- package/src/config/config.ts +10 -0
- package/src/index.ts +3 -0
- package/src/lib/const.ts +1 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/interceptor.ts +7 -0
- package/src/monkey.ts +24 -0
- package/src/service/composer.ts +142 -0
- package/src/types/index.ts +1 -0
- package/src/types/interceptor.ts +75 -0
- package/README.md +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Zova
|
|
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,7 @@
|
|
|
1
|
+
import type { IDecoratorInterceptorOptions } from 'zova-module-a-fetch';
|
|
2
|
+
import { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';
|
|
3
|
+
|
|
4
|
+
export interface IInterceptorOptions<%=argv.beanNameCapitalize%> extends IDecoratorInterceptorOptions {}
|
|
5
|
+
|
|
6
|
+
@Interceptor<IInterceptorOptions<%=argv.beanNameCapitalize%>>()
|
|
7
|
+
export class Interceptor<%=argv.beanNameCapitalize%> extends BeanInterceptorBase<IInterceptorOptions<%=argv.beanNameCapitalize%>> {}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { BeanBase, BeanInfo, BeanScopeBase, BeanSimple, SymbolErrorInstanceInfo, Use, Virtual, cast, createBeanDecorator, deepExtend } from "zova";
|
|
2
|
+
import axios, { Axios } from "axios";
|
|
3
|
+
import { markRaw } from "vue";
|
|
4
|
+
import { Bean, Scope, Service } from "zova-module-a-bean";
|
|
5
|
+
//#region src/service/composer.ts
|
|
6
|
+
var _dec$3, _dec2$3, _dec3$1, _dec4$1, _class$3, _class2, _descriptor;
|
|
7
|
+
function _initializerDefineProperty(e, i, r, l) {
|
|
8
|
+
r && Object.defineProperty(e, i, {
|
|
9
|
+
enumerable: r.enumerable,
|
|
10
|
+
configurable: r.configurable,
|
|
11
|
+
writable: r.writable,
|
|
12
|
+
value: r.initializer ? r.initializer.call(l) : void 0
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function _applyDecoratedDescriptor(i, e, r, n, l) {
|
|
16
|
+
var a = {};
|
|
17
|
+
return Object.keys(n).forEach(function(i) {
|
|
18
|
+
a[i] = n[i];
|
|
19
|
+
}), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function(r, n) {
|
|
20
|
+
return n(i, e, r) || r;
|
|
21
|
+
}, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
|
|
22
|
+
}
|
|
23
|
+
var ServiceComposer = (_dec$3 = Service(), _dec2$3 = BeanInfo({ module: "a-fetch" }), _dec3$1 = Use("a-bean.sys.onion"), _dec4$1 = Reflect.metadata("design:type", typeof SysOnion === "undefined" ? Object : SysOnion), _dec$3(_class$3 = _dec2$3(_class$3 = (_class2 = class ServiceComposer extends BeanBase {
|
|
24
|
+
constructor(...args) {
|
|
25
|
+
super(...args);
|
|
26
|
+
this.$beanFetch = void 0;
|
|
27
|
+
_initializerDefineProperty(this, "$$sysOnion", _descriptor, this);
|
|
28
|
+
this._composerRequest = void 0;
|
|
29
|
+
this._composerRequestError = void 0;
|
|
30
|
+
this._composerResponse = void 0;
|
|
31
|
+
this._composerResponseError = void 0;
|
|
32
|
+
}
|
|
33
|
+
async __init__(beanFetch, onionItems) {
|
|
34
|
+
this.$beanFetch = beanFetch;
|
|
35
|
+
await this._createComposer(onionItems);
|
|
36
|
+
}
|
|
37
|
+
executeRequest(config) {
|
|
38
|
+
return this._composerRequest(config);
|
|
39
|
+
}
|
|
40
|
+
executeRequestError(error) {
|
|
41
|
+
return this._composerRequestError(error);
|
|
42
|
+
}
|
|
43
|
+
executeResponse(response) {
|
|
44
|
+
return this._composerResponse(response);
|
|
45
|
+
}
|
|
46
|
+
async executeResponseError(error) {
|
|
47
|
+
error = await this._composerResponseError(error);
|
|
48
|
+
if (error instanceof Error) error[SymbolErrorInstanceInfo] = {
|
|
49
|
+
instance: this.ctx.instance,
|
|
50
|
+
info: "executeResponseError"
|
|
51
|
+
};
|
|
52
|
+
return error;
|
|
53
|
+
}
|
|
54
|
+
async _createComposer(onionItems) {
|
|
55
|
+
let onionSlices;
|
|
56
|
+
if (onionItems) onionSlices = await this.$$sysOnion.interceptor.loadOnions(onionItems);
|
|
57
|
+
else onionSlices = await this.$$sysOnion.interceptor.loadOnionsFromPackage();
|
|
58
|
+
for (const onionSlice of onionSlices) onionSlice.beanInstance = await this.bean._newBean(onionSlice.beanFullName, true, this.$beanFetch, onionSlice.options);
|
|
59
|
+
this._composerRequest = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, config, next) => {
|
|
60
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
61
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config.url)) return next(config);
|
|
62
|
+
const beanInstance = cast(onionSlice.beanInstance);
|
|
63
|
+
if (!beanInstance.onRequest) return next(config);
|
|
64
|
+
return beanInstance.onRequest(config, options, next);
|
|
65
|
+
});
|
|
66
|
+
this._composerRequestError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error, next) => {
|
|
67
|
+
const config = error.config;
|
|
68
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
69
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) return next(error);
|
|
70
|
+
const beanInstance = cast(onionSlice.beanInstance);
|
|
71
|
+
if (!beanInstance.onRequestError) return next(error);
|
|
72
|
+
return beanInstance.onRequestError(error, options, next);
|
|
73
|
+
});
|
|
74
|
+
this._composerResponse = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, response, next) => {
|
|
75
|
+
const config = response.config;
|
|
76
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
77
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) return next(response);
|
|
78
|
+
const beanInstance = cast(onionSlice.beanInstance);
|
|
79
|
+
if (!beanInstance.onResponse) return next(response);
|
|
80
|
+
return beanInstance.onResponse(response, options, next);
|
|
81
|
+
});
|
|
82
|
+
this._composerResponseError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error, next) => {
|
|
83
|
+
const config = error.config;
|
|
84
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
85
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) return next(error);
|
|
86
|
+
const beanInstance = cast(onionSlice.beanInstance);
|
|
87
|
+
if (!beanInstance.onResponseError) return next(error);
|
|
88
|
+
return beanInstance.onResponseError(error, options, next);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
_combineOnionOptions(item, config) {
|
|
92
|
+
let optionsDynamic;
|
|
93
|
+
if (config?.interceptors) optionsDynamic = config?.interceptors[item.name];
|
|
94
|
+
return optionsDynamic ? deepExtend({}, item.options, optionsDynamic) : item.options;
|
|
95
|
+
}
|
|
96
|
+
}, _descriptor = _applyDecoratedDescriptor(_class2.prototype, "$$sysOnion", [_dec3$1, _dec4$1], {
|
|
97
|
+
configurable: true,
|
|
98
|
+
enumerable: true,
|
|
99
|
+
writable: true,
|
|
100
|
+
initializer: null
|
|
101
|
+
}), _class2)) || _class$3) || _class$3);
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/bean/bean.fetch.ts
|
|
104
|
+
var _dec$2, _dec2$2, _class$2;
|
|
105
|
+
var SymbolFetch = Symbol("SymbolFetch");
|
|
106
|
+
var BeanFetch$1 = (_dec$2 = Bean(), _dec2$2 = BeanInfo({ module: "a-fetch" }), _dec$2(_class$2 = _dec2$2(_class$2 = class BeanFetch extends BeanBase {
|
|
107
|
+
constructor(...args) {
|
|
108
|
+
super(...args);
|
|
109
|
+
this._composer = void 0;
|
|
110
|
+
this[SymbolFetch] = void 0;
|
|
111
|
+
}
|
|
112
|
+
async __init__(options) {
|
|
113
|
+
patchAxios(Axios);
|
|
114
|
+
const axiosConfig = deepExtend({}, { baseURL: this.sys.util.getApiBaseURL() }, this.scope.config.axios.config, options?.axiosConfig);
|
|
115
|
+
this._composer = await this.bean._newBean(ServiceComposer, true, this, options?.onionItems);
|
|
116
|
+
this[SymbolFetch] = markRaw(axios.create(axiosConfig));
|
|
117
|
+
this._addInterceptors(this[SymbolFetch]);
|
|
118
|
+
}
|
|
119
|
+
__get__(prop) {
|
|
120
|
+
return this[SymbolFetch] && this[SymbolFetch][prop];
|
|
121
|
+
}
|
|
122
|
+
_addInterceptors(api) {
|
|
123
|
+
api.interceptors.request.use(async (config) => {
|
|
124
|
+
return await this._composer.executeRequest(config);
|
|
125
|
+
}, async (_error) => {
|
|
126
|
+
if (!(_error instanceof Error)) return Promise.reject(_error);
|
|
127
|
+
const error = await this._composer.executeRequestError(_error);
|
|
128
|
+
return Promise.reject(error);
|
|
129
|
+
});
|
|
130
|
+
api.interceptors.response.use(async (response) => {
|
|
131
|
+
return await this._composer.executeResponse(response);
|
|
132
|
+
}, async (_error) => {
|
|
133
|
+
if (!(_error instanceof Error)) return Promise.reject(_error);
|
|
134
|
+
const error = await this._composer.executeResponseError(_error);
|
|
135
|
+
return Promise.reject(error);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}) || _class$2) || _class$2);
|
|
139
|
+
function patchAxios(_Axios) {
|
|
140
|
+
if (_Axios.__requestPatched) return;
|
|
141
|
+
_Axios.__requestPatched = true;
|
|
142
|
+
const requestPrev = _Axios.prototype.request;
|
|
143
|
+
_Axios.prototype.request = async function(...args) {
|
|
144
|
+
try {
|
|
145
|
+
return await requestPrev.call(this, ...args);
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (err instanceof Error) throw err;
|
|
148
|
+
return err;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/bean/bean.interceptorBase.ts
|
|
154
|
+
var _dec$1, _dec2$1, _dec3, _dec4, _dec5, _class$1;
|
|
155
|
+
var BeanInterceptorBase = (_dec$1 = Bean(), _dec2$1 = Virtual(), _dec3 = BeanInfo({ module: "a-fetch" }), _dec4 = Reflect.metadata("design:type", Function), _dec5 = Reflect.metadata("design:paramtypes", [typeof BeanFetch === "undefined" ? Object : BeanFetch, typeof T === "undefined" ? Object : T]), _dec$1(_class$1 = _dec2$1(_class$1 = _dec3(_class$1 = _dec4(_class$1 = _dec5(_class$1 = class BeanInterceptorBase extends BeanBase {
|
|
156
|
+
constructor(beanFetch, options) {
|
|
157
|
+
super();
|
|
158
|
+
this.$beanFetch = void 0;
|
|
159
|
+
this.$options = void 0;
|
|
160
|
+
this.$beanFetch = beanFetch;
|
|
161
|
+
this.$options = options;
|
|
162
|
+
}
|
|
163
|
+
}) || _class$1) || _class$1) || _class$1) || _class$1) || _class$1);
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/config/config.ts
|
|
166
|
+
var config = (_sys) => {
|
|
167
|
+
return { axios: { config: {} } };
|
|
168
|
+
};
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/monkey.ts
|
|
171
|
+
var Monkey = class extends BeanSimple {
|
|
172
|
+
async appInitialize() {
|
|
173
|
+
this.app.meta.$fetch = await this.bean._getBean(BeanFetch$1, false);
|
|
174
|
+
}
|
|
175
|
+
async beanInit(bean, beanInstance) {
|
|
176
|
+
const self = this;
|
|
177
|
+
bean.defineProperty(beanInstance, "$fetch", {
|
|
178
|
+
enumerable: false,
|
|
179
|
+
configurable: true,
|
|
180
|
+
get() {
|
|
181
|
+
return self.app.meta.$fetch;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/.metadata/index.ts
|
|
188
|
+
/** monkey: end */
|
|
189
|
+
/** scope: begin */
|
|
190
|
+
var _dec, _dec2, _class;
|
|
191
|
+
var ScopeModuleAFetch = (_dec = Scope(), _dec2 = BeanInfo({ module: "a-fetch" }), _dec(_class = _dec2(_class = class ScopeModuleAFetch extends BeanScopeBase {}) || _class) || _class);
|
|
192
|
+
/** scope: end */
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/lib/const.ts
|
|
195
|
+
var SymbolInterceptorBodyResponseFlag = Symbol("SymbolInterceptorBodyResponseFlag");
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/lib/interceptor.ts
|
|
198
|
+
function Interceptor(options) {
|
|
199
|
+
return createBeanDecorator("interceptor", "new", true, options);
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
export { BeanFetch$1 as BeanFetch, BeanInterceptorBase, Interceptor, Monkey, ScopeModuleAFetch, ServiceComposer, SymbolInterceptorBodyResponseFlag, config };
|
|
203
|
+
|
|
204
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/service/composer.ts","../src/bean/bean.fetch.ts","../src/bean/bean.interceptorBase.ts","../src/config/config.ts","../src/monkey.ts","../src/.metadata/index.ts","../src/lib/const.ts","../src/lib/interceptor.ts"],"sourcesContent":["import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';\n\nimport { BeanBase, cast, deepExtend, SymbolErrorInstanceInfo, Use } from 'zova';\nimport { IOnionItem, IOnionSlice, SysOnion, TypeComposer } from 'zova-module-a-bean';\nimport { Service } from 'zova-module-a-bean';\n\nimport type { BeanFetch } from '../bean/bean.fetch.js';\nimport type {\n IDecoratorInterceptorOptions,\n IInterceptorRecord,\n IInterceptorRequest,\n IInterceptorRequestError,\n IInterceptorResponse,\n IInterceptorResponseError,\n} from '../types/interceptor.js';\n\n@Service()\nexport class ServiceComposer extends BeanBase {\n protected $beanFetch: BeanFetch;\n @Use()\n $$sysOnion: SysOnion;\n\n private _composerRequest: TypeComposer;\n private _composerRequestError: TypeComposer;\n private _composerResponse: TypeComposer;\n private _composerResponseError: TypeComposer;\n\n protected async __init__(\n beanFetch: BeanFetch,\n onionItems?:\n | IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>\n | IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[],\n ) {\n this.$beanFetch = beanFetch;\n await this._createComposer(onionItems);\n }\n\n public executeRequest(config: AxiosRequestConfig) {\n return this._composerRequest(config);\n }\n\n public executeRequestError(error: AxiosError) {\n return this._composerRequestError(error);\n }\n\n public executeResponse(response: AxiosResponse) {\n return this._composerResponse(response);\n }\n\n public async executeResponseError(error: AxiosError) {\n error = await this._composerResponseError(error);\n if (error instanceof Error) {\n error[SymbolErrorInstanceInfo] = {\n instance: this.ctx.instance,\n info: 'executeResponseError',\n };\n }\n return error;\n }\n\n private async _createComposer(\n onionItems?:\n | IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>\n | IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[],\n ) {\n // onionSlices\n let onionSlices: IOnionSlice<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[];\n if (onionItems) {\n onionSlices = await this.$$sysOnion.interceptor.loadOnions(onionItems);\n } else {\n onionSlices = await this.$$sysOnion.interceptor.loadOnionsFromPackage();\n }\n // create interceptors\n for (const onionSlice of onionSlices) {\n onionSlice.beanInstance = await this.bean._newBean(onionSlice.beanFullName as any, true, this.$beanFetch, onionSlice.options);\n }\n // compose\n this._composerRequest = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, config: AxiosRequestConfig, next) => {\n // options\n const options = this._combineOnionOptions(onionSlice, config);\n // enable match ignore\n if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config.url)) {\n return next(config);\n }\n // execute\n const beanInstance = cast<IInterceptorRequest>(onionSlice.beanInstance);\n if (!beanInstance.onRequest) return next(config);\n return beanInstance.onRequest(config, options, next as any);\n });\n this._composerRequestError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error: AxiosError, next) => {\n const config = error.config;\n // options\n const options = this._combineOnionOptions(onionSlice, config);\n // enable match ignore\n if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {\n return next(error);\n }\n // execute\n const beanInstance = cast<IInterceptorRequestError>(onionSlice.beanInstance);\n if (!beanInstance.onRequestError) return next(error);\n return beanInstance.onRequestError(error, options, next as any);\n });\n this._composerResponse = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, response: AxiosResponse, next) => {\n const config = response.config;\n // options\n const options = this._combineOnionOptions(onionSlice, config);\n // enable match ignore\n if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {\n return next(response);\n }\n // execute\n const beanInstance = cast<IInterceptorResponse>(onionSlice.beanInstance);\n if (!beanInstance.onResponse) return next(response);\n return beanInstance.onResponse(response, options, next as any);\n });\n this._composerResponseError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error: AxiosError, next) => {\n const config = error.config;\n // options\n const options = this._combineOnionOptions(onionSlice, config);\n // enable match ignore\n if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {\n return next(error);\n }\n // execute\n const beanInstance = cast<IInterceptorResponseError>(onionSlice.beanInstance);\n if (!beanInstance.onResponseError) return next(error);\n return beanInstance.onResponseError(error, options, next as any);\n });\n }\n\n private _combineOnionOptions(item: IOnionSlice<IDecoratorInterceptorOptions, keyof IInterceptorRecord>, config?: AxiosRequestConfig) {\n // options: dynamic\n let optionsDynamic;\n if (config?.interceptors) {\n optionsDynamic = config?.interceptors[item.name];\n }\n // final options\n const options = optionsDynamic ? deepExtend({}, item.options, optionsDynamic) : item.options;\n // ok\n return options;\n }\n}\n","import type { AxiosInstance } from 'axios';\n\nimport axios, { Axios } from 'axios';\nimport { markRaw } from 'vue';\nimport { BeanBase, deepExtend } from 'zova';\nimport { Bean } from 'zova-module-a-bean';\n\nimport type { IBeanFetchOptions } from '../types/interceptor.js';\n\nimport { ServiceComposer } from '../service/composer.js';\n\nconst SymbolFetch = Symbol('SymbolFetch');\n\nexport interface BeanFetch extends AxiosInstance {}\n\n@Bean()\nexport class BeanFetch extends BeanBase {\n private _composer: ServiceComposer;\n\n protected [SymbolFetch]: AxiosInstance;\n\n protected async __init__(options?: IBeanFetchOptions) {\n patchAxios(Axios);\n // axiosConfig\n const axiosConfig = deepExtend({}, { baseURL: this.sys.util.getApiBaseURL() }, this.scope.config.axios.config, options?.axiosConfig);\n // composer\n this._composer = await this.bean._newBean(ServiceComposer, true, this, options?.onionItems);\n // axios\n this[SymbolFetch] = markRaw(axios.create(axiosConfig));\n this._addInterceptors(this[SymbolFetch]);\n }\n\n protected __get__(prop: string) {\n return this[SymbolFetch] && this[SymbolFetch][prop];\n }\n\n private _addInterceptors(api: AxiosInstance) {\n // request\n api.interceptors.request.use(\n async config => {\n return await this._composer.executeRequest(config);\n },\n async (_error: any) => {\n if (!(_error instanceof Error)) return Promise.reject(_error);\n const error = await this._composer.executeRequestError(_error as any);\n return Promise.reject(error);\n },\n );\n // response\n api.interceptors.response.use(\n async response => {\n return await this._composer.executeResponse(response);\n },\n async (_error: any) => {\n if (!(_error instanceof Error)) return Promise.reject(_error);\n const error = await this._composer.executeResponseError(_error as any);\n return Promise.reject(error);\n },\n );\n }\n}\n\nfunction patchAxios(_Axios: any) {\n if (_Axios.__requestPatched) return;\n _Axios.__requestPatched = true;\n const requestPrev: Function = _Axios.prototype.request;\n _Axios.prototype.request = async function (...args: any[]) {\n try {\n return await requestPrev.call(this, ...args);\n } catch (err) {\n if (err instanceof Error) throw err;\n return err;\n }\n };\n}\n","import { BeanBase, Virtual } from 'zova';\nimport { Bean } from 'zova-module-a-bean';\n\nimport type { IDecoratorInterceptorOptions } from '../types/interceptor.js';\nimport type { BeanFetch } from './bean.fetch.js';\n\n@Bean()\n@Virtual()\nexport class BeanInterceptorBase<T extends IDecoratorInterceptorOptions = IDecoratorInterceptorOptions> extends BeanBase {\n protected $beanFetch: BeanFetch;\n protected $options: T;\n\n constructor(beanFetch: BeanFetch, options: T) {\n super();\n this.$beanFetch = beanFetch;\n this.$options = options;\n }\n}\n","import type { CreateAxiosDefaults } from 'axios';\nimport type { ZovaSys } from 'zova';\n\nexport const config = (_sys: ZovaSys) => {\n return {\n axios: {\n config: {} as CreateAxiosDefaults,\n },\n };\n};\n","import type { BeanBase, BeanContainer, IMonkeyAppInitialize, IMonkeyBeanInit } from 'zova';\n\nimport { BeanSimple } from 'zova';\n\nimport { BeanFetch } from './bean/bean.fetch.js';\n\nexport class Monkey extends BeanSimple implements IMonkeyAppInitialize, IMonkeyBeanInit {\n async appInitialize() {\n // $fetch: should in appInitialize, because should init many interceptors\n this.app.meta.$fetch = await this.bean._getBean(BeanFetch, false);\n }\n\n async beanInit(bean: BeanContainer, beanInstance: BeanBase) {\n const self = this;\n // $fetch\n bean.defineProperty(beanInstance, '$fetch', {\n enumerable: false,\n configurable: true,\n get() {\n return self.app.meta.$fetch;\n },\n });\n }\n}\n","// eslint-disable\n/** bean: begin */\nexport * from '../bean/bean.fetch.js';\nexport * from '../bean/bean.interceptorBase.js';\n\nimport 'zova';\ndeclare module 'zova' {\n \n \n}\ndeclare module 'zova-module-a-fetch' {\n \n export interface BeanFetch {\n /** @internal */\n get scope(): ScopeModuleAFetch;\n }\n\n export interface BeanFetch {\n get $beanFullName(): 'a-fetch.bean.fetch';\n get $onionName(): 'a-fetch:fetch';\n \n } \n}\n/** bean: end */\n/** bean: begin */\nimport { BeanFetch } from '../bean/bean.fetch.js';\nimport { BeanInterceptorBase } from '../bean/bean.interceptorBase.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordGeneral {\n 'a-fetch.bean.fetch': BeanFetch;\n'a-fetch.bean.interceptorBase': BeanInterceptorBase;\n }\n}\n/** bean: end */\n/** service: begin */\nexport * from '../service/composer.js';\n\nimport 'zova-module-a-bean';\ndeclare module 'zova-module-a-bean' {\n \n export interface IServiceRecord {\n 'a-fetch:composer': never;\n }\n\n \n}\ndeclare module 'zova-module-a-fetch' {\n \n export interface ServiceComposer {\n /** @internal */\n get scope(): ScopeModuleAFetch;\n }\n\n export interface ServiceComposer {\n get $beanFullName(): 'a-fetch.service.composer';\n get $onionName(): 'a-fetch:composer';\n \n } \n}\n/** service: end */\n/** service: begin */\nimport { ServiceComposer } from '../service/composer.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordGeneral {\n 'a-fetch.service.composer': ServiceComposer;\n }\n}\n/** service: end */\n/** config: begin */\nexport * from '../config/config.js';\nimport { config } from '../config/config.js';\n/** config: end */\n/** monkey: begin */\nexport * from '../monkey.js';\n/** monkey: end */\n/** scope: begin */\nimport { BeanScopeBase, type BeanScopeUtil, TypeModuleConfig } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\n\n@Scope()\nexport class ScopeModuleAFetch extends BeanScopeBase {}\n\nexport interface ScopeModuleAFetch {\n util: BeanScopeUtil;\nconfig: TypeModuleConfig<typeof config>;\n}\n\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanScopeRecord {\n 'a-fetch': ScopeModuleAFetch;\n }\n \n export interface IBeanScopeConfig {\n 'a-fetch': ReturnType<typeof config>;\n }\n\n \n\n \n}\n \n/** scope: end */\n","export const SymbolInterceptorBodyResponseFlag = Symbol('SymbolInterceptorBodyResponseFlag');\n","import { createBeanDecorator } from 'zova';\n\nimport type { IDecoratorInterceptorOptions } from '../types/interceptor.js';\n\nexport function Interceptor<T extends IDecoratorInterceptorOptions>(options?: T): ClassDecorator {\n return createBeanDecorator('interceptor', 'new', true, options);\n}\n"],"mappings":";;;;;AAAA,IAAA,QAAO,SAAO,SAAU,SAAE,UAAA,SAAoB;;;;;;;;;AAE9C,SAAS,0BAA0B,GAAE,GAAA,GAAA,GAAA,GAAA;CAAA,IAAA,IAAuB,EAAE;AAAA,QAAM,OAAU,KAAC,EAAA,CAAA,QAAA,SAAA,GAAA;AAAA,IAAA,KAAA,EAAA;GAAA,EAAA,EAAA,aAAA,CAAA,CAAA,EAAA,YAAA,EAAA,eAAA,CAAA,CAAA,EAAA,eAAA,WAAA,KAAA,EAAA,iBAAA,EAAA,WAAA,CAAA,IAAA,IAAA,EAAA,OAAA,CAAA,SAAA,CAAA,OAAA,SAAA,GAAA,GAAA;AAAA,SAAA,EAAA,GAAA,GAAA,EAAA,IAAA;IAAA,EA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AAU,OAAmB,OAAA,EAAA,QAAA,SAAA,EACpC,QAAK,WACN,CAAC,EAAC,KAAM,SAAK,MAAA,SAAkB,MAAA,0BAAmB,cAAA,GAAA,IAAA,OAAA,IAAA;;;;AC/BnD,IAAa,oCAAoC,OAAO,oCAAoC;;;;ACE5F,QAAO,oBAAO,eAA8B,OAAO,MAAG,QAAM"}
|
package/package.json
CHANGED
|
@@ -1,37 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zova-module-a-fetch",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Zova Module"
|
|
7
|
+
],
|
|
8
|
+
"author": "",
|
|
9
|
+
"files": [
|
|
10
|
+
"cli",
|
|
11
|
+
"dist",
|
|
12
|
+
"mock",
|
|
13
|
+
"src",
|
|
14
|
+
"icons",
|
|
15
|
+
"assets",
|
|
16
|
+
"rest"
|
|
17
|
+
],
|
|
4
18
|
"type": "module",
|
|
5
|
-
"publishConfig": {
|
|
6
|
-
"access": "public"
|
|
7
|
-
},
|
|
8
19
|
"exports": {
|
|
9
20
|
".": {
|
|
10
21
|
"types": [
|
|
11
22
|
"./src/index.ts",
|
|
12
23
|
"./dist/index.d.ts"
|
|
13
24
|
],
|
|
14
|
-
"
|
|
15
|
-
"import": "./dist/index.js",
|
|
16
|
-
"default": "./src/index.ts"
|
|
25
|
+
"default": "./dist/index.js"
|
|
17
26
|
},
|
|
18
|
-
"
|
|
27
|
+
"./*": "./*"
|
|
19
28
|
},
|
|
20
|
-
"description": "",
|
|
21
|
-
"files": [
|
|
22
|
-
"dist",
|
|
23
|
-
"static",
|
|
24
|
-
"typings"
|
|
25
|
-
],
|
|
26
29
|
"scripts": {
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
30
|
+
"clean": "rimraf dist tsconfig.build.tsbuildinfo",
|
|
31
|
+
"tsc:publish": "npm run clean && zova :bin:buildModule --sourcemap",
|
|
32
|
+
"prepublishOnly": "npm run tsc:publish",
|
|
33
|
+
"prepack": "clean-package",
|
|
34
|
+
"postpack": "clean-package restore && npm run clean"
|
|
31
35
|
},
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"axios": "^1.13.4"
|
|
38
|
+
},
|
|
39
|
+
"title": "a-fetch",
|
|
40
|
+
"zovaModule": {
|
|
41
|
+
"capabilities": {
|
|
42
|
+
"monkey": true
|
|
43
|
+
},
|
|
44
|
+
"globalDependencies": {
|
|
45
|
+
"axios": true
|
|
46
|
+
},
|
|
47
|
+
"onions": {
|
|
48
|
+
"interceptor": {
|
|
49
|
+
"optionsPackage": true,
|
|
50
|
+
"optionsGlobalInterfaceName": "IDecoratorInterceptorOptions",
|
|
51
|
+
"optionsGlobalInterfaceFrom": "zova-module-a-fetch",
|
|
52
|
+
"boilerplate": "interceptor/boilerplate"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"bundle": {
|
|
56
|
+
"vendors": [
|
|
57
|
+
{
|
|
58
|
+
"match": [
|
|
59
|
+
"axios"
|
|
60
|
+
],
|
|
61
|
+
"output": "axios"
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"gitHead": "bf1ceb4582381c74ba41ccf11b89d3b9e7b73628"
|
|
37
67
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// eslint-disable
|
|
2
|
+
/** bean: begin */
|
|
3
|
+
export * from '../bean/bean.fetch.js';
|
|
4
|
+
export * from '../bean/bean.interceptorBase.js';
|
|
5
|
+
|
|
6
|
+
import 'zova';
|
|
7
|
+
declare module 'zova' {
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
declare module 'zova-module-a-fetch' {
|
|
12
|
+
|
|
13
|
+
export interface BeanFetch {
|
|
14
|
+
/** @internal */
|
|
15
|
+
get scope(): ScopeModuleAFetch;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface BeanFetch {
|
|
19
|
+
get $beanFullName(): 'a-fetch.bean.fetch';
|
|
20
|
+
get $onionName(): 'a-fetch:fetch';
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** bean: end */
|
|
25
|
+
/** bean: begin */
|
|
26
|
+
import { BeanFetch } from '../bean/bean.fetch.js';
|
|
27
|
+
import { BeanInterceptorBase } from '../bean/bean.interceptorBase.js';
|
|
28
|
+
import 'zova';
|
|
29
|
+
declare module 'zova' {
|
|
30
|
+
export interface IBeanRecordGeneral {
|
|
31
|
+
'a-fetch.bean.fetch': BeanFetch;
|
|
32
|
+
'a-fetch.bean.interceptorBase': BeanInterceptorBase;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** bean: end */
|
|
36
|
+
/** service: begin */
|
|
37
|
+
export * from '../service/composer.js';
|
|
38
|
+
|
|
39
|
+
import 'zova-module-a-bean';
|
|
40
|
+
declare module 'zova-module-a-bean' {
|
|
41
|
+
|
|
42
|
+
export interface IServiceRecord {
|
|
43
|
+
'a-fetch:composer': never;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
declare module 'zova-module-a-fetch' {
|
|
49
|
+
|
|
50
|
+
export interface ServiceComposer {
|
|
51
|
+
/** @internal */
|
|
52
|
+
get scope(): ScopeModuleAFetch;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ServiceComposer {
|
|
56
|
+
get $beanFullName(): 'a-fetch.service.composer';
|
|
57
|
+
get $onionName(): 'a-fetch:composer';
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** service: end */
|
|
62
|
+
/** service: begin */
|
|
63
|
+
import { ServiceComposer } from '../service/composer.js';
|
|
64
|
+
import 'zova';
|
|
65
|
+
declare module 'zova' {
|
|
66
|
+
export interface IBeanRecordGeneral {
|
|
67
|
+
'a-fetch.service.composer': ServiceComposer;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** service: end */
|
|
71
|
+
/** config: begin */
|
|
72
|
+
export * from '../config/config.js';
|
|
73
|
+
import { config } from '../config/config.js';
|
|
74
|
+
/** config: end */
|
|
75
|
+
/** monkey: begin */
|
|
76
|
+
export * from '../monkey.js';
|
|
77
|
+
/** monkey: end */
|
|
78
|
+
/** scope: begin */
|
|
79
|
+
import { BeanScopeBase, type BeanScopeUtil, TypeModuleConfig } from 'zova';
|
|
80
|
+
import { Scope } from 'zova-module-a-bean';
|
|
81
|
+
|
|
82
|
+
@Scope()
|
|
83
|
+
export class ScopeModuleAFetch extends BeanScopeBase {}
|
|
84
|
+
|
|
85
|
+
export interface ScopeModuleAFetch {
|
|
86
|
+
util: BeanScopeUtil;
|
|
87
|
+
config: TypeModuleConfig<typeof config>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
import 'zova';
|
|
91
|
+
declare module 'zova' {
|
|
92
|
+
export interface IBeanScopeRecord {
|
|
93
|
+
'a-fetch': ScopeModuleAFetch;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface IBeanScopeConfig {
|
|
97
|
+
'a-fetch': ReturnType<typeof config>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** scope: end */
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
import axios, { Axios } from 'axios';
|
|
4
|
+
import { markRaw } from 'vue';
|
|
5
|
+
import { BeanBase, deepExtend } from 'zova';
|
|
6
|
+
import { Bean } from 'zova-module-a-bean';
|
|
7
|
+
|
|
8
|
+
import type { IBeanFetchOptions } from '../types/interceptor.js';
|
|
9
|
+
|
|
10
|
+
import { ServiceComposer } from '../service/composer.js';
|
|
11
|
+
|
|
12
|
+
const SymbolFetch = Symbol('SymbolFetch');
|
|
13
|
+
|
|
14
|
+
export interface BeanFetch extends AxiosInstance {}
|
|
15
|
+
|
|
16
|
+
@Bean()
|
|
17
|
+
export class BeanFetch extends BeanBase {
|
|
18
|
+
private _composer: ServiceComposer;
|
|
19
|
+
|
|
20
|
+
protected [SymbolFetch]: AxiosInstance;
|
|
21
|
+
|
|
22
|
+
protected async __init__(options?: IBeanFetchOptions) {
|
|
23
|
+
patchAxios(Axios);
|
|
24
|
+
// axiosConfig
|
|
25
|
+
const axiosConfig = deepExtend({}, { baseURL: this.sys.util.getApiBaseURL() }, this.scope.config.axios.config, options?.axiosConfig);
|
|
26
|
+
// composer
|
|
27
|
+
this._composer = await this.bean._newBean(ServiceComposer, true, this, options?.onionItems);
|
|
28
|
+
// axios
|
|
29
|
+
this[SymbolFetch] = markRaw(axios.create(axiosConfig));
|
|
30
|
+
this._addInterceptors(this[SymbolFetch]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected __get__(prop: string) {
|
|
34
|
+
return this[SymbolFetch] && this[SymbolFetch][prop];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private _addInterceptors(api: AxiosInstance) {
|
|
38
|
+
// request
|
|
39
|
+
api.interceptors.request.use(
|
|
40
|
+
async config => {
|
|
41
|
+
return await this._composer.executeRequest(config);
|
|
42
|
+
},
|
|
43
|
+
async (_error: any) => {
|
|
44
|
+
if (!(_error instanceof Error)) return Promise.reject(_error);
|
|
45
|
+
const error = await this._composer.executeRequestError(_error as any);
|
|
46
|
+
return Promise.reject(error);
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
// response
|
|
50
|
+
api.interceptors.response.use(
|
|
51
|
+
async response => {
|
|
52
|
+
return await this._composer.executeResponse(response);
|
|
53
|
+
},
|
|
54
|
+
async (_error: any) => {
|
|
55
|
+
if (!(_error instanceof Error)) return Promise.reject(_error);
|
|
56
|
+
const error = await this._composer.executeResponseError(_error as any);
|
|
57
|
+
return Promise.reject(error);
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function patchAxios(_Axios: any) {
|
|
64
|
+
if (_Axios.__requestPatched) return;
|
|
65
|
+
_Axios.__requestPatched = true;
|
|
66
|
+
const requestPrev: Function = _Axios.prototype.request;
|
|
67
|
+
_Axios.prototype.request = async function (...args: any[]) {
|
|
68
|
+
try {
|
|
69
|
+
return await requestPrev.call(this, ...args);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (err instanceof Error) throw err;
|
|
72
|
+
return err;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BeanBase, Virtual } from 'zova';
|
|
2
|
+
import { Bean } from 'zova-module-a-bean';
|
|
3
|
+
|
|
4
|
+
import type { IDecoratorInterceptorOptions } from '../types/interceptor.js';
|
|
5
|
+
import type { BeanFetch } from './bean.fetch.js';
|
|
6
|
+
|
|
7
|
+
@Bean()
|
|
8
|
+
@Virtual()
|
|
9
|
+
export class BeanInterceptorBase<T extends IDecoratorInterceptorOptions = IDecoratorInterceptorOptions> extends BeanBase {
|
|
10
|
+
protected $beanFetch: BeanFetch;
|
|
11
|
+
protected $options: T;
|
|
12
|
+
|
|
13
|
+
constructor(beanFetch: BeanFetch, options: T) {
|
|
14
|
+
super();
|
|
15
|
+
this.$beanFetch = beanFetch;
|
|
16
|
+
this.$options = options;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
package/src/lib/const.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SymbolInterceptorBodyResponseFlag = Symbol('SymbolInterceptorBodyResponseFlag');
|
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createBeanDecorator } from 'zova';
|
|
2
|
+
|
|
3
|
+
import type { IDecoratorInterceptorOptions } from '../types/interceptor.js';
|
|
4
|
+
|
|
5
|
+
export function Interceptor<T extends IDecoratorInterceptorOptions>(options?: T): ClassDecorator {
|
|
6
|
+
return createBeanDecorator('interceptor', 'new', true, options);
|
|
7
|
+
}
|
package/src/monkey.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BeanBase, BeanContainer, IMonkeyAppInitialize, IMonkeyBeanInit } from 'zova';
|
|
2
|
+
|
|
3
|
+
import { BeanSimple } from 'zova';
|
|
4
|
+
|
|
5
|
+
import { BeanFetch } from './bean/bean.fetch.js';
|
|
6
|
+
|
|
7
|
+
export class Monkey extends BeanSimple implements IMonkeyAppInitialize, IMonkeyBeanInit {
|
|
8
|
+
async appInitialize() {
|
|
9
|
+
// $fetch: should in appInitialize, because should init many interceptors
|
|
10
|
+
this.app.meta.$fetch = await this.bean._getBean(BeanFetch, false);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async beanInit(bean: BeanContainer, beanInstance: BeanBase) {
|
|
14
|
+
const self = this;
|
|
15
|
+
// $fetch
|
|
16
|
+
bean.defineProperty(beanInstance, '$fetch', {
|
|
17
|
+
enumerable: false,
|
|
18
|
+
configurable: true,
|
|
19
|
+
get() {
|
|
20
|
+
return self.app.meta.$fetch;
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
|
|
3
|
+
import { BeanBase, cast, deepExtend, SymbolErrorInstanceInfo, Use } from 'zova';
|
|
4
|
+
import { IOnionItem, IOnionSlice, SysOnion, TypeComposer } from 'zova-module-a-bean';
|
|
5
|
+
import { Service } from 'zova-module-a-bean';
|
|
6
|
+
|
|
7
|
+
import type { BeanFetch } from '../bean/bean.fetch.js';
|
|
8
|
+
import type {
|
|
9
|
+
IDecoratorInterceptorOptions,
|
|
10
|
+
IInterceptorRecord,
|
|
11
|
+
IInterceptorRequest,
|
|
12
|
+
IInterceptorRequestError,
|
|
13
|
+
IInterceptorResponse,
|
|
14
|
+
IInterceptorResponseError,
|
|
15
|
+
} from '../types/interceptor.js';
|
|
16
|
+
|
|
17
|
+
@Service()
|
|
18
|
+
export class ServiceComposer extends BeanBase {
|
|
19
|
+
protected $beanFetch: BeanFetch;
|
|
20
|
+
@Use()
|
|
21
|
+
$$sysOnion: SysOnion;
|
|
22
|
+
|
|
23
|
+
private _composerRequest: TypeComposer;
|
|
24
|
+
private _composerRequestError: TypeComposer;
|
|
25
|
+
private _composerResponse: TypeComposer;
|
|
26
|
+
private _composerResponseError: TypeComposer;
|
|
27
|
+
|
|
28
|
+
protected async __init__(
|
|
29
|
+
beanFetch: BeanFetch,
|
|
30
|
+
onionItems?:
|
|
31
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>
|
|
32
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[],
|
|
33
|
+
) {
|
|
34
|
+
this.$beanFetch = beanFetch;
|
|
35
|
+
await this._createComposer(onionItems);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public executeRequest(config: AxiosRequestConfig) {
|
|
39
|
+
return this._composerRequest(config);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public executeRequestError(error: AxiosError) {
|
|
43
|
+
return this._composerRequestError(error);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public executeResponse(response: AxiosResponse) {
|
|
47
|
+
return this._composerResponse(response);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async executeResponseError(error: AxiosError) {
|
|
51
|
+
error = await this._composerResponseError(error);
|
|
52
|
+
if (error instanceof Error) {
|
|
53
|
+
error[SymbolErrorInstanceInfo] = {
|
|
54
|
+
instance: this.ctx.instance,
|
|
55
|
+
info: 'executeResponseError',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return error;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private async _createComposer(
|
|
62
|
+
onionItems?:
|
|
63
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>
|
|
64
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[],
|
|
65
|
+
) {
|
|
66
|
+
// onionSlices
|
|
67
|
+
let onionSlices: IOnionSlice<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[];
|
|
68
|
+
if (onionItems) {
|
|
69
|
+
onionSlices = await this.$$sysOnion.interceptor.loadOnions(onionItems);
|
|
70
|
+
} else {
|
|
71
|
+
onionSlices = await this.$$sysOnion.interceptor.loadOnionsFromPackage();
|
|
72
|
+
}
|
|
73
|
+
// create interceptors
|
|
74
|
+
for (const onionSlice of onionSlices) {
|
|
75
|
+
onionSlice.beanInstance = await this.bean._newBean(onionSlice.beanFullName as any, true, this.$beanFetch, onionSlice.options);
|
|
76
|
+
}
|
|
77
|
+
// compose
|
|
78
|
+
this._composerRequest = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, config: AxiosRequestConfig, next) => {
|
|
79
|
+
// options
|
|
80
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
81
|
+
// enable match ignore
|
|
82
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config.url)) {
|
|
83
|
+
return next(config);
|
|
84
|
+
}
|
|
85
|
+
// execute
|
|
86
|
+
const beanInstance = cast<IInterceptorRequest>(onionSlice.beanInstance);
|
|
87
|
+
if (!beanInstance.onRequest) return next(config);
|
|
88
|
+
return beanInstance.onRequest(config, options, next as any);
|
|
89
|
+
});
|
|
90
|
+
this._composerRequestError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error: AxiosError, next) => {
|
|
91
|
+
const config = error.config;
|
|
92
|
+
// options
|
|
93
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
94
|
+
// enable match ignore
|
|
95
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {
|
|
96
|
+
return next(error);
|
|
97
|
+
}
|
|
98
|
+
// execute
|
|
99
|
+
const beanInstance = cast<IInterceptorRequestError>(onionSlice.beanInstance);
|
|
100
|
+
if (!beanInstance.onRequestError) return next(error);
|
|
101
|
+
return beanInstance.onRequestError(error, options, next as any);
|
|
102
|
+
});
|
|
103
|
+
this._composerResponse = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, response: AxiosResponse, next) => {
|
|
104
|
+
const config = response.config;
|
|
105
|
+
// options
|
|
106
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
107
|
+
// enable match ignore
|
|
108
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {
|
|
109
|
+
return next(response);
|
|
110
|
+
}
|
|
111
|
+
// execute
|
|
112
|
+
const beanInstance = cast<IInterceptorResponse>(onionSlice.beanInstance);
|
|
113
|
+
if (!beanInstance.onResponse) return next(response);
|
|
114
|
+
return beanInstance.onResponse(response, options, next as any);
|
|
115
|
+
});
|
|
116
|
+
this._composerResponseError = this.$$sysOnion.interceptor.compose(onionSlices, (onionSlice, error: AxiosError, next) => {
|
|
117
|
+
const config = error.config;
|
|
118
|
+
// options
|
|
119
|
+
const options = this._combineOnionOptions(onionSlice, config);
|
|
120
|
+
// enable match ignore
|
|
121
|
+
if (!this.$$sysOnion.checkOnionOptionsEnabled(options, config?.url)) {
|
|
122
|
+
return next(error);
|
|
123
|
+
}
|
|
124
|
+
// execute
|
|
125
|
+
const beanInstance = cast<IInterceptorResponseError>(onionSlice.beanInstance);
|
|
126
|
+
if (!beanInstance.onResponseError) return next(error);
|
|
127
|
+
return beanInstance.onResponseError(error, options, next as any);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private _combineOnionOptions(item: IOnionSlice<IDecoratorInterceptorOptions, keyof IInterceptorRecord>, config?: AxiosRequestConfig) {
|
|
132
|
+
// options: dynamic
|
|
133
|
+
let optionsDynamic;
|
|
134
|
+
if (config?.interceptors) {
|
|
135
|
+
optionsDynamic = config?.interceptors[item.name];
|
|
136
|
+
}
|
|
137
|
+
// final options
|
|
138
|
+
const options = optionsDynamic ? deepExtend({}, item.options, optionsDynamic) : item.options;
|
|
139
|
+
// ok
|
|
140
|
+
return options;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './interceptor.js';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { AxiosError, AxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from 'axios';
|
|
2
|
+
import type { OmitNever, PowerPartial } from 'zova';
|
|
3
|
+
import type {
|
|
4
|
+
IOnionItem,
|
|
5
|
+
IOnionOptionsDeps,
|
|
6
|
+
IOnionOptionsEnable,
|
|
7
|
+
IOnionOptionsMatch,
|
|
8
|
+
ServiceOnion,
|
|
9
|
+
TypeOnionOptionsMatchRule,
|
|
10
|
+
} from 'zova-module-a-bean';
|
|
11
|
+
|
|
12
|
+
import type { BeanFetch } from '../bean/bean.fetch.js';
|
|
13
|
+
|
|
14
|
+
export type NextInterceptorRequest = (config?: AxiosRequestConfig) => Promise<AxiosRequestConfig>;
|
|
15
|
+
export type NextInterceptorResponse = (response?: AxiosResponse) => Promise<AxiosResponse>;
|
|
16
|
+
export type NextInterceptorError = (error?: AxiosError) => Promise<AxiosError>;
|
|
17
|
+
|
|
18
|
+
export interface IBeanFetchOptions {
|
|
19
|
+
axiosConfig?: CreateAxiosDefaults;
|
|
20
|
+
onionItems?:
|
|
21
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>
|
|
22
|
+
| IOnionItem<IDecoratorInterceptorOptions, keyof IInterceptorRecord>[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IInterceptorRecord {}
|
|
26
|
+
|
|
27
|
+
export interface IInterceptorRequest {
|
|
28
|
+
onRequest(_config: AxiosRequestConfig, _options: IDecoratorInterceptorOptions, _next: NextInterceptorRequest): Promise<AxiosRequestConfig>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface IInterceptorRequestError {
|
|
32
|
+
onRequestError(_error: AxiosError, _options: IDecoratorInterceptorOptions, _next: NextInterceptorError): Promise<AxiosError>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IInterceptorResponse {
|
|
36
|
+
onResponse(_response: AxiosResponse, _options: IDecoratorInterceptorOptions, _next: NextInterceptorResponse): Promise<AxiosResponse>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface IInterceptorResponseError {
|
|
40
|
+
onResponseError(_error: AxiosError, _options: IDecoratorInterceptorOptions, _next: NextInterceptorError): Promise<AxiosError>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface IDecoratorInterceptorOptions
|
|
44
|
+
extends IOnionOptionsEnable, IOnionOptionsMatch<TypeOnionOptionsMatchRule<string>>, IOnionOptionsDeps<keyof IInterceptorRecord> {}
|
|
45
|
+
|
|
46
|
+
declare module 'zova-module-a-bean' {
|
|
47
|
+
export interface SysOnion {
|
|
48
|
+
interceptor: ServiceOnion<IDecoratorInterceptorOptions, keyof IInterceptorRecord>;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare module 'zova' {
|
|
53
|
+
export interface ConfigOnions {
|
|
54
|
+
interceptor: OmitNever<IInterceptorRecord>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface IBeanSceneRecord {
|
|
58
|
+
interceptor: never;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare module 'zova' {
|
|
63
|
+
export interface AppMeta {
|
|
64
|
+
$fetch: BeanFetch;
|
|
65
|
+
}
|
|
66
|
+
export interface BeanBase {
|
|
67
|
+
$fetch: BeanFetch;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare module 'axios' {
|
|
72
|
+
export interface AxiosRequestConfig {
|
|
73
|
+
interceptors?: PowerPartial<IInterceptorRecord>;
|
|
74
|
+
}
|
|
75
|
+
}
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# @cabloy/set
|