endpoint的处理微调

This commit is contained in:
Xu Chang 2025-04-15 11:49:34 +08:00
parent 970cae5764
commit 51c93a9402
3 changed files with 23 additions and 14 deletions

7
lib/AppLoader.d.ts vendored
View File

@ -3,7 +3,7 @@ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { AppLoader as GeneralAppLoader, Trigger, EntityDict, Watcher, OpRecord, FreeTimer, OperationResult } from "oak-domain/lib/types";
import { DbStore } from "./DbStore";
import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
import { IncomingHttpHeaders, IncomingMessage } from 'http';
import { Namespace } from 'socket.io';
import DataSubscriber from './cluster/DataSubscriber';
import Synchronizer from './Synchronizer';
@ -37,7 +37,10 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
}>;
initialize(): Promise<void>;
getStore(): DbStore<ED, Cxt>;
getEndpoints(prefix: string): ["rest" | "custom" | undefined, string, "post" | "get" | "put" | "delete", string, (koaCtx: Koa.ParameterizedContext) => Promise<any>][];
getEndpoints(prefix: string): [string, "post" | "get" | "put" | "delete", string, (params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<{
headers?: Record<string, string | string[]> | undefined;
data: any;
}>][];
protected operateInWatcher<T extends keyof ED>(entity: T, operation: ED[T]['Update'], context: Cxt, singleton?: true): Promise<OperationResult<ED>>;
protected selectInWatcher<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, forUpdate?: true, singleton?: true): Promise<Partial<ED[T]["Schema"]>[]>;
protected execWatcher(watcher: Watcher<ED, keyof ED, Cxt>): Promise<OperationResult<ED> | undefined>;

View File

@ -275,15 +275,18 @@ class AppLoader extends types_1.AppLoader {
}
}
endPointRouters.push([name, method, url, async (params, headers, req, body) => {
let result;
if (type == "custom") {
result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
if (type == "free") {
const result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
return result;
}
else {
const context = await this.makeContext(undefined, headers);
try {
result = await fn(context, params, headers, req, body);
const data = await fn(context, params, headers, req, body);
await context.commit();
return {
data,
};
}
catch (err) {
await context.rollback();
@ -291,7 +294,6 @@ class AppLoader extends types_1.AppLoader {
throw err;
}
}
return result;
}]);
};
if (endPointMap[k]) {

View File

@ -321,7 +321,10 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Backe
getEndpoints(prefix: string) {
const endpoints: Record<string, Endpoint<ED, Cxt>> = this.requireSth('lib/endpoints/index');
const endPointRouters: Array<[EndpointItem<ED, Cxt>['name'], EndpointItem<ED, Cxt>['method'], string, (params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<any>]> = [];
const endPointRouters: Array<[EndpointItem<ED, Cxt>['name'], EndpointItem<ED, Cxt>['method'], string, (params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<{
headers?: Record<string, string | string[]>;
data: any;
}>]> = [];
const endPointMap: Record<string, true> = {};
const transformEndpointItem = (key: string, item: EndpointItem<ED, Cxt>) => {
@ -337,15 +340,17 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Backe
}
endPointRouters.push(
[name, method, url, async (params, headers, req, body) => {
let result: any
if (type == "custom") {
result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
if (type == "free") {
const result = await fn(() => this.makeContext(undefined, headers), params, headers, req, body);
return result;
} else {
const context = await this.makeContext(undefined, headers);
try {
result = await fn(context, params, headers, req, body);
const data = await fn(context, params, headers, req, body);
await context.commit();
return {
data,
};
}
catch (err) {
await context.rollback();
@ -353,7 +358,6 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Backe
throw err;
}
}
return result;
}]
);