@servicetitan/ajax-handlers
Advanced tools
Comparing version 27.2.1 to 27.3.0
import { SymbolToken } from '@servicetitan/react-ioc'; | ||
import { AxiosInstance } from 'axios'; | ||
import { ComponentType, FC, PropsWithChildren } from 'react'; | ||
@@ -12,2 +13,3 @@ import { AuthAdapter } from './auth-adapters'; | ||
authURL?: string; | ||
axiosInstance?: AxiosInstance; | ||
baseURL: string; | ||
@@ -14,0 +16,0 @@ tokens?: SymbolToken<string>[]; |
@@ -15,10 +15,4 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
export const RETRY_LIMIT = 1; | ||
function fetchRequestWithRetryMetadata(config) { | ||
var _a, _b; | ||
(_a = config.metadata) !== null && _a !== void 0 ? _a : (config.metadata = {}); | ||
config.metadata.retries = ((_b = config.metadata.retries) !== null && _b !== void 0 ? _b : 0) + 1; | ||
return axios(config); | ||
} | ||
export function withMicroservice(options) { | ||
const { authURL, baseURL, tokens = [], component: UnwrappedComponent, authAdapter = 'bearer', extraHeaders = undefined, preAuthenticate = false, } = options; | ||
const { authURL, axiosInstance = axios, baseURL, tokens = [], component: UnwrappedComponent, authAdapter = 'bearer', extraHeaders = undefined, preAuthenticate = false, } = options; | ||
let MicroserviceAuthStore = class MicroserviceAuthStore extends Store { | ||
@@ -127,3 +121,3 @@ constructor() { | ||
addRequestInterceptor() { | ||
this.axiosRequestInterceptor = axios.interceptors.request.use(async (config) => { | ||
this.axiosRequestInterceptor = axiosInstance.interceptors.request.use(async (config) => { | ||
var _a, _b; | ||
@@ -156,3 +150,3 @@ if (!this.shouldInterceptRequest(config)) { | ||
addResponseInterceptor() { | ||
this.axiosResponseInterceptor = axios.interceptors.response.use(response => response, (axiosError) => { | ||
this.axiosResponseInterceptor = axiosInstance.interceptors.response.use(response => response, (axiosError) => { | ||
var _a, _b, _c, _d; | ||
@@ -167,3 +161,3 @@ if (!this.shouldInterceptResponseError(axiosError)) { | ||
this.requestQueue.push({ | ||
retry: () => resolve(fetchRequestWithRetryMetadata(config)), | ||
retry: () => resolve(this.fetchRequestWithRetryMetadata(config)), | ||
reject: () => reject(axiosError), | ||
@@ -175,3 +169,3 @@ cancel: () => reject(new CanceledError()), | ||
if ((_d = (_c = this.authAdapter).didAuthenticationHappenWhileRequestInFlight) === null || _d === void 0 ? void 0 : _d.call(_c, config)) { | ||
return fetchRequestWithRetryMetadata(config); | ||
return this.fetchRequestWithRetryMetadata(config); | ||
} | ||
@@ -203,3 +197,3 @@ return this.authenticate(config) | ||
this.requestQueue = []; | ||
return fetchRequestWithRetryMetadata(config); | ||
return this.fetchRequestWithRetryMetadata(config); | ||
}); | ||
@@ -210,3 +204,3 @@ }); | ||
if (this.axiosRequestInterceptor !== undefined) { | ||
axios.interceptors.request.eject(this.axiosRequestInterceptor); | ||
axiosInstance.interceptors.request.eject(this.axiosRequestInterceptor); | ||
} | ||
@@ -216,3 +210,3 @@ } | ||
if (this.axiosResponseInterceptor !== undefined) { | ||
axios.interceptors.response.eject(this.axiosResponseInterceptor); | ||
axiosInstance.interceptors.response.eject(this.axiosResponseInterceptor); | ||
} | ||
@@ -238,2 +232,8 @@ } | ||
} | ||
fetchRequestWithRetryMetadata(config) { | ||
var _a, _b; | ||
(_a = config.metadata) !== null && _a !== void 0 ? _a : (config.metadata = {}); | ||
config.metadata.retries = ((_b = config.metadata.retries) !== null && _b !== void 0 ? _b : 0) + 1; | ||
return axiosInstance(config); | ||
} | ||
claim(config) { | ||
@@ -240,0 +240,0 @@ var _a; |
{ | ||
"name": "@servicetitan/ajax-handlers", | ||
"version": "27.2.1", | ||
"version": "27.3.0", | ||
"description": "", | ||
@@ -21,3 +21,3 @@ "repository": { | ||
"devDependencies": { | ||
"@servicetitan/react-ioc": "27.2.1", | ||
"@servicetitan/react-ioc": "27.3.0", | ||
"@testing-library/dom": "^10.4.0", | ||
@@ -28,7 +28,8 @@ "@testing-library/jest-dom": "^6.5.0", | ||
"axios": "~0.28.1", | ||
"msw": "^2.3.5", | ||
"react": "~18.3.1" | ||
"msw": "^2.4.9", | ||
"react": "~18.3.1", | ||
"xhr-mock": "~2.5.1" | ||
}, | ||
"peerDependencies": { | ||
"@servicetitan/react-ioc": "27.2.1", | ||
"@servicetitan/react-ioc": "27.3.0", | ||
"axios": "~0.28.1", | ||
@@ -43,3 +44,3 @@ "react": ">=17.0.2" | ||
}, | ||
"gitHead": "0d9d923ebf34a808f42ad215f4538ef5920f5689" | ||
"gitHead": "692cebef0a151c37135dabd851dbe50de7b99ea6" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
171705
3302
9