@breautek/storm
Version:
Object-Oriented REST API framework
53 lines (52 loc) • 2.15 kB
TypeScript
import { StatusCode } from './StatusCode';
import { ResponseData } from './ResponseData';
import { StormError, IErrorResponse } from './StormError';
import * as express from 'express';
import { Stream } from 'stream';
import { Application } from './Application';
export type TSupportedResponsePrimitives = number | boolean | string | Date | IErrorResponse | void | TSupportedResponsePrimitives[] | TSerializableResponse<object>;
/**
* Utility type wrap, useful if you have a concrete interface of TSupportedResponsePrimitives properties.
* Use this to declare that your interface is Response Serializable.
*
* e.g.
*
* ```typescript
* interface MyInterface {...}
* type TMyInterface = TSerializableResponse<MyInterface>;
* ```
*
* OR
*
* ```typescript
* type MyInterface = TSerializableResponse<{...}>;
* ```
*
* NOTE: This actually will allow more than what is within the `TSupportedResponsePrimitives` union type.
* TypeScript doesn't offer a way to properly restrict or infer the type.
*/
export type TSerializableResponse<T extends object> = {
[k in keyof T]: TSupportedResponsePrimitives;
};
export type TSupportedResponseTypes = TSupportedResponsePrimitives | Error | StormError | Buffer | ReadableStream | Stream.Readable;
export interface IHeaderKeyValuePair {
[key: string]: string;
}
export declare class Response<TResponse extends TSupportedResponseTypes> {
private $app;
private $response;
private $created;
private $requestURL;
constructor(app: Application, response: express.Response, requestURL: string);
setStatus(status: StatusCode): Response<TResponse>;
getStatus(): StatusCode;
redirect(url: string): void;
private $send;
send(data?: TResponse | ResponseData<TResponse> | ResponseData<StormError> | StormError | IErrorResponse): void;
pipe(stream: NodeJS.ReadableStream): void;
success(data?: TResponse): void;
setHeader(key: string, value: string): void;
setHeaders(keyValuePair: IHeaderKeyValuePair): void;
isHeadersSent(): boolean;
error(error?: StormError | IErrorResponse | ResponseData<StormError> | unknown): void;
}