Skip to content

Commit

Permalink
refactor: minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed May 2, 2024
1 parent 5897ec4 commit b2e272e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/any-signals.ts
Expand Up @@ -4,6 +4,9 @@ export interface ClearableSignal extends AbortSignal {
clear: () => void;
}

const addEventListener = 'addEventListener';
const removeEventListener = 'removeEventListener';

/**
* Takes an array of AbortSignals and returns a single signal.
* If any signals are aborted, the returned signal will be aborted.
Expand All @@ -12,7 +15,7 @@ export function anySignal(
signals: (AbortSignal | undefined | null)[],
cleanCb?: Function
): ClearableSignal {
const controller = new globalThis.AbortController();
const controller = new AbortController();

function onAbort(reason: Error) {
controller.abort(reason);
Expand All @@ -26,16 +29,16 @@ export function anySignal(
break;
}

if (signal?.addEventListener != null) {
if (signal?.[addEventListener] != null) {
const cb = () => {
onAbort(signal.reason);
};
unsubscribe.push(() => {
if (signal?.removeEventListener != null) {
signal.removeEventListener('abort', cb);
if (signal?.[removeEventListener] != null) {
signal[removeEventListener]('abort', cb);
}
});
signal.addEventListener('abort', cb);
signal[addEventListener]('abort', cb);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/interceptors.ts
Expand Up @@ -2,7 +2,7 @@
import { encodeParams, merge } from 'xior/utils';

import type { XiorInterceptorRequestConfig } from './types';
import { trimUndefined } from './utils';
import { isObj, keys, trimUndefined } from './utils';

const appPrefix = 'application/';
const formUrl = `${appPrefix}x-www-form-urlencoded`;
Expand All @@ -27,7 +27,7 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ
if (data && !(data instanceof FormData)) {
let contentType = '';
if (req?.headers) {
const contentTypeKey = Object.keys(req.headers).find((key) => {
const contentTypeKey = keys(req.headers).find((key) => {
return key.toLowerCase() === 'content-type';
});
if (contentTypeKey) {
Expand All @@ -47,12 +47,12 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ
}
if (contentType === jsonType) {
_data = JSON.stringify(trimUndefined(data));
} else if (!isGet && contentType === formUrl && data && typeof data === 'object') {
} else if (!isGet && contentType === formUrl && data && isObj(data)) {
_data = paramsSerializer(data);
}
}

if (Object.keys(newParams).length > 0) {
if (keys(newParams).length > 0) {
const result = paramsSerializer(newParams, encodeURI);
_url += _url.includes('?') ? `&${result}` : `?${result}`;
}
Expand Down
14 changes: 9 additions & 5 deletions src/utils.ts
Expand Up @@ -20,11 +20,11 @@ export function encodeParams<T = any>(
if (value !== undefinedValue) {
const encodedKey = parentKey ? `${parentKey}[${encodeURIFunc(key)}]` : encodeURIFunc(key);

if (typeof value === 'object') {
if (isObj(value)) {
// If the value is an object or array, recursively encode its contents
const result = encodeParams(value, encodeURI, encodedKey);
if (result !== '') encodedParams.push(result);
} else if (Array.isArray(value)) {
} else if (isArray(value)) {
// If the value is an array, encode each element individually
value.forEach((element, index) => {
const arrayKey = `${encodedKey}[${index}]`;
Expand All @@ -42,10 +42,10 @@ export function encodeParams<T = any>(
}

export function trimUndefined(obj: any): any {
if (Array.isArray(obj)) {
if (isArray(obj)) {
return obj.map(trimUndefined);
} else if (obj && typeof obj === 'object') {
Object.keys(obj).forEach((key) => {
} else if (obj && isObj(obj)) {
keys(obj).forEach((key) => {
const value = obj[key];
if (value === undefinedValue) {
delete obj[key];
Expand Down Expand Up @@ -107,3 +107,7 @@ export class XiorTimeoutError extends XiorError {
export function isXiorError(error: any) {
return error.name === 'XiorError' || error.name === 'XiorTimeoutError';
}

export const keys: typeof Object.keys = (obj) => Object.keys(obj);
export const isObj = (obj: any) => typeof obj === 'object';
export const isArray = ((arr: any) => Array.isArray(arr)) as typeof Array.isArray;

0 comments on commit b2e272e

Please sign in to comment.