From aa8ae3479592dd0d7c0a04174ed0ed512764441c Mon Sep 17 00:00:00 2001 From: Rafal Kosla Date: Wed, 3 Aug 2022 15:12:05 +0200 Subject: [PATCH 1/4] Fix error parsing --- src/Http.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Http.ts b/src/Http.ts index fc46e359..e29fe31e 100644 --- a/src/Http.ts +++ b/src/Http.ts @@ -69,13 +69,14 @@ export default class Http { } protected processError(error: Error): SpreeSDKError { - if (error instanceof FetchError) { - if (error.response) { + if ((error as FetchError)?.response) { + const fetchError = (error as FetchError) + if (fetchError.response) { // Error from Spree outside HTTP 2xx codes return this.processSpreeError(error) } - if (error.request) { + if (fetchError.request) { // No response received from Spree return new NoResponseError() } From 456da650b4cb1a17d5c15b7ccb4c202ab8724d48 Mon Sep 17 00:00:00 2001 From: Rafal Kosla Date: Wed, 3 Aug 2022 15:13:23 +0200 Subject: [PATCH 2/4] Enable axios fetcher to get custom function --- src/Client.ts | 2 +- src/fetchers/createAxiosFetcher.ts | 3 +++ src/interfaces/ClientConfig.ts | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Client.ts b/src/Client.ts index f98bde0b..1d4adc05 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -45,7 +45,7 @@ class Client { ...customOptions } - const fetcherOptions: CreateFetcherConfig = { host: options.host } + const fetcherOptions: CreateFetcherConfig = { host: options.host, beforeRequestFunction: options?.beforeRequestFunction } this.fetcher = options.createFetcher(fetcherOptions) diff --git a/src/fetchers/createAxiosFetcher.ts b/src/fetchers/createAxiosFetcher.ts index 69233092..09c85635 100644 --- a/src/fetchers/createAxiosFetcher.ts +++ b/src/fetchers/createAxiosFetcher.ts @@ -18,6 +18,9 @@ const createAxiosFetcher: CreateFetcher = (fetcherOptions) => { headers: { 'Content-Type': 'application/json' }, paramsSerializer: (params) => objectToQuerystring(params) }) + if (fetcherOptions.beforeRequestFunction){ + fetcherOptions?.beforeRequestFunction(axios) + } return { fetch: async (fetchOptions) => { diff --git a/src/interfaces/ClientConfig.ts b/src/interfaces/ClientConfig.ts index e807774f..ba74b696 100644 --- a/src/interfaces/ClientConfig.ts +++ b/src/interfaces/ClientConfig.ts @@ -1,4 +1,5 @@ import type { FetchConfig } from './FetchConfig' +import { AxiosInstance } from 'axios'; export type Fetcher = { fetch: (options: FetchConfig) => Promise<{ data: any }> @@ -8,10 +9,12 @@ export type CreateFetcher = (options: CreateFetcherConfig) => Fetcher export type CreateFetcherConfig = { host: string + beforeRequestFunction?(axios: AxiosInstance): any } export type FetcherConfig = { createFetcher: CreateFetcher } + export type IClientConfig = CreateFetcherConfig & FetcherConfig From c27b303ee86a10c991726341856afba18f09c749 Mon Sep 17 00:00:00 2001 From: Rafal Kosla Date: Thu, 4 Aug 2022 14:46:45 +0200 Subject: [PATCH 3/4] Add custom function to fetchFetcher --- src/fetchers/createFetchFetcher.ts | 4 ++++ src/interfaces/ClientConfig.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fetchers/createFetchFetcher.ts b/src/fetchers/createFetchFetcher.ts index b927f02d..acbcb03d 100644 --- a/src/fetchers/createFetchFetcher.ts +++ b/src/fetchers/createFetchFetcher.ts @@ -8,6 +8,10 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (fetcherOptio const { host, fetch, requestConstructor } = fetcherOptions + if (fetcherOptions.beforeRequestFunction){ + fetcherOptions?.beforeRequestFunction() + } + return { fetch: async (fetchOptions) => { try { diff --git a/src/interfaces/ClientConfig.ts b/src/interfaces/ClientConfig.ts index ba74b696..6d9b48ef 100644 --- a/src/interfaces/ClientConfig.ts +++ b/src/interfaces/ClientConfig.ts @@ -9,7 +9,7 @@ export type CreateFetcher = (options: CreateFetcherConfig) => Fetcher export type CreateFetcherConfig = { host: string - beforeRequestFunction?(axios: AxiosInstance): any + beforeRequestFunction?(axios?: AxiosInstance): any } export type FetcherConfig = { From 6f61da12dce83677ad82f5f798a6f9cacb5fdc00 Mon Sep 17 00:00:00 2001 From: Rafal Kosla Date: Thu, 4 Aug 2022 14:49:56 +0200 Subject: [PATCH 4/4] Fix extra semi --- src/interfaces/ClientConfig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/ClientConfig.ts b/src/interfaces/ClientConfig.ts index 6d9b48ef..902ae529 100644 --- a/src/interfaces/ClientConfig.ts +++ b/src/interfaces/ClientConfig.ts @@ -1,5 +1,5 @@ import type { FetchConfig } from './FetchConfig' -import { AxiosInstance } from 'axios'; +import { AxiosInstance } from 'axios' export type Fetcher = { fetch: (options: FetchConfig) => Promise<{ data: any }>