@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
82 lines (81 loc) • 3.31 kB
TypeScript
import { type Context } from 'koa';
import { type Plugin } from 'koas-core';
declare function serializer(): Plugin;
declare function bodyParser(): Plugin;
export { bodyParser, serializer };
export * from 'koas-parameters';
export * from 'koas-security';
declare const errorStatusMap: {
readonly 400: "Bad Request";
readonly 401: "Unauthorized";
readonly 402: "Payment Required";
readonly 403: "Forbidden";
readonly 404: "Not Found";
readonly 405: "Method Not Allowed";
readonly 406: "Not Acceptable";
readonly 407: "Proxy Authentication Required";
readonly 408: "Request Timeout";
readonly 409: "Conflict";
readonly 410: "Gone";
readonly 411: "Length Required";
readonly 412: "Precondition Failed";
readonly 413: "Payload Too Large";
readonly 414: "Uri Too Long";
readonly 415: "Unsupported Media Type";
readonly 416: "Range Not Satisfiable";
readonly 417: "Expectation Failed";
readonly 418: "Im A Teapot";
readonly 421: "Misdirected Request";
readonly 422: "Unprocessable Entity";
readonly 423: "Locked";
readonly 424: "Failed Dependency";
readonly 425: "Too Early";
readonly 426: "Upgrade Required";
readonly 428: "Precondition Required";
readonly 429: "Too Many Requests";
readonly 431: "Request Header Fields Too Large";
readonly 451: "Unavailable For Legal Reasons";
readonly 500: "Internal Server Error";
readonly 501: "Not Implemented";
readonly 502: "Bad Gateway";
readonly 503: "Service Unavailable";
readonly 504: "Gateway Timeout";
readonly 505: "Http Version Not Supported";
readonly 506: "Variant Also Negotiates";
readonly 507: "Insufficient Storage";
readonly 508: "Loop Detected";
readonly 510: "Not Extended";
readonly 511: "Network Authentication Required";
};
type HttpErrorCodes = keyof typeof errorStatusMap;
/**
* Throw standard Koa error response.
*
* @param ctx The Koa context used to throw the error response.
* @param status The HTTP status code to use.
* @param message The message for in the response body.
* @param data Optional data to include.
*/
export declare function throwKoaError(ctx: Context, status: HttpErrorCodes, message?: string, data?: Record<string, any>): never;
/**
* Throw standard Koa error response when condition is met.
*
* @param condition The condition to check.
* @param ctx The Koa context used to throw the error response.
* @param status The HTTP status code to use.
* @param message The message for in the response body.
* @param data Optional data to include.
*/
export declare function assertKoaError(condition: boolean, ctx: Context, status: HttpErrorCodes, message?: string, data?: Record<string, any>): void;
/**
* Throws standard Koa error response when condition is _not_ met.
* Does the opposite of `assertKoaError`.
*
* @param condition The condition to check.
* @param ctx The Koa context used to throw the error response.
* @param status The HTTP status code to use.
* @param message The message for in the response body.
* @param data Optional data to include.
* @see throwKoaError
*/
export declare function assertKoaCondition(condition: boolean, ctx: Context, status: HttpErrorCodes, message?: string, data?: Record<string, any>): asserts condition;