mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2024-11-27 08:39:59 +08:00
feat(runtime, editor): support raw body in fetch trait
This commit is contained in:
parent
6d567af776
commit
320f527edb
@ -24,20 +24,30 @@ const EMPTY_ARRAY: string[] = [];
|
||||
export const Body: React.FC<Props> = props => {
|
||||
const { api, spec, formik, services } = props;
|
||||
const { values } = formik;
|
||||
const specWithWidgetOptions = useMemo(()=> mergeWidgetOptionsIntoSpec(spec, {
|
||||
minNum: 1,
|
||||
isShowHeader: true,
|
||||
}), [spec]);
|
||||
const specWithWidgetOptions = useMemo(
|
||||
() =>
|
||||
mergeWidgetOptionsIntoSpec(spec, {
|
||||
minNum: 1,
|
||||
isShowHeader: true,
|
||||
}),
|
||||
[spec]
|
||||
);
|
||||
|
||||
const onChange = useCallback((value: Record<string, unknown>) => {
|
||||
formik.setFieldValue('body', value);
|
||||
formik.submitForm();
|
||||
}, [formik]);
|
||||
const onChange = useCallback(
|
||||
(value: Record<string, unknown>) => {
|
||||
formik.setFieldValue('body', value);
|
||||
formik.submitForm();
|
||||
},
|
||||
[formik]
|
||||
);
|
||||
|
||||
const onBodyTypeChange = useCallback((e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
formik.setFieldValue('bodyType', e.target.value);
|
||||
formik.submitForm();
|
||||
}, [formik]);
|
||||
const onBodyTypeChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
formik.setFieldValue('bodyType', e.target.value);
|
||||
formik.submitForm();
|
||||
},
|
||||
[formik]
|
||||
);
|
||||
|
||||
return (
|
||||
<VStack alignItems="start">
|
||||
@ -47,6 +57,7 @@ export const Body: React.FC<Props> = props => {
|
||||
<Select value={values.bodyType} onChange={onBodyTypeChange}>
|
||||
<option value="json">JSON</option>
|
||||
<option value="formData">Form Data</option>
|
||||
<option value="raw">Raw</option>
|
||||
</Select>
|
||||
|
||||
<Box width="full">
|
||||
|
@ -36,6 +36,7 @@ export const FetchTraitPropertiesSpec = Type.Object({
|
||||
Type.Object({
|
||||
json: Type.String(),
|
||||
formData: Type.String(),
|
||||
raw: Type.String(),
|
||||
}),
|
||||
{ title: 'Body Type' }
|
||||
),
|
||||
@ -112,6 +113,9 @@ export default implementRuntimeTrait({
|
||||
let reqBody: string | FormData = '';
|
||||
|
||||
switch (bodyType) {
|
||||
case 'raw':
|
||||
reqBody = body.value || '';
|
||||
break;
|
||||
case 'json':
|
||||
reqBody = JSON.stringify(body);
|
||||
break;
|
||||
@ -125,7 +129,7 @@ export default implementRuntimeTrait({
|
||||
|
||||
// fetch data
|
||||
fetch(url, {
|
||||
method,
|
||||
method: method.toUpperCase(),
|
||||
headers,
|
||||
body: method === 'get' ? undefined : reqBody,
|
||||
}).then(
|
||||
|
Loading…
Reference in New Issue
Block a user