Skip to content

Commit e4d5050

Browse files
authored
Merge pull request #3 from open-rpc/fix/transports-and-coverage
fix: add server support and update readme with install and usage
2 parents 851bcad + 0adda89 commit e4d5050

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
build
3+
*openrpc.json

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,26 @@ Even after writing your OpenRPC Document, you want to test that the OpenRPC Docu
1515
- report back the coverage results
1616
- show errors and the result to help fix inconsistencies
1717

18+
### Installation:
19+
20+
```
21+
22+
npm install -g @open-rpc/test-coverage
23+
24+
```
25+
26+
27+
### Usage:
28+
29+
30+
```
31+
open-rpc-test-coverage -s https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/simple-math-openrpc.json --transport=http --reporter=console --skipMethods=addition
32+
```
33+
34+
1835

1936
#### Screenshot
2037

2138
##### console reporter
2239

23-
![image](https://user-images.githubusercontent.com/364566/56315309-377dbd80-610c-11e9-8be3-dd99f6325461.png)
40+
![image](https://user-images.githubusercontent.com/364566/56318521-3e103300-6114-11e9-85cd-f35eb7b42a0e.png)

src/coverage.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import coverage from "./coverage";
22
import { OpenRPC } from "@open-rpc/meta-schema";
33

4-
5-
64
const mockSchema = {
75
openrpc: "1.0.0",
86
info: {
97
title: "my api",
108
version: "0.0.0-development"
119
},
10+
servers: [
11+
{
12+
name: 'my api',
13+
url: 'http://localhost:3333'
14+
}
15+
],
1216
methods: [
1317
{
1418
name: 'foo',
@@ -23,7 +27,6 @@ const mockSchema = {
2327
]
2428
} as OpenRPC
2529

26-
2730
describe('coverage', () => {
2831
it('can call the reporter', (done) => {
2932
const reporter = () => done()

src/coverage.ts

+26-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const results: any[] = [];
1414
interface IOptions {
1515
schema: OpenRPC;
1616
skipMethods: string[];
17-
transport(method: string, params: any[]): PromiseLike<any>;
17+
transport(url: string, method: string, params: any[]): PromiseLike<any>;
1818
reporter(value: any[]): any;
1919
}
2020

@@ -24,25 +24,31 @@ export default async (options: IOptions) => {
2424
return Promise.resolve();
2525
}
2626
const params = await getParams(method.params);
27-
return options.transport(method.name, params)
28-
.then((r: any) => {
29-
if (r.error && r.error.message.includes('does not exist')) {
30-
results.push({
31-
method: method.name,
32-
error: 'Method Does Not Exist'
33-
});
34-
} else if (r.error) {
35-
results.push({
36-
method: method.name,
37-
error: r.error.message
38-
});
39-
} else {
40-
results.push({
41-
method: method.name,
42-
result: r.result
43-
});
44-
}
45-
});
27+
const urls = (options.schema.servers || []).map((u) => {
28+
// TODO: support server variables
29+
return u.url;
30+
})
31+
return Promise.all(urls.map((url) => {
32+
return options.transport(url, method.name, params)
33+
.then((r: any) => {
34+
if (r.error && r.error.message.includes('does not exist')) {
35+
results.push({
36+
method: method.name,
37+
error: 'Method Does Not Exist'
38+
});
39+
} else if (r.error) {
40+
results.push({
41+
method: method.name,
42+
error: r.error.message
43+
});
44+
} else {
45+
results.push({
46+
method: method.name,
47+
result: r.result
48+
});
49+
}
50+
});
51+
}))
4652
});
4753

4854
return Promise.all(promises).then(() => {

src/transports/HTTPTransport.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fetch from 'isomorphic-fetch';
22
let id = 1;
33

4-
export default (method: string, params: any[]) => {
5-
return fetch('http://localhost:8545', {
4+
export default (url: string, method: string, params: any[]) => {
5+
return fetch(url, {
66
method: "POST",
77
headers: { "Content-Type": "application/json" },
88
body: JSON.stringify({
9+
jsonrpc: "2.0",
910
id: id++,
1011
method,
1112
params

0 commit comments

Comments
 (0)