Skip to content

Commit f8a233b

Browse files
authored
Merge pull request #322 from open-rpc/fix/example-pairing-by-name
fix: add examples paramStructure by-name support
2 parents 97cb6f1 + 06c583d commit f8a233b

14 files changed

+7693
-2014
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ npm install @open-rpc/docs-react @open-rpc/meta-schema --save
4444
import React from 'react';
4545
import ReactDOM from 'react-dom';
4646
import Documentation from "@open-rpc/docs-react";
47-
import { OpenRPC } from '@open-rpc/meta-schema';
47+
import { OpenrpcDocument } from '@open-rpc/meta-schema';
4848
49-
const schema: OpenRPC = {
50-
openrpc: "1.0.0-rc1",
49+
const schema: OpenrpcDocument = {
50+
openrpc: "1.2.4",
5151
info: {
5252
"version": "0.0.0-development",
5353
"title": "My New API"

package-lock.json

+7,589-1,981
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@material-ui/icons": "^4.9.1",
1616
"@material-ui/lab": "^4.0.0-alpha.47",
1717
"@material-ui/styles": "^4.7.1",
18-
"@open-rpc/examples": "^1.3.3",
18+
"@open-rpc/examples": "^1.5.0",
1919
"@xops.net/json-schema-to-react-tree": "^1.0.2",
2020
"hash-color-material": "^1.1.3",
2121
"jest-transform-css": "^2.0.0",
@@ -27,7 +27,7 @@
2727
"react-markdown": "^4.0.6"
2828
},
2929
"devDependencies": {
30-
"@open-rpc/meta-schema": "^1.4.3",
30+
"@open-rpc/meta-schema": "^1.11.0",
3131
"@types/jest": "^24.0.11",
3232
"@types/json-schema": "^7.0.3",
3333
"@types/lodash": "^4.14.123",

src/ContentDescriptors/ContentDescriptors.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
33
import ContentDescriptors from "./ContentDescriptors";
4-
import { OpenRPC } from "@open-rpc/meta-schema";
4+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
55

66
it("renders without crashing", () => {
77
const div = document.createElement("div");
@@ -18,7 +18,7 @@ it("renders empty with no schema", () => {
1818

1919
it("renders empty with empty schema", () => {
2020
const div = document.createElement("div");
21-
const emptySchema = {} as OpenRPC;
21+
const emptySchema = {} as OpenrpcDocument;
2222
ReactDOM.render(<ContentDescriptors schema={emptySchema}/>, div);
2323
expect(div.innerHTML).toBe("");
2424
ReactDOM.unmountComponentAtNode(div);

src/ContentDescriptors/ContentDescriptors.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { Component } from "react";
22
import ContentDescriptor from "../ContentDescriptor/ContentDescriptor";
33
import { Typography } from "@material-ui/core";
4-
import { OpenRPC, ContentDescriptorObject } from "@open-rpc/meta-schema";
4+
import { OpenrpcDocument, ContentDescriptorObject } from "@open-rpc/meta-schema";
55

66
interface IProps {
7-
schema?: OpenRPC;
7+
schema?: OpenrpcDocument;
88
disableTransitionProps?: boolean;
99
uiSchema?: any;
1010
}

src/Documentation.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import Info from "./Info/Info";
33
import Servers from "./Servers/Servers";
44
import Methods, { IMethodPluginProps } from "./Methods/Methods";
55
import ContentDescriptors from "./ContentDescriptors/ContentDescriptors";
6-
import { OpenRPC } from "@open-rpc/meta-schema";
6+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
77

88
interface IProps {
9-
schema: OpenRPC;
9+
schema: OpenrpcDocument;
1010
uiSchema?: any;
1111
reactJsonOptions?: any;
1212
methodPlugins?: Array<React.FC<IMethodPluginProps>>;

src/ExamplePairing/ExamplePairing.test.tsx

+62-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,73 @@ import ReactDOM from "react-dom";
33
import ExamplePairing from "./ExamplePairing";
44
import examples from "@open-rpc/examples";
55
import refParser from "json-schema-ref-parser";
6-
import { OpenRPC } from "@open-rpc/meta-schema";
6+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
7+
8+
it("renders handles no method", async () => {
9+
const div = document.createElement("div");
10+
ReactDOM.render(<ExamplePairing method={undefined} examplePosition={0}/>, div);
11+
expect(div.innerHTML).toBe("");
12+
ReactDOM.unmountComponentAtNode(div);
13+
});
14+
15+
it("renders handles no method examples", async () => {
16+
const div = document.createElement("div");
17+
ReactDOM.render(<ExamplePairing method={{} as any} examplePosition={0} />, div);
18+
expect(div.innerHTML).toBe("");
19+
ReactDOM.unmountComponentAtNode(div);
20+
});
21+
22+
it("renders handles no examplePosition", async () => {
23+
const div = document.createElement("div");
24+
const simpleMath = await refParser.dereference(examples.simpleMath) as OpenrpcDocument;
25+
ReactDOM.render(<ExamplePairing method={simpleMath.methods[0]} />, div);
26+
expect(div.innerHTML).toBe("");
27+
ReactDOM.unmountComponentAtNode(div);
28+
});
729

830
it("renders examples", async () => {
931
const div = document.createElement("div");
10-
const simpleMath = await refParser.dereference(examples.simpleMath) as OpenRPC;
32+
const simpleMath = await refParser.dereference(examples.simpleMath) as OpenrpcDocument;
1133
ReactDOM.render(<ExamplePairing method={simpleMath.methods[0]} examplePosition={0} />, div);
1234
expect(div.innerHTML.includes("2")).toBe(true);
1335
expect(div.innerHTML.includes("4")).toBe(true);
1436
ReactDOM.unmountComponentAtNode(div);
1537
});
38+
39+
it("renders examples with params by-name", async () => {
40+
const div = document.createElement("div");
41+
ReactDOM.render(<ExamplePairing method={{
42+
examples: [
43+
{
44+
name: "fooExample",
45+
params: [
46+
{
47+
name: "foo",
48+
value: "bar",
49+
},
50+
],
51+
result: {
52+
name: "exampleResultThing",
53+
value: "potato",
54+
},
55+
},
56+
],
57+
name: "myMethod",
58+
paramStructure: "by-name",
59+
params: [{
60+
name: "foo",
61+
schema: {
62+
type: "string",
63+
},
64+
}],
65+
result: {
66+
name: "resultThing",
67+
schema: {
68+
type: "string",
69+
},
70+
},
71+
}} examplePosition={0} />, div);
72+
expect(div.innerHTML.includes("foo")).toBe(true);
73+
expect(div.innerHTML.includes("bar")).toBe(true);
74+
ReactDOM.unmountComponentAtNode(div);
75+
});

src/ExamplePairing/ExamplePairing.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import ReactMarkdown from "react-markdown";
66
import { MethodObject, ExampleObject, ExamplePairingObject } from "@open-rpc/meta-schema";
77
import _ from "lodash";
88

9+
export type TParamStructure = "either" | "by-name" | "by-position";
10+
911
interface IProps extends WithStyles<typeof styles> {
1012
examplePosition?: number;
1113
method?: MethodObject;
@@ -31,10 +33,18 @@ class ExamplePairing extends Component<IProps, {}> {
3133
if (!example || _.isEmpty(example)) {
3234
return null;
3335
}
36+
const paramStructure: TParamStructure = method?.paramStructure || "either";
37+
const params = paramStructure === "by-name"
38+
? (example.params as ExampleObject[]).reduce(((memo, p) => {
39+
memo[p.name] = p.value;
40+
return memo;
41+
}), {} as any)
42+
: (example.params as ExampleObject[]).map(((p) => p.value));
43+
3444
return (
3545
<Grid container spacing={10}>
3646
<Grid item xs={12}>
37-
<ReactMarkdown source={example.description} className={classes.description}/>
47+
<ReactMarkdown source={example.description} className={classes.description} />
3848
</Grid>
3949
<Grid item xs={6}>
4050
<Card>
@@ -43,7 +53,7 @@ class ExamplePairing extends Component<IProps, {}> {
4353
id: 1,
4454
jsonrpc: "2.0",
4555
method: method && method.name,
46-
params: (example.params as ExampleObject[]).map(((p) => p.value)),
56+
params,
4757
}} {...this.props.reactJsonOptions} />}
4858
</CardContent>
4959
</Card>

src/ExamplePairings/ExamplePairings.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactDOM from "react-dom";
33
import ExamplePairings from "./ExamplePairings";
44
import examples from "@open-rpc/examples";
55
import refParser from "json-schema-ref-parser";
6-
import { OpenRPC, ExamplePairingObject } from "@open-rpc/meta-schema";
6+
import { OpenrpcDocument, ExamplePairingObject } from "@open-rpc/meta-schema";
77

88
it("renders without crashing", () => {
99
const div = document.createElement("div");
@@ -27,7 +27,7 @@ it("renders empty with empty example", () => {
2727

2828
it("renders examples", async () => {
2929
const div = document.createElement("div");
30-
const simpleMath = await refParser.dereference(examples.simpleMath) as OpenRPC;
30+
const simpleMath = await refParser.dereference(examples.simpleMath) as OpenrpcDocument;
3131
ReactDOM.render(
3232
<ExamplePairings
3333
method={simpleMath.methods[0]}

src/ExamplePairings/ExamplePairings.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ExamplePairings extends Component<IProps, IState> {
8080
{example.name}
8181
</MenuItem>
8282
))}
83-
</Menu>
83+
</Menu>
8484
</List>
8585
</Grid>
8686
<Grid item xs={12}>

src/Info/Info.test.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
33
import Info from "./Info";
4-
import { OpenRPC } from "@open-rpc/meta-schema";
4+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
55

66
it("renders without crashing", () => {
77
const div = document.createElement("div");
@@ -18,14 +18,14 @@ it("renders empty with no schema", () => {
1818

1919
it("renders empty with empty schema", () => {
2020
const div = document.createElement("div");
21-
ReactDOM.render(<Info schema={{} as OpenRPC}/>, div);
21+
ReactDOM.render(<Info schema={{} as OpenrpcDocument}/>, div);
2222
expect(div.innerHTML).toBe("");
2323
ReactDOM.unmountComponentAtNode(div);
2424
});
2525

2626
it("renders empty with empty schema info", () => {
2727
const div = document.createElement("div");
28-
ReactDOM.render(<Info schema={{ info: {} } as OpenRPC}/>, div);
28+
ReactDOM.render(<Info schema={{ info: {} } as OpenrpcDocument}/>, div);
2929
expect(div.innerHTML).toBe("");
3030
ReactDOM.unmountComponentAtNode(div);
3131
});
@@ -36,7 +36,7 @@ it("renders an info.title for a given schema", () => {
3636
info: {
3737
title: "foo",
3838
},
39-
} as OpenRPC;
39+
} as OpenrpcDocument;
4040
ReactDOM.render(<Info schema={schema} />, div);
4141
expect(div.innerHTML.includes("foo")).toBe(true);
4242
ReactDOM.unmountComponentAtNode(div);
@@ -48,7 +48,7 @@ it("renders an info.version for a given schema", () => {
4848
info: {
4949
version: "1.0.0-rc0",
5050
},
51-
} as OpenRPC;
51+
} as OpenrpcDocument;
5252
ReactDOM.render(<Info schema={schema} />, div);
5353
expect(div.innerHTML.includes("1.0.0-rc0")).toBe(true);
5454
ReactDOM.unmountComponentAtNode(div);
@@ -60,7 +60,7 @@ it("renders an info.description for a given schema", () => {
6060
info: {
6161
description: "my long verbose description",
6262
},
63-
} as OpenRPC;
63+
} as OpenrpcDocument;
6464
ReactDOM.render(<Info schema={schema} />, div);
6565
expect(div.innerHTML.includes("my long verbose description")).toBe(true);
6666
ReactDOM.unmountComponentAtNode(div);
@@ -72,7 +72,7 @@ it("renders an info terms of service for a given schema", () => {
7272
info: {
7373
termsOfService: "http://open-rpc.org",
7474
},
75-
} as OpenRPC;
75+
} as OpenrpcDocument;
7676
ReactDOM.render(<Info schema={schema} />, div);
7777
expect(div.innerHTML.includes('"http://open-rpc.org"')).toBe(true);
7878
ReactDOM.unmountComponentAtNode(div);
@@ -88,7 +88,7 @@ it("renders an info contact for a given schema", () => {
8888
url: "http://open-rpc.org",
8989
},
9090
},
91-
} as OpenRPC;
91+
} as OpenrpcDocument;
9292
ReactDOM.render(<Info schema={schema} />, div);
9393
expect(div.innerHTML.includes("OpenRPC Team")).toBe(true);
9494
expect(div.innerHTML.includes('"http://open-rpc.org"')).toBe(true);

src/Info/Info.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Chip from "@material-ui/core/Chip";
44
import Button from "@material-ui/core/Button";
55
import { withStyles, Theme, WithStyles } from "@material-ui/core/styles";
66
import ReactMarkdown from "react-markdown";
7-
import { OpenRPC } from "@open-rpc/meta-schema";
7+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
88

99
const styles = (theme: Theme) => ({
1010
button: {
@@ -20,7 +20,7 @@ const styles = (theme: Theme) => ({
2020
});
2121

2222
interface IProps extends WithStyles<typeof styles> {
23-
schema?: OpenRPC;
23+
schema?: OpenrpcDocument;
2424
}
2525

2626
class Info extends Component<IProps> {

src/Methods/Methods.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
33
import Methods, { IMethodPluginProps } from "./Methods";
4-
import { OpenRPC } from "@open-rpc/meta-schema";
4+
import { OpenrpcDocument } from "@open-rpc/meta-schema";
55

66
it("renders without crashing", () => {
77
const div = document.createElement("div");
@@ -151,7 +151,7 @@ it("renders schema methods description", () => {
151151
description: "verbose get_pet description",
152152
},
153153
],
154-
} as OpenRPC;
154+
} as OpenrpcDocument;
155155
ReactDOM.render(<Methods schema={schema} disableTransitionProps={true} />, div);
156156
expect(div.innerHTML.includes("verbose get_pet description")).toBe(true);
157157
ReactDOM.unmountComponentAtNode(div);

src/Methods/Methods.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import ContentDescriptor from "../ContentDescriptor/ContentDescriptor";
1212
import ExamplePairings from "../ExamplePairings/ExamplePairings";
1313
import Errors from "../Errors/Errors";
1414
import {
15-
OpenRPC,
15+
OpenrpcDocument,
1616
MethodObject,
1717
ContentDescriptorObject,
1818
ErrorObject,
1919
ExamplePairingObject,
20+
LinkObject,
2021
} from "@open-rpc/meta-schema";
2122
import Links from "../Links/Links";
2223
import Tags from "../Tags/Tags";
@@ -46,7 +47,7 @@ export interface IMethodPluginProps {
4647
}
4748

4849
interface IProps extends WithStyles<typeof styles> {
49-
schema?: OpenRPC;
50+
schema?: OpenrpcDocument;
5051
uiSchema?: any;
5152
reactJsonOptions?: object;
5253
methodPlugins?: Array<React.FC<IMethodPluginProps>>;
@@ -128,7 +129,7 @@ class Methods extends Component<IProps> {
128129
}
129130
{method.links && method.links.length > 0 &&
130131
<ExpansionPanelDetails key="links">
131-
<Links links={method.links} reactJsonOptions={this.props.reactJsonOptions} />
132+
<Links links={method.links as LinkObject[]} reactJsonOptions={this.props.reactJsonOptions} />
132133
</ExpansionPanelDetails>
133134
}
134135
{this.props.methodPlugins && this.props.methodPlugins.length > 0 &&

0 commit comments

Comments
 (0)