Fix adding header if not exist

This commit is contained in:
Gregory Schier
2024-03-10 17:10:16 -07:00
parent 4c7ee5ef80
commit 6b239c4e3f
3 changed files with 25 additions and 35 deletions

View File

@@ -44,8 +44,6 @@ export function BinaryFileEditor({
const filePath = typeof body.filePath === 'string' ? body.filePath : undefined; const filePath = typeof body.filePath === 'string' ? body.filePath : undefined;
const mimeType = mime.getType(filePath ?? '') ?? 'application/octet-stream'; const mimeType = mime.getType(filePath ?? '') ?? 'application/octet-stream';
console.log('mimeType', mimeType, contentType);
return ( return (
<VStack space={2}> <VStack space={2}>
<HStack space={2} alignItems="center"> <HStack space={2} alignItems="center">

View File

@@ -82,10 +82,9 @@ export const RequestPane = memo(function RequestPane({
], ],
onChange: async (bodyType) => { onChange: async (bodyType) => {
const patch: Partial<HttpRequest> = { bodyType }; const patch: Partial<HttpRequest> = { bodyType };
let newContentType: string | null | undefined;
if (bodyType === BODY_TYPE_NONE) { if (bodyType === BODY_TYPE_NONE) {
patch.headers = activeRequest.headers.filter( newContentType = null;
(h) => h.name.toLowerCase() !== 'content-type',
);
} else if ( } else if (
bodyType === BODY_TYPE_FORM_URLENCODED || bodyType === BODY_TYPE_FORM_URLENCODED ||
bodyType === BODY_TYPE_FORM_MULTIPART || bodyType === BODY_TYPE_FORM_MULTIPART ||
@@ -94,32 +93,17 @@ export const RequestPane = memo(function RequestPane({
bodyType === BODY_TYPE_XML bodyType === BODY_TYPE_XML
) { ) {
patch.method = 'POST'; patch.method = 'POST';
patch.headers = [ newContentType = bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType;
...(activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type') ??
[]),
{
name: 'Content-Type',
value: bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType,
enabled: true,
},
];
} else if (bodyType == BODY_TYPE_GRAPHQL) { } else if (bodyType == BODY_TYPE_GRAPHQL) {
patch.method = 'POST'; patch.method = 'POST';
patch.headers = [ newContentType = 'application/json';
...(activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type') ??
[]),
{
name: 'Content-Type',
value: 'application/json',
enabled: true,
},
];
} }
await updateRequest.mutateAsync(patch); await updateRequest.mutateAsync(patch);
// Force update header editor so any changed headers are reflected if (newContentType !== undefined) {
setTimeout(() => setForceUpdateHeaderEditorKey((u) => u + 1), 100); await handleContentTypeChange(newContentType);
}
}, },
}, },
}, },
@@ -176,14 +160,18 @@ export const RequestPane = memo(function RequestPane({
(body: HttpRequest['body']) => updateRequest.mutate({ body }), (body: HttpRequest['body']) => updateRequest.mutate({ body }),
[updateRequest], [updateRequest],
); );
const handleContentTypeChange = useCallback( const handleContentTypeChange = useCallback(
async (contentType: string | null) => { async (contentType: string | null) => {
const headers = const headers = activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type');
contentType != null
? activeRequest.headers.map((h) => if (contentType != null) {
h.name.toLowerCase() === 'content-type' ? { ...h, value: contentType } : h, headers.push({
) name: 'Content-Type',
: activeRequest.headers; value: contentType,
enabled: true,
});
}
await updateRequest.mutateAsync({ headers }); await updateRequest.mutateAsync({ headers });
// Force update header editor so any changed headers are reflected // Force update header editor so any changed headers are reflected

View File

@@ -80,10 +80,14 @@ export function useIntrospectGraphQL(baseRequest: HttpRequest) {
setRefetchKey((k) => k + 1); setRefetchKey((k) => k + 1);
}, []); }, []);
const schema = useMemo( const schema = useMemo(() => {
() => (introspection ? buildClientSchema(introspection) : undefined), try {
[introspection], return introspection ? buildClientSchema(introspection) : undefined;
); // eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
setError('message' in e ? e.message : String(e));
}
}, [introspection]);
return { schema, isLoading, error, refetch }; return { schema, isLoading, error, refetch };
} }