Skip to content

Commit

Permalink
deps: updated axios to v0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 18, 2022
1 parent 93dda05 commit 66f29c9
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_modules
/umd
/cjs
/esm
/dist

# Random
/ignore
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bench": "node js/benchmark.js > pages/_comparison-benchmark.log"
},
"devDependencies": {
"axios": "^0.24.0",
"axios": "^0.25.0",
"axios-cache-adapter": "^2.7.3",
"docsify-cli": "^4.4.3",
"express": "^4.17.2"
Expand Down
14 changes: 7 additions & 7 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ __metadata:
languageName: node
linkType: hard

"axios@npm:^0.24.0":
version: 0.24.0
resolution: "axios@npm:0.24.0"
"axios@npm:^0.25.0":
version: 0.25.0
resolution: "axios@npm:0.25.0"
dependencies:
follow-redirects: ^1.14.4
checksum: 468cf496c08a6aadfb7e699bebdac02851e3043d4e7d282350804ea8900e30d368daa6e3cd4ab83b8ddb5a3b1e17a5a21ada13fc9cebd27b74828f47a4236316
follow-redirects: ^1.14.7
checksum: 2a8a3787c05f2a0c9c3878f49782357e2a9f38945b93018fb0c4fd788171c43dceefbb577988628e09fea53952744d1ecebde234b561f1e703aa43e0a598a3ad
languageName: node
linkType: hard

Expand Down Expand Up @@ -950,7 +950,7 @@ __metadata:
languageName: node
linkType: hard

"follow-redirects@npm:^1.14.4":
"follow-redirects@npm:^1.14.7":
version: 1.14.7
resolution: "follow-redirects@npm:1.14.7"
peerDependenciesMeta:
Expand Down Expand Up @@ -2204,7 +2204,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
axios: ^0.24.0
axios: ^0.25.0
axios-cache-adapter: ^2.7.3
docsify-cli: ^4.4.3
express: ^4.17.2
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"runkitExampleFilename": "./examples/runkit.js",
"scripts": {
"build": "sh build/build.sh",
"test": "jest --coverage",
"test": "tsc --noEmit && jest --coverage",
"check": "sh build/check.sh",
"format": "prettier --write .",
"lint": "eslint . --ext .ts",
Expand Down Expand Up @@ -71,7 +71,7 @@
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"auto-changelog": "^2.3.0",
"axios": "~0.24.0",
"axios": "~0.25.0",
"es-check": "^6.1.1",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -88,6 +88,6 @@
"webpack-cli": "^4.9.1"
},
"peerDependencies": {
"axios": "~0.24.0"
"axios": "~0.25.0"
}
}
6 changes: 4 additions & 2 deletions src/header/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
const cacheControl = headers[Header.CacheControl];

