Skip to content

Commit 8578983

Browse files
authored
chore: refactor RPC interaction module (#830)
* chore: refactor RPC interaction module * chore: fix error * chore: fix build error * chore: move dir and sidebar position
1 parent 550c238 commit 8578983

File tree

159 files changed

+9904
-20311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+9904
-20311
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ yarn-debug.log*
2020
yarn-error.log*
2121

2222
.vscode/
23-
.idea
23+
.idea
24+
/docs/core/build/json-rpc/rpcs
25+
/docs/espace/build/rpcs

docusaurus.config.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,27 @@ function getConfig() {
8383
},
8484
],
8585
[
86-
"./plugins/openrpc/index.ts",
86+
"./plugins/docusaurus-openrpc/src/index.ts",
8787
{
88-
id: "cspace",
89-
path: "/docs/core/rpc",
90-
openrpcDocument: "./plugins/openrpc/src/rpc/cfx.json", // path or url to openrpc document.
91-
defaultEndpoint: "https://test.confluxrpc.com",
88+
id: "coreSpace",
89+
openRPCPath: "./plugins/docusaurus-openrpc/src/rpcs/cfx.json",
90+
outputPath: "./docs/core/build/json-rpc/rpcs",
91+
category: {
92+
label: "Core Space JSON RPC(Interactive)",
93+
position: 7
94+
},
9295
},
9396
],
9497
[
95-
"./plugins/openrpc/index.ts",
98+
"./plugins/docusaurus-openrpc/src/index.ts",
9699
{
97-
id: "espace",
98-
path: "/docs/espace/rpc",
99-
openrpcDocument: "./plugins/openrpc/src/rpc/Eth.json", // path or url to openrpc document.
100-
defaultEndpoint: "https://evmtestnet.confluxrpc.com",
100+
id: "eSpace",
101+
openRPCPath: "./plugins/docusaurus-openrpc/src/rpcs/Eth.json",
102+
outputPath: "./docs/espace/build/rpcs",
103+
category: {
104+
label: "eSpace JSON RPC(Interactive)",
105+
position: 5
106+
},
101107
},
102108
],
103109
],

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
"node": ">=18.0"
5252
},
5353
"workspaces": [
54-
"./plugins/openrpc"
54+
"./plugins/openrpc",
55+
"./plugins/docusaurus-openrpc"
56+
5557
]
5658
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "docusaurus-openrpc",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "./lib/index.js",
6+
"scripts": {
7+
"build": "tsc && node copyUntypedFiles.mjs",
8+
"build:watch": "tsc --watch"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "MIT",
13+
"devDependencies": {
14+
"@docusaurus/module-type-aliases": "3.6.1",
15+
"@docusaurus/types": "3.6.1",
16+
"@types/mustache": "^4.2.5",
17+
"@types/node": "^22.9.0",
18+
"@types/react": "^18.3.12",
19+
"fs-extra": "^11.2.0",
20+
"typescript": "~5.6.2"
21+
},
22+
"dependencies": {
23+
"@docusaurus/utils-validation": "^3.6.1",
24+
"@open-rpc/meta-schema": "^1.14.9",
25+
"@open-rpc/schema-utils-js": "^2.0.5",
26+
"@rjsf/bootstrap-4": "^5.23.2",
27+
"@rjsf/core": "^5.23.2",
28+
"@rjsf/utils": "^5.23.2",
29+
"@rjsf/validator-ajv8": "^5.23.2",
30+
"bootstrap": "4.6.2",
31+
"framer-motion": "^5",
32+
"json-schema-typed": "^8.0.1",
33+
"mustache": "^4.2.0",
34+
"react-bootstrap": "^1.6.5"
35+
}
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.json-schema-viewer {
2+
background-color: inherit;
3+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
ContentDescriptorObject,
3+
JSONSchema,
4+
MethodObject,
5+
MethodObjectParams,
6+
Servers,
7+
} from "@open-rpc/meta-schema";
8+
import React from "react";
9+
import Params from "../Params";
10+
import Examples from "../Examples";
11+
import "./index.css";
12+
import { Result } from "../Result";
13+
import Playground from "../Playground";
14+
15+
import "bootstrap/dist/css/bootstrap.min.css";
16+
interface Props {
17+
method: MethodObject;
18+
servers: { name: string; url: string }[];
19+
}
20+
21+
const Content = ({ method, servers }: Props) => {
22+
return (
23+
<div>
24+
<p>{method.summary}</p>
25+
<Playground
26+
method={method}
27+
servers={servers.filter((serve) => serve.url.startsWith("http"))}
28+
/>
29+
<Params params={method.params} />
30+
<Result result={method.result} />
31+
<Examples examples={method.examples} method={method} />
32+
</div>
33+
);
34+
};
35+
36+
export default Content;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.example_card {
2+
margin-bottom: 10px;
3+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, { useCallback } from "react";
2+
import {
3+
ContentDescriptorObject,
4+
ExamplePairingObjectParams,
5+
MethodObject,
6+
MethodObjectName,
7+
} from "@open-rpc/meta-schema";
8+
import Tabs from "@theme-original/Tabs";
9+
import TabItem from "@theme-original/TabItem";
10+
import CodeBlock from "@theme-original/CodeBlock";
11+
import "./index.css";
12+
interface Props {
13+
examples: MethodObject["examples"];
14+
method: MethodObject;
15+
}
16+
17+
function Examples({ examples, method }: Props) {
18+
if (typeof examples === "undefined" || examples.length === 0) return null;
19+
const formatParams = useCallback(
20+
(params: ExamplePairingObjectParams) => {
21+
if (method.paramStructure === "by-name") {
22+
return params.reduce((acc, param) => {
23+
if ("$ref" in param) return acc;
24+
acc[param.name] = param.value;
25+
return acc;
26+
}, {} as Record<MethodObjectName, string>);
27+
}
28+
return params
29+
.map((param) => {
30+
if ("$ref" in param) return null;
31+
return param.value;
32+
})
33+
.filter(Boolean);
34+
},
35+
[method.paramStructure]
36+
);
37+
return (
38+
<div>
39+
<h2>{examples.length > 1 ? "Examples" : "Example"}</h2>
40+
41+
<Tabs>
42+
{examples.map((example, key) => {
43+
if ("$ref" in example) return null;
44+
return (
45+
<TabItem
46+
key={key}
47+
value={key}
48+
label={<span className="text--normal">{example.name}</span>}
49+
>
50+
<div className="card example_card">
51+
<div className="card__header">
52+
<h3>Request</h3>
53+
</div>
54+
<div className="card__body">
55+
<CodeBlock language="js">{`curl -X POST --data \\
56+
'${JSON.stringify(
57+
{
58+
jsonrpc: "2.0",
59+
id: 1,
60+
method: method.name,
61+
params: formatParams(example.params),
62+
},
63+
null,
64+
4
65+
)}' \\
66+
-H "Content-Type: application/json" \\
67+
localhost:12539`}</CodeBlock>
68+
</div>
69+
</div>
70+
71+
<div className="card example_card">
72+
<div className="card__header">
73+
<h3>Response</h3>
74+
</div>
75+
<div className="card__body">
76+
<CodeBlock language="js">
77+
{example.result &&
78+
!("$ref" in example.result) &&
79+
JSON.stringify(example.result.value, null, 2)}
80+
</CodeBlock>
81+
</div>
82+
</div>
83+
</TabItem>
84+
);
85+
})}
86+
</Tabs>
87+
</div>
88+
);
89+
}
90+
91+
export default Examples;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from "react"
2+
import Translate from "@docusaurus/Translate"
3+
4+
import { TypeLabel, FalseLabel } from "../labels"
5+
6+
// When schema has the value "true", it means that it is ALWAYS valid
7+
export default function CreateAlwaysValid(): JSX.Element {
8+
return (
9+
<>
10+
<TypeLabel />
11+
&nbsp;&#58;&nbsp;
12+
<FalseLabel />
13+
<div style={{ marginTop: "var(--ifm-table-cell-padding)" }}>
14+
<Translate
15+
values={{
16+
id: "json-schema.labels.false",
17+
}}
18+
>
19+
{"Always invalid"}
20+
</Translate>
21+
</div>
22+
</>
23+
)
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from "react"
2+
import Translate from "@docusaurus/Translate"
3+
4+
import { TypeLabel, TrueLabel } from "../labels"
5+
import { useJSVOptionsContext } from "../contexts"
6+
import { QualifierMessages } from "../utils"
7+
import { CreateDescription } from "../JSONSchemaElements"
8+
9+
import type { JSONSchema } from "../types"
10+
11+
type Props = {
12+
[x: string]: any
13+
schema: JSONSchema
14+
}
15+
16+
// When schema has the value "true" or empty object, it means that it is ALWAYS valid
17+
export default function CreateAlwaysValid({ schema }: Props): JSX.Element {
18+
const options = useJSVOptionsContext()
19+
const notBoolean = typeof schema !== "boolean"
20+
const description = notBoolean ? schema.description : undefined
21+
22+
return (
23+
<>
24+
<TypeLabel />
25+
&nbsp;&#58;&nbsp;
26+
<TrueLabel />
27+
<div style={{ marginTop: "var(--ifm-table-cell-padding)" }}>
28+
<Translate
29+
values={{
30+
id: "json-schema.labels.true",
31+
}}
32+
>
33+
{"Always valid"}
34+
</Translate>
35+
</div>
36+
{notBoolean && (
37+
<div style={{ marginTop: "var(--ifm-table-cell-padding)" }}>
38+
<QualifierMessages schema={schema} options={options} />
39+
</div>
40+
)}
41+
{description !== undefined && (
42+
<CreateDescription description={description} />
43+
)}
44+
</>
45+
)
46+
}

0 commit comments

Comments
 (0)