@nahkies/typescript-koa-runtime
Version:
Runtime package for code generated by @nahkies/openapi-code-generator using the typescript-koa template
130 lines (129 loc) • 5.15 kB
text/typescript
import { Server } from "node:http";
import { AddressInfo, ListenOptions } from "node:net";
import Cors from "@koa/cors";
import Router from "@koa/router";
import Koa, { Context, Middleware } from "koa";
import { KoaBodyMiddlewareOptions } from "koa-body";
//#region ../typescript-common-runtime/dist/esm/request-bodies/octet-stream.d.mts
//#region src/request-bodies/octet-stream.d.ts
type SizeLimit = number | `${number}${"b" | "kb" | "mb" | "gb"}`;
//#endregion
//#region ../typescript-common-runtime/dist/esm/types.d.mts
//#region src/types.d.ts
type Enumerate<N extends number, Acc extends number[] = []> = Acc["length"] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc["length"]]>;
type IntRange<F extends number, T extends number> = F extends T ? F : Exclude<Enumerate<T>, Enumerate<F>> extends never ? never : Exclude<Enumerate<T>, Enumerate<F>> | T;
type StatusCode1xx = IntRange<100, 199>;
type StatusCode2xx = IntRange<200, 299>;
type StatusCode3xx = IntRange<300, 399>;
type StatusCode4xx = IntRange<400, 499>;
type StatusCode5xx = IntRange<500, 599>;
type StatusCode = StatusCode1xx | StatusCode2xx | StatusCode3xx | StatusCode4xx | StatusCode5xx;
type Res<Status extends StatusCode, Type> = {
status: Status;
body: Type;
};
type Params<Params, Query, Body, Header> = {
params: Params;
query: Query;
body: Body;
headers: Header;
};
declare const SkipResponse: unique symbol;
//#endregion
//#region ../typescript-common-runtime/dist/esm/query-parser.d.mts
//#region src/query-parser.d.ts
/**
* Simplified flattened schema structure, where compositions, nullability, etc, don't matter.
*
* We just need to know what is **possible**, as after initial parsing we'll validate against
* the actual schema.
*/
type PrimitiveType = {
type: "string" | "number" | "boolean" | "null";
};
type ObjectSchema = {
type: "object";
properties: Record<string, SchemaStructure>;
};
type ArraySchema = {
type: "array";
items: SchemaStructure;
};
type SchemaStructure = PrimitiveType | ObjectSchema | ArraySchema;
type Style = "deepObject" | "form" | "pipeDelimited" | "spaceDelimited";
interface QueryParameter {
name: string;
schema: SchemaStructure;
style?: Style;
explode?: boolean;
}
declare function parseQueryParameters(rawQuery: string, parameters: QueryParameter[]): Record<string, unknown>; //#endregion
//#endregion
//#region src/server.d.ts
declare class KoaRuntimeResponse<Type> {
private readonly status;
private _body?;
constructor(status: StatusCode);
body(body: Type): this;
unpack(): Res<StatusCode, Type | undefined>;
}
declare function handleResponse(ctx: Context, validator: (status: number, value: unknown) => unknown): (response: KoaRuntimeResponse<unknown> | typeof SkipResponse | Res<StatusCode, unknown>) => Promise<void>;
declare function handleImplementationError(err: unknown): never;
type KoaRuntimeResponder<Status extends StatusCode = StatusCode, Type = any> = {
withStatus: (status: Status) => KoaRuntimeResponse<Type>;
};
type ServerConfig = {
/**
* set to "disabled" to disable cors middleware, omit or pass undefined for defaults
*
* the default behavior is to allow all origins
**/
cors?: "disabled" | Cors.Options | undefined;
/**
* set to "disabled" to disable body parsing middleware, omit or pass undefined for defaults.
*
* if disabling, ensure you pass a body parsing middleware that places the parsed
* body on `ctx.body` for request body processing to work.
**/
body?: "disabled" | Partial<KoaBodyMiddlewareOptions> | undefined;
/**
* allows you to provide arbitrary koa middleware
* useful for mounting logging, error handlers, 404 handling, alternative body parsers, etc.
*/
middleware?: Middleware[];
/**
* the router to use, normally obtained by calling the generated `createRouter`
* function
*/
router: Router;
/**
* the port to listen on, a randomly allocated port will be used if none passed
* alternatively ListenOptions can be passed to control the network interface
* bound to.
*/
port?: number | ListenOptions;
};
declare function parseOctetStream(ctx: Context, sizeLimit: SizeLimit): Promise<Blob | undefined>;
/**
* Starts a Koa server and listens on `port` or a randomly allocated port if none provided.
* Enables CORS and body parsing by default. It's recommended to customize the CORS options
* for production usage.
*
* If you need more control over your Koa server, you should avoid calling this function
* and instead mount the router from your generated codes `createRouter` call directly
* onto a server you have constructed.
*/
declare function startServer({
middleware,
cors,
body,
port,
router
}: ServerConfig): Promise<{
app: Koa;
server: Server;
address: AddressInfo;
}>;
//#endregion
export { KoaRuntimeResponder, KoaRuntimeResponse, type Params, type Res, ServerConfig, SkipResponse, type StatusCode, type StatusCode1xx, type StatusCode2xx, type StatusCode3xx, type StatusCode4xx, type StatusCode5xx, handleImplementationError, handleResponse, parseOctetStream, parseQueryParameters, startServer };
//# sourceMappingURL=server.d.cts.map