All files / models api-response.model.ts

4.34% Statements 1/23
0% Branches 0/16
0% Functions 0/2
4.34% Lines 1/23

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                                3x                                                                                                                                                                    
export type ApiResponseType =
  | "created"
  | "updated"
  | "deleted"
  | "denied"
  | "granted"
  | "not-created"
  | "not-updated"
  | "not-deleted"
  | "not-found"
  | "done"
  | "found"
  | "no"
  | "yes"
  | "error";
 
export const API_RESPONSE_TYPE = {
  CREATED: "created" as ApiResponseType,
  UPDATED: "updated" as ApiResponseType,
  DELETED: "deleted" as ApiResponseType,
  DENIED: "denied" as ApiResponseType,
  GRANTED: "granted" as ApiResponseType,
  NOT_CREATED: "not-created" as ApiResponseType,
  NOT_UPDATED: "not-updated" as ApiResponseType,
  NOT_DELETED: "not-deleted" as ApiResponseType,
  NOT_FOUND: "not-found" as ApiResponseType,
  FOUND: "found" as ApiResponseType,
  NEGATIVE: "no" as ApiResponseType,
  POSITIVE: "yes" as ApiResponseType,
  DONE: "done" as ApiResponseType,
  ERROR: "error" as ApiResponseType,
};
 
export class ApiResponse {
  result: ApiResponseType;
  payload: any;
  isSuccess: boolean = false;
  message: string = "";
 
  constructor(
    result: ApiResponseType,
    payload: any = {},
    message: string = ""
  ) {
    this.result = result;
    this.payload = payload;
    this.isSuccess = this.evaluateSuccess(result);
    this.message = message;
  }
 
  private evaluateSuccess(code: ApiResponseType) {
    switch (code) {
      case API_RESPONSE_TYPE.CREATED:
        return true;
 
      case API_RESPONSE_TYPE.UPDATED:
        return true;
 
      case API_RESPONSE_TYPE.DELETED:
        return true;
 
      case API_RESPONSE_TYPE.DENIED:
        return false;
 
      case API_RESPONSE_TYPE.GRANTED:
        return true;
 
      case API_RESPONSE_TYPE.NOT_CREATED:
        return false;
 
      case API_RESPONSE_TYPE.NOT_UPDATED:
        return false;
 
      case API_RESPONSE_TYPE.NOT_DELETED:
        return false;
 
      case API_RESPONSE_TYPE.NOT_FOUND:
        return true;
 
      case API_RESPONSE_TYPE.FOUND:
        return true;
 
      case API_RESPONSE_TYPE.DONE:
        return true;
 
      case API_RESPONSE_TYPE.ERROR:
        return false;
 
      case API_RESPONSE_TYPE.POSITIVE:
        return true;
 
      case API_RESPONSE_TYPE.NEGATIVE:
        return true;
    }
 
    return false;
  }
}