-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1246 from multiversx/development
2.38.8
- Loading branch information
Showing
7 changed files
with
321 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { axiosInstance } from 'apiCalls/utils/axiosInstance'; | ||
import { networkSelector } from 'reduxStore/selectors/networkConfigSelectors'; | ||
import { store } from 'reduxStore/store'; | ||
import { tokenDataStorage } from './tokenDataStorage'; | ||
|
||
export async function getPersistedToken<T>(url: string): Promise<T> { | ||
const { apiAddress, apiTimeout } = networkSelector(store.getState()); | ||
|
||
const config = { | ||
baseURL: apiAddress, | ||
timeout: Number(apiTimeout) | ||
}; | ||
|
||
const cachedToken: T | null = await tokenDataStorage.getItem(url); | ||
|
||
if (cachedToken) { | ||
return cachedToken; | ||
} | ||
|
||
const response = await axiosInstance.get<T>(url, config); | ||
|
||
await tokenDataStorage.setItem(url, response.data); | ||
|
||
return response.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
let memoryCache: Record<string, string> = {}; | ||
|
||
export let tokenDataStorage = { | ||
setItem: async <T>(key: string, tokenData: T) => { | ||
try { | ||
memoryCache[key] = JSON.stringify(tokenData); | ||
} catch (e) { | ||
console.error('tokenDataStorage unable to serialize', e); | ||
} | ||
}, | ||
getItem: async (key: string) => { | ||
try { | ||
return JSON.parse(memoryCache[key]); | ||
} catch (e) { | ||
console.error('tokenDataStorage unable to parse', e); | ||
} | ||
}, | ||
clear: async () => { | ||
memoryCache = {}; | ||
}, | ||
removeItem: async (key: string) => { | ||
delete memoryCache[key]; | ||
} | ||
}; | ||
|
||
export const setTokenDataStorage = ( | ||
tokenDataCacheStorage: typeof tokenDataStorage | ||
) => { | ||
tokenDataStorage = tokenDataCacheStorage; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.