Skip to content

Commit

Permalink
feat(client): add async support to beforeRequest hook
Browse files Browse the repository at this point in the history
  • Loading branch information
deini committed Oct 23, 2024
1 parent ef64ed5 commit 5e46b4a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-carrots-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-client": minor
---

Adds async support to beforeRequest hook
9 changes: 6 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ interface Config<FetcherRequestInit extends RequestInit = RequestInit> {
backendUserAgentExtensions?: string;
logger?: boolean;
getChannelId?: (defaultChannelId: string) => Promise<string> | string;
beforeRequest?: (fetchOptions?: FetcherRequestInit) => Partial<FetcherRequestInit> | undefined;
beforeRequest?: (
fetchOptions?: FetcherRequestInit,
) => Promise<Partial<FetcherRequestInit> | undefined> | Partial<FetcherRequestInit> | undefined;
}

interface BigCommerceResponse<T> {
Expand All @@ -32,7 +34,8 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
private getChannelId: (defaultChannelId: string) => Promise<string> | string;
private beforeRequest?: (
fetchOptions?: FetcherRequestInit,
) => Partial<FetcherRequestInit> | undefined;
) => Promise<Partial<FetcherRequestInit> | undefined> | Partial<FetcherRequestInit> | undefined;

private trustedProxySecret = process.env.BIGCOMMERCE_TRUSTED_PROXY_SECRET;

constructor(private config: Config<FetcherRequestInit>) {
Expand Down Expand Up @@ -85,7 +88,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {

const graphqlUrl = await this.getGraphQLEndpoint(channelId);
const { headers: additionalFetchHeaders = {}, ...additionalFetchOptions } =
this.beforeRequest?.(fetchOptions) ?? {};
(await this.beforeRequest?.(fetchOptions)) ?? {};

const response = await fetch(graphqlUrl, {
method: 'POST',
Expand Down

0 comments on commit 5e46b4a

Please sign in to comment.