Fix body change setting headers/method

This commit is contained in:
Gregory Schier
2024-08-09 08:33:49 -07:00
parent 0c9d532c1f
commit 6232a46ca8
4 changed files with 43 additions and 15 deletions

View File

@@ -32,6 +32,7 @@ import { BinaryFileEditor } from './BinaryFileEditor';
import { CountBadge } from './core/CountBadge'; import { CountBadge } from './core/CountBadge';
import { Editor } from './core/Editor'; import { Editor } from './core/Editor';
import type { GenericCompletionOption } from './core/Editor/genericCompletion'; import type { GenericCompletionOption } from './core/Editor/genericCompletion';
import { InlineCode } from './core/InlineCode';
import type { TabItem } from './core/Tabs/Tabs'; import type { TabItem } from './core/Tabs/Tabs';
import { TabContent, Tabs } from './core/Tabs/Tabs'; import { TabContent, Tabs } from './core/Tabs/Tabs';
import { EmptyStateText } from './EmptyStateText'; import { EmptyStateText } from './EmptyStateText';
@@ -39,6 +40,7 @@ import { FormMultipartEditor } from './FormMultipartEditor';
import { FormUrlencodedEditor } from './FormUrlencodedEditor'; import { FormUrlencodedEditor } from './FormUrlencodedEditor';
import { GraphQLEditor } from './GraphQLEditor'; import { GraphQLEditor } from './GraphQLEditor';
import { HeadersEditor } from './HeadersEditor'; import { HeadersEditor } from './HeadersEditor';
import { useToast } from './ToastContext';
import { UrlBar } from './UrlBar'; import { UrlBar } from './UrlBar';
import { UrlParametersEditor } from './UrlParameterEditor'; import { UrlParametersEditor } from './UrlParameterEditor';
@@ -67,6 +69,7 @@ export const RequestPane = memo(function RequestPane({
const handleContentTypeChange = useCallback( const handleContentTypeChange = useCallback(
async (contentType: string | null) => { async (contentType: string | null) => {
console.log('UPDATE CONTENT TYPE', contentType);
const headers = activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type'); const headers = activeRequest.headers.filter((h) => h.name.toLowerCase() !== 'content-type');
if (contentType != null) { if (contentType != null) {
@@ -84,6 +87,8 @@ export const RequestPane = memo(function RequestPane({
[activeRequest.headers, activeRequestId, updateRequest], [activeRequest.headers, activeRequestId, updateRequest],
); );
const toast = useToast();
const tabs: TabItem[] = useMemo( const tabs: TabItem[] = useMemo(
() => [ () => [
{ {
@@ -104,25 +109,44 @@ export const RequestPane = memo(function RequestPane({
{ label: 'No Body', shortLabel: 'Body', value: BODY_TYPE_NONE }, { label: 'No Body', shortLabel: 'Body', value: BODY_TYPE_NONE },
], ],
onChange: async (bodyType) => { onChange: async (bodyType) => {
if (bodyType === activeRequest.bodyType) return;
const showMethodToast = (newMethod: string) => {
if (activeRequest.method.toLowerCase() === newMethod.toLowerCase()) return;
toast.show({
id: 'switched-method',
message: (
<>
Request method switched to <InlineCode>POST</InlineCode>
</>
),
});
};
const patch: Partial<HttpRequest> = { bodyType }; const patch: Partial<HttpRequest> = { bodyType };
let newContentType: string | null | undefined; let newContentType: string | null | undefined;
if (bodyType === BODY_TYPE_NONE) { if (bodyType === BODY_TYPE_NONE) {
newContentType = null; newContentType = null;
} else if ( } else if (
activeRequest.method.toLowerCase() !== 'put' && bodyType === BODY_TYPE_FORM_URLENCODED ||
activeRequest.method.toLowerCase() !== 'patch' && bodyType === BODY_TYPE_FORM_MULTIPART ||
activeRequest.method.toLowerCase() !== 'post' && bodyType === BODY_TYPE_JSON ||
(bodyType === BODY_TYPE_FORM_URLENCODED || bodyType === BODY_TYPE_OTHER ||
bodyType === BODY_TYPE_FORM_MULTIPART || bodyType === BODY_TYPE_XML
bodyType === BODY_TYPE_JSON ||
bodyType === BODY_TYPE_OTHER ||
bodyType === BODY_TYPE_XML)
) { ) {
patch.method = 'POST'; const isDefaultishRequest =
activeRequest.bodyType === BODY_TYPE_NONE &&
activeRequest.method.toLowerCase() === 'get';
const requiresPost = bodyType === BODY_TYPE_FORM_MULTIPART;
if (isDefaultishRequest || requiresPost) {
patch.method = 'POST';
showMethodToast(patch.method);
}
newContentType = bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType; newContentType = bodyType === BODY_TYPE_OTHER ? 'text/plain' : bodyType;
} else if (bodyType == BODY_TYPE_GRAPHQL) { } else if (bodyType == BODY_TYPE_GRAPHQL) {
patch.method = 'POST'; patch.method = 'POST';
newContentType = 'application/json'; newContentType = 'application/json';
showMethodToast(patch.method);
} }
await updateRequest.mutateAsync({ id: activeRequestId, update: patch }); await updateRequest.mutateAsync({ id: activeRequestId, update: patch });
@@ -191,6 +215,7 @@ export const RequestPane = memo(function RequestPane({
activeRequest.urlParameters, activeRequest.urlParameters,
activeRequestId, activeRequestId,
handleContentTypeChange, handleContentTypeChange,
toast,
updateRequest, updateRequest,
], ],
); );

View File

@@ -55,7 +55,6 @@ export const placeholders = function (variables: { name: string }[]) {
} }
const isFunction = groupMatch.includes('('); const isFunction = groupMatch.includes('(');
console.log('VAIRABLES', variables, groupMatch);
return Decoration.replace({ return Decoration.replace({
inclusive: true, inclusive: true,
widget: new PlaceholderWidget( widget: new PlaceholderWidget(

View File

@@ -15,10 +15,11 @@ export interface ToastProps {
className?: string; className?: string;
timeout: number | null; timeout: number | null;
action?: ReactNode; action?: ReactNode;
variant?: 'copied' | 'success' | 'info' | 'warning' | 'error'; variant?: 'custom' | 'copied' | 'success' | 'info' | 'warning' | 'error';
} }
const ICONS: Record<NonNullable<ToastProps['variant']>, IconProps['icon']> = { const ICONS: Record<NonNullable<ToastProps['variant']>, IconProps['icon'] | null> = {
custom: null,
copied: 'copyCheck', copied: 'copyCheck',
warning: 'alert', warning: 'alert',
error: 'alert', error: 'alert',
@@ -33,7 +34,7 @@ export function Toast({
onClose, onClose,
timeout, timeout,
action, action,
variant, variant = 'info',
}: ToastProps) { }: ToastProps) {
useKey( useKey(
'Escape', 'Escape',
@@ -45,6 +46,8 @@ export function Toast({
[open], [open],
); );
const icon = variant in ICONS && ICONS[variant];
return ( return (
<motion.div <motion.div
initial={{ opacity: 0, right: '-10%' }} initial={{ opacity: 0, right: '-10%' }}
@@ -65,9 +68,9 @@ export function Toast({
)} )}
> >
<div className="px-3 py-3 flex items-center gap-2"> <div className="px-3 py-3 flex items-center gap-2">
{variant != null && ( {icon && (
<Icon <Icon
icon={ICONS[variant]} icon={icon}
className={classNames( className={classNames(
variant === 'success' && 'text-fg-success', variant === 'success' && 'text-fg-success',
variant === 'warning' && 'text-fg-warning', variant === 'warning' && 'text-fg-warning',

View File

@@ -27,6 +27,7 @@ export function useNotificationToast() {
id: payload.id, id: payload.id,
timeout: null, timeout: null,
message: payload.message, message: payload.message,
variant: 'custom',
onClose: () => markRead(payload.id), onClose: () => markRead(payload.id),
action: action:
actionLabel && actionUrl ? ( actionLabel && actionUrl ? (