vona-module-a-executor 5.0.8 → 5.0.10

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.
@@ -0,0 +1,65 @@
1
+ import type { BeanScopeUtil } from 'vona';
2
+ /** bean: end */
3
+ /** bean: begin */
4
+ import type { BeanExecutor } from '../bean/bean.executor.ts';
5
+ /** service: end */
6
+ /** service: begin */
7
+ import type { ServiceExecutor } from '../service/executor.ts';
8
+ /** service: end */
9
+ /** scope: begin */
10
+ import { BeanScopeBase } from 'vona';
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.ts';
20
+ declare module 'vona' {
21
+ }
22
+ declare module 'vona-module-a-executor' {
23
+ interface BeanExecutor {
24
+ }
25
+ }
26
+ declare module 'vona' {
27
+ interface IBeanRecordGlobal {
28
+ executor: BeanExecutor;
29
+ }
30
+ }
31
+ /** bean: end */
32
+ /** service: begin */
33
+ export * from '../service/executor.ts';
34
+ declare module 'vona-module-a-web' {
35
+ interface IServiceRecord {
36
+ 'a-executor:executor': never;
37
+ }
38
+ }
39
+ declare module 'vona-module-a-executor' {
40
+ interface ServiceExecutor {
41
+ }
42
+ }
43
+ export interface IModuleService {
44
+ executor: ServiceExecutor;
45
+ }
46
+ declare module 'vona' {
47
+ interface IBeanRecordGeneral {
48
+ 'a-executor.service.executor': ServiceExecutor;
49
+ }
50
+ }
51
+ export declare class ScopeModuleAExecutor extends BeanScopeBase {
52
+ }
53
+ export interface ScopeModuleAExecutor {
54
+ util: BeanScopeUtil;
55
+ service: IModuleService;
56
+ }
57
+ declare module 'vona' {
58
+ interface IBeanScopeRecord {
59
+ 'a-executor': ScopeModuleAExecutor;
60
+ }
61
+ interface IBeanScopeContainer {
62
+ executor: ScopeModuleAExecutor;
63
+ }
64
+ }
65
+ /** scope: end */
@@ -0,0 +1,29 @@
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 */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-executor";
2
+ export { ScopeModuleAExecutor as ScopeModule } from './index.ts';
@@ -0,0 +1,2 @@
1
+ export const __ThisModule__ = 'a-executor';
2
+ export { ScopeModuleAExecutor as ScopeModule } from "./index.js";
@@ -0,0 +1,12 @@
1
+ import type { FunctionAsync } from 'vona';
2
+ import type { IApiPathRecordMethodMap } from 'vona-module-a-web';
3
+ import type { INewCtxOptions, IPerformActionOptions } from '../types/executor.ts';
4
+ import { BeanBase } from 'vona';
5
+ export declare class BeanExecutor extends BeanBase {
6
+ performAction<METHOD extends keyof IApiPathRecordMethodMap, PATH extends keyof IApiPathRecordMethodMap[METHOD]>(method: METHOD, path: PATH, options?: IPerformActionOptions): Promise<any>;
7
+ runInBackground(fn: FunctionAsync<void>): Promise<void>;
8
+ newCtxIsolate<RESULT>(fn: FunctionAsync<RESULT>, options?: INewCtxOptions): Promise<RESULT>;
9
+ mockCtx<RESULT>(fn: FunctionAsync<RESULT>, options?: INewCtxOptions): Promise<RESULT>;
10
+ newCtx<RESULT>(fn: FunctionAsync<RESULT>, options?: INewCtxOptions): Promise<RESULT>;
11
+ private _newCtxInner;
12
+ }
@@ -0,0 +1,111 @@
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 };
@@ -0,0 +1,2 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./.metadata/index.js";
2
+ export * from "./types/index.js";
@@ -0,0 +1,2 @@
1
+ export declare function __delegateProperties(ctx: any, ctxCaller: any): void;
2
+ export declare function __delegateProperty(ctx: any, ctxCaller: any, property: any): void;
@@ -0,0 +1,43 @@
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
+ // }
@@ -0,0 +1,7 @@
1
+ import type { IApiPathRecordMethodMap } from 'vona-module-a-web';
2
+ import type { IGeneralInfoOptions, IPerformActionOptions } from '../types/executor.ts';
3
+ import { BeanBase } from 'vona';
4
+ export declare class ServiceExecutor extends BeanBase {
5
+ performActionInner<METHOD extends keyof IApiPathRecordMethodMap>(method: METHOD, url: string, options?: IPerformActionOptions): Promise<any>;
6
+ prepareGeneralInfo(options?: IGeneralInfoOptions): IGeneralInfoOptions;
7
+ }
@@ -0,0 +1,127 @@
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
+ // }
@@ -0,0 +1,40 @@
1
+ import type { ContextState, ILocaleInfos, PowerPartial } from 'vona';
2
+ import type { IDbInfo, ITransactionOptions } from 'vona-module-a-database';
3
+ import type { ConfigOnions } from 'vona-module-a-onion';
4
+ export declare const SymbolRouterMiddleware: unique symbol;
5
+ export interface INewCtxExtraData {
6
+ state?: ContextState;
7
+ request?: {
8
+ headers?: Record<string, string>;
9
+ };
10
+ }
11
+ export interface INewCtxOptions extends INewCtxBaseOptions {
12
+ dbInfo?: IDbInfo | true;
13
+ }
14
+ export interface INewCtxBaseOptions {
15
+ locale?: keyof ILocaleInfos;
16
+ instanceName?: string | undefined | null;
17
+ instance?: boolean;
18
+ transaction?: boolean;
19
+ transactionOptions?: ITransactionOptions;
20
+ extraData?: INewCtxExtraData | undefined;
21
+ innerAccess?: boolean;
22
+ req?: any;
23
+ res?: any;
24
+ }
25
+ export interface IPerformActionOptions {
26
+ innerAccess?: boolean;
27
+ params?: object;
28
+ query?: object;
29
+ headers?: object;
30
+ body?: any;
31
+ onions?: PowerPartial<ConfigOnions>;
32
+ authToken?: string;
33
+ extraData?: INewCtxExtraData | undefined;
34
+ }
35
+ export interface IGeneralInfoOptions {
36
+ dbInfo?: IDbInfo;
37
+ locale?: keyof ILocaleInfos;
38
+ instanceName?: string | undefined | null;
39
+ extraData?: INewCtxExtraData;
40
+ }
@@ -0,0 +1 @@
1
+ export const SymbolRouterMiddleware = Symbol('SymbolRouterMiddleware');
@@ -0,0 +1 @@
1
+ export * from './executor.ts';
@@ -0,0 +1 @@
1
+ export * from "./executor.js";
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.8",
4
+ "version": "5.0.10",
5
5
  "title": "a-executor",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -28,8 +28,12 @@
28
28
  "dependencies": {
29
29
  "koa-compose": "^4.1.0"
30
30
  },
31
+ "devDependencies": {
32
+ "clean-package": "^2.2.0",
33
+ "rimraf": "^6.0.1"
34
+ },
31
35
  "scripts": {
32
- "clean": "rimraf dist tsconfig.tsbuildinfo",
33
- "tsc:publish": "npm run clean && tsc"
36
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
37
+ "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
34
38
  }
35
39
  }