All files / src/core/extensions responseExtensions.ts

100% Statements 17/17
75% Branches 3/4
100% Functions 8/8
100% Lines 9/9

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    7x 39x   7x   7x       13x   7x   7x   7x   62x            
import { Response } from "express";
 
export class ResponseExtensions {    
    public static ok = (res: Response, response: object | string) => ResponseExtensions.jsonResponse(res, 200, response);
    
    public static added = (res: Response, added: number, entity: string) => ResponseExtensions.jsonResponse(res, 201, { message: `Added ${added} ${entity}${added == 1 ? '' : 's'} to database`});
    
    public static noContent = (res: Response) => res
        .status(204)
        .end();
    
    public static badRequest = (res: Response, message: string) => ResponseExtensions.jsonResponse(res, 400, { error: message });
 
    public static unauthorized = (res: Response, message: string) => ResponseExtensions.jsonResponse(res, 401, { error: message });
 
    public static forbidden = (res: Response, message: string) => ResponseExtensions.jsonResponse(res, 403, { error: message });
    
    public static internalError = (res: Response, message: string) => ResponseExtensions.jsonResponse(res, 500, { error: message });
 
    private static jsonResponse = (res: Response, status: number, response: object | string) => res
        .status(status)
        .json(typeof response === 'object'
            ? response
            : { response })
        .end();
}