Skip to content

Commit

Permalink
Merge pull request #471 from open-rpc/feat/allow-transport-passed
Browse files Browse the repository at this point in the history
feat: allow transport passed + added raw json reporter
  • Loading branch information
shanejonas authored Mar 28, 2024
2 parents 2f565e0 + d4b6e7d commit 65d5da0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bin",
"build"
],
"main": "build/coverage.js",
"main": "build/index.js",
"author": "",
"license": "Apache-2.0",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ export default async (options: IOptions) => {
}
}

options.reporter(exampleCalls, options.openrpcDocument);
return options.reporter(exampleCalls, options.openrpcDocument);
};
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import consoleReporter from "./reporters/console";
import coverage from "./coverage";
import HTTPTransport from "./transports/HTTPTransport";
import jsonReporter from "./reporters/json";
import rawReporter from "./reporters/raw";
import { OpenrpcDocument } from "@open-rpc/meta-schema";
import { ITransport } from "./transports/ITransport";

const reporters = {
console: consoleReporter,
json: jsonReporter,
raw: rawReporter,
};

const transports = {
Expand All @@ -17,16 +20,17 @@ interface IOptions {
openrpcDocument: OpenrpcDocument;
skip?: string[];
only?: string[];
reporter: "console" | "json";
transport: "http";
reporter: "console" | "json" | "raw";
transport: "http" | ITransport;
}

export default async (options: IOptions) => {
const transport = typeof options.transport === "function" ? options.transport : transports[options.transport || "http"];
return coverage({
reporter: reporters[options.reporter || "console"],
openrpcDocument: options.openrpcDocument,
skip: options.skip || [],
only: options.only || [],
transport: transports[options.transport || "http"],
transport: transport,
});
};
5 changes: 5 additions & 0 deletions src/reporters/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OpenrpcDocument } from "@open-rpc/meta-schema";

export default (callResults: any[], schema: OpenrpcDocument) => {
return callResults;
};
5 changes: 4 additions & 1 deletion src/transports/HTTPTransport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fetch from "isomorphic-fetch";
import { ITransport } from "./ITransport";
let id = 1;

export default (url: string, method: string, params: any[]) => {
const httpTransport: ITransport = async (url: string, method: string, params: any[]) => {
return fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -15,3 +16,5 @@ export default (url: string, method: string, params: any[]) => {
return r.json();
});
};

export default httpTransport;
1 change: 1 addition & 0 deletions src/transports/ITransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ITransport = (url: string, method: string, params: any) => Promise<any>;

0 comments on commit 65d5da0

Please sign in to comment.