From a890fc2a8cd32547d18e8ab9aea5186e19828ef2 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Wed, 13 Dec 2023 12:19:06 -0300 Subject: [PATCH] fix: support for URLSearchParams --- package.json | 2 +- pnpm-lock.yaml | 8 ++++---- src/interceptors/request.ts | 22 ++++------------------ test/util/key-generator.test.ts | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index f0b3e957..0c85513c 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "dependencies": { "cache-parser": "1.2.4", "fast-defer": "1.1.8", - "object-code": "1.3.0" + "object-code": "1.3.2" }, "devDependencies": { "@biomejs/biome": "1.4.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ad99c0b4..b8ac2d71 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,8 +15,8 @@ dependencies: specifier: 1.1.8 version: 1.1.8 object-code: - specifier: 1.3.0 - version: 1.3.0 + specifier: 1.3.2 + version: 1.3.2 devDependencies: '@biomejs/biome': @@ -2436,8 +2436,8 @@ packages: resolution: {integrity: sha512-ub5E4+FBPKwAZx0UwIQOjYWGHTEq5sPqHQNRN8Z9e4A7u3Tj1weLJsL59yH9vmvqEtBHaOmT6cYQKIZOxp35FQ==} dev: true - /object-code@1.3.0: - resolution: {integrity: sha512-PLplgvzuFhSPBuTX/mtaXEnU3c6g7qKflVVQbV9VWEnV/34iKeAX1jeDNCKq1OgGlsnkA/NjldCzTbHxa7Wj4A==} + /object-code@1.3.2: + resolution: {integrity: sha512-3CVDmQiru7YYVr+4OpCGrqkVE7GogmWinDcgfno1fXlKgIhtW9FhgHTiV98gMPUjQwWuWvijQDCY8sGnqKrhTw==} dev: false /once@1.4.0: diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index adc74022..a2f818b7 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -1,18 +1,9 @@ import { deferred } from 'fast-defer'; import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios'; import { Header } from '../header/headers'; -import type { - CachedResponse, - CachedStorageValue, - LoadingStorageValue -} from '../storage/types'; +import type { CachedResponse, CachedStorageValue, LoadingStorageValue } from '../storage/types'; import type { RequestInterceptor } from './build'; -import { - ConfigWithCache, - createValidateStatus, - isMethodIn, - updateStaleRequest -} from './util'; +import { ConfigWithCache, createValidateStatus, isMethodIn, updateStaleRequest } from './util'; export function defaultRequestInterceptor(axios: AxiosCacheInstance) { const onFulfilled: RequestInterceptor['onFulfilled'] = async (config) => { @@ -58,11 +49,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { // Not cached, continue the request, and mark it as fetching // biome-ignore lint/suspicious/noConfusingLabels: required to break condition in simultaneous accesses - ignoreAndRequest: if ( - cache.state === 'empty' || - cache.state === 'stale' || - overrideCache - ) { + ignoreAndRequest: if (cache.state === 'empty' || cache.state === 'stale' || overrideCache) { // This checks for simultaneous access to a new key. The js event loop jumps on the // first await statement, so the second (asynchronous call) request may have already // started executing. @@ -110,8 +97,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) { data: cache.data as any, // If the cache is empty and asked to override it, use the current timestamp - createdAt: - overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any) + createdAt: overrideCache && !cache.createdAt ? Date.now() : (cache.createdAt as any) }, config ); diff --git a/test/util/key-generator.test.ts b/test/util/key-generator.test.ts index 69fe44e8..21e7f29f 100644 --- a/test/util/key-generator.test.ts +++ b/test/util/key-generator.test.ts @@ -233,4 +233,30 @@ describe('KeyGeneration', () => { assert.doesNotThrow(() => keyGenerator(recursive)); assert.doesNotThrow(() => defaultKeyGenerator(recursive)); }); + + it('works with URLSearchParams', () => { + const keyGenerator = defaultKeyGenerator; + + const params = new URLSearchParams(); + params.append('a', '1'); + params.append('b', '2'); + + const key = keyGenerator({ + baseURL: 'http://example.com', + url: '/test/path', + params + }); + + const params2 = new URLSearchParams(); + params2.append('a', '2'); + params2.append('b', '1'); + + const key2 = keyGenerator({ + baseURL: 'http://example.com', + url: '/test/path', + params: params2 + }); + + assert.notEqual(key, key2); + }); });