Skip to content

Commit

Permalink
refactor: use './utils' instead of 'xior/utils'
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Jan 15, 2025
1 parent 946eb47 commit 6d77f42
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Xior } from './xior';

export * from './xior';
export * from './types';
// @ts-ignore
export * from 'xior/utils';
export * from './utils';

const xior = Object.assign(Xior.create(), { create: Xior.create, VERSION: Xior.VERSION });
export default xior;
5 changes: 1 addition & 4 deletions src/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// @ts-ignore
import { encodeParams, merge } from 'xior/utils';

import type { XiorInterceptorRequestConfig } from './types';
import { trimUndefined } from './utils';
import { trimUndefined, encodeParams, merge } from './utils';

const appPrefix = 'application/';
const formUrl = `${appPrefix}x-www-form-urlencoded`;
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lru } from 'tiny-lru';
// @ts-ignore
import { buildSortedURL, isAbsoluteURL, joinPath } from 'xior/utils';
import { buildSortedURL, isAbsoluteURL, joinPath } from 'xior';

import { ICacheLike } from './utils';
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../../types';
Expand Down Expand Up @@ -28,6 +28,7 @@ declare module 'xior' {
interface XiorResponse {
fromCache?: boolean;
cacheTime?: number;
cacheKey?: string;
}
}

Expand Down Expand Up @@ -62,40 +63,43 @@ export default function xiorCachePlugin(options: XiorCacheOptions = {}): XiorPlu
enabled = t === 'undefined' ? isGet : Boolean(enableCache);
}

let key = '';
if (enabled) {
const cache: ICacheLike<XiorPromise> = defaultCache;

const index = buildSortedURL(
key = buildSortedURL(
config.url && isAbsoluteURL(config.url)
? config.url
: joinPath(config.baseURL, config.url),
{ a: config.data, b: config.params },
paramsSerializer as (obj: Record<string, any>) => string
);
let responsePromise = cache.get(index);
let responsePromise = cache.get(key);

if (!responsePromise || forceUpdate) {
responsePromise = (async () => {
try {
return await adapter(config);
} catch (reason) {
if ('delete' in cache) {
cache.delete(index);
cache.delete(key);
} else {
cache.del(index);
cache.del(key);
}
throw reason;
}
})();

cache.set(index, responsePromise);
cache.set(key, responsePromise);

return responsePromise;
}

return responsePromise.then((res) => {
(res as any).fromCache = true;
(res as any).cacheTime = Date.now();
(res as any).cacheKey = key;

return res;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import { joinPath, isAbsoluteURL, buildSortedURL } from 'xior/utils';
import { joinPath, isAbsoluteURL, buildSortedURL } from 'xior';

import type { XiorPlugin, XiorRequestConfig } from '../types';

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/error-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lru } from 'tiny-lru';
// @ts-ignore
import { XiorError, joinPath, isAbsoluteURL, buildSortedURL } from 'xior/utils';
import { XiorError, joinPath, isAbsoluteURL, buildSortedURL } from 'xior';

import { ICacheLike } from './utils';
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/error-retry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import { XiorError, delay } from 'xior/utils';
import { XiorError, delay } from 'xior';

import { XiorInterceptorRequestConfig, XiorPlugin, XiorRequestConfig } from '../types';

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
joinPath,
merge,
// @ts-ignore
} from 'xior/utils';
} from 'xior';

import { MockHeaders, StatusOrCallback, RequestOptions, RequestData, VERBS } from './types';
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lru } from 'tiny-lru';
// @ts-ignore
import { isAbsoluteURL, joinPath, buildSortedURL } from 'xior/utils';
import { isAbsoluteURL, joinPath, buildSortedURL } from 'xior';

import { ICacheLike } from './cache/utils';
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
Expand Down
22 changes: 10 additions & 12 deletions src/xior.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import {
ClearableSignal,
XiorTimeoutError,
anySignal,
isAbsoluteURL,
XiorError,
merge,
joinPath,
encodeParams,
// @ts-ignore
} from 'xior/utils';

import defaultRequestInterceptor from './interceptors';
import type {
XiorInterceptorOptions,
Expand All @@ -19,6 +7,16 @@ import type {
XiorResponse,
XiorResponseInterceptorConfig,
} from './types';
import {
ClearableSignal,
XiorTimeoutError,
anySignal,
isAbsoluteURL,
XiorError,
merge,
joinPath,
encodeParams,
} from './utils';

const undefinedValue = undefined;
const supportAbortController = typeof AbortController !== `${undefinedValue}`;
Expand Down

0 comments on commit 6d77f42

Please sign in to comment.