Skip to content

Commit

Permalink
Feat: Add support for custom APIs @W-15585881@ (#110)
Browse files Browse the repository at this point in the history
* update types and export underlying function

* lint

* remove custom api type

* add flag for disabling default behavior of transforming request body

* fix disable transform body toggle
  • Loading branch information
joeluong-sfcc committed May 8, 2024
1 parent c65e814 commit ac713a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/base/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ config();
*
* @class ClientConfig
*/
export class ClientConfig {
export class ClientConfig<Params extends CommonParameters = CommonParameters> {
public baseUri?: string;
public cacheManager?: ICacheManager;
public headers?: BasicHeaders;
public parameters?: CommonParameters;
// Params defaults to CommonParameters
public parameters?: Params;
public retrySettings?: OperationOptions;
public fetchOptions?: RequestInit;
}
Expand Down
7 changes: 5 additions & 2 deletions src/base/staticClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type SdkFetchOptions = {
rawResponse?: boolean;
retrySettings?: OperationOptions;
fetchOptions?: RequestInit;
disableTransformBody?: boolean;
body?: unknown;
};

Expand Down Expand Up @@ -175,7 +176,7 @@ export const transformRequestBody = (
* @returns Either the Response object or the DTO inside it wrapped in a promise,
* depending upon options.rawResponse
*/
async function runFetch(
export async function runFetch(
method: "delete" | "get" | "patch" | "post" | "put",
options: SdkFetchOptions
): Promise<object> {
Expand Down Expand Up @@ -218,7 +219,9 @@ async function runFetch(
};

if (typeof options.body !== "undefined") {
fetchOptions.body = transformRequestBody(options.body, fetchOptions);
fetchOptions.body = options.disableTransformBody
? (options.body as BodyInit)
: transformRequestBody(options.body, fetchOptions);
}

logFetch(resource, fetchOptions);
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export {
ResponseError,
} from "./base/client";

import { _get, _delete, _patch, _post, _put } from "./base/staticClient";
import {
_get,
_delete,
_patch,
_post,
_put,
runFetch,
} from "./base/staticClient";

export {
getObjectFromResponse,
Expand All @@ -26,6 +33,7 @@ export const StaticClient = {
patch: _patch,
post: _post,
put: _put,
runFetch,
};

export { IAuthToken, ShopperToken, stripBearer } from "./base/authHelper";
Expand Down

0 comments on commit ac713a0

Please sign in to comment.