Skip to content

Commit

Permalink
feat(response): improved comments for code editor intellisense
Browse files Browse the repository at this point in the history
  • Loading branch information
yatendra121 committed May 23, 2024
1 parent d1ed2ee commit cd1cb2e
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 90 deletions.
79 changes: 73 additions & 6 deletions packages/client/src/lib/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ type ApiMessage = string
type ApiError = string
type ApiServerError = any

/**
* Interface defining methods to access various properties of an API response.
*
* @template T - The type of the response data.
*/
interface ApiResponseInterface<T> {
getData: () => T | undefined
getMessage: () => ApiMessage | undefined
Expand All @@ -13,7 +18,7 @@ interface ApiResponseInterface<T> {
}

/**
* Represents the response value returned by the API.
* Represents the structure of the value returned by the API.
*
* @template T - The type of the response data.
*/
Expand All @@ -27,87 +32,149 @@ export interface ApiResponseValue<T = any> {
}

/**
* Will use collect data from api response
* Class to collect and provide data from an API response.
*
* @template T - The type of the response data.
*/
class ApiResponse<T = any> implements ApiResponseInterface<T> {
constructor(private readonly response: ApiResponseValue<T> = {}) {}

/**
* Gets the error code from the response.
*
* @returns The error code, if present.
*/
getErrorCode(): ApiErrorCode | undefined {
return this.response.errorCode
}

/**
* Gets the errors from the response.
*
* @returns An object containing the errors, if present.
*/
getErrors(): ApiErrors | undefined {
return this.response.errors
}

/**
* Gets the error message from the response.
*
* @returns The error message, if present.
*/
getError(): ApiError | undefined {
return this.response.error
}

/**
* Gets the message from the response.
*
* @returns The message, if present.
*/
getMessage(): ApiMessage | undefined {
return this.response.message
}

/**
* Gets the data from the response.
*
* @returns The data, if present.
*/
getData(): T | undefined {
return this.response.data
}
}

/**
* Interface defining methods to access properties of a successful API response.
*
* @template T - The type of the response data.
*/
interface ApiSuccessResponseInterface<T> {
getData: () => T
getMessage: () => ApiMessage
}

/**
* Represents a successful API response value.
* Represents the structure of a successful API response value.
* It includes the response data and a message.
*
* @template T - The type of the response data.
*/
export type ApiSuccessResponseValue<T> = Pick<Required<ApiResponseValue<T>>, 'data' | 'message'>

/**
* Will use collect data from success api response
* Class to collect and provide data from a successful API response.
*
* @template T - The type of the response data.
*/
class ApiSuccessResponse<T> implements ApiSuccessResponseInterface<T> {
constructor(private readonly response: ApiSuccessResponseValue<T>) {}

/**
* Gets the data from the response.
*
* @returns The response data.
*/
getData(): T {
return this.response.data
}

/**
* Gets the message from the response.
*
* @returns The response message.
*/
getMessage(): ApiMessage {
return this.response.message
}
}

/**
* Interface defining methods to access properties of an error API response.
*/
interface ApiErrorResponseInterface {
getError: () => string
getErrors: () => Record<string, string[]> | undefined
getErrorCode: () => string | undefined
}

/**
* Represents an error API response value.
* Represents the structure of an error API response value.
* It includes error details such as error message and error codes.
*/
export type ApiErrorResponseValue = Pick<Required<ApiResponseValue<unknown>>, 'errors' | 'error'> &
Pick<ApiResponseValue<unknown>, 'errorCode'>

/**
* Will use collect data from error api response
* Class to collect and provide data from an error API response.
*/
class ApiErrorResponse implements ApiErrorResponseInterface {
constructor(private readonly response: ApiErrorResponseValue) {}

/**
* Gets the error message from the response.
*
* @returns The error message.
*/
getError() {
return this.response.error
}

/**
* Gets the errors from the response.
*
* @returns An object containing the errors, if present.
*/
getErrors() {
return this.response.errors
}

/**
* Gets the error code from the response.
*
* @returns The error code, if present.
*/
getErrorCode() {
return this.response.errorCode
}
Expand Down
77 changes: 46 additions & 31 deletions packages/response/src/lib/apiResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type { ApiResponseErrors } from '@qnx/errors'
const SHOW_SERVER_ERROR = true

/**
* This class is useful for send api response
* Class for sending API responses.
*
* @template T - The type of the data included in the response.
*/
export class ApiResponse<T = unknown> {
#statusCode = 200
Expand All @@ -17,15 +19,20 @@ export class ApiResponse<T = unknown> {
protected message?: string
protected serverError?: unknown

//Get new instance
/**
* Returns a new instance of ApiResponse.
*
* @returns A new ApiResponse instance.
*/
static getInstance(): ApiResponse {
return new ApiResponse()
}

/**
* Set any data for response
* @param data
* @returns ApiResponse
* Sets the data for the response.
*
* @param data - The data to be included in the response.
* @returns The current ApiResponse instance.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setData(data: any) {
Expand All @@ -34,59 +41,65 @@ export class ApiResponse<T = unknown> {
}

/**
* Set any error code
* @param errorCode
* @returns
* Sets the error code for the response.
*
* @param errorCode - The error code to be included in the response.
* @returns The current ApiResponse instance.
*/
setErrorCode(errorCode: string) {
this.errorCode = errorCode
return this
}

/**
* Set any error
* @param error
* @returns
* Sets a single error message for the response.
*
* @param error - The error message to be included in the response.
* @returns The current ApiResponse instance.
*/
setError(error: string) {
this.error = error
return this
}

/**
* Set multiple errors
* @param errors
* @returns
* Sets multiple errors for the response.
*
* @param errors - An object containing multiple error messages.
* @returns The current ApiResponse instance.
*/
setErrors(errors: ApiResponseErrors) {
this.errors = errors
return this
}

/**
* Set any message
* @param message
* @returns
* Sets a message for the response.
*
* @param message - The message to be included in the response.
* @returns The current ApiResponse instance.
*/
setMessage(message: string) {
this.message = message
return this
}

/**
* Set status code
* @param message
* @returns
* Sets the status code for the response.
*
* @param statusCode - The HTTP status code to be used for the response.
* @returns The current ApiResponse instance.
*/
setStatusCode(statusCode: number) {
this.#statusCode = statusCode
return this
}

/**
* Set server error
* @param error
* @returns
* Sets a server error for the response. The server error will only be included if SHOW_SERVER_ERROR is true.
*
* @param error - The server error to be included in the response.
* @returns The current ApiResponse instance.
*/
setServerError(error: unknown) {
if (!SHOW_SERVER_ERROR) return this
Expand All @@ -100,20 +113,21 @@ export class ApiResponse<T = unknown> {
}

/**
* Set additional data
* @param data
* @returns
* Sets additional data to be included in the response.
*
* @param data - An object containing additional data.
* @returns The current ApiResponse instance.
*/
setAdditional(data: { [key: string]: unknown }) {
this.#additionalData = data
return this
}

/**
* Return api response
* @param response
* @param status
* @returns
* Sends the API response using the provided Express response object.
*
* @param response - The Express response object to send the response with.
* @returns The response sent by the Express response object.
*/
response(response: ExResponse) {
if (this.errors) this.error = this.errors[Object.keys(this.errors)?.[0]]?.[0]
Expand All @@ -122,8 +136,9 @@ export class ApiResponse<T = unknown> {
}

/**
* Initializes and returns a new instance of ApiResponse.
*
* @returns Instance of ApiResponse class
* @returns A new instance of the ApiResponse class.
*/
export function initializeApiResponse() {
return ApiResponse.getInstance()
Expand Down
Loading

0 comments on commit cd1cb2e

Please sign in to comment.