@openapi-ts/request-types
Version:
TypeScript types for HTTP requests, supporting code generated by @openapi-ts/backend.
70 lines • 2.89 kB
TypeScript
export declare type OneOrMany<T> = T | Array<T>;
export declare type Params<K extends string = string, V = OneOrMany<string | number | boolean | undefined>> = Record<K, V>;
export declare namespace Params {
function getValue<K extends string, V>(obj: Params<K, OneOrMany<V>>, key: string): V | undefined;
function getValues<K extends string, V>(obj: Params<K, OneOrMany<V>>, key: string): V[] | undefined;
}
export declare type StringParams = Params<string, OneOrMany<string>>;
/**
* A typed request
* @template Body Type of request body
* @template PathParams Type of request path parameters
* @template Query Type of request query
* @template Headers Type of request headers
*
* @property method HTTP method
* @property path Path
* @property params Path params
* @property headers Headers
* @property body Body content parsed from JSON
* @property query Query string or parsed query object
*
*/
export interface Request<Body = unknown, PathParams extends Params = Params, Query extends Params = Params, Headers extends Params = Params, Cookies extends Params = Params> {
method: string;
path: string;
params: PathParams;
query: Query;
headers: Headers;
cookies: Cookies;
body: Body;
}
/**
* Response - this is an output parameter for a handler function to fill in
* @template Body Type of response body
* @template Headers Type of response headers
*
* @property statusCode HTTP status code
* @property body Body content which will be sent as JSON
* @property headers Headers
*/
export interface Response<Body = unknown, Headers extends Params = Params> {
statusCode?: number;
headers: Partial<Headers>;
body?: Body;
}
/**
* @template T Type of value or promised value
*/
export declare type Awaitable<T> = T | Promise<T>;
/**
* A handler implementing a single API operation.
* The request and response types are coerced to fit the schemas of the matched operation.
*
* This function may alter the given response object and/or return a response body.
* If res.body is not set when this function returns, the return value of the handler will be used as the response body.
* If res.statusCode is not set when this function returns and a single 2xx status code exists in the response schema,
* it will be used. Otherwise, not setting any status code will cause a 500 error.
*
* @template T Type of custom data passed in params
* @template Req Type of request
* @template Res Type of response
*
* @param req Request
* @param res Response
* @param params Operation params
* @async
* @returns Response body or nothing
*/
export declare type RequestHandler<P = unknown, Req extends Request = Request, Res extends Response = Response, Result = Awaitable<Res['body'] | void>> = (req: Req, res: Res, params: P) => Result;
//# sourceMappingURL=types.d.ts.map