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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 7589 additions & 1981 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 & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 62 additions & 2 deletions
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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}>

0 commit comments

Comments
 (0)