Introduce Method type to narrow js/ts runtime APIs.

This commit is contained in:
Sebastian Jeltsch
2024-11-20 13:48:41 +01:00
parent ac672c43a9
commit b004188a5a
2 changed files with 14 additions and 4 deletions
+3 -2
View File
@@ -23,6 +23,7 @@ export type ResponseType = {
};
export type MaybeResponse<T> = Promise<T | undefined> | T | undefined;
export type CallbackType = (req: RequestType) => MaybeResponse<ResponseType>;
export type Method = "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE";
export declare enum StatusCodes {
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
@@ -122,8 +123,8 @@ export interface JsonResponseType {
body: object;
}
export declare function jsonHandler(f: (req: JsonRequestType) => MaybeResponse<JsonRequestType | object>): CallbackType;
export declare function addRoute(method: string, route: string, callback: CallbackType): void;
export declare function dispatch(method: string, route: string, uri: string, pathParams: [string, string][], headers: [string, string][], user: UserType | undefined, body: Uint8Array): Promise<ResponseType>;
export declare function addRoute(method: Method, route: string, callback: CallbackType): void;
export declare function dispatch(method: Method, route: string, uri: string, pathParams: [string, string][], headers: [string, string][], user: UserType | undefined, body: Uint8Array): Promise<ResponseType>;
export declare function query(queryStr: string, params: unknown[]): Promise<unknown[][]>;
export declare function execute(queryStr: string, params: unknown[]): Promise<number>;
export type ParsedPath = {
+11 -2
View File
@@ -25,6 +25,15 @@ export type ResponseType = {
};
export type MaybeResponse<T> = Promise<T | undefined> | T | undefined;
export type CallbackType = (req: RequestType) => MaybeResponse<ResponseType>;
export type Method =
| "DELETE"
| "GET"
| "HEAD"
| "OPTIONS"
| "PATCH"
| "POST"
| "PUT"
| "TRACE";
/// HTTP status codes.
///
@@ -570,7 +579,7 @@ export function jsonHandler(
const callbacks = new Map<string, CallbackType>();
export function addRoute(
method: string,
method: Method,
route: string,
callback: CallbackType,
) {
@@ -580,7 +589,7 @@ export function addRoute(
}
export async function dispatch(
method: string,
method: Method,
route: string,
uri: string,
pathParams: [string, string][],