@mwcp/kmore
Version:
midway component for knex, supports declarative transaction and OpenTelemetry
105 lines • 4.43 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
/* eslint-disable @typescript-eslint/no-misused-promises */
import assert from 'node:assert';
import { ASYNC_CONTEXT_KEY, ASYNC_CONTEXT_MANAGER_KEY, ApplicationContext, Inject, Singleton, } from '@midwayjs/core';
import { DecoratorHandlerBase, MConfig, genError } from '@mwcp/share';
import { TrxStatusService } from '../lib/trx-status.service.js';
import { ConfigKey } from '../lib/types.js';
import { afterReturn, afterThrow, before, genDecoratorExecutorOptionsAsync } from './transactional.helper.js';
let DecoratorHandlerTransactional = class DecoratorHandlerTransactional extends DecoratorHandlerBase {
applicationContext;
trxStatusSvc;
propagationConfig;
getWebContext() {
try {
const contextManager = this.applicationContext.get(ASYNC_CONTEXT_MANAGER_KEY);
const ctx = contextManager.active().getValue(ASYNC_CONTEXT_KEY);
return ctx;
}
catch (ex) {
// void ex
console.warn(new Error('getWebContext() failed', { cause: ex }));
return void 0;
}
}
getWebContextThenApp() {
try {
const webContext = this.getWebContext();
assert(webContext, 'getActiveContext() webContext should not be null, maybe this calling is not in a request context');
return webContext;
}
catch (ex) {
console.warn('getWebContextThenApp() failed', ex);
return this.app;
}
}
// isEnable(options: DecoratorExecutorOptions): boolean {
// return true
// }
genExecutorParam(options) {
if (!options.methodIsAsyncFunction) {
return options;
}
const optsExt = {
propagationConfig: this.propagationConfig,
scope: this.getWebContextThenApp(),
// scope: Symbol(Date.now()),
trxStatusSvc: this.trxStatusSvc,
};
if (!options.webContext) {
options.webContext = this.getWebContext();
}
return genDecoratorExecutorOptionsAsync(options, optsExt);
}
before(options) {
// Do NOT use isAsyncFunction(options.method), result may not correct
if (!options.methodIsAsyncFunction) {
return;
}
return before(options);
// don't cache error here
// .catch(ex => [
// this.afterThrow(options, ex),
// ])
}
afterReturn(options) {
if (!options.methodIsAsyncFunction) {
return;
}
return afterReturn(options);
}
afterThrow(options, errorExt) {
const error = genError({
error: errorExt ?? options.error,
throwMessageIfInputUndefined: `[@mwcp/${ConfigKey.namespace}] ${ConfigKey.Transactional}() afterThrow error is undefined`,
altMessage: `[@mwcp/${ConfigKey.namespace}] ${ConfigKey.Transactional}() decorator afterThrow error`,
});
options.error = error;
return afterThrow(options, this.trxStatusSvc);
}
};
__decorate([
ApplicationContext(),
__metadata("design:type", Object)
], DecoratorHandlerTransactional.prototype, "applicationContext", void 0);
__decorate([
Inject(),
__metadata("design:type", TrxStatusService)
], DecoratorHandlerTransactional.prototype, "trxStatusSvc", void 0);
__decorate([
MConfig(ConfigKey.propagationConfig),
__metadata("design:type", Object)
], DecoratorHandlerTransactional.prototype, "propagationConfig", void 0);
DecoratorHandlerTransactional = __decorate([
Singleton()
], DecoratorHandlerTransactional);
export { DecoratorHandlerTransactional };
//# sourceMappingURL=transactional.handler.js.map