mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-25 21:04:04 +02:00
Address response bodies by response id instead of a filesystem path (#550)
This commit is contained in:
@@ -5,7 +5,6 @@ import type { GraphQLSchema, IntrospectionQuery } from "graphql";
|
||||
import { buildClientSchema, getIntrospectionQuery } from "graphql";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { minPromiseMillis } from "../lib/minPromiseMillis";
|
||||
import { getResponseBodyText } from "../lib/responseBody";
|
||||
import { sendEphemeralRequest } from "../lib/sendEphemeralRequest";
|
||||
import { useActiveEnvironment } from "./useActiveEnvironment";
|
||||
import { useDebouncedValue } from "@yaakapp-internal/ui";
|
||||
@@ -55,7 +54,7 @@ export function useIntrospectGraphQL(
|
||||
bodyType: "application/json",
|
||||
body: { text: introspectionRequestBody },
|
||||
};
|
||||
const response = await minPromiseMillis(
|
||||
const { response, body } = await minPromiseMillis(
|
||||
sendEphemeralRequest(args, activeEnvironment?.id ?? null),
|
||||
700,
|
||||
);
|
||||
@@ -64,14 +63,16 @@ export function useIntrospectGraphQL(
|
||||
return setError(response.error);
|
||||
}
|
||||
|
||||
const bodyText = await getResponseBodyText({ response, filter: null });
|
||||
// The send hands back the only copy of the body — an unsaved response has
|
||||
// nothing on disk and no row to read it back from
|
||||
const bodyText = new TextDecoder("utf-8").decode(new Uint8Array(body));
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
return setError(
|
||||
`Request failed with status ${response.status}.\nThe response text is:\n\n${bodyText}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (bodyText === null) {
|
||||
if (bodyText === "") {
|
||||
return setError("Empty body returned in response");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { HttpResponse } from "@yaakapp-internal/models";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
|
||||
/**
|
||||
* A URL for a stored response body, for the viewers that hand one to an element
|
||||
* instead of reading the bytes themselves.
|
||||
*
|
||||
* Resolved once here rather than inside each viewer: the host may have to ask
|
||||
* the backend where the body is, and a viewer that computes its source during
|
||||
* render (the PDF one, deliberately) needs it settled before it mounts.
|
||||
*
|
||||
* Null data means the response has no stored body.
|
||||
*/
|
||||
export function useResponseBodyUrl(response: HttpResponse | null) {
|
||||
const responseId = response?.id ?? null;
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["response_body_url", responseId, response?.updatedAt ?? ""],
|
||||
enabled: responseId != null,
|
||||
// A response body is stored under the response's own id
|
||||
queryFn: () => (responseId == null ? null : platform.blobs.url(responseId)),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user