fix: support for URLSearchParams

This commit is contained in:
Arthur Fiorette 2023-12-13 12:19:06 -03:00
parent 8d03709482
commit a890fc2a8c
No known key found for this signature in database
GPG Key ID: 79FA8EC214FA0233
4 changed files with 35 additions and 23 deletions

View File

@ -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",

8
pnpm-lock.yaml generated
View File

@ -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:

View File

@ -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
);

View File

@ -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);
});
});