Skip to content

Commit bdf35e8

Browse files
committed
fix: add tslint + fix linting
1 parent 9d9b405 commit bdf35e8

File tree

10 files changed

+177
-87
lines changed

10 files changed

+177
-87
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
build
33
*openrpc.json
4+
coverage

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.0-development",
44
"description": "Test your OpenRPC Document against an API.",
55
"scripts": {
6-
"test": "jest",
6+
"lint": "tslint --fix -p .",
7+
"test": "jest --coverage && npm run lint",
78
"build": "tsc"
89
},
910
"bin": {
@@ -31,6 +32,7 @@
3132
"@types/node": "^11.13.4",
3233
"jest": "^24.7.1",
3334
"ts-jest": "^24.0.2",
35+
"tslint": "^5.16.0",
3436
"typescript": "^3.4.3"
3537
}
3638
}

src/coverage.test.ts

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,71 @@ const mockSchema = {
55
openrpc: "1.0.0",
66
info: {
77
title: "my api",
8-
version: "0.0.0-development"
8+
version: "0.0.0-development",
99
},
1010
servers: [
1111
{
12-
name: 'my api',
13-
url: 'http://localhost:3333'
14-
}
12+
name: "my api",
13+
url: "http://localhost:3333",
14+
},
1515
],
1616
methods: [
1717
{
18-
name: 'foo',
18+
name: "foo",
1919
params: [],
2020
result: {
21-
name: 'fooResult',
21+
name: "fooResult",
2222
schema: {
23-
type: 'boolean'
24-
}
25-
}
26-
}
27-
]
28-
} as OpenRPC
23+
type: "boolean",
24+
},
25+
},
26+
},
27+
],
28+
} as OpenRPC;
2929

30-
describe('coverage', () => {
31-
it('can call the reporter', (done) => {
32-
const reporter = () => done()
33-
const transport = () => Promise.resolve({})
34-
coverage({
35-
reporter,
36-
transport,
37-
schema: mockSchema,
38-
skipMethods: []
39-
})
40-
})
41-
it('can call the transport', (done) => {
42-
const reporter = () => {}
43-
const transport = () => {
44-
done();
45-
return Promise.resolve({});
46-
}
47-
coverage({
48-
reporter,
49-
transport,
50-
schema: mockSchema,
51-
skipMethods: []
52-
})
53-
})
54-
});
30+
describe("coverage", () => {
31+
describe("reporter", () => {
32+
it("can call the reporter", (done) => {
33+
const reporter = (callResults: any[], schema: OpenRPC) => {
34+
done();
35+
};
36+
const transport = () => Promise.resolve();
37+
coverage({
38+
reporter,
39+
transport,
40+
schema: mockSchema,
41+
skipMethods: [],
42+
});
43+
});
44+
it("can call the reporter with the results", (done) => {
45+
const reporter = (callResults: any[], schema: OpenRPC) => {
46+
expect(callResults[0].result.foo).toBe("bar");
47+
done();
48+
};
49+
const transport = (url: string, method: string, params: any[]) => Promise.resolve({ result: { foo: "bar" } });
50+
coverage({
51+
reporter,
52+
transport,
53+
schema: mockSchema,
54+
skipMethods: [],
55+
});
56+
});
57+
});
58+
describe("transport", () => {
59+
it("can call the transport", (done) => {
60+
const reporter = () => {
61+
// empty reporter
62+
};
63+
const transport = () => {
64+
done();
65+
return Promise.resolve({});
66+
};
67+
coverage({
68+
reporter,
69+
transport,
70+
schema: mockSchema,
71+
skipMethods: [],
72+
});
73+
});
74+
});
75+
});

src/coverage.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { OpenRPC } from "@open-rpc/meta-schema";
2-
const refParser = require('json-schema-ref-parser');
3-
const jsf = require('json-schema-faker');
2+
const jsf = require("json-schema-faker"); // tslint:disable-line
43

54
const getParams = async (params: any[]) => {
65
const promises = params.map((p) => {
76
return jsf.generate(p.schema);
8-
})
7+
});
98
return Promise.all(promises);
10-
}
11-
9+
};
1210

1311
interface IOptions {
1412
schema: OpenRPC;
@@ -27,19 +25,19 @@ export default async (options: IOptions) => {
2725
const urls = (options.schema.servers || []).map((u) => {
2826
// TODO: support server variables
2927
return u.url;
30-
})
28+
});
3129
return Promise.all(urls.map((url) => {
3230
return options.transport(url, method.name, params)
3331
.then((r: any) => {
3432
results.push({
3533
method: method.name,
3634
params,
37-
...r
38-
})
35+
...r,
36+
});
3937
});
40-
}))
38+
}));
4139
});
4240

4341
await Promise.all(promises);
4442
options.reporter(results, options.schema);
45-
}
43+
};

