ts5deco-express-controller
Version:
TypeScript 5 Modern Decorator Express Controller Framework
60 lines • 2.04 kB
TypeScript
import { Response } from 'express';
import { BaseResponse } from './BaseResponse';
/**
* JSON response class for sending JSON data with appropriate status codes
*
* @template TData - The type of the JSON data
* @template TStatus - The HTTP status code (defaults to number)
*
* @example
* ```typescript
* // Success response with typed data
* return new JsonResponse<User[], 200>(200, users);
*
* // Created response with specific type
* return new JsonResponse<CreateUserResponse, 201>(201, { id: 1, name: 'John' });
*
* // Error response with error object
* return new JsonResponse<ErrorResponse, 400>(400, { error: 'Invalid input' });
*
* // Using without generics (still works)
* return new JsonResponse(200, { users: [...] });
* ```
*/
export declare class JsonResponse<TData = any, TStatus extends number = number> extends BaseResponse {
readonly data?: TData | undefined;
constructor(statusCode: TStatus, data?: TData | undefined);
send(res: Response): void;
/**
* Static convenience methods for common JSON responses
*/
/**
* 200 OK with JSON data
*/
static ok<TData = any>(data?: TData): JsonResponse<TData, 200>;
/**
* 201 Created with JSON data
*/
static created<TData = any>(data?: TData): JsonResponse<TData, 201>;
/**
* 400 Bad Request with error data
*/
static badRequest<TError = any>(error?: TError): JsonResponse<TError, 400>;
/**
* 401 Unauthorized with error data
*/
static unauthorized<TError = any>(error?: TError): JsonResponse<TError, 401>;
/**
* 403 Forbidden with error data
*/
static forbidden<TError = any>(error?: TError): JsonResponse<TError, 403>;
/**
* 404 Not Found with error data
*/
static notFound<TError = any>(error?: TError): JsonResponse<TError, 404>;
/**
* 500 Internal Server Error with error data
*/
static internalError<TError = any>(error?: TError): JsonResponse<TError, 500>;
}
//# sourceMappingURL=JsonResponse.d.ts.map