-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extracted static functions to a single file
- Loading branch information
1 parent
4c1e0ec
commit c57916f
Showing
5 changed files
with
127 additions
and
123 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,92 @@ | ||
import type { Method } from 'axios'; | ||
import type { CachedResponse, CacheProperties, StaleStorageValue } from '..'; | ||
import type { CacheAxiosResponse, CacheRequestConfig } from '../cache/axios'; | ||
import { Header } from '../util/headers'; | ||
|
||
/** | ||
* Creates a new validateStatus function that will use the one already used and also | ||
* accept status code 304. | ||
*/ | ||
export function createValidateStatus( | ||
oldValidate?: CacheRequestConfig['validateStatus'] | ||
): (status: number) => boolean { | ||
return oldValidate | ||
? (status) => oldValidate(status) || status === 304 | ||
: (status) => (status >= 200 && status < 300) || status === 304; | ||
} | ||
|
||
/** Checks if the given method is in the methods array */ | ||
export function isMethodIn(requestMethod: Method, methodList: Method[] = []): boolean { | ||
requestMethod = requestMethod.toLowerCase() as Lowercase<Method>; | ||
|
||
for (const method of methodList) { | ||
if (method.toLowerCase() === requestMethod) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export type ConfigWithCache<D> = CacheRequestConfig<D> & { | ||
cache: Partial<CacheProperties>; | ||
}; | ||
|
||
export function setRevalidationHeaders<D>( | ||
cache: StaleStorageValue, | ||
config: ConfigWithCache<D> | ||
): void { | ||
config.headers ||= {}; | ||
|
||
const { etag, modifiedSince } = config.cache; | ||
|
||
if (etag) { | ||
const etagValue = etag === true ? cache.data?.headers[Header.ETag] : etag; | ||
if (etagValue) { | ||
config.headers[Header.IfNoneMatch] = etagValue; | ||
} | ||
} | ||
|
||
if (modifiedSince) { | ||
config.headers[Header.IfModifiedSince] = | ||
modifiedSince === true | ||
? // If last-modified is not present, use the createdAt timestamp | ||
cache.data.headers[Header.LastModified] || | ||
new Date(cache.createdAt).toUTCString() | ||
: modifiedSince.toUTCString(); | ||
} | ||
} | ||
|
||
/** | ||
* Creates the new date to the cache by the provided response. Also handles possible 304 | ||
* Not Modified by updating response properties. | ||
*/ | ||
export function setupCacheData<R, D>( | ||
response: CacheAxiosResponse<R, D>, | ||
cache?: CachedResponse | ||
): CachedResponse { | ||
if (response.status === 304 && cache) { | ||
// Set the cache information into the response object | ||
response.cached = true; | ||
response.data = cache.data; | ||
response.status = cache.status; | ||
response.statusText = cache.statusText; | ||
|
||
// Update possible new headers | ||
response.headers = { | ||
...cache.headers, | ||
...response.headers | ||
}; | ||
|
||
// return the old cache | ||
return cache; | ||
} | ||
|
||
// New Response | ||
return { | ||
data: response.data, | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers | ||
}; | ||
} |
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,22 @@ | ||
import { createValidateStatus } from '../../src/interceptors/util'; | ||
|
||
describe('test util functions', () => { | ||
it('tests validate-status function', async () => { | ||
const def = createValidateStatus(); | ||
expect(def(200)).toBe(true); | ||
expect(def(345)).toBe(false); | ||
expect(def(304)).toBe(true); | ||
|
||
const only200 = createValidateStatus((s) => s >= 200 && s < 300); | ||
expect(only200(200)).toBe(true); | ||
expect(only200(299)).toBe(true); | ||
expect(only200(304)).toBe(true); | ||
expect(only200(345)).toBe(false); | ||
|
||
const randomValue = createValidateStatus((s) => s >= 405 && s <= 410); | ||
expect(randomValue(200)).toBe(false); | ||
expect(randomValue(404)).toBe(false); | ||
expect(randomValue(405)).toBe(true); | ||
expect(randomValue(304)).toBe(true); | ||
}); | ||
}); |