src/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import consoleReporter from './reporters/console';
2-
import coverage from './coverage';
3-
import HTTPTransport from './transports/HTTPTransport';
4-
import jsonReporter from './reporters/json';
5-
import { OpenRPC } from '@open-rpc/meta-schema';
1+
import consoleReporter from "./reporters/console";
2+
import coverage from "./coverage";
3+
import HTTPTransport from "./transports/HTTPTransport";
4+
import jsonReporter from "./reporters/json";
5+
import { OpenRPC } from "@open-rpc/meta-schema";
66

77
const reporters = {
88
console: consoleReporter,
9-
json: jsonReporter
10-
}
9+
json: jsonReporter,
10+
};
1111

1212
const transports = {
13-
http: HTTPTransport
14-
}
13+
http: HTTPTransport,
14+
};
1515

1616
interface IOptions {
1717
schema: OpenRPC;
1818
skipMethods?: string[];
19-
reporter: 'console' | 'json';
20-
transport: 'http';
19+
reporter: "console" | "json";
20+
transport: "http";
2121
}
2222

2323
export default async (options: IOptions) => {
2424
return coverage({
25-
reporter: reporters[options.reporter || 'console'],
25+
reporter: reporters[options.reporter || "console"],
2626
schema: options.schema,
2727
skipMethods: options.skipMethods || [],
28-
transport: transports[options.transport || 'http']
28+
transport: transports[options.transport || "http"],
2929
});
30-
}
30+
};

src/reporters/console.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1-
import colors from 'colors';
1+
import colors from "colors";
22
import Ajv, { ErrorObject, Ajv as IAjv } from "ajv";
3-
import { OpenRPC, MethodObject, ContentDescriptorObject } from '@open-rpc/meta-schema';
3+
import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
44

55
export default (callResults: any[], schema: OpenRPC) => {
66
const ajv = new Ajv();
77
const metrics = {
88
errors: 0,
9-
success: 0
10-
}
11-
12-
callResults.forEach((call) => {
9+
success: 0,
10+
};
11+
12+
callResults.forEach(async (call) => {
1313
if (call.error) {
1414
metrics.errors++;
15-
console.log(colors.red.underline('JSON-RPC Request Error: '), colors.cyan(call.method));
15+
console.log(colors.red.underline("JSON-RPC Request Error: "), colors.cyan(call.method));
1616
console.log(call.error);
1717
console.log(call.params);
1818
} else {
1919

20-
const methodSchema = schema.methods.find((m) => m.name === call.method);
20+
const methodSchema = schema.methods.find((m: MethodObject) => m.name === call.method);
2121
if (!methodSchema) {
2222
return console.log(`Error: no result defined for ${call.method}`);
2323
}
24+
const result = methodSchema.result as ContentDescriptorObject;
25+
26+
let resultSchema: any = result.schema;
27+
28+
if (result.oneOf) {
29+
resultSchema = {
30+
oneOf: result.oneOf.map((cd: ContentDescriptorObject) => cd.schema),
31+
};
32+
}
2433

25-
const isValid = ajv.validate((methodSchema.result as ContentDescriptorObject).schema, call.result);
34+
ajv.validate(resultSchema, call.result);
2635
const errors = ajv.errors as ErrorObject[];
2736

28-
if (isValid) {
37+
if (!errors || errors.length === 0) {
2938
metrics.success++;
30-
console.log(colors.green('Success: '), call.method)
39+
console.log(colors.green("Success: "), call.method);
3140
} else {
32-
console.log(colors.red.underline('Result Validation Error: '), colors.cyan(call.method));
41+
console.log(colors.red.underline("Result Validation Error: "), colors.cyan(call.method));
3342
console.log(call);
3443
console.log(errors);
3544
console.log(methodSchema);
3645
}
3746

3847
}
39-
})
40-
console.log('==========');
41-
console.log('Success: ', colors.green(metrics.success.toString()));
42-
console.log('Errors: ', colors.red(metrics.errors.toString()));
43-
console.log('==========');
48+
});
49+
console.log("==========");
50+
console.log("Success: ", colors.green(metrics.success.toString()));
51+
console.log("Errors: ", colors.red(metrics.errors.toString()));
52+
console.log("==========");
4453
};

src/reporters/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { OpenRPC } from "@open-rpc/meta-schema";
22

33
export default (callResults: any[], schema: OpenRPC) => {
44
console.log(JSON.stringify(callResults, undefined, 4));
5-
}
5+
};

src/transports/HTTPTransport.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'isomorphic-fetch';
1+
import fetch from "isomorphic-fetch";
22
let id = 1;
33

44
export default (url: string, method: string, params: any[]) => {
@@ -9,9 +9,9 @@ export default (url: string, method: string, params: any[]) => {
99
jsonrpc: "2.0",
1010
id: id++,
1111
method,
12-
params
13-
})
12+
params,
13+
}),
1414
}).then((r: any) => {
1515
return r.json();
16-
})
17-
};
16+
});
17+
};

0 commit comments

Comments
 (0)