feat: added tests for getFileById method. fix: refactor getApiResponse and TryToGetMessage methods in ApiResponseFactory class to be able to return the proper string message when the responseType passed to axios was 'stream'

This commit is contained in:
Stevan Freeborn
2023-02-16 16:57:52 -06:00
parent 4370574bbd
commit ee5aaf3c1f
5 changed files with 244 additions and 27 deletions
+37 -5
View File
@@ -1,6 +1,7 @@
import { type AxiosResponse } from 'axios';
import { HttpStatusCode } from '../enums/HttpStatusCode';
import { ApiResponse } from './ApiResponse';
import { type Readable } from 'stream';
/**
* @class ApiResponseFactory - Factory class for creating ApiResponse objects
@@ -9,10 +10,12 @@ export class ApiResponseFactory {
/**
* @method getApiResponse - Creates an ApiResponse object from an AxiosResponse object
* @param {AxiosResponse} response - The AxiosResponse object that will be used to create the ApiResponse object
* @returns {ApiResponse<T>} - An ApiResponse object of type T
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse object
*/
public static getApiResponse<T>(response: AxiosResponse): ApiResponse<T> {
const message = this.TryToGetMessage(response);
public static async getApiResponse<T>(
response: AxiosResponse
): Promise<ApiResponse<T>> {
const message = await this.TryToGetMessage(response);
if (this.isSuccessStatusCode(response.status) === true) {
return new ApiResponse<T>(response.status, message, response.data);
@@ -24,24 +27,53 @@ export class ApiResponseFactory {
/**
* @method TryToGetMessage - Attempts to get the message from the response data
* @param {AxiosResponse} response - The AxiosResponse object that will be used to get the message
* @returns {string} - The message from the response data
* @returns {Promise<string>} - A promise that resolves to the message
*/
private static TryToGetMessage(response: AxiosResponse): string {
private static async TryToGetMessage(
response: AxiosResponse
): Promise<string> {
if (
response.status === HttpStatusCode.Unauthorized ||
response.status === HttpStatusCode.Forbidden ||
response.status === HttpStatusCode.NotFound
) {
if (response.config.responseType === 'stream') {
const dataString = await this.getStreamDataAsString(response.data);
const dataObject =
dataString.length > 0 ? JSON.parse(dataString) : '{}';
return dataObject.message;
}
return response.data?.message;
}
if (this.isSuccessStatusCode(response.status) === false) {
if (response.config.responseType === 'stream') {
const data = response.data as Readable;
return await this.getStreamDataAsString(data);
}
return JSON.stringify(response.data);
}
return '';
}
/**
*
*/
private static async getStreamDataAsString(
stream: Readable
): Promise<string> {
const chunks = [] as Buffer[];
for await (const chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString('utf-8');
}
/**
* @method isSuccessStatusCode - Determines if the specified status code is a success status code
* @param {number} statusCode - The status code that will be used to determine if it is a success status code
+5 -5
View File
@@ -252,7 +252,7 @@ export class OnspringClient {
responseType: 'stream',
});
const apiResponse = ApiResponseFactory.getApiResponse<any>(response);
const apiResponse = await ApiResponseFactory.getApiResponse<any>(response);
if (apiResponse.isSuccessful === false) {
return apiResponse;
@@ -511,7 +511,7 @@ export class OnspringClient {
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.get(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
const apiResponse = await ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
@@ -528,7 +528,7 @@ export class OnspringClient {
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.post(endpoint, data, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
const apiResponse = await ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
@@ -545,7 +545,7 @@ export class OnspringClient {
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.put(endpoint, data, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
const apiResponse = await ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
@@ -560,7 +560,7 @@ export class OnspringClient {
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.delete(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
const apiResponse = await ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
}