Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preview form data payload in a table #92

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/components/WebLogView/RequestDetails/FormPayloadPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { safeJsonParse } from '@/utils/json'
import { Table } from '@radix-ui/themes'

export function FormPayloadPreview({
payloadJsonString,
}: {
payloadJsonString: string
}) {
const contentObject = safeJsonParse<Record<string, string>>(payloadJsonString)

return (
<Table.Root size="1" variant="surface">
<Table.Header>
<Table.Row>
<Table.ColumnHeaderCell>Name</Table.ColumnHeaderCell>
<Table.ColumnHeaderCell>Value</Table.ColumnHeaderCell>
</Table.Row>
</Table.Header>

<Table.Body>
{contentObject &&
Object.entries(contentObject).map(([key, value]) => (
<Table.Row key={key}>
<Table.Cell>{key}</Table.Cell>
<Table.Cell>{value}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
7 changes: 7 additions & 0 deletions src/components/WebLogView/RequestDetails/Payload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { ProxyData } from '@/types'

import { ReadOnlyEditor } from '../../Monaco/ReadOnlyEditor'
import { parseParams } from './utils'
import { getContentType } from '@/utils/headers'
import { FormPayloadPreview } from './FormPayloadPreview'

export function Payload({ data }: { data: ProxyData }) {
const content = parseParams(data)
const contentType = getContentType(data.request?.headers ?? [])

if (!content) {
return (
Expand All @@ -16,5 +19,9 @@ export function Payload({ data }: { data: ProxyData }) {
)
}

if (contentType === 'application/x-www-form-urlencoded') {
return <FormPayloadPreview payloadJsonString={content} />
}

return <ReadOnlyEditor language="javascript" value={content} />
}
2 changes: 1 addition & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const launchProxy = (
return
}

const proxyData: ProxyData = safeJsonParse(data)
const proxyData = safeJsonParse<ProxyData>(data)
if (proxyData) {
browserWindow.webContents.send('proxy:data', proxyData)
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const setJsonObjectFromPath = (
value: string
) => {
const jsonObject = safeJsonParse(json)
set(jsonObject, path, value)
set(jsonObject ?? {}, path, value)
return JSON.stringify(jsonObject)
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function safeJsonParse(value: string) {
export function safeJsonParse<T extends object>(value: string) {
try {
return JSON.parse(value)
return JSON.parse(value) as T
} catch (error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the function would return any and typescript didn't recognize the return value could be undefined

console.error('Failed to parse JSON', error)
return undefined
Expand Down
Loading