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

90 lines 4.15 kB
import { __awaiter, __generator } from "tslib"; import { fetchBaseQuery, } from '@reduxjs/toolkit/query'; import { Mutex } from 'async-mutex'; import { setCompany } from '../features/company-slice'; import { logout } from '../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 Mutex(); var baseQuery = 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 __awaiter(void 0, void 0, void 0, function () { var result, release, refreshResult; var _a, _b, _c, _d; return __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(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(setCompany({ company: null })); // const { refetch } = api.dispatch( // companyApi.endpoints.checkKey.initiate(null) // ); // refetch(); } return [2 /*return*/, result]; } }); }); }; export default customFetchBase; //# sourceMappingURL=custom-fetch-base.js.map