From 278fb1dd407528b3f473bb9339d28b32ca3c2dd6 Mon Sep 17 00:00:00 2001 From: Kodai Aoyama Date: Thu, 17 Mar 2022 23:56:17 +0900 Subject: [PATCH] add xml pretty print and some fix --- app/pages/projects/[projectId].tsx | 30 +-- app/pages/stubs/[stubId].tsx | 263 ++++++++++++------------ app/projects/mutations/updateProject.ts | 2 +- app/stubs/mutations/updateStub.ts | 2 +- package-lock.json | 33 +++ package.json | 1 + 6 files changed, 189 insertions(+), 142 deletions(-) diff --git a/app/pages/projects/[projectId].tsx b/app/pages/projects/[projectId].tsx index 96e66c1..f62be98 100644 --- a/app/pages/projects/[projectId].tsx +++ b/app/pages/projects/[projectId].tsx @@ -77,7 +77,7 @@ const UpdatedInfo = ({ stub, project }) => { return ( <> {project.updatedBy} - {project.updatedAt.toLocaleString()} + {project.updatedAt.toLocaleString()} ) } @@ -85,14 +85,14 @@ const UpdatedInfo = ({ stub, project }) => { return ( <> {stub.stubs[0].updatedBy} - {stub.stubs[0].updatedAt.toLocaleString()} + {stub.stubs[0].updatedAt.toLocaleString()} ) } else { return ( <> {project.updatedBy} - {project.updatedAt.toLocaleString()} + {project.updatedAt.toLocaleString()} ) } @@ -181,8 +181,8 @@ export const Project = () => { createdBy createdAt updatedBy - updatedAt - memo + updatedAt + {project.memo !== "" && memo} @@ -191,15 +191,17 @@ export const Project = () => { {project.createdBy} {project.createdAt.toLocaleString()} - - {project.memo} - + {project.memo !== "" && ( + + {project.memo} + + )} diff --git a/app/pages/stubs/[stubId].tsx b/app/pages/stubs/[stubId].tsx index 38d9ac6..3302e68 100644 --- a/app/pages/stubs/[stubId].tsx +++ b/app/pages/stubs/[stubId].tsx @@ -18,6 +18,7 @@ import getProject from "app/projects/queries/getProject" import { DeleteIcon, EditIcon } from "@chakra-ui/icons" import { HiOutlineClipboardCopy } from "react-icons/hi" import { CopyToClipboard } from "react-copy-to-clipboard" +import formatXml from "xml-formatter" const BreadCrumb = ({ stub }) => { const [project] = useQuery(getProject, { id: stub.projectId }) @@ -107,137 +108,144 @@ export const Stub = () => { DELETE - - - - - - path - - - createdBy - - - createdAt - - - updatedBy - - - updatedAt - - - method - - - contentType - - - statusCode - - {stub.sleep !== 0 && ( - <> - - sleep - - - )} - {stub.ntimesError !== 0 && ( - <> - - ntimesError - - - ntimesErrorStatusCode - - - )} - {stub.memo !== "" && ( - - memo + + + + + path + + + createdBy + + + createdAt + + + updatedBy + + + updatedAt + + + method + + + contentType + + + statusCode + + {stub.sleep !== 0 && ( + <> + + sleep - )} - response - - - - - - {stub.path} - - - {stub.createdBy} - - - {stub.createdAt.toLocaleString()} - - - {stub.updatedBy} - - - {stub.updatedAt.toLocaleString()} - - - {stub.method} - - - {stub.contentType} - - - {stub.statusCode} + + )} + {stub.ntimesError !== 0 && ( + <> + + ntimesError + + + ntimesErrorStatusCode + + + )} + {stub.memo !== "" && ( + + memo - {stub.sleep !== 0 && ( - <> - - {stub.sleep} s - - - )} - {stub.ntimesError !== 0 && ( - <> - - {stub.ntimesError} times - - - {stub.ntimesErrorStatusCode} - - - )} - {stub.memo !== "" && ( - - {stub.memo} + )} + response + + + + + + {stub.path} + + + {stub.createdBy} + + + {stub.createdAt.toLocaleString()} + + + {stub.updatedBy} + + + {stub.updatedAt.toLocaleString()} + + + {stub.method} + + + {stub.contentType} + + + {stub.statusCode} + + {stub.sleep !== 0 && ( + <> + + {stub.sleep} s - )} - + )} + {stub.ntimesError !== 0 && ( + <> + + {stub.ntimesError} times + + + {stub.ntimesErrorStatusCode} + + + )} + {stub.memo !== "" && ( + -
-                      {(() => {
-                        try {
+                    {stub.memo}
+                  
+                )}
+                
+                  
+                    {(() => {
+                      try {
+                        if (
+                          stub.contentType === "application/json" ||
+                          stub.contentType === "text/json"
+                        ) {
                           return JSON.stringify(JSON.parse(stub.response), null, 2)
-                        } catch {
+                        } else if (stub.contentType === "application/xml") {
+                          return formatXml(stub.response, { lineSeparator: "\n" })
+                        } else {
                           return stub.response
                         }
-                      })()}
-                    
-
- - - - + } catch { + return stub.response + } + })()} +
+
+
+
+
Recent Requests @@ -255,9 +263,12 @@ export const Stub = () => { .split("\t") .filter((s) => s !== "") .map((l, i) => ( -
-                      {i !== 0 ? `\n${l}` : l}
-                    
+ <> +
+                        {i !== 0 ? `\n${l}` : l}
+                      
+
+ )) : "Never been requested..."}
diff --git a/app/projects/mutations/updateProject.ts b/app/projects/mutations/updateProject.ts index efc3b84..d8f4090 100644 --- a/app/projects/mutations/updateProject.ts +++ b/app/projects/mutations/updateProject.ts @@ -9,7 +9,7 @@ const UpdateProject = z.object({ basePath: z .string() .regex(/^\/[^\/]+$/, { message: "The only slash allowed is at the beginning of a basePath" }), - memo: z.string(), + memo: z.string().default(""), }) export default resolver.pipe( diff --git a/app/stubs/mutations/updateStub.ts b/app/stubs/mutations/updateStub.ts index 3921541..0ec8dcf 100644 --- a/app/stubs/mutations/updateStub.ts +++ b/app/stubs/mutations/updateStub.ts @@ -29,7 +29,7 @@ const UpdateStub = z.object({ ntimesErrorStatusCode: z .string() .regex(/^\d{3}$/, { message: "The ntimes error status code must be a three-digit number." }), - memo: z.string(), + memo: z.string().default(""), }) export default resolver.pipe( diff --git a/package-lock.json b/package-lock.json index 913977c..9566e92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "react-dom": "alpha", "react-final-form": "6.5.8", "react-icons": "4.3.1", + "xml-formatter": "2.6.1", "zod": "3.12.0" }, "devDependencies": { @@ -18178,11 +18179,30 @@ } } }, + "node_modules/xml-formatter": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/xml-formatter/-/xml-formatter-2.6.1.tgz", + "integrity": "sha512-dOiGwoqm8y22QdTNI7A+N03tyVfBlQ0/oehAzxIZtwnFAHGeSlrfjF73YQvzSsa/Kt6+YZasKsrdu6OIpuBggw==", + "dependencies": { + "xml-parser-xo": "^3.2.0" + }, + "engines": { + "node": ">= 10" + } + }, "node_modules/xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", "integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==" }, + "node_modules/xml-parser-xo": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/xml-parser-xo/-/xml-parser-xo-3.2.0.tgz", + "integrity": "sha512-8LRU6cq+d7mVsoDaMhnkkt3CTtAs4153p49fRo+HIB3I1FD1o5CeXRjRH29sQevIfVJIcPjKSsPU/+Ujhq09Rg==", + "engines": { + "node": ">= 10" + } + }, "node_modules/xmlchars": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", @@ -32270,11 +32290,24 @@ "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.5.tgz", "integrity": "sha512-BAkMFcAzl8as1G/hArkxOxq3G7pjUqQ3gzYbLL0/5zNkph70e+lCoxBGnm6AW1+/aiNeV4fnKqZ8m4GZewmH2w==" }, + "xml-formatter": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/xml-formatter/-/xml-formatter-2.6.1.tgz", + "integrity": "sha512-dOiGwoqm8y22QdTNI7A+N03tyVfBlQ0/oehAzxIZtwnFAHGeSlrfjF73YQvzSsa/Kt6+YZasKsrdu6OIpuBggw==", + "requires": { + "xml-parser-xo": "^3.2.0" + } + }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", "integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==" }, + "xml-parser-xo": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/xml-parser-xo/-/xml-parser-xo-3.2.0.tgz", + "integrity": "sha512-8LRU6cq+d7mVsoDaMhnkkt3CTtAs4153p49fRo+HIB3I1FD1o5CeXRjRH29sQevIfVJIcPjKSsPU/+Ujhq09Rg==" + }, "xmlchars": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", diff --git a/package.json b/package.json index 9e87601..bb3acaf 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "react-dom": "alpha", "react-final-form": "6.5.8", "react-icons": "4.3.1", + "xml-formatter": "2.6.1", "zod": "3.12.0" }, "devDependencies": {