Commit f1b8dc29 authored by chenos's avatar chenos
Browse files

fix: use request params merge

parent cae11da7
Showing with 130 additions and 7 deletions
+130 -7
import lodash from 'lodash';
import deepmerge from 'deepmerge';
type MergeStrategyType = 'merge' | 'deepMerge' | 'overwrite' | 'andMerge' | 'orMerge' | 'intersect' | 'union';
type MergeStrategyFunc = (x: any, y: any) => any;
export type MergeStrategy = MergeStrategyType | MergeStrategyFunc;
export interface MergeStrategies {
[key: string]: MergeStrategy;
}
export default function isPlainObject(value) {
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}
function getEnumerableOwnPropertySymbols(target: any): any[] {
return Object.getOwnPropertySymbols
? Object.getOwnPropertySymbols(target).filter((symbol) => target.propertyIsEnumerable(symbol))
: [];
}
function getKeys(target: any) {
return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target));
}
export const mergeStrategies = new Map<MergeStrategyType, MergeStrategyFunc>();
mergeStrategies.set('overwrite', (_, y) => {
return y;
});
mergeStrategies.set('andMerge', (x, y) => {
if (!x && !y) {
return;
}
if (!x) {
return y;
}
if (!y) {
return x;
}
return {
$and: [x, y],
};
});
mergeStrategies.set('orMerge', (x, y) => {
if (!x && !y) {
return;
}
if (!x) {
return y;
}
if (!y) {
return x;
}
return {
$or: [x, y],
};
});
mergeStrategies.set('deepMerge', (x, y) => {
return isPlainObject(x) && isPlainObject(y)
? deepmerge(x, y, {
arrayMerge: (x, y) => y,
})
: y;
});
mergeStrategies.set('merge', (x, y) => {
return isPlainObject(x) && isPlainObject(y) ? Object.assign(x, y) : y;
});
mergeStrategies.set('union', (x, y) => {
if (typeof x === 'string') {
x = x.split(',');
}
if (typeof y === 'string') {
y = y.split(',');
}
return lodash.uniq((x || []).concat(y || []));
});
mergeStrategies.set('intersect', (x, y) => {
if (typeof x === 'string') {
x = x.split(',');
}
if (typeof y === 'string') {
y = y.split(',');
}
if (!Array.isArray(x) || x.length === 0) {
return y || [];
}
if (!Array.isArray(y) || y.length === 0) {
return x || [];
}
return x.filter((v) => y.includes(v));
});
export function assign(target: any, source: any, strategies: MergeStrategies = {}) {
getKeys(source).forEach((sourceKey) => {
const strategy = strategies[sourceKey];
let func = mergeStrategies.get('deepMerge');
if (typeof strategy === 'function') {
func = strategy;
} else if (typeof strategy === 'string' && mergeStrategies.has(strategy as any)) {
func = mergeStrategies.get(strategy as any);
}
target[sourceKey] = func(target[sourceKey], source[sourceKey]);
});
return target;
}
import { useContext } from 'react';
import { AxiosRequestConfig } from 'axios';
import { Options } from 'ahooks/lib/useRequest/src/types';
import { merge } from '@formily/shared';
import { default as useReq } from 'ahooks/lib/useRequest';
import { Options } from 'ahooks/lib/useRequest/src/types';
import { AxiosRequestConfig } from 'axios';
import cloneDeep from 'lodash/cloneDeep';
import { useContext } from 'react';
import { APIClientContext } from '../context';
import { assign } from './assign';
type FunctionService = (...args: any[]) => Promise<any>;
......@@ -21,14 +24,16 @@ export function useRequest<P>(
if (typeof service === 'function') {
return useReq(service, options);
}
return useReq(async (params) => {
return useReq(async (params = {}) => {
const { resource } = service as ResourceActionOptions;
let args = cloneDeep(service);
if (resource) {
Object.assign(service, { params });
args.params = args.params || {};
assign(args.params, params);
} else {
Object.assign(service, params);
args = merge(args, params);
}
const response = await api.request(service);
const response = await api.request(args);
return response?.data;
}, options);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment