All files / src Response.ts

100% Statements 48/48
100% Branches 12/12
100% Functions 18/18
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                              1x 1x 1x   1x 1x               1x           41x 41x 41x       38x 38x       45x       1x       45x 2x   43x 4x     39x     45x       1x 1x     1x       15x 14x     1x     15x       1x       1x       2x       4x 3x 1x   2x     1x     1x       1x               1x 1x             1x 1x             1x 1x             1x 1x             1x 1x             2x 2x      
// Copyright (C) 2017  Norman Breau
 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import {StatusCode} from './StatusCode';
import {ResponseData} from './ResponseData';
import {StormError, IErrorResponse} from './StormError';
import * as express from 'express';
import {getApplicationLogger} from './instance';
import { InternalError } from './InternalError';
 
export type SendableData = ResponseData | Error | IErrorResponse | any;
 
export interface IHeaderKeyValuePair {
    [key: string]: string;
}
 
export class Response<TResponse = SendableData, TErrorResponse = Error | IErrorResponse | string> {
    private response: express.Response;
    private created: Date;
    private requestURL: string;
 
    public constructor(response: express.Response, requestURL: string) {
        this.response = response;
        this.created = new Date();
        this.requestURL = requestURL;
    }
 
    public setStatus(status: StatusCode): Response<TResponse, TErrorResponse> {
        this.response.status(status);
        return this;
    }
 
    public getStatus(): StatusCode {
        return this.response.statusCode;
    }
 
    public redirect(url: string): void {
        this.response.redirect(url);
    }
 
    public send(data?: TResponse | TErrorResponse | StormError | IErrorResponse): void {
        if (data instanceof ResponseData) {
            this.setStatus(data.getStatus()).send(data.getData());
        }
        else if (data instanceof StormError) {
            this.setStatus(data.getHTTPCode()).send(data.getErrorResponse());
        }
        else {
            this.response.send(data);
        }
        
        getApplicationLogger().info(`API ${this.requestURL} (${this.getStatus()}) responded in ${new Date().getTime() - this.created.getTime()}ms`);
    }
 
    public pipe(stream: NodeJS.ReadableStream): void {
        stream.on('end', () => {
            stream.unpipe(this.response);
        });
        
        stream.pipe(this.response);
    }
 
    public success(data?: TResponse): void {
        if (data === undefined) {
            this.setStatus(StatusCode.OK_NO_CONTENT);
        }
        else {
            this.setStatus(StatusCode.OK);
        }
 
        this.send(data);
    }
 
    public setHeader(key: string, value: string): void {
        this.response.set(key, value);
    }
 
    public setHeaders(keyValuePair: IHeaderKeyValuePair): void {
        this.response.set(keyValuePair);
    }
 
    public isHeadersSent(): boolean {
        return this.response.headersSent;
    }
 
    public error(error?: TErrorResponse | ResponseData<TErrorResponse>): void {
        if (error) {
            if (error instanceof StormError) {
                this.send(error);
            }
            else if (error instanceof ResponseData) {
                // If it was not ResponseData<TResponse> then
                // the method signature should have caught it
                this.send((<TErrorResponse><unknown>error));
            }
            else {
                this.send(new InternalError(error));
            }
        }
        else {
            this.send(new InternalError(error));
        }
    }
 
    /**
     * @deprecated
     */
    public badRequest(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.ERR_BAD_REQUEST).send(data);
    }
 
    /**
     * @deprecated
     */
    public unauthorized(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.ERR_UNAUTHORIZED).send(data);
    }
 
    /**
     * @deprecated
     */
    public forbidden(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.ERR_FORBIDDEN).send(data);
    }
 
    /**
     * @deprecated
     */
    public conflict(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.ERR_CONFLICT).send(data);
    }
 
    /**
     * @deprecated
     */
    public notFound(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.ERR_NOT_FOUND).send(data);
    }
 
    /**
     * @deprecated
     */
    public internalError(data?: TErrorResponse | StormError): void {
        getApplicationLogger().deprecate();
        this.setStatus(StatusCode.INTERNAL_ERROR).send(data);
    }
}