vona-module-a-executor 5.0.10 → 5.0.12

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/dist/index.js CHANGED
@@ -1,2 +1,291 @@
1
- export * from "./.metadata/index.js";
2
- export * from "./types/index.js";
1
+ import { BeanInfo, BeanBase, cast, deepExtend, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import { combineParamsAndQuery } from '@cabloy/utils';
4
+ import { Log } from 'vona-module-a-logger';
5
+ import { Service } from 'vona-module-a-web';
6
+
7
+ function __delegateProperties(ctx, ctxCaller) {
8
+ for (const property of ['state']) {
9
+ __delegateProperty(ctx, ctxCaller, property);
10
+ }
11
+ for (const property of ['headers']) {
12
+ __delegateProperty(ctx.request, ctxCaller.request, property);
13
+ // if (ctx.request[property]) req[property] = ctx.request[property];
14
+ }
15
+ }
16
+ function __delegateProperty(ctx, ctxCaller, property) {
17
+ if (!ctxCaller[property]) ctxCaller[property] = {};
18
+ ctx[property] = ctxCaller[property];
19
+ }
20
+
21
+ // export function delegateProperty(ctx, ctxCaller, property) {
22
+ // if (!ctxCaller[property]) return;
23
+ // if (!ctx[property]) ctx[property] = {};
24
+ // for (const key in ctxCaller[property]) {
25
+ // ctx[property][key] = ctxCaller[property][key];
26
+ // }
27
+ // }
28
+
29
+ // function _delegateProperty(ctx, ctxCaller, property) {
30
+ // const keyMock = `__executeBean__mock__${property}__`;
31
+ // const keyOriginal = `__executeBean__mock__${property}__original__`;
32
+ // if (['headers'].includes(property)) {
33
+ // ctx[keyOriginal] = ctx[property];
34
+ // }
35
+ // Object.defineProperty(ctx, property, {
36
+ // get() {
37
+ // const value = ctxCaller && ctxCaller[property];
38
+ // if (value) return value;
39
+ // //
40
+ // if (['headers'].includes(property)) {
41
+ // return ctx[keyOriginal];
42
+ // }
43
+ // //
44
+ // if (!ctx[keyMock]) {
45
+ // ctx[keyMock] = {};
46
+ // }
47
+ // return ctx[keyMock];
48
+ // },
49
+ // });
50
+ // }
51
+
52
+ var _dec$2, _dec2$2, _class$2;
53
+ let BeanExecutor = (_dec$2 = Bean(), _dec2$2 = BeanInfo({
54
+ module: "a-executor"
55
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class BeanExecutor extends BeanBase {
56
+ async performAction(method, path, options) {
57
+ // url
58
+ const url = this.app.util.combineApiPath(path, '', true, true);
59
+ return await this.scope.service.executor.performActionInner(method, url, options);
60
+ }
61
+ runInBackground(fn) {
62
+ return this.bean.database.newDbIsolate(() => {
63
+ return this.newCtx(fn);
64
+ });
65
+ }
66
+ async newCtxIsolate(fn, options) {
67
+ options = Object.assign({}, options);
68
+ // instanceName !== undefined means isolate
69
+ options.instanceName = options.instanceName === undefined ? this.ctx?.instanceName : options.instanceName;
70
+ return this.newCtx(fn, options);
71
+ }
72
+ async mockCtx(fn, options) {
73
+ options = Object.assign({}, options);
74
+ options.dbInfo = options.dbInfo === undefined ? true : options.dbInfo;
75
+ options.instanceName = options.instanceName === undefined ? this.ctx?.instanceName ?? '' : options.instanceName;
76
+ return this.newCtx(fn, options);
77
+ }
78
+ async newCtx(fn, options) {
79
+ if (!options?.dbInfo) return this._newCtxInner(fn, options);
80
+ const dbInfo = options.dbInfo === true ? {} : options.dbInfo;
81
+ return this.bean.database.newDb(() => {
82
+ return this._newCtxInner(fn, options);
83
+ }, dbInfo);
84
+ }
85
+ async _newCtxInner(fn, options) {
86
+ options = Object.assign({}, options);
87
+ // innerAccess
88
+ const innerAccess = options.innerAccess !== false;
89
+ // ctx ref
90
+ const ctxRef = innerAccess ? this.ctx : undefined;
91
+ // isolate
92
+ const isolate = !ctxRef || options.instanceName !== undefined;
93
+ const ctxCaller = !isolate && ctxRef ? ctxRef : undefined;
94
+ // locale/instanceName
95
+ if (ctxRef) {
96
+ options.locale = options.locale === undefined ? ctxRef.locale : options.locale;
97
+ options.instanceName = options.instanceName === undefined ? ctxRef.instanceName : options.instanceName;
98
+ }
99
+ // run
100
+ const ctx = this.app.createAnonymousContext(options.req, options.res);
101
+ return await this.app.ctxStorage.run(ctx, async () => {
102
+ // innerAccess
103
+ ctx.innerAccess = innerAccess;
104
+ // locale
105
+ if (options.locale !== undefined) {
106
+ ctx.locale = options.locale;
107
+ }
108
+ // instanceName: undefined/null is different
109
+ if (options.instanceName !== undefined) {
110
+ ctx.instanceName = options.instanceName;
111
+ }
112
+ // ctxCaller
113
+ if (ctxCaller) {
114
+ // delegateProperties
115
+ __delegateProperties(ctx, ctxCaller);
116
+ // ctxCaller
117
+ ctx.ctxCaller = ctxCaller;
118
+ } else {
119
+ // isolate: extraData
120
+ if (options.extraData) {
121
+ // delegateProperties
122
+ __delegateProperties(ctx, options.extraData);
123
+ }
124
+ }
125
+ // instance
126
+ const instanceName = ctx.instanceName; // use default instanceName when undefined
127
+ if (instanceName !== undefined && instanceName !== null) {
128
+ ctx.instance = await this.bean.instance.get(instanceName);
129
+ // start instance
130
+ if (options.instance) {
131
+ await this.$scope.instance.service.instance.checkAppReadyInstance(true);
132
+ }
133
+ }
134
+ // execute
135
+ let res;
136
+ if (options.transaction) {
137
+ res = await this.bean.database.current.transaction.begin(async () => {
138
+ return await fn();
139
+ }, options.transactionOptions);
140
+ } else {
141
+ res = await fn();
142
+ }
143
+ // commitDone
144
+ await ctx.commitDone();
145
+ // ok
146
+ return res;
147
+ });
148
+ }
149
+ }) || _class$2) || _class$2);
150
+
151
+ function _applyDecoratedDescriptor(i, e, r, n, l) {
152
+ var a = {};
153
+ return Object.keys(n).forEach(function (i) {
154
+ a[i] = n[i];
155
+ }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
156
+ return n(i, e, r) || r;
157
+ }, 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;
158
+ }
159
+
160
+ const SymbolRouterMiddleware = Symbol('SymbolRouterMiddleware');
161
+
162
+ var _dec$1, _dec2$1, _dec3, _dec4, _dec5, _class$1, _class2;
163
+ let ServiceExecutor = (_dec$1 = Service(), _dec2$1 = BeanInfo({
164
+ module: "a-executor"
165
+ }), _dec3 = Log({
166
+ level: 'debug'
167
+ }), _dec4 = Reflect.metadata("design:type", Function), _dec5 = Reflect.metadata("design:paramtypes", [typeof METHOD === "undefined" ? Object : METHOD, String, typeof IPerformActionOptions === "undefined" ? Object : IPerformActionOptions]), _dec$1(_class$1 = _dec2$1(_class$1 = (_class2 = class ServiceExecutor extends BeanBase {
168
+ async performActionInner(method, url, options) {
169
+ // app
170
+ const app = this.app;
171
+ // new ctx
172
+ return await this.bean.executor.newCtx(async () => {
173
+ const ctx = this.ctx;
174
+ // default status code
175
+ ctx.res.statusCode = 404;
176
+ ctx.req.method = method.toUpperCase();
177
+ // url
178
+ ctx.req.url = combineParamsAndQuery(url, {
179
+ params: options?.params,
180
+ query: options?.query
181
+ });
182
+ // headers
183
+ ctx.req.headers = Object.assign({}, ctx.req.headers, options?.headers);
184
+ // json
185
+ if (!ctx.req.headers.accept) {
186
+ ctx.req.headers.accept = 'application/json';
187
+ }
188
+ // authToken
189
+ if (options?.authToken) {
190
+ ctx.req.headers.authorization = `Bearer ${options?.authToken}`;
191
+ }
192
+ // // query
193
+ // if (options?.query !== undefined) {
194
+ // cast(ctx.req).query = cast(ctx.request).query = options?.query;
195
+ // }
196
+ // body
197
+ cast(ctx.req).body = ctx.request.body = options?.body ?? {}; // body should set {} if undefined/null
198
+ // onion
199
+ ctx.onionsDynamic = options?.onions;
200
+ // invoke middleware
201
+ await app[SymbolRouterMiddleware](ctx);
202
+ // check result
203
+ if (ctx.status === 200) {
204
+ if (!ctx.body || ctx.body.code === undefined) {
205
+ // not check code, e.g. text/xml
206
+ return ctx.body;
207
+ }
208
+ if (ctx.body.code === 0) {
209
+ return ctx.body.data;
210
+ }
211
+ throw app.util.createError(ctx.body);
212
+ } else {
213
+ if (ctx.body && typeof ctx.body === 'object') {
214
+ throw app.util.createError(ctx.body);
215
+ } else {
216
+ throw app.util.createError({
217
+ code: ctx.status,
218
+ message: ctx.message
219
+ });
220
+ }
221
+ }
222
+ }, {
223
+ innerAccess: options?.innerAccess,
224
+ extraData: options?.extraData
225
+ });
226
+ }
227
+ prepareGeneralInfo(options) {
228
+ const current = this.bean.database.current;
229
+ const level = (options?.dbInfo?.level ?? current?.level ?? 0) + 1;
230
+ const clientName = options?.dbInfo?.clientName ?? current?.clientName;
231
+ const locale = options?.locale === undefined ? this.ctx?.locale : options.locale;
232
+ const instanceName = options?.instanceName === undefined ? this.ctx?.instanceName : options.instanceName;
233
+ options = {
234
+ dbInfo: {
235
+ level,
236
+ clientName
237
+ },
238
+ locale,
239
+ instanceName
240
+ };
241
+ if (this.ctx) {
242
+ options = deepExtend({
243
+ extraData: {
244
+ request: {
245
+ headers: {}
246
+ }
247
+ }
248
+ }, options);
249
+ // extraData: headers
250
+ const headers = options.extraData.request.headers;
251
+ for (const key in this.ctx.request.headers) {
252
+ if (key.startsWith('x-vona-data-') && !headers[key]) {
253
+ const value = this.ctx.request.headers[key];
254
+ if (value) {
255
+ headers[key] = value;
256
+ }
257
+ }
258
+ }
259
+ }
260
+ return options;
261
+ }
262
+ }, _applyDecoratedDescriptor(_class2.prototype, "performActionInner", [_dec3, _dec4, _dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "performActionInner"), _class2.prototype), _class2)) || _class$1) || _class$1);
263
+
264
+ // for (const key of ['x-clientid', 'x-scene']) {
265
+ // if (!headers[key]) {
266
+ // const value =
267
+ // key === 'x-clientid'
268
+ // ? (<any>ctx.app.bean).util.getFrontClientId()
269
+ // : (<any>ctx.app.bean).util.getFrontScene();
270
+ // if (value) {
271
+ // headers[key] = value;
272
+ // }
273
+ // }
274
+ // }
275
+ // for (const key of ['host', 'origin', 'referer', 'user-agent']) {
276
+ // if (!headers[key]) {
277
+ // const value = this.ctx.request.headers[key];
278
+ // if (value) {
279
+ // headers[key] = value;
280
+ // }
281
+ // }
282
+ // }
283
+
284
+ var _dec, _dec2, _class;
285
+ let ScopeModuleAExecutor = (_dec = Scope(), _dec2 = BeanInfo({
286
+ module: "a-executor"
287
+ }), _dec(_class = _dec2(_class = class ScopeModuleAExecutor extends BeanScopeBase {}) || _class) || _class);
288
+
289
+ /** scope: end */
290
+
291
+ export { BeanExecutor, ScopeModuleAExecutor, ServiceExecutor, SymbolRouterMiddleware };
@@ -1,4 +1,4 @@
1
- import type { ContextState, ILocaleInfos, PowerPartial } from 'vona';
1
+ import type { ContextState, IInstanceRecord, ILocaleInfos, PowerPartial } from 'vona';
2
2
  import type { IDbInfo, ITransactionOptions } from 'vona-module-a-database';
3
3
  import type { ConfigOnions } from 'vona-module-a-onion';
4
4
  export declare const SymbolRouterMiddleware: unique symbol;
@@ -13,7 +13,7 @@ export interface INewCtxOptions extends INewCtxBaseOptions {
13
13
  }
14
14
  export interface INewCtxBaseOptions {
15
15
  locale?: keyof ILocaleInfos;
16
- instanceName?: string | undefined | null;
16
+ instanceName?: keyof IInstanceRecord | undefined | null;
17
17
  instance?: boolean;
18
18
  transaction?: boolean;
19
19
  transactionOptions?: ITransactionOptions;
@@ -35,6 +35,6 @@ export interface IPerformActionOptions {
35
35
  export interface IGeneralInfoOptions {
36
36
  dbInfo?: IDbInfo;
37
37
  locale?: keyof ILocaleInfos;
38
- instanceName?: string | undefined | null;
38
+ instanceName?: keyof IInstanceRecord | undefined | null;
39
39
  extraData?: INewCtxExtraData;
40
40
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-executor",
3
3
  "type": "module",
4
- "version": "5.0.10",
4
+ "version": "5.0.12",
5
5
  "title": "a-executor",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -34,6 +34,6 @@
34
34
  },
35
35
  "scripts": {
36
36
  "clean": "rimraf dist tsconfig.build.tsbuildinfo",
37
- "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
37
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
38
38
  }
39
39
  }
@@ -1,29 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- /** service: end */
8
- /** scope: begin */
9
- import { BeanScopeBase } from 'vona';
10
- import { Scope } from 'vona-module-a-bean';
11
- /** bean: begin */
12
- import 'vona';
13
- /** service: end */
14
- /** service: begin */
15
- import 'vona';
16
- import 'vona';
17
- import 'vona';
18
- import 'vona';
19
- export * from "../bean/bean.executor.js";
20
- /** bean: end */
21
- /** service: begin */
22
- export * from "../service/executor.js";
23
- let ScopeModuleAExecutor = class ScopeModuleAExecutor extends BeanScopeBase {
24
- };
25
- ScopeModuleAExecutor = __decorate([
26
- Scope()
27
- ], ScopeModuleAExecutor);
28
- export { ScopeModuleAExecutor };
29
- /** scope: end */
@@ -1,2 +0,0 @@
1
- export const __ThisModule__ = 'a-executor';
2
- export { ScopeModuleAExecutor as ScopeModule } from "./index.js";
@@ -1,111 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { BeanBase } from 'vona';
8
- import { Bean } from 'vona-module-a-bean';
9
- import { __delegateProperties } from "../lib/utils.js";
10
- let BeanExecutor = class BeanExecutor extends BeanBase {
11
- async performAction(method, path, options) {
12
- // url
13
- const url = this.app.util.combineApiPath(path, '', true, true);
14
- return await this.scope.service.executor.performActionInner(method, url, options);
15
- }
16
- runInBackground(fn) {
17
- return this.bean.database.newDbIsolate(() => {
18
- return this.newCtx(fn);
19
- });
20
- }
21
- async newCtxIsolate(fn, options) {
22
- options = Object.assign({}, options);
23
- // instanceName !== undefined means isolate
24
- options.instanceName = options.instanceName === undefined ? this.ctx?.instanceName : options.instanceName;
25
- return this.newCtx(fn, options);
26
- }
27
- async mockCtx(fn, options) {
28
- options = Object.assign({}, options);
29
- options.dbInfo = options.dbInfo === undefined ? true : options.dbInfo;
30
- options.instanceName = options.instanceName === undefined ? (this.ctx?.instanceName ?? '') : options.instanceName;
31
- return this.newCtx(fn, options);
32
- }
33
- async newCtx(fn, options) {
34
- if (!options?.dbInfo)
35
- return this._newCtxInner(fn, options);
36
- const dbInfo = options.dbInfo === true ? {} : options.dbInfo;
37
- return this.bean.database.newDb(() => {
38
- return this._newCtxInner(fn, options);
39
- }, dbInfo);
40
- }
41
- async _newCtxInner(fn, options) {
42
- options = Object.assign({}, options);
43
- // innerAccess
44
- const innerAccess = options.innerAccess !== false;
45
- // ctx ref
46
- const ctxRef = innerAccess ? this.ctx : undefined;
47
- // isolate
48
- const isolate = !ctxRef || options.instanceName !== undefined;
49
- const ctxCaller = (!isolate && ctxRef) ? ctxRef : undefined;
50
- // locale/instanceName
51
- if (ctxRef) {
52
- options.locale = options.locale === undefined ? ctxRef.locale : options.locale;
53
- options.instanceName = options.instanceName === undefined ? ctxRef.instanceName : options.instanceName;
54
- }
55
- // run
56
- const ctx = this.app.createAnonymousContext(options.req, options.res);
57
- return await this.app.ctxStorage.run(ctx, async () => {
58
- // innerAccess
59
- ctx.innerAccess = innerAccess;
60
- // locale
61
- if (options.locale !== undefined) {
62
- ctx.locale = options.locale;
63
- }
64
- // instanceName: undefined/null is different
65
- if (options.instanceName !== undefined) {
66
- ctx.instanceName = options.instanceName;
67
- }
68
- // ctxCaller
69
- if (ctxCaller) {
70
- // delegateProperties
71
- __delegateProperties(ctx, ctxCaller);
72
- // ctxCaller
73
- ctx.ctxCaller = ctxCaller;
74
- }
75
- else {
76
- // isolate: extraData
77
- if (options.extraData) {
78
- // delegateProperties
79
- __delegateProperties(ctx, options.extraData);
80
- }
81
- }
82
- // instance
83
- const instanceName = ctx.instanceName; // use default instanceName when undefined
84
- if (instanceName !== undefined && instanceName !== null) {
85
- ctx.instance = (await this.bean.instance.get(instanceName));
86
- // start instance
87
- if (options.instance) {
88
- await this.$scope.instance.service.instance.checkAppReadyInstance(true);
89
- }
90
- }
91
- // execute
92
- let res;
93
- if (options.transaction) {
94
- res = await this.bean.database.current.transaction.begin(async () => {
95
- return await fn();
96
- }, options.transactionOptions);
97
- }
98
- else {
99
- res = await fn();
100
- }
101
- // commitDone
102
- await ctx.commitDone();
103
- // ok
104
- return res;
105
- });
106
- }
107
- };
108
- BeanExecutor = __decorate([
109
- Bean()
110
- ], BeanExecutor);
111
- export { BeanExecutor };
package/dist/lib/utils.js DELETED
@@ -1,43 +0,0 @@
1
- export function __delegateProperties(ctx, ctxCaller) {
2
- for (const property of ['state']) {
3
- __delegateProperty(ctx, ctxCaller, property);
4
- }
5
- for (const property of ['headers']) {
6
- __delegateProperty(ctx.request, ctxCaller.request, property);
7
- // if (ctx.request[property]) req[property] = ctx.request[property];
8
- }
9
- }
10
- export function __delegateProperty(ctx, ctxCaller, property) {
11
- if (!ctxCaller[property])
12
- ctxCaller[property] = {};
13
- ctx[property] = ctxCaller[property];
14
- }
15
- // export function delegateProperty(ctx, ctxCaller, property) {
16
- // if (!ctxCaller[property]) return;
17
- // if (!ctx[property]) ctx[property] = {};
18
- // for (const key in ctxCaller[property]) {
19
- // ctx[property][key] = ctxCaller[property][key];
20
- // }
21
- // }
22
- // function _delegateProperty(ctx, ctxCaller, property) {
23
- // const keyMock = `__executeBean__mock__${property}__`;
24
- // const keyOriginal = `__executeBean__mock__${property}__original__`;
25
- // if (['headers'].includes(property)) {
26
- // ctx[keyOriginal] = ctx[property];
27
- // }
28
- // Object.defineProperty(ctx, property, {
29
- // get() {
30
- // const value = ctxCaller && ctxCaller[property];
31
- // if (value) return value;
32
- // //
33
- // if (['headers'].includes(property)) {
34
- // return ctx[keyOriginal];
35
- // }
36
- // //
37
- // if (!ctx[keyMock]) {
38
- // ctx[keyMock] = {};
39
- // }
40
- // return ctx[keyMock];
41
- // },
42
- // });
43
- // }
@@ -1,127 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- var _a;
11
- import { combineParamsAndQuery } from '@cabloy/utils';
12
- import { BeanBase, cast, deepExtend } from 'vona';
13
- import { Log } from 'vona-module-a-logger';
14
- import { Service } from 'vona-module-a-web';
15
- import { SymbolRouterMiddleware } from "../types/executor.js";
16
- let ServiceExecutor = class ServiceExecutor extends BeanBase {
17
- async performActionInner(method, url, options) {
18
- // app
19
- const app = this.app;
20
- // new ctx
21
- return await this.bean.executor.newCtx(async () => {
22
- const ctx = this.ctx;
23
- // default status code
24
- ctx.res.statusCode = 404;
25
- ctx.req.method = method.toUpperCase();
26
- // url
27
- ctx.req.url = combineParamsAndQuery(url, { params: options?.params, query: options?.query });
28
- // headers
29
- ctx.req.headers = Object.assign({}, ctx.req.headers, options?.headers);
30
- // json
31
- if (!ctx.req.headers.accept) {
32
- ctx.req.headers.accept = 'application/json';
33
- }
34
- // authToken
35
- if (options?.authToken) {
36
- ctx.req.headers.authorization = `Bearer ${options?.authToken}`;
37
- }
38
- // // query
39
- // if (options?.query !== undefined) {
40
- // cast(ctx.req).query = cast(ctx.request).query = options?.query;
41
- // }
42
- // body
43
- cast(ctx.req).body = ctx.request.body = options?.body ?? {}; // body should set {} if undefined/null
44
- // onion
45
- ctx.onionsDynamic = options?.onions;
46
- // invoke middleware
47
- await app[SymbolRouterMiddleware](ctx);
48
- // check result
49
- if (ctx.status === 200) {
50
- if (!ctx.body || ctx.body.code === undefined) {
51
- // not check code, e.g. text/xml
52
- return ctx.body;
53
- }
54
- if (ctx.body.code === 0) {
55
- return ctx.body.data;
56
- }
57
- throw app.util.createError(ctx.body);
58
- }
59
- else {
60
- if (ctx.body && typeof ctx.body === 'object') {
61
- throw app.util.createError(ctx.body);
62
- }
63
- else {
64
- throw app.util.createError({
65
- code: ctx.status,
66
- message: ctx.message,
67
- });
68
- }
69
- }
70
- }, { innerAccess: options?.innerAccess, extraData: options?.extraData });
71
- }
72
- prepareGeneralInfo(options) {
73
- const current = this.bean.database.current;
74
- const level = (options?.dbInfo?.level ?? current?.level ?? 0) + 1;
75
- const clientName = options?.dbInfo?.clientName ?? current?.clientName;
76
- const locale = options?.locale === undefined ? this.ctx?.locale : options.locale;
77
- const instanceName = options?.instanceName === undefined ? this.ctx?.instanceName : options.instanceName;
78
- options = {
79
- dbInfo: { level, clientName },
80
- locale,
81
- instanceName,
82
- };
83
- if (this.ctx) {
84
- options = deepExtend({ extraData: { request: { headers: {} } } }, options);
85
- // extraData: headers
86
- const headers = options.extraData.request.headers;
87
- for (const key in this.ctx.request.headers) {
88
- if (key.startsWith('x-vona-data-') && !headers[key]) {
89
- const value = this.ctx.request.headers[key];
90
- if (value) {
91
- headers[key] = value;
92
- }
93
- }
94
- }
95
- }
96
- return options;
97
- }
98
- };
99
- __decorate([
100
- Log({ level: 'debug' }),
101
- __metadata("design:type", Function),
102
- __metadata("design:paramtypes", [typeof (_a = typeof METHOD !== "undefined" && METHOD) === "function" ? _a : Object, String, Object]),
103
- __metadata("design:returntype", Promise)
104
- ], ServiceExecutor.prototype, "performActionInner", null);
105
- ServiceExecutor = __decorate([
106
- Service()
107
- ], ServiceExecutor);
108
- export { ServiceExecutor };
109
- // for (const key of ['x-clientid', 'x-scene']) {
110
- // if (!headers[key]) {
111
- // const value =
112
- // key === 'x-clientid'
113
- // ? (<any>ctx.app.bean).util.getFrontClientId()
114
- // : (<any>ctx.app.bean).util.getFrontScene();
115
- // if (value) {
116
- // headers[key] = value;
117
- // }
118
- // }
119
- // }
120
- // for (const key of ['host', 'origin', 'referer', 'user-agent']) {
121
- // if (!headers[key]) {
122
- // const value = this.ctx.request.headers[key];
123
- // if (value) {
124
- // headers[key] = value;
125
- // }
126
- // }
127
- // }
@@ -1 +0,0 @@
1
- export const SymbolRouterMiddleware = Symbol('SymbolRouterMiddleware');
@@ -1 +0,0 @@
1
- export * from "./executor.js";