Skip to content

Commit a962046

Browse files
committed
fix: make lifecycle methods optional in interface + try/catch in json-schema-faker-rule
1 parent ee334de commit a962046

File tree

5 files changed

+21
-38
lines changed

5 files changed

+21
-38
lines changed

src/coverage.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,12 @@ describe("coverage", () => {
131131
only: ["foo", "bar"],
132132
});
133133
class MyCustomRule implements Rule {
134-
onBegin(options: IOptions, exampleCalls: ExampleCall[]): void {}
135-
onEnd(options: IOptions, exampleCalls: ExampleCall[]): void {}
136134
getExampleCalls(openrpcDocument: OpenrpcDocument, method: any) {
137135
return [];
138136
}
139137
async validateExampleCall(exampleCall: ExampleCall) {
140138
return exampleCall;
141139
}
142-
async beforeRequest(options: IOptions, exampleCall: ExampleCall) {
143-
return;
144-
}
145-
async afterRequest(options: IOptions, exampleCall: ExampleCall) {
146-
return;
147-
}
148-
async afterResponse(options: IOptions, exampleCall: ExampleCall) {
149-
return;
150-
}
151140
}
152141
const myCustomRule = new MyCustomRule();
153142

src/coverage.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export default async (options: IOptions) => {
7272
for (const reporter of options.reporters) {
7373
reporter.onBegin(options, exampleCalls);
7474
}
75+
if (options.rules && options.rules.length > 0) {
76+
for (const rule of options.rules) {
77+
await Promise.resolve(rule.onBegin?.(options));
78+
}
79+
}
7580

7681
for (const exampleCall of exampleCalls) {
7782
for (const reporter of options.reporters) {

src/rules/examples-rule.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class ExamplesRule implements Rule {
1616
this.skip = options?.skip;
1717
this.only = options?.only;
1818
}
19-
onBegin(options: IOptions, exampleCalls: ExampleCall[]) {}
2019
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[] {
2120
if (this.skip && this.skip.includes(method.name)) {
2221
return [];
@@ -56,13 +55,6 @@ class ExamplesRule implements Rule {
5655
}
5756
return exampleCall;
5857
}
59-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {}
60-
61-
// example call lifecycle
62-
beforeRequest(options: IOptions, exampleCall: ExampleCall) {}
63-
afterRequest(options: IOptions, exampleCall: ExampleCall) {}
64-
65-
afterResponse(options: IOptions, exampleCall: ExampleCall) {}
6658
}
6759

6860
export default ExamplesRule;

src/rules/json-schema-faker-rule.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class JsonSchemaFakerRule implements Rule {
2121
this.skip = options?.skip;
2222
this.only = options?.only;
2323
}
24-
onBegin(options: IOptions, exampleCalls: ExampleCall[]) {}
2524
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[] {
2625
if (this.skip && this.skip.includes(method.name)) {
2726
return [];
@@ -51,23 +50,21 @@ class JsonSchemaFakerRule implements Rule {
5150
return exampleCalls;
5251
}
5352
validateExampleCall(exampleCall: ExampleCall): ExampleCall {
54-
const ajv = new Ajv();
55-
ajv.validate(exampleCall.resultSchema, exampleCall.result);
56-
if (ajv.errors && ajv.errors.length > 0) {
53+
try {
54+
const ajv = new Ajv();
55+
ajv.validate(exampleCall.resultSchema, exampleCall.result);
56+
if (ajv.errors && ajv.errors.length > 0) {
57+
exampleCall.valid = false;
58+
exampleCall.reason = JSON.stringify(ajv.errors);
59+
} else {
60+
exampleCall.valid = true;
61+
}
62+
} catch (e: any) {
5763
exampleCall.valid = false;
58-
exampleCall.reason = JSON.stringify(ajv.errors);
59-
} else {
60-
exampleCall.valid = true;
64+
exampleCall.reason = e.message;
6165
}
6266
return exampleCall;
6367
}
64-
onEnd(options: IOptions, exampleCalls: ExampleCall[]) {}
65-
66-
// example call lifecycle
67-
async beforeRequest(options: IOptions, exampleCall: ExampleCall) {}
68-
async afterRequest(options: IOptions, exampleCall: ExampleCall) {}
69-
70-
afterResponse(options: IOptions, exampleCall: ExampleCall) {}
7168
}
7269

7370
export default JsonSchemaFakerRule;

src/rules/rule.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { MethodObject, OpenrpcDocument } from "@open-rpc/meta-schema";
22
import { ExampleCall, IOptions } from "../coverage";
33

44
interface Rule {
5-
onBegin(options: IOptions, exampleCalls: ExampleCall[]): void;
5+
onBegin?(options: IOptions): Promise<void> | void;
66
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[] | Promise<ExampleCall[]>;
77
validateExampleCall(exampleCall: ExampleCall): Promise<ExampleCall> | ExampleCall;
8-
onEnd(options: IOptions, exampleCalls: ExampleCall[]): void;
8+
onEnd?(options: IOptions, exampleCalls: ExampleCall[]): void;
99
// example call lifecycle
10-
beforeRequest(options: IOptions, exampleCall: ExampleCall): Promise<any> | void;
11-
afterRequest(options: IOptions, exampleCall: ExampleCall): Promise<any> | void;
12-
afterResponse(options: IOptions, exampleCall: ExampleCall): Promise<any> | void;
10+
beforeRequest?(options: IOptions, exampleCall: ExampleCall): Promise<void> | void;
11+
afterRequest?(options: IOptions, exampleCall: ExampleCall): Promise<void> | void;
12+
afterResponse?(options: IOptions, exampleCall: ExampleCall): Promise<void> | void;
1313
}
1414

1515
export default Rule;

0 commit comments

Comments
 (0)