Skip to content

Commit

Permalink
fix: support for URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Dec 13, 2023
1 parent 8d03709 commit a890fc2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 4 additions & 18 deletions src/interceptors/request.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
);
Expand Down
26 changes: 26 additions & 0 deletions test/util/key-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit a890fc2

Please sign in to comment.