mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
fix(custom-request): support configuring content type (#4144)
This commit is contained in:
parent
fd4c9cb288
commit
d5ed2d5037
@ -7,6 +7,7 @@ import {
|
||||
actionSettingsItems,
|
||||
useCollection_deprecated,
|
||||
useDataSourceKey,
|
||||
useDesignable,
|
||||
useRequest,
|
||||
} from '@nocobase/client';
|
||||
import { App } from 'antd';
|
||||
@ -25,7 +26,7 @@ export function CustomRequestSettingsItem() {
|
||||
const customRequestsResource = useCustomRequestsResource();
|
||||
const { message } = App.useApp();
|
||||
const { data, refresh } = useGetCustomRequest();
|
||||
|
||||
const { dn } = useDesignable();
|
||||
return (
|
||||
<>
|
||||
<SchemaSettingsActionModalItem
|
||||
@ -41,6 +42,7 @@ export function CustomRequestSettingsItem() {
|
||||
}}
|
||||
onSubmit={async (config) => {
|
||||
const { ...requestSettings } = config;
|
||||
fieldSchema['x-response-type'] = requestSettings.responseType;
|
||||
await customRequestsResource.updateOrCreate({
|
||||
values: {
|
||||
key: fieldSchema['x-uid'],
|
||||
@ -52,6 +54,12 @@ export function CustomRequestSettingsItem() {
|
||||
},
|
||||
filterKeys: ['key'],
|
||||
});
|
||||
dn.emit('patch', {
|
||||
schema: {
|
||||
'x-response-type': requestSettings.responseType,
|
||||
'x-uid': fieldSchema['x-uid'],
|
||||
},
|
||||
});
|
||||
refresh();
|
||||
return message.success(t('Saved successfully'));
|
||||
}}
|
||||
|
@ -2,6 +2,7 @@ import { useField, useFieldSchema, useForm } from '@formily/react';
|
||||
import { useAPIClient, useActionContext, useCompile, useDataSourceKey, useRecord } from '@nocobase/client';
|
||||
import { isURL } from '@nocobase/utils/client';
|
||||
import { App } from 'antd';
|
||||
import { saveAs } from 'file-saver';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
export const useCustomizeRequestActionProps = () => {
|
||||
@ -33,7 +34,7 @@ export const useCustomizeRequestActionProps = () => {
|
||||
actionField.data ??= {};
|
||||
actionField.data.loading = true;
|
||||
try {
|
||||
await apiClient.request({
|
||||
const res = await apiClient.request({
|
||||
url: `/customRequests:send/${fieldSchema['x-uid']}`,
|
||||
method: 'POST',
|
||||
data: {
|
||||
@ -44,7 +45,15 @@ export const useCustomizeRequestActionProps = () => {
|
||||
data: formValues,
|
||||
},
|
||||
},
|
||||
responseType: fieldSchema['x-response-type'] === 'stream' ? 'blob' : 'json',
|
||||
});
|
||||
if (res.headers['content-disposition']) {
|
||||
const regex = /attachment;\s*filename="([^"]+)"/;
|
||||
const match = res.headers['content-disposition'].match(regex);
|
||||
if (match[1]) {
|
||||
saveAs(res.data, match[1]);
|
||||
}
|
||||
}
|
||||
actionField.data.loading = false;
|
||||
// service?.refresh?.();
|
||||
if (callBack) {
|
||||
|
@ -178,5 +178,17 @@ export const CustomRequestConfigurationFieldsSchema = {
|
||||
defaultValue: 5000,
|
||||
},
|
||||
},
|
||||
responseType: {
|
||||
type: 'string',
|
||||
title: generateNTemplate('Response type'),
|
||||
'x-decorator': 'FormItem',
|
||||
'x-decorator-props': {},
|
||||
'x-component': 'Select',
|
||||
default: 'json',
|
||||
enum: [
|
||||
{ value: 'json', label: 'JSON' },
|
||||
{ value: 'stream', label: 'Stream' },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -157,6 +157,8 @@ export async function send(this: CustomRequestPlugin, ctx: Context, next: Next)
|
||||
data: getParsedValue(data),
|
||||
};
|
||||
|
||||
console.log(axiosRequestConfig);
|
||||
|
||||
const requestUrl = axios.getUri(axiosRequestConfig);
|
||||
this.logger.info(`custom-request:send:${filterByTk} request url ${requestUrl}`);
|
||||
this.logger.info(
|
||||
@ -170,10 +172,12 @@ export async function send(this: CustomRequestPlugin, ctx: Context, next: Next)
|
||||
);
|
||||
|
||||
try {
|
||||
ctx.body = await axios(axiosRequestConfig).then((res) => {
|
||||
this.logger.info(`custom-request:send:${filterByTk} success`);
|
||||
return res.data;
|
||||
});
|
||||
const res = await axios(axiosRequestConfig);
|
||||
this.logger.info(`custom-request:send:${filterByTk} success`);
|
||||
ctx.body = res.data;
|
||||
if (res.headers['content-disposition']) {
|
||||
ctx.set('Content-Disposition', res.headers['content-disposition']);
|
||||
}
|
||||
} catch (err) {
|
||||
if (axios.isAxiosError(err)) {
|
||||
ctx.status = err.response?.status || 500;
|
||||
|
Loading…
x
Reference in New Issue
Block a user