jclib-ui 1.0.288 → 1.0.291

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4232,7 +4232,9 @@ const initConfig = {
4232
4232
  version: "",
4233
4233
  ident: "",
4234
4234
  loginRoute: "/",
4235
+ refreshRoute: "",
4235
4236
  isProductionMode: true,
4237
+ withCredentials: false,
4236
4238
  getToken: () => "",
4237
4239
  setToken: (token) => console.error(MESSAGE_ERROR, token),
4238
4240
  afterRequest: (_res) => {
@@ -5706,6 +5708,33 @@ function MessageNoConection() {
5706
5708
  ] })
5707
5709
  ] });
5708
5710
  }
5711
+ let renovando = null;
5712
+ function ehRotaDeRefresh(config, apiConfig) {
5713
+ const url = String((config == null ? void 0 : config.url) || "");
5714
+ return url === apiConfig.refreshRoute || url.endsWith(apiConfig.refreshRoute);
5715
+ }
5716
+ async function renovarSessao(error, apiConfig, config) {
5717
+ var _a;
5718
+ if (!(apiConfig == null ? void 0 : apiConfig.refreshRoute)) return false;
5719
+ if (((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status) !== 401) return false;
5720
+ if (config == null ? void 0 : config._retry) return false;
5721
+ if (ehRotaDeRefresh(config, apiConfig)) return false;
5722
+ if (!renovando) {
5723
+ renovando = axios.post(apiConfig.refreshRoute, null, {
5724
+ baseURL: apiConfig.baseURL,
5725
+ timeout: apiConfig.timeout,
5726
+ withCredentials: true
5727
+ }).finally(() => {
5728
+ renovando = null;
5729
+ });
5730
+ }
5731
+ try {
5732
+ await renovando;
5733
+ return true;
5734
+ } catch {
5735
+ return false;
5736
+ }
5737
+ }
5709
5738
  const initialRequestInfo = {
5710
5739
  isError: false,
5711
5740
  //deprecated
@@ -5757,6 +5786,7 @@ function useApi(initialConfig = {}) {
5757
5786
  baseURL: globalApiConfig.baseURL,
5758
5787
  timeout: globalApiConfig.timeout,
5759
5788
  auth,
5789
+ withCredentials: globalApiConfig.withCredentials,
5760
5790
  validateStatus: (status) => status >= 200 && status < 400,
5761
5791
  ...initialConfig,
5762
5792
  ...localConfig,
@@ -5770,7 +5800,16 @@ function useApi(initialConfig = {}) {
5770
5800
  finalConfig.onBefore(finalConfig);
5771
5801
  }
5772
5802
  try {
5773
- const res = await axios({ ...finalConfig, signal: controller.signal });
5803
+ let res;
5804
+ try {
5805
+ res = await axios({ ...finalConfig, signal: controller.signal });
5806
+ } catch (err) {
5807
+ if (!await renovarSessao(err, globalApiConfig, finalConfig)) {
5808
+ throw err;
5809
+ }
5810
+ finalConfig._retry = true;
5811
+ res = await axios({ ...finalConfig, signal: controller.signal });
5812
+ }
5774
5813
  if (globalApiConfig.afterRequest) {
5775
5814
  globalApiConfig.afterRequest(res);
5776
5815
  }