Skip to content

Commit

Permalink
feat: remote refs for open rpc resources
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Jul 24, 2020
1 parent b20f533 commit dcbfa15
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@json-schema-spec/json-pointer": "^0.1.2",
"@json-schema-tools/dereferencer": "^1.2.0",
"@json-schema-tools/meta-schema": "^1.3.0",
"@json-schema-tools/reference-resolver": "^1.0.0",
"@open-rpc/meta-schema": "^1.12.5",
"ajv": "^6.10.0",
"detect-node": "^2.0.4",
Expand Down
44 changes: 22 additions & 22 deletions src/dereference-document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Dereferencer from "@json-schema-tools/dereferencer";
import { OpenrpcDocument as OpenRPC, ReferenceObject, ExamplePairingObject, JSONSchema, SchemaComponents, ContentDescriptorComponents, ContentDescriptorObject, OpenrpcDocument, MethodObject } from "@open-rpc/meta-schema";
import Ptr, { EvalError, InvalidPtrError } from "@json-schema-spec/json-pointer"
import referenceResolver from "@json-schema-tools/reference-resolver";

/**
* Provides an error interface for OpenRPC Document dereferencing problems
Expand All @@ -19,30 +20,31 @@ export class OpenRPCDocumentDereferencingError implements Error {
}
}

const derefItem = (item: ReferenceObject, doc: OpenRPC) => {
const derefItem = async (item: ReferenceObject, doc: OpenRPC) => {
const { $ref } = item;
if ($ref === undefined) { return item; }

const ref = $ref.replace("#", "");
let pointer;
try {
pointer = Ptr.parse(ref);
} catch (err) {
throw new OpenRPCDocumentDereferencingError([`Invalid JSON Pointer - ${$ref}`, `The doc: ${JSON.stringify(doc)}`].join("\n"));
}

try {
return pointer.eval(doc);
return await referenceResolver($ref, doc);
} catch (err) {
throw new OpenRPCDocumentDereferencingError([
`unable to eval pointer against OpenRPC Document.`,
`error type: ${err.name}`,
`instance: ${err.instance}`,
`token: ${err.token}`,
`pointer: ${$ref}`,
].join("\n"));
}
};

const derefItems = async (items: ReferenceObject[], doc: OpenRPC) => {
const dereffed = [];
for (const i of items) {
dereffed.push(await derefItem(i, doc))
}
return dereffed;
};

const handleSchemaWithSchemaComponents = async (s: JSONSchema, schemaComponents: SchemaComponents) => {
if (s === true || s === false) {
return Promise.resolve(s);
Expand Down Expand Up @@ -113,29 +115,27 @@ const handleSchemasInsideContentDescriptorComponents = async (doc: OpenrpcDocume

const handleMethod = async (method: MethodObject, doc: OpenrpcDocument): Promise<MethodObject> => {
if (method.tags !== undefined) {
method.tags = method.tags.map((t) => derefItem(t as ReferenceObject, doc));
method.tags = await derefItems(method.tags as ReferenceObject[], doc);
}

if (method.errors !== undefined) {
method.errors = method.errors.map((e) => derefItem(e as ReferenceObject, doc));
method.errors = await derefItems(method.errors as ReferenceObject[], doc);
}

if (method.links !== undefined) {
method.links = method.links.map((e) => derefItem(e as ReferenceObject, doc));
method.links = await derefItems(method.links as ReferenceObject[], doc);
}

if (method.examples !== undefined) {
method.examples = method.examples.map((ex) => derefItem(ex as ReferenceObject, doc));
method.examples.map((ex) => {
const exAsPairing = ex as ExamplePairingObject;
exAsPairing.params = exAsPairing.params.map((exParam) => derefItem(exParam as ReferenceObject, doc));
exAsPairing.result = derefItem(exAsPairing.result as ReferenceObject, doc);
return exAsPairing;
});
method.examples = await derefItems(method.examples as ReferenceObject[], doc);
for (const exPairing of method.examples as ExamplePairingObject[]) {
exPairing.params = await derefItems(exPairing.params as ReferenceObject[], doc);
exPairing.result = await derefItem(exPairing.result as ReferenceObject, doc);
}
}

method.params = method.params.map((p) => derefItem(p as ReferenceObject, doc));
method.result = derefItem((method.result as ReferenceObject), doc);
method.params = await derefItems(method.params as ReferenceObject[], doc);
method.result = await derefItem(method.result as ReferenceObject, doc);


let componentSchemas: SchemaComponents = {};
Expand Down

0 comments on commit dcbfa15

Please sign in to comment.