if (cacheControl) {
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(cacheControl);
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(
String(cacheControl)
);

// Header told that this response should not be cached.
if (noCache || noStore) {
Expand Down Expand Up @@ -40,7 +42,7 @@ export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
const expires = headers[Header.Expires];

if (expires) {
const milliseconds = Date.parse(expires) - Date.now();
const milliseconds = Date.parse(String(expires)) - Date.now();
return milliseconds >= 0 ? milliseconds : 'dont cache';
}

Expand Down
6 changes: 4 additions & 2 deletions src/header/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AxiosRequestHeaders } from 'axios';

export type InterpreterResult = 'dont cache' | 'not enough headers' | number;

/**
Expand All @@ -8,7 +10,7 @@ export type InterpreterResult = 'dont cache' | 'not enough headers' | number;
* enough to determine a valid value. Or a `number` containing the number of
* **milliseconds** to cache the response.
*/
export type HeadersInterpreter = (headers?: Record<string, string>) => InterpreterResult;
export type HeadersInterpreter = (headers?: AxiosRequestHeaders) => InterpreterResult;

/**
* Interpret a single string header
Expand All @@ -20,5 +22,5 @@ export type HeadersInterpreter = (headers?: Record<string, string>) => Interpret
*/
export type HeaderInterpreter = (
header: string,
headers: Record<string, string>
headers: AxiosRequestHeaders
) => InterpreterResult;
20 changes: 10 additions & 10 deletions test/interceptors/etag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ describe('ETag handling', () => {
const config = { cache: { interpretHeader: true, etag: true } };

// initial request
await axios.get('', config);
await axios.get('http://test.com', config);

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
expect(response.cached).toBe(true);
expect(response.data).toBe(true);

// Sleep entire max age time.
await sleep(1000);

const response2 = await axios.get('', config);
const response2 = await axios.get('http://test.com', config);
// from revalidation
expect(response2.cached).toBe(true);
// ensure value from stale cache is kept
Expand All @@ -31,16 +31,16 @@ describe('ETag handling', () => {
);

// initial request
await axios.get('');
await axios.get('http://test.com');

const response = await axios.get('');
const response = await axios.get('http://test.com');
expect(response.cached).toBe(true);
expect(response.data).toBe(true);

// Sleep entire max age time.
await sleep(1000);

const response2 = await axios.get('');
const response2 = await axios.get('http://test.com');
// from revalidation
expect(response2.cached).toBe(true);
// ensure value from stale cache is kept
Expand All @@ -51,12 +51,12 @@ describe('ETag handling', () => {
const axios = mockAxios({}, { etag: 'fakeEtag', 'cache-control': 'must-revalidate' });
const config = { cache: { interpretHeader: true, etag: true } };

await axios.get('', config);
await axios.get('http://test.com', config);

// 0ms cache
await sleep(1);

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
// from etag revalidation
expect(response.cached).toBe(true);
expect(response.data).toBe(true);
Expand All @@ -66,13 +66,13 @@ describe('ETag handling', () => {
const axios = mockAxios({ ttl: 0 }, { etag: 'fake-etag-2' });
const config = { cache: { interpretHeader: true, etag: 'fake-etag' } };

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
expect(response.cached).toBe(false);
expect(response.data).toBe(true);
expect(response.config.headers?.[Header.IfModifiedSince]).toBeUndefined();
expect(response.headers?.[Header.LastModified]).toBeUndefined();

const response2 = await axios.get('', config);
const response2 = await axios.get('http://test.com', config);
expect(response2.cached).toBe(true);
expect(response2.data).toBe(true);
expect(response2.config.headers?.[Header.IfNoneMatch]).toBe('fake-etag');
Expand Down
28 changes: 15 additions & 13 deletions test/interceptors/last-modified.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ describe('Last-Modified handling', () => {

const config = { cache: { interpretHeader: true, modifiedSince: true } };

await axios.get('', config);
await axios.get('http://test.com', config);

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
expect(response.cached).toBe(true);
expect(response.data).toBe(true);

// Sleep entire max age time.
await sleep(1000);

const response2 = await axios.get('', config);
const response2 = await axios.get('http://test.com', config);
// from revalidation
expect(response2.cached).toBe(true);
expect(response2.status).toBe(200);
Expand All @@ -38,16 +38,16 @@ describe('Last-Modified handling', () => {
}
);

await axios.get('');
await axios.get('http://test.com');

const response = await axios.get('');
const response = await axios.get('http://test.com');
expect(response.cached).toBe(true);
expect(response.data).toBe(true);

// Sleep entire max age time.
await sleep(1000);

const response2 = await axios.get('');
const response2 = await axios.get('http://test.com');
// from revalidation
expect(response2.cached).toBe(true);
expect(response2.status).toBe(200);
Expand All @@ -60,13 +60,13 @@ describe('Last-Modified handling', () => {
cache: { modifiedSince: new Date(2014, 1, 1) }
};

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
expect(response.cached).toBe(false);
expect(response.data).toBe(true);
expect(response.config.headers?.[Header.IfModifiedSince]).toBeUndefined();
expect(response.headers?.[Header.XAxiosCacheLastModified]).toBeDefined();

const response2 = await axios.get('', config);
const response2 = await axios.get('http://test.com', config);
expect(response2.cached).toBe(true);
expect(response2.data).toBe(true);
expect(response2.config.headers?.[Header.IfModifiedSince]).toBeDefined();
Expand All @@ -85,10 +85,10 @@ describe('Last-Modified handling', () => {
cache: { interpretHeader: true, modifiedSince: true }
};

await axios.get('', config);
const response = await axios.get('', config);
await axios.get('http://test.com', config);
const response = await axios.get('http://test.com', config);

const modifiedSince = response.config.headers?.[Header.IfModifiedSince];
const modifiedSince = response.config.headers?.[Header.IfModifiedSince] as string;

if (!modifiedSince) {
throw new Error('modifiedSince is not defined');
Expand All @@ -103,14 +103,16 @@ describe('Last-Modified handling', () => {
const axios = mockAxios();

// First request, return x-my-header. Ttl 1 to make the cache stale
const firstResponse = await axios.get('', { cache: { ttl: -1 } });
const firstResponse = await axios.get('http://test.com', { cache: { ttl: -1 } });
const firstMyHeader = firstResponse.headers?.[XMockRandom];

expect(firstMyHeader).toBeDefined();
expect(Number(firstMyHeader)).not.toBeNaN();

// Second request with 304 Not Modified
const secondResponse = await axios.get('', { cache: { modifiedSince: true } });
const secondResponse = await axios.get('http://test.com', {
cache: { modifiedSince: true }
});
const secondMyHeader = secondResponse.headers?.[XMockRandom];

expect(secondMyHeader).toBeDefined();
Expand Down
37 changes: 20 additions & 17 deletions test/interceptors/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('test request interceptor', () => {
methods: ['post']
});

const response = await axios.get('');
const response = await axios.get('http://test.com');
const cacheKey = axios.generateKey(response.config);
const cache = await axios.storage.get(cacheKey);

Expand All @@ -21,7 +21,7 @@ describe('test request interceptor', () => {
methods: ['get']
});

const response = await axios.get('');
const response = await axios.get('http://test.com');
const cacheKey = axios.generateKey(response.config);
const cache = await axios.storage.get(cacheKey);

Expand All @@ -31,7 +31,10 @@ describe('test request interceptor', () => {
it('tests concurrent requests', async () => {
const axios = mockAxios();

const [resp1, resp2] = await Promise.all([axios.get(''), axios.get('')]);
const [resp1, resp2] = await Promise.all([
axios.get('http://test.com'),
axios.get('http://test.com')
]);

expect(resp1.cached).toBe(false);
expect(resp2.cached).toBe(true);
Expand All @@ -41,9 +44,9 @@ describe('test request interceptor', () => {
const axios = mockAxios();

const results = await Promise.all([
axios.get('', { cache: false }),
axios.get(''),
axios.get('', { cache: false })
axios.get('http://test.com', { cache: false }),
axios.get('http://test.com'),
axios.get('http://test.com', { cache: false })
]);
for (const result of results) {
expect(result.cached).toBe(false);
Expand All @@ -60,11 +63,11 @@ describe('test request interceptor', () => {
const axios = mockAxios();

const [, resp2] = await Promise.all([
axios.get('', {
axios.get('http://test.com', {
// Simple predicate to ignore cache in the response step.
cache: { cachePredicate: () => false }
}),
axios.get('')
axios.get('http://test.com')
]);

expect(resp2.cached).toBe(false);
Expand All @@ -73,43 +76,43 @@ describe('test request interceptor', () => {
it('tests response.cached', async () => {
const axios = mockAxios();

const response = await axios.get('');
const response = await axios.get('http://test.com');
expect(response.cached).toBe(false);

const response2 = await axios.get('');
const response2 = await axios.get('http://test.com');
expect(response2.cached).toBe(true);

const response3 = await axios.get('', { id: 'random-id' });
const response3 = await axios.get('http://test.com', { id: 'random-id' });
expect(response3.cached).toBe(false);

const response4 = await axios.get('', { id: 'random-id' });
const response4 = await axios.get('http://test.com', { id: 'random-id' });
expect(response4.cached).toBe(true);
});

it('test cache expiration', async () => {
const axios = mockAxios({}, { 'cache-control': 'max-age=1' });

await axios.get('', { cache: { interpretHeader: true } });
await axios.get('http://test.com', { cache: { interpretHeader: true } });

const resultCache = await axios.get('');
const resultCache = await axios.get('http://test.com');
expect(resultCache.cached).toBe(true);

// Sleep entire max age time.
await sleep(1000);

const response2 = await axios.get('');
const response2 = await axios.get('http://test.com');
expect(response2.cached).toBe(false);
});

it('tests "must revalidate" handling without any headers to do so', async () => {
const axios = mockAxios({}, { 'cache-control': 'must-revalidate' });
const config = { cache: { interpretHeader: true } };
await axios.get('', config);
await axios.get('http://test.com', config);

// 0ms cache
await sleep(1);

const response = await axios.get('', config);
const response = await axios.get('http://test.com', config);
// nothing to use for revalidation
expect(response.cached).toBe(false);
});
Expand Down

0 comments on commit 66f29c9

Please sign in to comment.