oak-domain/lib/types/Endpoint.d.ts

31 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// <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>[];