31 lines
1.6 KiB
TypeScript
31 lines
1.6 KiB
TypeScript
/// <reference types="node" />
|
||
import { IncomingHttpHeaders, IncomingMessage } from "http";
|
||
import { AsyncContext } from "../store/AsyncRowStore";
|
||
import { EntityDict } from "./Entity";
|
||
import { EntityDict as BaseEntityDict } from '../base-app-domain';
|
||
export type EndpointItem<ED extends EntityDict & BaseEntityDict, BackCxt extends AsyncContext<ED>> = SimpleEndpoint<ED, BackCxt> | FreeEndpoint<ED, BackCxt>;
|
||
export interface SimpleEndpoint<ED extends EntityDict & BaseEntityDict, BackCxt extends AsyncContext<ED>> {
|
||
name: string;
|
||
params?: string[];
|
||
method: 'get' | 'post' | 'put' | 'delete';
|
||
type?: "simple";
|
||
fn: (context: BackCxt, params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<any>;
|
||
}
|
||
/**
|
||
* 此类型的接口可能会保持长连接,逐步返回内容,所以直接控制res和req即可
|
||
* backendcontext也不能直接传入,在需要时调用方法开启事务
|
||
* 返回时可以指定contentType
|
||
*/
|
||
export interface FreeEndpoint<ED extends EntityDict & BaseEntityDict, BackCxt extends AsyncContext<ED>> {
|
||
name: string;
|
||
params?: string[];
|
||
method: 'get' | 'post' | 'put' | 'delete';
|
||
type: "free";
|
||
fn: (contextBuilder: () => Promise<BackCxt>, params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<{
|
||
headers?: Record<string, string | string[]>;
|
||
statusCode?: number;
|
||
data: any;
|
||
}>;
|
||
}
|
||
export type Endpoint<ED extends EntityDict & BaseEntityDict, BackCxt extends AsyncContext<ED>> = EndpointItem<ED, BackCxt> | EndpointItem<ED, BackCxt>[];
|