Skip to content

Commit a0b737e

Browse files
authored
Merge pull request #133 from open-rpc/feat/method-plugin
feat: add method plugin + fix dark theme descriptions
2 parents b057b3c + 0d7c642 commit a0b737e

File tree

10 files changed

+333
-240
lines changed

10 files changed

+333
-240
lines changed

package-lock.json

Lines changed: 250 additions & 226 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@open-rpc/examples": "^1.3.3",
1717
"hash-color-material": "^1.1.3",
1818
"json-schema": "^0.2.3",
19-
"lodash": "^4.17.11",
19+
"lodash": "^4.17.15",
2020
"react": "^16.8.4",
2121
"react-dom": "^16.8.4",
2222
"react-json-view": "^1.19.1",
@@ -28,6 +28,7 @@
2828
"@types/json-schema": "^7.0.3",
2929
"@types/lodash": "^4.14.123",
3030
"@types/node": "^12.0.2",
31+
"@types/react": "^16.9.2",
3132
"@types/react-dom": "^16.8.3",
3233
"jest": "^24.5.0",
3334
"json-schema-ref-parser": "^7.0.1",

src/ContentDescriptor/ContentDescriptor.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import ReactMarkdown from "react-markdown";
99
import { ContentDescriptorObject } from "@open-rpc/meta-schema";
1010

