Skip to content

Commit e3aaa74

Browse files
authored
Merge pull request #17 from open-rpc/fix/add-server-variables
fix: add server variables
2 parents cd89a54 + 187ff5d commit e3aaa74

File tree

5 files changed

+100
-36
lines changed

5 files changed

+100
-36
lines changed

jest.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ module.exports = {
66
"rootDir": "./src",
77
"testEnvironment": "jsdom",
88
"testPathIgnorePatterns": ["./build"],
9-
"preset": "ts-jest",
10-
"files": ["src/**/*.tsx"]
9+
"preset": "ts-jest"
1110
}

src/Documentation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class Documentation extends React.Component<IProps> {
2323
return (
2424
<>
2525
<Info schema={schema} />
26-
<Servers servers={schema.servers} />
26+
<Servers servers={schema.servers} reactJsonOptions={reactJsonOptions}/>
2727
<Methods schema={schema} uiSchema={uiSchema} reactJsonOptions={reactJsonOptions}/>
2828
<ContentDescriptors schema={schema} uiSchema={uiSchema}></ContentDescriptors>
2929
</>

src/Links/Links.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Links extends Component<IProps> {
4747
<TableRow>
4848
<TableCell colSpan={6}>
4949
{links.map((link, i) => (
50-
<div style={{ width: "100%" }}>
50+
<div style={{ width: "100%" }} key={i}>
5151
<ExpansionPanel
5252
style={{ width: "100%" }} defaultExpanded={uiSchema && uiSchema.links["ui:defaultExpanded"]} key={i}>
5353
<ExpansionPanelSummary
@@ -63,7 +63,8 @@ class Links extends Component<IProps> {
6363
{link.params && <ReactJson src={link.params} {...reactJsonOptions} />}
6464
{link.server &&
6565
<Typography variant="h6" gutterBottom className={classes.paramsMargin}>Server</Typography>}
66-
{link.server && <Servers servers={[link.server]} noTitle={true}/>}
66+
{link.server && <Servers
67+
servers={[link.server]} noTitle={true} reactJsonOptions={reactJsonOptions} />}
6768
</ExpansionPanelDetails>
6869
</ExpansionPanel>
6970
</div>

src/Servers/Servers.test.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,48 @@ it("renders schema servers", () => {
3737
name: "Pet Store",
3838
url: "http://petstore.openrpc.io/api",
3939
},
40-
]
40+
];
4141
ReactDOM.render(<Servers servers={servers} />, div);
4242
expect(div.innerHTML.includes("Pet Store")).toBe(true);
43-
expect(div.innerHTML.includes('href="http://petstore.openrpc.io/api"')).toBe(true);
43+
expect(div.innerHTML.includes("http://petstore.openrpc.io/api")).toBe(true);
4444
expect(div.innerHTML.includes("foobar")).toBe(true);
4545
ReactDOM.unmountComponentAtNode(div);
4646
});
47+
48+
it("renders schema servers with variables", () => {
49+
const div = document.createElement("div");
50+
const servers = [
51+
{
52+
description: "foobar",
53+
name: "Pet Store",
54+
url: "http://{username}.open-rpc.org:{port}/{basePath}/{exampleName}",
55+
variables: {
56+
basePath: {
57+
default: "jsonrpc",
58+
},
59+
exampleName: {
60+
default: "petstore",
61+
},
62+
port: {
63+
default: "443",
64+
enum: [
65+
"8545",
66+
"443",
67+
],
68+
},
69+
username: {
70+
default: "demo",
71+
description: "this is applied to the url as the subdomain",
72+
},
73+
},
74+
},
75+
];
76+
ReactDOM.render(<Servers servers={servers} />, div);
77+
expect(div.innerHTML.includes("open-rpc.org")).toBe(true);
78+
expect(div.innerHTML.includes("petstore")).toBe(true);
79+
expect(div.innerHTML.includes("jsonrpc")).toBe(true);
80+
expect(div.innerHTML.includes("443")).toBe(true);
81+
expect(div.innerHTML.includes("8545")).toBe(true);
82+
expect(div.innerHTML.includes("demo")).toBe(true);
83+
ReactDOM.unmountComponentAtNode(div);
84+
});

src/Servers/Servers.tsx

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,77 @@
11
import React, { Component } from "react";
2-
import { withStyles } from "@material-ui/core/styles";
3-
import Link from "@material-ui/core/Link";
4-
import Table from "@material-ui/core/Table";
5-
import TableBody from "@material-ui/core/TableBody";
2+
import { withStyles, WithStyles, Theme } from "@material-ui/core/styles";
63
import TableCell from "@material-ui/core/TableCell";
7-
import TableHead from "@material-ui/core/TableHead";
84
import TableRow from "@material-ui/core/TableRow";
9-
import { Typography } from "@material-ui/core";
5+
import ReactMarkdown from "react-markdown";
6+
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
7+
import { Typography, ExpansionPanelSummary, ExpansionPanelDetails, ExpansionPanel } from "@material-ui/core";
108
import { types } from "@open-rpc/meta-schema";
9+
import ReactJson from "react-json-view";
10+
import ExpansionTable from "../ExpansionTable/ExpansionTable";
1111

12-
interface IProps {
12+
const styles = (theme: Theme) => ({
13+
heading: {
14+
flexBasis: "33.33%",
15+
flexShrink: 0,
16+
fontSize: theme.typography.pxToRem(15),
17+
},
18+
paramsMargin: {
19+
marginTop: theme.spacing.unit,
20+
},
21+
secondaryHeading: {
22+
alignSelf: "end",
23+
color: theme.palette.text.secondary,
24+
fontSize: theme.typography.pxToRem(15),
25+
},
26+
});
27+
28+
interface IProps extends WithStyles<typeof styles> {
1329
servers?: types.ServerObject[];
30+
uiSchema?: any;
31+
reactJsonOptions?: any;
1432
noTitle?: boolean;
1533
}
1634

1735
class Servers extends Component<IProps> {
1836
public render() {
19-
const { servers, noTitle } = this.props;
37+
const { servers, noTitle, reactJsonOptions, uiSchema, classes } = this.props;
2038
if (!servers || servers.length === 0) {
2139
return null;
2240
}
2341
return (
2442
<>
2543
{noTitle ? null : <Typography variant="h2">Servers</Typography>}
26-
<Table>
27-
<TableHead>
28-
<TableRow>
29-
<TableCell>Name</TableCell>
30-
<TableCell align="right">Url</TableCell>
31-
<TableCell align="right">Description</TableCell>
32-
</TableRow>
33-
</TableHead>
34-
<TableBody>
35-
{servers.map((server) => (
36-
<TableRow key={server.url}>
37-
<TableCell component="th" scope="row">
38-
{server.name}
39-
</TableCell>
40-
<TableCell align="right"><Link href={server.url}>{server.url}</Link></TableCell>
41-
<TableCell align="right">{server.description}</TableCell>
42-
</TableRow>
43-
))}
44-
</TableBody>
45-
</Table>
44+
<ExpansionTable headers={["Name", "Url", "Summary"]}>
45+
<TableRow>
46+
<TableCell colSpan={6}>
47+
{servers.map((server, i) => (
48+
<div style={{ width: "100%" }} key={i}>
49+
<ExpansionPanel
50+
style={{ width: "100%" }}
51+
defaultExpanded={uiSchema && uiSchema.servers["ui:defaultExpanded"]} key={i}>
52+
<ExpansionPanelSummary
53+
style={{ justifyContent: "space-between" }} key="servers-header" expandIcon={<ExpandMoreIcon />}>
54+
<div style={{ display: "flex", justifyContent: "space-between", width: "100%", height: "100%" }}>
55+
<Typography className={classes.heading}>{server.name}</Typography>
56+
<Typography className={classes.secondaryHeading}>{server.url}</Typography>
57+
<Typography className={classes.secondaryHeading}>{server.summary}</Typography>
58+
</div>
59+
</ExpansionPanelSummary>
60+
<ExpansionPanelDetails style={{ display: "block" }} key="servers-body">
61+
{server.description && <ReactMarkdown source={server.description} />}
62+
{server.variables &&
63+
<Typography variant="h6" gutterBottom className={classes.paramsMargin}>Variables</Typography>}
64+
{server.variables && <ReactJson src={server.variables} {...reactJsonOptions} />}
65+
</ExpansionPanelDetails>
66+
</ExpansionPanel>
67+
</div>
68+
))}
69+
</TableCell>
70+
</TableRow>
71+
</ExpansionTable>
4672
</>
4773
);
4874
}
4975
}
5076

51-
export default Servers;
77+
export default withStyles(styles)(Servers);

0 commit comments

Comments
 (0)