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

feat: use collapsable JSON viewer #100

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
76 changes: 40 additions & 36 deletions packages/trpc-panel/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
{
"name": "trpc-panel",
"version": "1.0.2",
"description": "UI for testing tRPC backends",
"main": "lib/index.js",
"module": "lib/index.mjs",
"typings": "lib/src/index.d.ts",
"scripts": {
"test": "jest",
"test:watch": "jest --watchAll",
"build": "npx rollup --bundleConfigAsCjs --config rollup.config.js",
"dev": "rollup --config rollup.config.js --watch --bundleConfigAsCjs"
},
"author": "",
"license": "ISC",
"exports": {
".": {
"types": "./lib/src/index.d.ts",
"import": "./lib/index.mjs",
"require": "./lib/index.js",
"default": "./lib/index.js"
}
},
"files": [
"lib/"
],
"directories": {
"lib": "lib"
},
"peerDependencies": {
"@trpc/server": "^10.0.0",
"zod": "^3.19.1"
"dependencies": {
"@textea/json-viewer": "^3.0.0",
"fuzzysort": "^2.0.4",
"path": "^0.12.7",
"pretty-bytes": "^6.1.0",
"pretty-ms": "^8.0.0",
"string-byte-length": "^1.6.0",
"url": "^0.11.0",
"zod-to-json-schema": "^3.20.0"
},
"description": "UI for testing tRPC backends",
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/preset-react": "^7.18.6",
Expand Down Expand Up @@ -87,10 +67,34 @@
"zod": "^3.19.1",
"zustand": "^4.1.5"
},
"dependencies": {
"fuzzysort": "^2.0.4",
"path": "^0.12.7",
"url": "^0.11.0",
"zod-to-json-schema": "^3.20.0"
}
"directories": {
"lib": "lib"
},
"exports": {
".": {
"types": "./lib/src/index.d.ts",
"import": "./lib/index.mjs",
"require": "./lib/index.js",
"default": "./lib/index.js"
}
},
"files": [
"lib/"
],
"license": "ISC",
"main": "lib/index.js",
"module": "lib/index.mjs",
"name": "trpc-panel",
"peerDependencies": {
"@trpc/server": "^10.0.0",
"zod": "^3.19.1"
},
"scripts": {
"build": "npx rollup --bundleConfigAsCjs --config rollup.config.js",
"dev": "rollup --config rollup.config.js --watch --bundleConfigAsCjs",
"test": "jest",
"test:watch": "jest --watchAll"
},
"typings": "lib/src/index.d.ts",
"version": "1.0.2"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import React from "react";
import { Response } from "./Response";
import json from "json-bigint";

export function RequestResult({ result }: { result: any }) {
return <Response>{`${json.stringify(result, null, 2)}`}</Response>;

export function RequestResult({
result,
size,
time,
}: {
result: any;
size?: number;
time?: number;
}) {
return (
<Response size={size} time={time}>
{result}
</Response>
);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import React from "react";
import { FormSection } from "./FormSection";
import { JsonViewer } from "@textea/json-viewer";
import prettyBytes from "pretty-bytes";
import prettyMs from "pretty-ms";

export function Response({
children,
size,
time,
}: {
children: string | object;
size?: number;
time?: number;
}) {
const title = size
? time
? `Response (${prettyBytes(size)}, ${prettyMs(time)})`
: `Response (${prettyBytes(size)})`
: time
? `Response (${prettyMs(time)})`
: `Response`;

if (typeof children === "object") {
return (
<FormSection title={title}>
<JsonViewer rootName={false} value={children} quotesOnKeys={false} />
</FormSection>
);
}

export function Response({ children }: { children: string }) {
return (
<FormSection title="Response">
<FormSection title={title}>
<p className="font-mono whitespace-pre-wrap break-words">{children}</p>
</FormSection>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { ParsedInputNode } from "@src/parse/parseNodeTypes";
import { DocumentationSection } from "@src/react-app/components/form/ProcedureForm/DescriptionSection";
import { Field } from "@src/react-app/components/form/Field";
import { ProcedureFormContextProvider } from "@src/react-app/components/form/ProcedureForm/ProcedureFormContext";
import getSize from "string-byte-length";

const TRPCErrorSchema = z.object({
shape: z.object({
Expand Down Expand Up @@ -54,9 +55,12 @@ export function ProcedureForm({
const [queryInput, setQueryInput] = useState<any>(null);
const formRef = useRef<HTMLFormElement | null>(null);
const context = trpc.useContext();
const [dataSize, setDataSize] = useState<number | undefined>();
const [startTime, setStartTime] = useState<number | undefined>();
const [opDuration, setOpDuration] = useState<number | undefined>();

function getProcedure() {
var cur: typeof trpc | typeof trpc[string] = trpc;
var cur: typeof trpc | (typeof trpc)[string] = trpc;
for (var p of procedure.pathFromRootRouter) {
// TODO - Maybe figure out these typings?
//@ts-ignore
Expand All @@ -73,6 +77,11 @@ export function ProcedureForm({
initialData: null,
retry: false,
refetchOnWindowFocus: false,
onSuccess: (data: unknown) => {
if (startTime) setOpDuration(Date.now() - startTime);
setDataSize(getSize(JSON.stringify(data)));
setStartTime(undefined);
},
});
})() as UseQueryResult<any>;

Expand All @@ -89,6 +98,11 @@ export function ProcedureForm({
//@ts-ignore
return router.useMutation({
retry: false,
onSuccess: (data: unknown) => {
if (startTime) setOpDuration(Date.now() - startTime);
setDataSize(getSize(JSON.stringify(data)));
setStartTime(undefined);
},
});
})() as UseMutationResult<any>;

Expand All @@ -104,8 +118,8 @@ export function ProcedureForm({
[ROOT_VALS_PROPERTY_NAME]: defaultFormValuesForNode(procedure.node),
},
});

function onSubmit(data: { [ROOT_VALS_PROPERTY_NAME]: any }) {
setStartTime(Date.now());
if (procedure.procedureType === "query") {
const newData = { ...data };
setQueryInput(newData[ROOT_VALS_PROPERTY_NAME]);
Expand Down Expand Up @@ -197,15 +211,17 @@ export function ProcedureForm({
</div>
</form>
<div className="flex flex-col space-y-4">
{data && <RequestResult result={data} />}
{data && (
<RequestResult result={data} size={dataSize} time={opDuration} />
)}
{!data && data !== null && (
<Response>Successful request but no data was returned</Response>
)}
{error &&
(isTrpcError(error) ? (
<Error error={error} />
) : (
<Response>{JSON.stringify(error)}</Response>
<Response>{error}</Response>
))}
</div>
</CollapsableSection>
Expand Down
Loading