Skip to content

Commit

Permalink
feat(parse): accept openrpc document
Browse files Browse the repository at this point in the history
fixes #55
  • Loading branch information
BelfordZ committed Apr 10, 2019
1 parent d12b5a7 commit ed2d443
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jest.mock("fs-extra", () => ({
pathExists: jest.fn(),
readJson: jest.fn(),
}));

Expand Down Expand Up @@ -46,6 +47,11 @@ describe("get-schema", () => {
expect(schema.methods).toBeDefined();
});

it("handles being passed an open rpc object", async () => {
const schema: any = await parse(workingSchema);
expect(schema.methods).toBeDefined();
});

describe("errors", () => {
it("rejects when unable to find file via default", () => {
expect.assertions(1);
Expand Down
20 changes: 12 additions & 8 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { readJson } from "fs-extra";
import { readJson, pathExists } from "fs-extra";
import isUrl = require("is-url");
import refParser from "json-schema-ref-parser";
import fetch from "node-fetch";
import { getValidationErrors } from "./get-validation-errors";
import { types } from "@open-rpc/meta-schema";

const cwd = process.cwd();

Expand Down Expand Up @@ -31,19 +32,22 @@ const readSchemaFromFile = async (schema: string) => {
}
};

export async function parse(schema?: string) {
let parsedSchema;
export async function parse(schema?: string | types.OpenRPC) {
let parsedSchema: types.OpenRPC;

if (schema === undefined) {
schema = `${cwd}/openrpc.json`;
}

if (isJson(schema)) {
parsedSchema = JSON.parse(schema);
} else if (isUrl(schema)) {
parsedSchema = await fetchUrlSchemaFile(schema);
if (typeof schema !== "string") {
parsedSchema = schema;
} else if (isJson(schema as string)) {
parsedSchema = JSON.parse(schema as string);
} else if (isUrl(schema as string)) {
parsedSchema = await fetchUrlSchemaFile(schema as string);
} else {
parsedSchema = await readSchemaFromFile(schema);
const isCorrectPath = await pathExists(schema as string);
parsedSchema = await readSchemaFromFile(schema as string);
}

const errors = getValidationErrors(parsedSchema);
Expand Down

0 comments on commit ed2d443

Please # to comment.