@open-rpc/schema-utils-js
Version:
<center> <span> <img alt="CircleCI branch" src="https://img.shields.io/circleci/project/github/open-rpc/schema-utils-js/master.svg"> <img src="https://codecov.io/gh/open-rpc/schema-utils-js/branch/master/graph/badge.svg" /> <img alt="npm" sr
129 lines (128 loc) • 4.52 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateMethodResultId = exports.generateMethodParamId = exports.ContentDescriptorNotFoundInMethodError = void 0;
var helper_functions_1 = require("./helper-functions");
/**
* Provides an error interface for handling when we are unable to find a contentDescriptor in a methodObject
* when it is expected.
*
* @category Errors
*
*/
var ContentDescriptorNotFoundInMethodError = /** @class */ (function () {
/**
* @param method OpenRPC Method which was used for the lookup
* @param contentDescriptor OpenRPC Content Descriptor that was expected to be in the method param.
*/
function ContentDescriptorNotFoundInMethodError(method, contentDescriptor) {
this.method = method;
this.contentDescriptor = contentDescriptor;
this.name = "OpenRPCDocumentDereferencingError";
this.message = [
"Content Descriptor not found in method.",
"Method: ".concat(JSON.stringify(method, undefined, " ")),
"ContentDescriptor: ".concat(JSON.stringify(contentDescriptor, undefined, " ")),
].join("\n");
}
return ContentDescriptorNotFoundInMethodError;
}());
exports.ContentDescriptorNotFoundInMethodError = ContentDescriptorNotFoundInMethodError;
/**
* Create a unique identifier for a parameter within a given method.
* This is typically used to create hashmap keys for method to parameter mappings.
*
* @param method The OpenRPC Method which encloses the content descriptor
* @param contentDescriptor The OpenRPC Content Descriptor that is a param in the method
*
* @returns 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
* 1. otherwise, the return value will use the params index in the list of params.
*
* @throws [[ContentDescriptorNotFoundInMethodError]]
*
* @example
* ```typescript
*
* 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"
* ```
*
* @category GenerateID
*
*/
function generateMethodParamId(method, contentDescriptor) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var pos = (0, helper_functions_1.findIndex)(method.params, function (o) {
return o.name == contentDescriptor.name;
});
if (pos === -1) {
throw new ContentDescriptorNotFoundInMethodError(method, contentDescriptor);
}
var paramId;
if (method.paramStructure === "by-position") {
paramId = pos.toString();
}
else if (method.paramStructure === "by-name") {
var paramCD = method.params[pos];
paramId = paramCD.name;
}
else {
var paramCD = method.params[pos];
paramId = "".concat(paramCD.name, "/").concat(pos.toString());
}
return "".concat(method.name, "/").concat(paramId);
}
exports.generateMethodParamId = generateMethodParamId;
/**
* Create a unique identifier for a result within a given method.
* This is typically used to create hashmap keys for method to result mappings.
*
* @param method The OpenRPC Method which encloses the content descriptor
* @param contentDescriptor The OpenRPC Content Descriptor (either a method param or the result).
*
* @returns an ID for the result/method combo.
* It follows the format `{method.name}/result`.
*
* @throws [[ContentDescriptorNotFoundInMethodError]]
*
* @example
* ```typescript
*
* 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"
* ```
*
* @category GenerateID
*
*/
function generateMethodResultId(method, contentDescriptor) {
var result = method.result;
if (result.name !== contentDescriptor.name) {
throw new ContentDescriptorNotFoundInMethodError(method, contentDescriptor);
}
return "".concat(method.name, "/result");
}
exports.generateMethodResultId = generateMethodResultId;
;