Add a first cut at an express-based backend
This commit is contained in:
59
express/types.ts
Normal file
59
express/types.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
// types.ts
|
||||
|
||||
// FIXME: split this up into types used by app developers and types internal
|
||||
// to the framework.
|
||||
import {
|
||||
Request as ExpressRequest,
|
||||
Response as ExpressResponse,
|
||||
} from "express";
|
||||
import { MatchFunction } from "path-to-regexp";
|
||||
import { z } from "zod";
|
||||
import { ContentType, contentTypes } from "./content-types";
|
||||
import { HttpCode, httpCodes } from "./http-codes";
|
||||
|
||||
const methodParser = z.union([
|
||||
z.literal("GET"),
|
||||
z.literal("POST"),
|
||||
z.literal("PUT"),
|
||||
z.literal("PATCH"),
|
||||
z.literal("DELETE"),
|
||||
]);
|
||||
|
||||
export type Method = z.infer<typeof methodParser>;
|
||||
const massageMethod = (input: string): Method => {
|
||||
const r = methodParser.parse(input.toUpperCase());
|
||||
|
||||
return r;
|
||||
};
|
||||
|
||||
export type Call = {
|
||||
pattern: string;
|
||||
path: string;
|
||||
method: Method;
|
||||
parameters: object;
|
||||
request: ExpressRequest;
|
||||
};
|
||||
|
||||
export type InternalHandler = (req: ExpressRequest) => Promise<Result>;
|
||||
|
||||
export type Handler = (call: Call) => Promise<Result>;
|
||||
export type ProcessedRoute = {
|
||||
matcher: MatchFunction<Record<string, string>>;
|
||||
method: Method;
|
||||
handler: InternalHandler;
|
||||
};
|
||||
|
||||
export type Result = {
|
||||
code: HttpCode;
|
||||
contentType: ContentType;
|
||||
result: string;
|
||||
};
|
||||
|
||||
export type Route = {
|
||||
path: string;
|
||||
methods: Method[];
|
||||
handler: Handler;
|
||||
interruptable?: boolean;
|
||||
};
|
||||
|
||||
export { methodParser, massageMethod };
|
||||
Reference in New Issue
Block a user