From 6b239c4e3fa5cff406010ba6fa66ececa5c310f0 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sun, 10 Mar 2024 17:10:16 -0700 Subject: [PATCH] Fix adding header if not exist --- src-web/components/BinaryFileEditor.tsx | 2 -- src-web/components/RequestPane.tsx | 46 +++++++++---------------- src-web/hooks/useIntrospectGraphQL.ts | 12 ++++--- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src-web/components/BinaryFileEditor.tsx b/src-web/components/BinaryFileEditor.tsx index ceb1f1b3..1117b25e 100644 --- a/src-web/components/BinaryFileEditor.tsx +++ b/src-web/components/BinaryFileEditor.tsx @@ -44,8 +44,6 @@ export function BinaryFileEditor({ const filePath = typeof body.filePath === 'string' ? body.filePath : undefined; const mimeType = mime.getType(filePath ?? '') ?? 'application/octet-stream'; - console.log('mimeType', mimeType, contentType); - return ( diff --git a/src-web/components/RequestPane.tsx b/src-web/components/RequestPane.tsx index 76d63558..7a77bac4 100644 --- a/src-web/components/RequestPane.tsx +++ b/src-web/components/RequestPane.tsx @@ -82,10 +82,9 @@ export const RequestPane = memo(function RequestPane({ ], onChange: async (bodyType) => { const patch: Partial = { bodyType }; + let newContentType: string | null | undefined; if (bodyType === BODY_TYPE_NONE) { - patch.headers = activeRequest.headers.filter( - (h) => h.name.toLowerCase() !== 'content-type', - ); + newContentType = null; } else if ( bodyType === BODY_TYPE_FORM_URLENCODED || bodyType === BODY_TYPE_FORM_MULTIPART || @@ -94,32 +93,17 @@ export const RequestPane = memo(function RequestPane({ bodyType === BODY_TYPE_XML ) { patch.method = 'POST'; - patch.headers = [ - ...(activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type') ?? - []), - { - name: 'Content-Type', - value: bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType, - enabled: true, - }, - ]; + newContentType = bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType; } else if (bodyType == BODY_TYPE_GRAPHQL) { patch.method = 'POST'; - patch.headers = [ - ...(activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type') ?? - []), - { - name: 'Content-Type', - value: 'application/json', - enabled: true, - }, - ]; + newContentType = 'application/json'; } await updateRequest.mutateAsync(patch); - // Force update header editor so any changed headers are reflected - setTimeout(() => setForceUpdateHeaderEditorKey((u) => u + 1), 100); + if (newContentType !== undefined) { + await handleContentTypeChange(newContentType); + } }, }, }, @@ -176,14 +160,18 @@ export const RequestPane = memo(function RequestPane({ (body: HttpRequest['body']) => updateRequest.mutate({ body }), [updateRequest], ); + const handleContentTypeChange = useCallback( async (contentType: string | null) => { - const headers = - contentType != null - ? activeRequest.headers.map((h) => - h.name.toLowerCase() === 'content-type' ? { ...h, value: contentType } : h, - ) - : activeRequest.headers; + const headers = activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type'); + + if (contentType != null) { + headers.push({ + name: 'Content-Type', + value: contentType, + enabled: true, + }); + } await updateRequest.mutateAsync({ headers }); // Force update header editor so any changed headers are reflected diff --git a/src-web/hooks/useIntrospectGraphQL.ts b/src-web/hooks/useIntrospectGraphQL.ts index c3051494..badc0852 100644 --- a/src-web/hooks/useIntrospectGraphQL.ts +++ b/src-web/hooks/useIntrospectGraphQL.ts @@ -80,10 +80,14 @@ export function useIntrospectGraphQL(baseRequest: HttpRequest) { setRefetchKey((k) => k + 1); }, []); - const schema = useMemo( - () => (introspection ? buildClientSchema(introspection) : undefined), - [introspection], - ); + const schema = useMemo(() => { + try { + 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 }; }