UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

48 lines (47 loc) 2.75 kB
import { Application } from './Application'; import { Request } from './Request'; import { Response } from './Response'; import { Middleware } from './Middleware'; import { StormError } from './StormError'; import { ResponseData } from './ResponseData'; import { ReadStream } from 'fs'; /** * IHandlerResponse can actually accept any arbitrary object, however it may do * certain things depending on the type of object it receives. * * - If the response object is a stream, it will pipe the stream to stream the HTTP response. * - If the response is ResponseData, the status code and response data will be passed as the HTTP response. * - Passing nothing/undefined will return a status code of 204 with no body content * - Primitive data types will be passed as is * - Buffers will be passed through * - Any other object will be passed through JSON.stringify */ export type IHandlerResponse = ResponseData | ReadableStream | ReadStream | any | void; /** * Like IHandlerResponse, an IHandlerError can be any arbitrary type of object, * however it's recommended that the type be of a StormError. * * If the type is not a StormError, the error will be wrapped in an InternalError object. * This is to avoid accidental leakage of privilege data (e.g. snippets of database queries with sensitive information) */ export type IHandlerError = StormError | Error | any; export declare class Handler<TApplication extends Application = Application, TGetRequest = any, TGetResponse = IHandlerResponse, TPostRequest = any, TPostResponse = IHandlerResponse, TPutRequest = any, TPutResponse = IHandlerResponse, TDeleteRequest = any, TDeleteResponse = IHandlerResponse> { private $app; private $middlewares; constructor(app: TApplication); getApplication(): TApplication; protected _initMiddlewares(): Middleware[]; getAccessToken(request: Request): string; private $executeMiddlewares; protected _onMiddlewareReject(request: Request, response: Response, error: StormError): void; private $handleResponse; private $handleResponseError; get(request: Request<TGetRequest>, response: Response<TGetResponse>): Promise<void>; put(request: Request<TPutRequest>, response: Response<TPutResponse>): Promise<void>; post(request: Request<TPostRequest>, response: Response<TPostResponse>): Promise<void>; delete(request: Request<TDeleteRequest>, response: Response<TDeleteResponse>): Promise<void>; protected _get(request: Request<TGetRequest>): Promise<TGetResponse>; protected _post(request: Request<TPostRequest>): Promise<TPostResponse>; protected _put(request: Request<TPutRequest>): Promise<TPutResponse>; protected _delete(request: Request<TDeleteRequest>): Promise<TDeleteResponse>; }