-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚒ refactor : axiosinstance 코드 리팩토링 진행
- Loading branch information
Showing
6 changed files
with
68 additions
and
36 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
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,36 @@ | ||
import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'; | ||
|
||
export const getRequestHeaders = async (config: AxiosRequestConfig) => { | ||
const queryStr = new URLSearchParams(config.params).toString(); | ||
const cacheStorage = await caches.open(config.url ? config.url : ''); | ||
|
||
const cachedResponse = await cacheStorage.match(queryStr); | ||
const cached = await cachedResponse?.json(); | ||
|
||
return { | ||
'If-None-Match': cached ? `${cached.etag}` : '', | ||
}; | ||
}; | ||
|
||
export const setCacheStorage = async (response: AxiosResponse) => { | ||
const customData = { | ||
etag: response.headers.etag, | ||
data: response.data, | ||
}; | ||
|
||
const queryStr = new URLSearchParams(response.config.params).toString(); | ||
const cacheStorage = await caches.open(response.config.url ? response.config.url : ''); | ||
cacheStorage.put(queryStr, new Response(JSON.stringify(customData))); | ||
}; | ||
|
||
export const getCacheStorage = async (error: AxiosError) => { | ||
if (error.config) { | ||
const queryStr = new URLSearchParams(error.config.params).toString(); | ||
const cacheStorage = await caches.open(error.config.url ? error.config.url : ''); | ||
const cachedResponse = await cacheStorage.match(queryStr); | ||
const cached = await cachedResponse?.json(); | ||
return cached; | ||
} | ||
|
||
return { data: [] }; | ||
}; |
File renamed without changes.