feat(runtime, editor): support raw body in fetch trait

This commit is contained in:
Yanzhen Yu 2022-06-22 14:18:01 +08:00
parent 6d567af776
commit 320f527edb
2 changed files with 28 additions and 13 deletions

View File

@ -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">

View File

@ -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(