Skip to content

Commit

Permalink
refactor: Data parsing function
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCCC committed Aug 22, 2024
1 parent eb36379 commit 35c1ba5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
105 changes: 56 additions & 49 deletions src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,57 +510,9 @@ export class RequestHandler {
requestConfig,
)) as FetchResponse<ResponseData>;

// Attempt to collect response data regardless of response status
const contentType = String(
(response as Response)?.headers?.get('Content-Type') || '',
);
let data;

// Handle edge case of no content type being provided... We assume json here.
if (!contentType) {
const responseClone = response.clone();

try {
data = await responseClone.json();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
// JSON parsing failed
data = null;
}
}

if (typeof data === 'undefined') {
try {
if (
contentType.includes(APPLICATION_JSON) ||
// This Media Type Suffix is standardizded by IETF in RFC 6839
contentType.includes('+json')
) {
data = await response.json(); // Parse JSON response
} else if (contentType.includes('multipart/form-data')) {
data = await response.formData(); // Parse as FormData
} else if (contentType.includes('application/octet-stream')) {
data = await response.blob(); // Parse as blob
} else if (
contentType.includes('application/x-www-form-urlencoded')
) {
data = await response.formData(); // Handle URL-encoded forms
} else if (typeof response.text !== 'undefined') {
data = await response.text();
} else {
// Handle streams
data = response.body || response.data || null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
// JSON parsing failed
data = null;
}
}

// Add more information to response object
response.config = requestConfig;
response.data = data;
response.data = await this.parseData(response);

// Check if the response status is not outside the range 200-299 and if so, output error
if (!response.ok) {
Expand Down Expand Up @@ -609,6 +561,61 @@ export class RequestHandler {
FetchResponse<ResponseData>;
}

/**
* Parses the response data based on the Content-Type header.
*
* @param response - The Response object to parse.
* @returns A Promise that resolves to the parsed data.
*/
public async parseData<ResponseData = APIResponse>(
response: FetchResponse<ResponseData>,
): Promise<any> {
const contentType = String(
(response as Response).headers?.get('Content-Type') || '',
);
let data;

// Handle edge case of no content type being provided... We assume JSON here.
if (!contentType) {
const responseClone = response.clone();
try {
data = await responseClone.json();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
// JSON parsing failed, fallback to null
data = null;
}
}

if (typeof data === 'undefined') {
try {
if (
contentType.includes(APPLICATION_JSON) ||
contentType.includes('+json')
) {
data = await response.json(); // Parse JSON response
} else if (contentType.includes('multipart/form-data')) {
data = await response.formData(); // Parse as FormData
} else if (contentType.includes('application/octet-stream')) {
data = await response.blob(); // Parse as blob
} else if (contentType.includes('application/x-www-form-urlencoded')) {
data = await response.formData(); // Handle URL-encoded forms
} else if (typeof response.text === 'function') {
data = await response.text(); // Parse as text
} else {
// Handle streams
data = response.body || response.data || null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
// Parsing failed, fallback to null
data = null;
}
}

return data;
}

public processHeaders<ResponseData>(
response: FetchResponse<ResponseData>,
): HeadersObject {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function appendQueryParams(url: string, params: QueryParams): string {
obj[i],
);
}
} else if (Object.prototype.toString.call(obj) === '[object Object]') {
} else if (typeof obj === 'object' && obj !== null) {
for (key in obj) {
buildParams(prefix + '[' + key + ']', obj[key]);
}
Expand Down

0 comments on commit 35c1ba5

Please sign in to comment.