UNPKG

@rtbjs/use-state

Version:

`@rtbjs/use-state` is a state management tool that can act as a local state and be easily turned into a global redux state. It is an innovative approach to state management that combines the advantages of both React's useState and Redux's state management

92 lines 4.27 kB
"use strict"; exports.__esModule = true; var tslib_1 = require("tslib"); var query_1 = require("@reduxjs/toolkit/query"); var async_mutex_1 = require("async-mutex"); var company_slice_1 = require("../features/company-slice"); var user_slice_1 = require("../features/user-slice"); // import { companyApi } from './companyApi'; // const baseUrl = `http://localhost:8000/api/`; var baseUrl = "https://toolboxjs.com/api/"; // Create a new mutex var mutex = new async_mutex_1.Mutex(); var baseQuery = (0, query_1.fetchBaseQuery)({ baseUrl: baseUrl, prepareHeaders: function (headers, _a) { var _b, _c; var getState = _a.getState; var _d = getState().companyState, companyId = _d.companyId, apiKey = _d.apiKey; headers.set('companyId', companyId); headers.set('apiKey', apiKey); headers.set('accesstoken', (_b = localStorage.getItem('toolboxjs_access_token')) !== null && _b !== void 0 ? _b : ''); headers.set('refreshtoken', (_c = localStorage.getItem('toolboxjs_refresh_token')) !== null && _c !== void 0 ? _c : ''); headers.set('apiKey', apiKey); headers.set('feVersion', '1.0.0'); return headers; } }); var customFetchBase = function (args, api, extraOptions) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var result, release, refreshResult; var _a, _b, _c, _d; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: // wait until the mutex is available without locking it return [4 /*yield*/, mutex.waitForUnlock()]; case 1: // wait until the mutex is available without locking it _e.sent(); return [4 /*yield*/, baseQuery(args, api, extraOptions)]; case 2: result = _e.sent(); if (!(((_b = (_a = result.error) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) === 'You are not logged in')) return [3 /*break*/, 14]; if (!!mutex.isLocked()) return [3 /*break*/, 11]; return [4 /*yield*/, mutex.acquire()]; case 3: release = _e.sent(); _e.label = 4; case 4: _e.trys.push([4, , 9, 10]); return [4 /*yield*/, baseQuery({ credentials: 'same-origin', url: 'auth/refresh' }, api, extraOptions)]; case 5: refreshResult = _e.sent(); if (!refreshResult.data) return [3 /*break*/, 7]; return [4 /*yield*/, baseQuery(args, api, extraOptions)]; case 6: // Retry the initial query result = _e.sent(); return [3 /*break*/, 8]; case 7: api.dispatch((0, user_slice_1.logout)()); _e.label = 8; case 8: return [3 /*break*/, 10]; case 9: // release must be called once the mutex should be released again. release(); return [7 /*endfinally*/]; case 10: return [3 /*break*/, 14]; case 11: // wait until the mutex is available without locking it return [4 /*yield*/, mutex.waitForUnlock()]; case 12: // wait until the mutex is available without locking it _e.sent(); return [4 /*yield*/, baseQuery(args, api, extraOptions)]; case 13: result = _e.sent(); _e.label = 14; case 14: if (((_d = (_c = result.error) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message) === 'Company credentials are not correct') { api.dispatch((0, company_slice_1.setCompany)({ company: null })); // const { refetch } = api.dispatch( // companyApi.endpoints.checkKey.initiate(null) // ); // refetch(); } return [2 /*return*/, result]; } }); }); }; exports["default"] = customFetchBase; //# sourceMappingURL=custom-fetch-base.js.map