fix: missing request params

This commit is contained in:
chenos 2022-01-22 00:38:39 +08:00
parent 11bbec7fd4
commit 31340f4773
2 changed files with 13 additions and 11 deletions

View File

@ -32,6 +32,7 @@ export class APIClient {
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | ResourceActionOptions): Promise<R> {
const { resource, resourceOf, action, params } = config as any;
console.log('config', config);
if (resource) {
return this.resource(resource, resourceOf)[action](params);
}
@ -51,8 +52,11 @@ export class APIClient {
config['method'] = 'post';
}
return (params?: ActionParams) => {
config[config.method === 'get' ? 'params' : 'data'] = { __params__: params };
console.log({ config });
const { values, ...others } = params || {};
config['params'] = others;
if (config.method !== 'get') {
config['data'] = values || {};
}
return this.request(config);
};
},

View File

@ -21,16 +21,14 @@ export function useRequest<P>(
if (typeof service === 'function') {
return useReq(service, options);
}
return useReq(async () => {
const { url, resource, resourceOf, action, params } = service as any;
if (url) {
return useReq(async (params) => {
const { resource } = service as ResourceActionOptions;
if (resource) {
Object.assign(service, { params });
} else {
Object.assign(service, params);
}
const response = await api.request(service);
return response?.data;
}
if (resource) {
const response = await api.resource(resource, resourceOf)[action](params);
return response?.data?.data;
}
return;
}, options);
}