@deployport/specular-runtime
Version:
Runtime for Specular API clients with support for Node.js and Browser
46 lines (45 loc) • 1.33 kB
JavaScript
export default class Resource {
name;
package;
parent;
operations = [];
resources = [];
packageUniqueName;
constructor(pkg, name, parent = null) {
this.name = name;
this.package = pkg;
this.parent = parent;
if (parent) {
parent._addResource(this);
this.packageUniqueName = parent.packageUniqueName + name;
}
else {
pkg._addResource(this);
this.packageUniqueName = name;
}
}
operationByName(name) {
return this.operations.find(operation => operation.name === name);
}
_addOperation(operation) {
if (this.operationByName(operation.name)) {
throw new Error(`duplicate operation name ${operation.name}`);
}
this.operations.push(operation);
}
_addResource(resource) {
if (this.resourceByName(resource.name)) {
throw new Error(`duplicate resource name ${resource.name}`);
}
this.resources.push(resource);
}
/**
* Resource by name returns the resource with the given name
* or undefined if not found
* @param name name of the resource
* @returns
*/
resourceByName(name) {
return this.resources.find(resource => resource.name === name) || null;
}
}