Skip to content

Commit

Permalink
refactor client to use header parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Oct 17, 2024
1 parent 3c6b537 commit b6b7c6a
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpService, IHttpService, IRetryStrategyOptions, retryHelper } from "@kontent-ai/core-sdk";
import { HttpService, IRetryStrategyOptions, retryHelper } from "@kontent-ai/core-sdk";
import { createDeliveryClient, DeliveryClient } from "@kontent-ai/delivery-sdk";
import { ManagementClient } from "@kontent-ai/management-sdk";
import { CancelToken, isAxiosError } from "axios";
import { isAxiosError } from "axios";

import packageJson from "../../package.json" with { type: "json" };

Expand All @@ -20,12 +20,21 @@ type DeliveryParams = Readonly<{

const sourceTrackingHeaderName = "X-KC-SOURCE";

const retryStrategy: IRetryStrategyOptions = {
...retryHelper.defaultRetryStrategy,
canRetryError: (error: any) =>
retryHelper.defaultRetryStrategy.canRetryError(error)
|| (isAxiosError(error) && !!error.response?.status.toString().startsWith("5")),
};

export const createClient = ({ environmentId, apiKey, commandName }: Params): ManagementClient =>
// eslint-disable-next-line no-restricted-syntax
new ManagementClient({
environmentId,
apiKey,
httpService: createHttpService(commandName),
retryStrategy,
headers: [{ header: sourceTrackingHeaderName, value: `${packageJson.name};${packageJson.version};${commandName}` }],
httpService: defaultHttpService,
});

export const createClientDelivery = (
Expand All @@ -34,31 +43,16 @@ export const createClientDelivery = (
createDeliveryClient({
environmentId: environmentId,
previewApiKey: previewApiKey,
defaultQueryConfig: { usePreviewMode: usePreviewMode ?? false },
httpService: createHttpService(commandName),
});

const createHttpService = (commandName: string): IHttpService<CancelToken> => {
const originalHttpService = new HttpService({
logErrorsToConsole: false,
axiosRequestConfig: {
headers: { [sourceTrackingHeaderName]: `${packageJson.name};${packageJson.version};${commandName}` },
defaultQueryConfig: {
usePreviewMode: usePreviewMode ?? false,
customHeaders: [{
header: sourceTrackingHeaderName,
value: `${packageJson.name};${packageJson.version};${commandName}`,
}],
},
httpService: defaultHttpService,
});

const retryStrategy: IRetryStrategyOptions = {
...retryHelper.defaultRetryStrategy,
canRetryError: (error: any) =>
retryHelper.defaultRetryStrategy.canRetryError(error)
|| (isAxiosError(error) && !!error.response?.status.toString().startsWith("5")),
};

return {
getAsync: (call, config) => originalHttpService.getAsync(call, { ...config, retryStrategy }),
postAsync: (call, config) => originalHttpService.postAsync(call, { ...config, retryStrategy }),
putAsync: (call, config) => originalHttpService.putAsync(call, { ...config, retryStrategy }),
patchAsync: (call, config) => originalHttpService.patchAsync(call, { ...config, retryStrategy }),
deleteAsync: (call, config) => originalHttpService.deleteAsync(call, { ...config, retryStrategy }),
createCancelToken: originalHttpService.createCancelToken,
};
};
const defaultHttpService = new HttpService({
logErrorsToConsole: false,
});

0 comments on commit b6b7c6a

Please sign in to comment.