新增插件配置与aspectService

This commit is contained in:
Pan Qiancheng 2024-11-25 22:08:27 +08:00
parent 1710ad9113
commit 8658fea0a3
2 changed files with 102 additions and 0 deletions

29
oak.config.json Normal file
View File

@ -0,0 +1,29 @@
{
"projectDir": "./",
"checker": {
"onInvalidReturn": "warn",
"onInvalidDestructuring": "error",
"onNeedPromiseCheck": "error"
},
"trigger": {
"onReturnLiteral": "warn",
"onNoAsyncFn": "error",
"onNoAwaitContext": "error"
},
"i18n": {
"onMissingKey": "error",
"onKeyBlank": "warn",
"onParamError": "warn",
"onParamMissing": "error",
"onParamRedundant": "warn"
},
"oakComponent": {
"onInvalidEntity": "error",
"onInvalidIsList": "error",
"onMissingDataAttrs": "warn",
"onMissingMethods": "error"
},
"oakPath": {
"onInvalidPath": "error"
}
}

View File

@ -0,0 +1,73 @@
import { BasicFeatures } from "../features";
import assert from "assert";
import { AspectDict as CommonAspectDict } from "oak-common-aspect/es/AspectDict";
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
import { EntityDict as BaseEntityDict } from "oak-domain/lib/base-app-domain";
type AspectDict<
ED extends BaseEntityDict,
BCT extends AsyncContext<ED>
> = Record<string, (params: any, context: BCT) => Promise<any>>;
type TransformAspectDict<
T extends AspectDict<ED, BCT>,
ED extends BaseEntityDict,
BCT extends AsyncContext<ED>
> = {
[K in keyof T]: T[K] extends (params: infer P, context: BCT) => infer R
? R extends Promise<unknown>
? (params: P) => R
: never
: never;
};
export type AspectDictTrans<
ED extends BaseEntityDict,
AD extends AspectDict<ED, BCT>,
BCT extends AsyncContext<ED>
> = TransformAspectDict<AD & CommonAspectDict<ED>, ED, BCT>;
export const createService = <
ED extends BaseEntityDict,
AD extends AspectDict<ED, any>
>(
cache: BasicFeatures<ED>["cache"]
) => {
return new Proxy({} as Record<string, Function>, {
get: (target, prop) => {
assert(
typeof prop === "string",
"Aspect service name must be a string"
);
if (process.env.NODE_ENV === "development") {
const service = target[prop];
if (typeof service !== "function") {
console.log(
"Calling a aspect which is not in this Project:",
prop
);
}
}
return (params: unknown) => {
const paramsCopy = structuredClone(params);
if (process.env.NODE_ENV === "development") {
console.log("Aspect call:", prop, paramsCopy);
}
return new Promise((resolve, reject) => {
cache
.exec(prop, paramsCopy)
.then((res) => {
resolve(res.result);
})
.catch((err) => {
reject(err);
});
});
};
},
// 禁止任何set
set: () => {
throw new Error("Aspect service is readonly");
},
}) as unknown as AspectDictTrans<ED, AD, any>;
};