Options
All
  • Public
  • Public/Protected
  • All
Menu

@open-rpc/schema-utils-js

Index

Type aliases

ReferenceResolver

ReferenceResolver: typeof referenceResolver

TGetOpenRPCDocument

TGetOpenRPCDocument: (schema: string) => Promise<OpenRPC>

Type declaration

    • (schema: string): Promise<OpenRPC>
    • Parameters

      • schema: string

      Returns Promise<OpenRPC>

TPredicate

TPredicate: (value: any) => boolean

Type declaration

    • (value: any): boolean
    • Parameters

      • value: any

      Returns boolean

Variables

isUrl

isUrl: isUrl

Const parseOpenRPCDocument

parseOpenRPCDocument: parseOpenRPCDocument = makeParseOpenRPCDocument(fetchUrlSchema, readSchemaFromFile)

GenerateID Functions

generateMethodParamId

  • generateMethodParamId(method: MethodObject, contentDescriptor: ContentDescriptorObject): string
  • Create a unique identifier for a parameter within a given method. This is typically used to create hashmap keys for method to parameter mappings.

    throws

    ContentDescriptorNotFoundInMethodError

    example
    
    const { generateMethodParamId }
    const methodObject = {
      name: "foo",
      params: [{
        name: "fooParam",
        schema: { type: "integer" }
      }],
      result: {}
    };
    const paramId = generateMethodParamId(methodObject, methodObject.params[0]);
    console.log(paramId);
    // outputs:
    // "foo/0/fooParam"

    Parameters

    • method: MethodObject

      The OpenRPC Method which encloses the content descriptor

    • contentDescriptor: ContentDescriptorObject

      The OpenRPC Content Descriptor that is a param in the method

    Returns string

    an ID for the param/method combo. It follows the format {method.name}/{indexWithinParams}|{contentDescriptor.name} where:

    1. if the method's parameter structure is "by-name", the format returned uses the contentDescriptor.name
    2. otherwise, the return value will use the params index in the list of params.

generateMethodResultId

  • generateMethodResultId(method: MethodObject, contentDescriptor: ContentDescriptorObject): string
  • Create a unique identifier for a result within a given method. This is typically used to create hashmap keys for method to result mappings.

    throws

    ContentDescriptorNotFoundInMethodError

    example
    
    const { generateMethodResultId }
    const methodObject = {
      name: "foo",
      params: [],
      result: {
        name: "fooResult",
        schema: { type: "string" }
      }
    };
    const resultId = generateMethodResultId(methodObject, methodObject.result);
    console.log(paramId);
    // outputs:
    // "foo/result"

    Parameters

    • method: MethodObject

      The OpenRPC Method which encloses the content descriptor

    • contentDescriptor: ContentDescriptorObject

      The OpenRPC Content Descriptor (either a method param or the result).

    Returns string

    an ID for the result/method combo. It follows the format {method.name}/result.

Other Functions

Const compact

  • compact(array: any[]): any[]
  • Removes false and null values from array

    Parameters

    • array: any[]

    Returns any[]

Const derefItem

  • derefItem(item: ReferenceObject, doc: OpenRPC, resolver: ReferenceResolver): Promise<any>
  • Parameters

    • item: ReferenceObject
    • doc: OpenRPC
    • resolver: ReferenceResolver

    Returns Promise<any>

Const derefItems

  • derefItems(items: ReferenceObject[], doc: OpenRPC, resolver: ReferenceResolver): Promise<any[]>
  • Parameters

    • items: ReferenceObject[]
    • doc: OpenRPC
    • resolver: ReferenceResolver

    Returns Promise<any[]>

dereferenceDocument

  • dereferenceDocument(openrpcDocument: OpenRPC, resolver: ReferenceResolver): Promise<OpenRPC>
  • replaces $ref's within a document and its schemas. The replaced value will be a javascript object reference to the real schema / open-rpc component

    throws

    OpenRPCDocumentDereferencingError

    example
    
    import { OpenRPC } from "@open-rpc/meta-schema"
    import { dereferenceDocument } from "@open-rpc/schema-utils-js";
    
    try {
      const dereffedDocument = await dereferenceDocument({ ... }) as OpenRPC;
    } catch (e) {
      // handle validation errors
    }

    Parameters

    • openrpcDocument: OpenRPC
    • resolver: ReferenceResolver

    Returns Promise<OpenRPC>

    The same OpenRPC Document that was passed in, but with all $ref's dereferenced.

Const fetchUrlSchema

  • fetchUrlSchema(schema: string): Promise<OpenrpcDocument>

Const find

  • finds an array elements which matches the predicate

    Parameters

    Returns any

    || {undefined}

Const findIndex

  • findIndex(array: any[], predicate: TPredicate): number
  • finds array index of array object which matches predicate

    Parameters

    Returns number

    || {undefined}

Const handleMethod

  • handleMethod(methodOrRef: MethodOrReference, doc: OpenrpcDocument, resolver: ReferenceResolver): Promise<MethodObject>
  • Parameters

    • methodOrRef: MethodOrReference
    • doc: OpenrpcDocument
    • resolver: ReferenceResolver

    Returns Promise<MethodObject>

Const handleSchemaComponents

  • handleSchemaComponents(doc: OpenrpcDocument): Promise<OpenrpcDocument>

Const handleSchemaWithSchemaComponents

  • handleSchemaWithSchemaComponents(s: JSONSchema, schemaComponents: SchemaComponents | undefined): Promise<JSONSchema>
  • Parameters

    • s: JSONSchema
    • schemaComponents: SchemaComponents | undefined

    Returns Promise<JSONSchema>

Const handleSchemasInsideContentDescriptorComponents

  • handleSchemasInsideContentDescriptorComponents(doc: OpenrpcDocument): Promise<OpenrpcDocument>

makeCustomResolver

  • makeCustomResolver(protocolMapHandler: ProtocolHandlerMap): ReferenceResolver

Const makeParseOpenRPCDocument

Const noop

  • noop(schema: string): Promise<OpenrpcDocument>
  • Parameters

    • schema: string

    Returns Promise<OpenrpcDocument>

Const readSchemaFromFile

  • readSchemaFromFile(filePath: string): Promise<OpenrpcDocument>

Const rpcDocIsEqual

  • rpcDocIsEqual(doc1: any, doc2: any): boolean

validateOpenRPCDocument

  • Returns any JSON Schema validation errors that are found with the OpenRPC document passed in.

    example
    
    const { types } from "@open-rpc/meta-schema"
    const { validateOpenRPCDocument } from "@open-rpc/schema-utils-js";
    const badOpenRPCDocument = {};
    const errors = validateOpenRPCDocument({});
    if (errors) {
      // handle errors
    }

    Parameters

    • document: OpenRPC

      OpenRPC Document to validate.

    Returns OpenRPCDocumentValidationError | true

    Either true if everything checks out, or a well formatted error.

Object literals

Const defaultParseOpenRPCDocumentOptions

defaultParseOpenRPCDocumentOptions: object

dereference

dereference: boolean = true

validate

validate: boolean = true