### File

`src/core/services/common/http-response.service.ts`

### Description

HTTP response service

### Index

###### Methods
generate Private getDescription Private getMessage Private getType

### Methods

generate
`generate(status: number, data, message: string, description: string)`
Defined in src/core/services/common/http-response.service.ts:57
generates the HTTP response Parameters :
Name | Type | Optional | Default value | Description
status | `number` | No |  | HTTP status
data |  | No | `{}` | data
message | `string` | No | `this.getMessage(status)` | custom message
description | `string` | No | `this.getDescription(status)` | custom description
Returns : `HttpResponse` response

Private getDescription
`getDescription(status: number)`
Defined in src/core/services/common/http-response.service.ts:32
gets the description Parameters :
Name | Type | Optional | Description
status | `number` | No | HTTP status
Returns : `string` description

Private getMessage
`getMessage(status: number)`
Defined in src/core/services/common/http-response.service.ts:22
gets the message Parameters :
Name | Type | Optional | Description
status | `number` | No | HTTP status
Returns : `string` message

Private getType
`getType(status: number)`
Defined in src/core/services/common/http-response.service.ts:42
gets the type Parameters :
Name | Type | Optional | Description
status | `number` | No | HTTP status
Returns : `string` type

    import { HttpStatus, Injectable } from '@nestjs/common';
    import {
      HttpResponseDescriptions,
      HttpResponseMessages,
      HttpResponseTypes,
      HttpResponseTypesCodes,
    } from '../../domain/enums'

    import { HttpResponse } from '../../domain/interfaces';

    /**
     * HTTP response service
     */
    @Injectable()
    export class HttpResponseService {
      //==================================================================================================
      /**
       * gets the message
       * @param status HTTP status
       * @returns message
       */
      private getMessage(status: number): string {
        return HttpResponseMessages[HttpStatus[status].toString() as keyof typeof HttpResponseMessages];
      }

      //==================================================================================================
      /**
       * gets the description
       * @param status HTTP status
       * @returns description
       */
      private getDescription(status: number): string {
        return HttpResponseDescriptions[HttpStatus[status].toString() as keyof typeof HttpResponseMessages];
      }

      //==================================================================================================
      /**
       * gets the type
       * @param status HTTP status
       * @returns type
       */
      private getType(status: number): string {
        return HttpResponseTypes[
          HttpResponseTypesCodes[Math.floor(status / 100)].toString() as keyof typeof HttpResponseTypes
        ];
      }

      //==================================================================================================
      /**
       * generates the HTTP response
       * @param status HTTP status
       * @param data data
       * @param message custom message
       * @param description custom description
       * @returns response
       */
      generate(
        status: number,
        data: unknown = {},
        message: string = this.getMessage(status),
        description: string = this.getDescription(status)
      ): HttpResponse {
        const response: HttpResponse = {
          type: this.getType(status),
          status: status,
          message: message,
          description: description,
          data: data,
        };

        return response;
      }
    }

# results matching ""

# No results matching ""