-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d8b698
commit 9f6e1a4
Showing
15 changed files
with
326 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CACHED_RESPONSE_STATUS = 304; | ||
export const CACHED_RESPONSE_STATUS_TEXT = '304 Cached by axios-cache-adapter'; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { createCache } from './axios/cache'; | ||
export * from './constants'; | ||
export * from './storage'; |
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 |
---|---|---|
@@ -1,7 +1,54 @@ | ||
import { AxiosCacheInstance } from '../axios/types'; | ||
import { AxiosCacheInstance } from '#/axios/types'; | ||
import { | ||
CACHED_RESPONSE_STATUS, | ||
CACHED_RESPONSE_STATUS_TEXT | ||
} from '#/constants'; | ||
import { Deferred } from '#/utils/deferred'; | ||
|
||
export function applyRequestInterceptor(axios: AxiosCacheInstance) { | ||
axios.interceptors.request.use(async (config) => { | ||
// Only cache specified methods | ||
if ( | ||
config.cache?.methods?.some( | ||
(method) => (config.method || 'get').toLowerCase() == method | ||
) | ||
) { | ||
return config; | ||
} | ||
|
||
const key = axios.generateKey(config); | ||
const cache = await axios.storage.get(key); | ||
|
||
// Not cached, continue the request, and mark it as fetching | ||
if (cache.state == 'empty') { | ||
await axios.storage.set(key, { | ||
state: 'loading', | ||
data: new Deferred(), | ||
// The cache header will be set after the response has been read, until that time, the expiration will be -1 | ||
expiration: config.cache?.interpretHeader | ||
? -1 | ||
: config.cache?.maxAge! || axios.defaults.cache?.maxAge! | ||
}); | ||
return config; | ||
} | ||
|
||
// Only check for expiration if the cache exists, because if it is loading, the expiration value may be -1. | ||
if (cache.state === 'cached' && cache.expiration < Date.now()) { | ||
await axios.storage.remove(key); | ||
return config; | ||
} | ||
|
||
const { body, headers } = await cache.data; | ||
|
||
config.adapter = () => | ||
Promise.resolve({ | ||
data: body, | ||
config, | ||
headers, | ||
status: CACHED_RESPONSE_STATUS, | ||
statusText: CACHED_RESPONSE_STATUS_TEXT | ||
}); | ||
|
||
return config; | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,77 @@ | ||
import { AxiosCacheInstance } from '../axios/types'; | ||
import { AxiosCacheInstance } from '#/axios/types'; | ||
import { parse } from '@tusbar/cache-control'; | ||
|
||
export function applyResponseInterceptor(axios: AxiosCacheInstance) { | ||
axios.interceptors.response.use(async (config) => { | ||
return config; | ||
axios.interceptors.response.use(async (response) => { | ||
// Update other entries before updating himself | ||
for (const [cacheKey, value] of Object.entries( | ||
response.config.cache?.update || {} | ||
)) { | ||
if (value == 'delete') { | ||
await axios.storage.remove(cacheKey); | ||
continue; | ||
} | ||
|
||
const oldValue = await axios.storage.get(cacheKey); | ||
const newValue = value(oldValue, response.data); | ||
if(newValue !== undefined) { | ||
await axios.storage.set(cacheKey, newValue); | ||
} else { | ||
await axios.storage.remove(cacheKey); | ||
} | ||
} | ||
|
||
// Config told that this response should be cached. | ||
if (!response.config.cache?.shouldCache!(response)) { | ||
return response; | ||
} | ||
|
||
const key = axios.generateKey(response.config); | ||
const cache = await axios.storage.get(key); | ||
|
||
if ( | ||
// Response already is in cache. | ||
cache.state === 'cached' || | ||
// Received response without being intercepted in the response | ||
cache.state === 'empty' | ||
) { | ||
return response; | ||
} | ||
|
||
if (response.config.cache?.interpretHeader) { | ||
const cacheControl = response.headers['cache-control'] || ''; | ||
const { noCache, noStore, maxAge } = parse(cacheControl); | ||
|
||
// Header told that this response should not be cached. | ||
if (noCache || noStore) { | ||
return response; | ||
} | ||
|
||
const expirationTime = maxAge | ||
? // Header max age in seconds | ||
Date.now() + maxAge * 1000 | ||
: response.config.cache?.maxAge || axios.defaults.cache?.maxAge!; | ||
|
||
cache.expiration = expirationTime; | ||
} else { | ||
// If the cache expiration has not been set, use the default expiration. | ||
cache.expiration = | ||
cache.expiration || | ||
response.config.cache?.maxAge || | ||
axios.defaults.cache?.maxAge!; | ||
} | ||
|
||
const data = { body: response.data, headers: response.headers }; | ||
|
||
// Resolve this deferred to update the cache after it | ||
cache.data.resolve(data); | ||
|
||
await axios.storage.set(key, { | ||
data, | ||
expiration: cache.expiration, | ||
state: 'cached' | ||
}); | ||
|
||
return response; | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './memory'; | ||
export * from './types'; | ||
export * from './web'; | ||
export * from './wrapper'; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.