Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(devex): return the json body of generated delete calls (#104415)

Julian Bez3ad9f699e53d
diff --git a/frontend/src/lib/api-orval-mutator.test.ts b/frontend/src/lib/api-orval-mutator.test.ts
new file mode 100644
index 000000000000..4c7d9d50b147
--- /dev/null
+++ b/frontend/src/lib/api-orval-mutator.test.ts
@@ -0,0 +1,21 @@
+import { apiMutator } from 'lib/api-orval-mutator'
+
+import { useMocks } from '~/mocks/jest'
+
+describe('apiMutator', () => {
+ it('returns the parsed body of a delete that answers with one', async () => {
+ useMocks({ delete: { '/api/projects/:team_id/probe/:id/': { status: 'cancelled', report_id: 7 } } })
+
+ const result = await apiMutator('/api/projects/1/probe/2/', { method: 'DELETE' })
+
+ expect(result).toEqual({ status: 'cancelled', report_id: 7 })
+ })
+
+ it('returns null for a delete with no content', async () => {
+ useMocks({ delete: { '/api/projects/:team_id/probe/:id/': () => [204] } })
+
+ const result = await apiMutator('/api/projects/1/probe/2/', { method: 'DELETE' })
+
+ expect(result).toBeNull()
+ })
+})
diff --git a/frontend/src/lib/api-orval-mutator.ts b/frontend/src/lib/api-orval-mutator.ts
index 22392ad18612..4140f96855d7 100644
--- a/frontend/src/lib/api-orval-mutator.ts
+++ b/frontend/src/lib/api-orval-mutator.ts
@@ -4,7 +4,7 @@
*
* Orval calls: apiMutator(url, { method, body, signal, ... })
*/
-import api from 'lib/api'
+import api, { getJSONFromSuccessResponse } from 'lib/api'
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
@@ -33,8 +33,13 @@ export const apiMutator = async <T>(url: string, options: RequestInit & { method
return api.put(url, data, apiOptions)
case 'PATCH':
return api.update(url, data, apiOptions)
- case 'DELETE':
- return api.delete(url)
+ case 'DELETE': {
+ // `api.delete` resolves the raw Response. A generated destroy call can still be typed
+ // with a body, because a delete endpoint may answer with a status envelope, so parse it
+ // the way every other verb does rather than handing the Response to the caller.
+ const response = await api.delete(url)
+ return await getJSONFromSuccessResponse(response, 'DELETE', url)
+ }
default:
throw new Error(`Unsupported HTTP method: ${method}`)
}
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index b7fc66d84460..0b572dd7b3a9 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -409,7 +409,7 @@ function apiErrorFallback(response: Response, method: string, url: string): stri
* with unparsable content) throws `ResponseBodyReadError`, so it can be recognized as wire-level
* noise and left out of error tracking.
*/
-async function getJSONFromSuccessResponse(response: Response, method: string, url: string): Promise<any> {
+export async function getJSONFromSuccessResponse(response: Response, method: string, url: string): Promise<any> {
const requestContext = (): string =>
`[${method} ${new URL(url, location.origin).pathname}] (status ${response.status})`
// A no-content response must not depend on reading its body: some engines (in our telemetry,