Skip to content

Commit 2f33fd4

Browse files
committed
plugins: option omitKey -> normalizeParams
1 parent 05d105b commit 2f33fd4

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

src/plugins/cache.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ICacheLike } from './cache/utils';
44
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
55
import { buildSortedURL, isAbsoluteURL, joinPath } from '../utils';
66
import { f, GET, undefinedValue } from '../shorts';
7+
import { mergeConfig } from '..';
78

89
type XiorPromise = Promise<XiorResponse>;
910

@@ -17,11 +18,9 @@ export type XiorCacheOptions = {
1718
cacheItems?: number;
1819
/** cache time in ms, default is 5 minutes */
1920
cacheTime?: number;
20-
/** remove the key from data/params */
21-
omitKey?: (
22-
data?: Record<string, string>,
23-
params?: Record<string, string>
24-
) => { data?: Record<string, string>; params?: Record<string, string> };
21+
/**
22+
Process the config before generating the key based on data/params. The config is a new object, not a request reference. */
23+
normalizeParams?: (config: XiorRequestConfig) => XiorRequestConfig;
2524
};
2625

2726
/** @ts-ignore */
@@ -46,7 +45,7 @@ declare module 'xior' {
4645
export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlugin {
4746
const {
4847
enableCache: _enableCache,
49-
omitKey: _omitKey,
48+
normalizeParams: _normalizeParams,
5049
defaultCache: _defaultCache = lru(
5150
options.cacheItems || 100,
5251
options.cacheTime || 1000 * 60 * 5
@@ -57,7 +56,7 @@ export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlu
5756
return async (config) => {
5857
const {
5958
enableCache = _enableCache,
60-
omitKey = _omitKey,
59+
normalizeParams = _normalizeParams,
6160
forceUpdate,
6261
defaultCache = _defaultCache,
6362
paramsSerializer,
@@ -81,7 +80,7 @@ export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlu
8180
let key = '';
8281
if (enabled) {
8382
const cache: ICacheLike<XiorPromise> = defaultCache;
84-
const { data, params } = omitKey?.(config.data, config.params) || config;
83+
const { data, params } = normalizeParams?.(mergeConfig(config, {})) || config;
8584
key = buildSortedURL(
8685
config.url && isAbsoluteURL(config.url)
8786
? config.url

src/plugins/dedupe.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mergeConfig } from '..';
12
import { f, GET, undefinedValue } from '../shorts';
23
import type { XiorPlugin, XiorRequestConfig } from '../types';
34
import { joinPath, isAbsoluteURL, buildSortedURL } from '../utils';
@@ -8,12 +9,9 @@ export type XiorDedupeOptions = {
89
*/
910
enableDedupe?: boolean | ((config?: XiorRequestConfig) => boolean);
1011
onDedupe?: (config: XiorRequestConfig) => void;
11-
12-
/** remove the key from data/params */
13-
omitKey?: (
14-
data?: Record<string, string>,
15-
params?: Record<string, string>
16-
) => { data?: Record<string, string>; params?: Record<string, string> };
12+
/**
13+
Process the config before generating the key based on data/params. The config is a new object, not a request reference. */
14+
normalizeParams?: (config: XiorRequestConfig) => XiorRequestConfig;
1715
};
1816

1917
/** @ts-ignore */
@@ -29,14 +27,18 @@ export const inflight = new Map<string, any[]>();
2927
* Prevents having multiple identical requests on the fly at the same time.
3028
*/
3129
export default function xiorDedupePlugin(options: XiorDedupeOptions = {}): XiorPlugin {
32-
const { enableDedupe: _enableDedupe, onDedupe: _onDedupe, omitKey: _omitKey } = options;
30+
const {
31+
enableDedupe: _enableDedupe,
32+
onDedupe: _onDedupe,
33+
normalizeParams: _normalizeParams,
34+
} = options;
3335

3436
return function (adapter) {
3537
return async (config) => {
3638
const {
3739
paramsSerializer,
3840
enableDedupe = _enableDedupe,
39-
omitKey = _omitKey,
41+
normalizeParams = _normalizeParams,
4042
onDedupe = _onDedupe,
4143
} = config as XiorDedupeOptions & XiorRequestConfig;
4244

@@ -56,8 +58,7 @@ export default function xiorDedupePlugin(options: XiorDedupeOptions = {}): XiorP
5658
if (!enabled) {
5759
return adapter(config);
5860
}
59-
const { data, params } = omitKey?.(config.data, config.params) || config;
60-
61+
const { data, params } = normalizeParams?.(mergeConfig(config, {})) || config;
6162
const key = buildSortedURL(
6263
config.url && isAbsoluteURL(config.url) ? config.url : joinPath(config.baseURL, config.url),
6364
{ a: data, b: params },

src/plugins/error-cache.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ICacheLike } from './utils';
44
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
55
import { XiorError, joinPath, isAbsoluteURL, buildSortedURL } from '../utils';
66
import { f, GET, undefinedValue } from '../shorts';
7+
import { mergeConfig } from '..';
78

89
export type XiorErrorCacheOptions = {
910
/**
@@ -18,11 +19,8 @@ export type XiorErrorCacheOptions = {
1819
/** max cache numbers in LRU, default is 100 */
1920
cacheItems?: number;
2021

21-
/** remove the key from data/params */
22-
omitKey?: (
23-
data?: Record<string, string>,
24-
params?: Record<string, string>
25-
) => { data?: Record<string, string>; params?: Record<string, string> };
22+
/** Process the config before generating the key based on data/params. The config is a new object, not a request reference. */
23+
normalizeParams?: (config: XiorRequestConfig) => XiorRequestConfig;
2624
};
2725

2826
/** @ts-ignore */
@@ -40,7 +38,7 @@ declare module 'xior' {
4038
export default function xiorErrorCachePlugin(options: XiorErrorCacheOptions = {}): XiorPlugin {
4139
const {
4240
enableCache: _enableCache,
43-
omitKey: _omitKey,
41+
normalizeParams: _normalizeParams,
4442
defaultCache: _defaultCache = lru<{
4543
loading?: boolean;
4644
res?: XiorResponse;
@@ -54,7 +52,7 @@ export default function xiorErrorCachePlugin(options: XiorErrorCacheOptions = {}
5452
return async (config) => {
5553
const {
5654
enableCache = _enableCache,
57-
omitKey = _omitKey,
55+
normalizeParams = _normalizeParams,
5856
defaultCache = _defaultCache,
5957
useCacheFirst = _inBg,
6058
onCacheRequest = _cacheRequest,
@@ -77,7 +75,7 @@ export default function xiorErrorCachePlugin(options: XiorErrorCacheOptions = {}
7775
if (!enabled) return adapter(config);
7876

7977
const cache = defaultCache;
80-
const { data, params } = omitKey?.(config.data, config.params) || config;
78+
const { data, params } = normalizeParams?.(mergeConfig(config, {})) || config;
8179
const index = buildSortedURL(
8280
config.url && isAbsoluteURL(config.url) ? config.url : joinPath(config.baseURL, config.url),
8381
{ a: data, b: params },

src/plugins/throttle.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ICacheLike } from './cache/utils';
44
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
55
import { isAbsoluteURL, joinPath, buildSortedURL } from '../utils';
66
import { GET, f, undefinedValue } from '../shorts';
7+
import { mergeConfig } from '..';
78

89
type XiorPromise = Promise<XiorResponse>;
910

@@ -24,11 +25,8 @@ export type XiorThrottleOptions = {
2425
/** max throttle numbers in LRU, default is 100 */
2526
throttleItems?: number;
2627

27-
/** remove the key from data/params */
28-
omitKey?: (
29-
data?: Record<string, string>,
30-
params?: Record<string, string>
31-
) => { data?: Record<string, string>; params?: Record<string, string> };
28+
/** Process the config before generating the key based on data/params. The config is a new object, not a request reference. */
29+
normalizeParams?: (config: XiorRequestConfig) => XiorRequestConfig;
3230
};
3331

3432
/** @ts-ignore */
@@ -41,7 +39,7 @@ declare module 'xior' {
4139
export default function xiorThrottlePlugin(options: XiorThrottleOptions = {}): XiorPlugin {
4240
const {
4341
enableThrottle: _enableThrottle,
44-
omitKey: _omitKey,
42+
normalizeParams: _normalizeParams,
4543
threshold: _threshold = 1000,
4644
throttleCache = lru<RecordedCache>(options.throttleItems || 100),
4745
onThrottle: _onThrottle,
@@ -83,7 +81,7 @@ export default function xiorThrottlePlugin(options: XiorThrottleOptions = {}): X
8381
paramsSerializer,
8482
threshold = _threshold,
8583
enableThrottle = _enableThrottle,
86-
omitKey = _omitKey,
84+
normalizeParams = _normalizeParams,
8785
onThrottle = _onThrottle,
8886
} = config as XiorThrottleOptions & XiorRequestConfig;
8987

@@ -105,7 +103,7 @@ export default function xiorThrottlePlugin(options: XiorThrottleOptions = {}): X
105103
}
106104

107105
if (enabled) {
108-
const { data, params } = omitKey?.(config.data, config.params) || config;
106+
const { data, params } = normalizeParams?.(mergeConfig(config, {})) || config;
109107
const index = buildSortedURL(
110108
config.url && isAbsoluteURL(config.url)
111109
? config.url

0 commit comments

Comments
 (0)