Skip to content

Commit

Permalink
feat: Updated sdks/ts/src/utils/requestConstructor (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com>
  • Loading branch information
sweep-ai[bot] authored Apr 18, 2024
1 parent 639088b commit 65b9577
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sdks/ts/src/utils/requestConstructor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Utility functions for constructing and handling API requests.
import type { OpenAPIConfig } from "../api";

import type { ApiRequestOptions } from "../api/core/ApiRequestOptions";
Expand All @@ -7,6 +8,7 @@ import { isPlainObject, mapKeys, camelCase } from "lodash";
import { AxiosHttpRequest } from "../api/core/AxiosHttpRequest";
import { CancelablePromise } from "../api/core/CancelablePromise";

// Converts all keys in the given object to camelCase. Useful for normalizing API response objects.
const camelCaseify = (responseBody: any) =>
mapKeys(responseBody, (_value: any, key: string) => camelCase(key));

Expand All @@ -15,12 +17,14 @@ export class CustomHttpRequest extends AxiosHttpRequest {
super(config);
}

// Overrides the base request method to apply additional processing on the response, such as camelCasing keys and handling collections.
public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
const cancelableResponse = super.request(options);

return new CancelablePromise<T>((resolve, reject, onCancel) => {
onCancel(() => cancelableResponse.cancel());

// Processes the API response, converting keys to camelCase and specifically handling 'items' arrays if present.
const handleResponse = (responseBody: any) => {
// If the response body is not a plain object, return it as is
if (!isPlainObject(responseBody)) {
Expand All @@ -32,6 +36,7 @@ export class CustomHttpRequest extends AxiosHttpRequest {
updatedResponseBody = camelCaseify(updatedResponseBody);

if ("items" in updatedResponseBody) {
// Apply camelCase conversion to each item in the 'items' array, if it exists.
updatedResponseBody.items =
updatedResponseBody.items.map(camelCaseify);
}
Expand Down

0 comments on commit 65b9577

Please sign in to comment.