1111
const styles = (theme: Theme) => ({
12+
description: {
13+
color: theme.palette.text.primary,
14+
},
1215
heading: {
1316
flexBasis: "33.33%",
1417
flexShrink: 0,
@@ -52,7 +55,9 @@ class ContentDescriptor extends Component<IProps> {
5255
</ExpansionPanelSummary>
5356
<ExpansionPanelDetails style={{ display: "block" }}>
5457
<div>
55-
{contentDescriptor.description && <ReactMarkdown source={contentDescriptor.description} />}
58+
{contentDescriptor.description &&
59+
<ReactMarkdown source={contentDescriptor.description} className={classes.description} />
60+
}
5661
{contentDescriptor.schema &&
5762
<>
5863
<Typography variant="body1" color="primary">schema</Typography>

src/Documentation.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from "react";
22
import Info from "./Info/Info";
33
import Servers from "./Servers/Servers";
4-
import Methods from "./Methods/Methods";
4+
import Methods, { IMethodPluginProps } from "./Methods/Methods";
55
import ContentDescriptors from "./ContentDescriptors/ContentDescriptors";
66
import { OpenRPC } from "@open-rpc/meta-schema";
77

88
interface IProps {
99
schema: OpenRPC;
1010
uiSchema?: any;
1111
reactJsonOptions?: any;
12+
methodPlugins?: Array<React.FC<IMethodPluginProps>>;
1213
}
1314

1415
export default class Documentation extends React.Component<IProps> {
@@ -24,7 +25,12 @@ export default class Documentation extends React.Component<IProps> {
2425
<>
2526
<Info schema={schema} />
2627
<Servers servers={schema.servers} reactJsonOptions={reactJsonOptions} />
27-
<Methods schema={schema} uiSchema={uiSchema} reactJsonOptions={reactJsonOptions} />
28+
<Methods
29+
schema={schema}
30+
uiSchema={uiSchema}
31+
reactJsonOptions={reactJsonOptions}
32+
methodPlugins={this.props.methodPlugins}
33+
/>
2834
<ContentDescriptors schema={schema} uiSchema={uiSchema}></ContentDescriptors>
2935
</>
3036
);

src/ExamplePairing/ExamplePairing.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import React, { Component } from "react";
22
import Grid from "@material-ui/core/Grid";
3-
import { Card, CardContent } from "@material-ui/core";
3+
import { Card, CardContent, Theme, withStyles, WithStyles } from "@material-ui/core";
44
import ReactJson from "react-json-view";
55
import ReactMarkdown from "react-markdown";
66
import { MethodObject, ExampleObject, ExamplePairingObject } from "@open-rpc/meta-schema";
77
import _ from "lodash";
88

9-
interface IProps {
9+
interface IProps extends WithStyles<typeof styles> {
1010
examplePosition?: number;
1111
method?: MethodObject;
1212
reactJsonOptions?: any;
1313
}
1414

15-
export default class ExamplePairing extends Component<IProps, {}> {
15+
const styles = (theme: Theme) => ({
16+
description: {
17+
color: theme.palette.text.primary,
18+
},
19+
});
20+
21+
class ExamplePairing extends Component<IProps, {}> {
1622
public render() {
17-
const { examplePosition, method } = this.props;
23+
const { examplePosition, method, classes } = this.props;
1824
if (_.isUndefined(examplePosition)) {
1925
return null;
2026
}
@@ -28,7 +34,7 @@ export default class ExamplePairing extends Component<IProps, {}> {
2834
return (
2935
<Grid container spacing={24}>
3036
<Grid item xs={12}>
31-
<ReactMarkdown source={example.description} />
37+
<ReactMarkdown source={example.description} className={classes.description}/>
3238
</Grid>
3339
<Grid item xs={6}>
3440
<Card>
@@ -57,3 +63,5 @@ export default class ExamplePairing extends Component<IProps, {}> {
5763
);
5864
}
5965
}
66+
67+
export default withStyles(styles)(ExamplePairing);

src/Info/Info.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const styles = (theme: Theme) => ({
1414
margin: theme.spacing.unit,
1515
},
1616
description: {
17+
color: theme.palette.text.primary,
1718
padding: `${theme.spacing.unit}px 0 ${theme.spacing.unit}px 0`,
1819
},
1920
});
@@ -41,7 +42,7 @@ class Info extends Component<IProps> {
4142
clickable
4243
color="primary"
4344
label={info.license.name} />}
44-
{info.description && <ReactMarkdown source={info.description}/>}
45+
{info.description && <ReactMarkdown className={classes.description} source={info.description}/>}
4546
{info.termsOfService &&
4647
<Button className={classes.button} variant="contained" href={info.termsOfService}>Terms Of Service</Button>}
4748
{info.contact &&

src/Links/Links.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import Servers from "../Servers/Servers";
1717
import ReactJson from "react-json-view";
1818

1919
const styles = (theme: Theme) => ({
20+
description: {
21+
color: theme.palette.text.primary,
22+
},
2023
heading: {
2124
flexBasis: "33.33%",
2225
flexShrink: 0,
@@ -58,7 +61,7 @@ class Links extends Component<IProps> {
5861
</div>
5962
</ExpansionPanelSummary>
6063
<ExpansionPanelDetails style={{ display: "block" }} key="links-body">
61-
{link.description && <ReactMarkdown source={link.description} />}
64+
{link.description && <ReactMarkdown source={link.description} className={classes.description} />}
6265
{link.params && <Typography variant="h6" gutterBottom>Params</Typography>}
6366
{link.params && <ReactJson src={link.params} {...reactJsonOptions} />}
6467
{link.server &&

src/Methods/Methods.test.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import Methods from "./Methods";
3+
import Methods, { IMethodPluginProps } from "./Methods";
44
import { OpenRPC } from "@open-rpc/meta-schema";
55

66
it("renders without crashing", () => {
@@ -44,6 +44,29 @@ it("renders schema methods name", () => {
4444
ReactDOM.unmountComponentAtNode(div);
4545
});
4646

47+
it("renders schema plugin", () => {
48+
const div = document.createElement("div");
49+
const schema = {
50+
methods: [
51+
{
52+
name: "get_pet",
53+
},
54+
],
55+
};
56+
const TestComponent: React.FC<IMethodPluginProps> = (props) => {
57+
return (
58+
<div>
59+
Plugin Test
60+
</div>
61+
);
62+
};
63+
64+
ReactDOM.render(<Methods schema={schema as any} methodPlugins={[TestComponent]}/>, div);
65+
expect(div.innerHTML.includes("get_pet")).toBe(true);
66+
expect(div.innerHTML.includes("Plugin Test")).toBe(true);
67+
ReactDOM.unmountComponentAtNode(div);
68+
});
69+
4770
it("renders schema methods summary", () => {
4871
const div = document.createElement("div");
4972
const schema = {

src/Methods/Methods.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import Links from "../Links/Links";
2222
import Tags from "../Tags/Tags";
2323

2424
const styles = (theme: Theme) => ({
25+
description: {
26+
color: theme.palette.text.primary,
27+
},
2528
heading: {
2629
flexBasis: "33.33%",
2730
flexShrink: 0,
@@ -38,10 +41,15 @@ const styles = (theme: Theme) => ({
3841
},
3942
});
4043

44+
export interface IMethodPluginProps {
45+
openrpcMethodObject: MethodObject;
46+
}
47+
4148
interface IProps extends WithStyles<typeof styles> {
4249
schema?: OpenRPC;
4350
uiSchema?: any;
4451
reactJsonOptions?: object;
52+
methodPlugins?: Array<React.FC<IMethodPluginProps>>;
4553
}
4654

4755
class Methods extends Component<IProps> {
@@ -70,7 +78,7 @@ class Methods extends Component<IProps> {
7078
}
7179
{method.description &&
7280
<ExpansionPanelDetails key="description">
73-
<ReactMarkdown source={method.description} />
81+
<ReactMarkdown source={method.description} className={classes.description}/>
7482
</ExpansionPanelDetails>
7583
}
7684
{method.params && method.params.length > 0 &&
@@ -118,6 +126,15 @@ class Methods extends Component<IProps> {
118126
<Links links={method.links} reactJsonOptions={this.props.reactJsonOptions} />
119127
</ExpansionPanelDetails>
120128
}
129+
{this.props.methodPlugins && this.props.methodPlugins.length > 0 &&
130+
<ExpansionPanelDetails key="method-plugins">
131+
{this.props.methodPlugins.map((CompDef: any) => {
132+
return (
133+
<CompDef openrpcMethodObject={method} />
134+
);
135+
})}
136+
</ExpansionPanelDetails>
137+
}
121138
</ExpansionPanel>
122139
))}
123140
</div>

src/Servers/Servers.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import ReactJson from "react-json-view";
1010
import ExpansionTable from "../ExpansionTable/ExpansionTable";
1111

1212
const styles = (theme: Theme) => ({
13+
description: {
14+
color: theme.palette.text.primary,
15+
},
1316
heading: {
1417
flexBasis: "33.33%",
1518
flexShrink: 0,
@@ -58,7 +61,9 @@ class Servers extends Component<IProps> {
5861
</div>
5962
</ExpansionPanelSummary>
6063
<ExpansionPanelDetails style={{ display: "block" }} key="servers-body">
61-
{server.description && <ReactMarkdown source={server.description} />}
64+
{server.description &&
65+
<ReactMarkdown source={server.description} className={classes.description} />
66+
}
6267
{server.variables &&
6368
<Typography variant="h6" gutterBottom className={classes.paramsMargin}>Variables</Typography>}
6469
{server.variables && <ReactJson src={server.variables} {...reactJsonOptions} />}

0 commit comments

Comments
 (0)