@opra/common
Version:
Opra common package
117 lines (116 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiField = void 0;
const objects_1 = require("@jsopen/objects");
const ts_gems_1 = require("ts-gems");
const document_element_js_1 = require("../common/document-element.js");
const constants_js_1 = require("../constants.js");
const api_field_decorator_js_1 = require("../decorators/api-field-decorator.js");
const test_scope_match_js_1 = require("../utils/test-scope-match.js");
const complex_type_base_js_1 = require("./complex-type-base.js");
/**
* @decorator ApiField
*/
exports.ApiField = function (...args) {
// Decorator
if (!this) {
const [options] = args;
return exports.ApiField[constants_js_1.DECORATOR](options);
}
// Constructor
const [owner, initArgs] = args;
document_element_js_1.DocumentElement.call(this, owner);
const _this = (0, ts_gems_1.asMutable)(this);
_this.name = initArgs.name;
const origin = initArgs.origin || owner;
/* istanbul ignore next */
if (!(origin instanceof complex_type_base_js_1.ComplexTypeBase)) {
throw new Error('Field origin should be one of ComplexType, MappedType or MixinType');
}
_this.origin = origin;
_this.scopePattern = initArgs.scopePattern
? Array.isArray(initArgs.scopePattern)
? initArgs.scopePattern
: [initArgs.scopePattern]
: undefined;
_this.type = initArgs.type || owner.node.getDataType('any');
_this.description = initArgs.description;
_this.isArray = initArgs.isArray;
_this.isNestedEntity = initArgs.isNestedEntity;
_this.default = initArgs.default;
_this.fixed = initArgs.fixed;
_this.required = initArgs.required;
_this.exclusive = initArgs.exclusive;
_this.localization = initArgs.localization;
_this.keyField = initArgs.keyField;
_this.deprecated = initArgs.deprecated;
_this.readonly = initArgs.readonly;
_this.writeonly = initArgs.writeonly;
_this.examples = initArgs.examples;
_this.override = initArgs.override;
};
/**
* The ApiFieldClass represents a descriptive metadata structure for API fields,
* supporting features like data type definition, scoping, localization, and constraints.
* This class extends DocumentElement, inheriting base document structure capabilities.
*/
class ApiFieldClass extends document_element_js_1.DocumentElement {
inScope(scope) {
return (0, test_scope_match_js_1.testScopeMatch)(scope, this.scopePattern);
}
forScope(scope) {
if (!(scope && this.override))
return this;
this._overrideCache = this._overrideCache || {};
let field = this._overrideCache[scope];
if (field)
return field;
if (scope !== '*') {
for (const o of this.override) {
if ((0, test_scope_match_js_1.testScopeMatch)(scope, o.scopePattern)) {
field = (0, objects_1.omitUndefined)({
...o,
id: undefined,
owner: undefined,
node: undefined,
origin: undefined,
name: undefined,
type: undefined,
override: undefined,
_overrideCache: undefined,
scopePattern: o.scopePattern,
});
Object.setPrototypeOf(field, this);
break;
}
}
}
field = field || this;
this._overrideCache[scope] = field;
return field;
}
toJSON(options) {
const typeName = this.type
? this.node.getDataTypeNameWithNs(this.type)
: undefined;
return (0, objects_1.omitUndefined)({
type: typeName ? typeName : this.type?.toJSON(options),
description: this.description,
isArray: this.isArray || undefined,
isNestedEntity: this.isNestedEntity || undefined,
default: this.default,
fixed: this.fixed,
required: this.required || undefined,
exclusive: this.exclusive || undefined,
localization: this.localization || undefined,
keyField: this.keyField || undefined,
deprecated: this.deprecated || undefined,
readonly: this.readonly || undefined,
writeonly: this.writeonly || undefined,
examples: this.examples,
});
}
}
exports.ApiField.prototype = ApiFieldClass.prototype;
Object.assign(exports.ApiField, api_field_decorator_js_1.ApiFieldDecoratorFactory);
exports.ApiField[constants_js_1.DECORATOR] = api_field_decorator_js_1.ApiFieldDecoratorFactory;