Skip to content

Commit

Permalink
remove custom http service, use headers param
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Oct 8, 2024
1 parent b4367de commit f7c10f2
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 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 { 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,22 @@ 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}` },
],
});

export const createClientDelivery = (
Expand All @@ -34,31 +44,11 @@ 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}` },
retryStrategy,
defaultQueryConfig: {
usePreviewMode: usePreviewMode ?? false,
customHeaders: [
{ header: sourceTrackingHeaderName, value: `${packageJson.name};${packageJson.version};${commandName}` },
],
},
});

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,
};
};

0 comments on commit f7c10f2

Please sign in to comment.