@opra/common
Version:
Opra common package
50 lines (49 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiFieldDecoratorFactory = ApiFieldDecoratorFactory;
const objects_1 = require("@jsopen/objects");
const index_js_1 = require("../../schema/index.js");
const constants_js_1 = require("../constants.js");
function ApiFieldDecoratorFactory(options) {
const decoratorChain = [];
/**
*
*/
const decorator = function (target, propertyKey) {
if (typeof propertyKey !== 'string')
throw new TypeError(`Symbol properties can't be used as a field`);
const metadata = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, target.constructor) ||
{};
metadata.kind = index_js_1.OpraSchema.ComplexType.Kind;
metadata.fields = metadata.fields || {};
const designType = Reflect.getMetadata('design:type', target, propertyKey);
const elemMeta = (metadata.fields[propertyKey] = {
...options,
});
if (designType === Array) {
elemMeta.isArray = true;
}
else
elemMeta.type = elemMeta.type || designType;
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, target.constructor);
for (const fn of decoratorChain)
fn(elemMeta);
};
/**
*
*/
decorator.Override = (scopes, opts) => {
decoratorChain.push((meta) => {
meta.override = meta.override || [];
meta.override.push((0, objects_1.omitUndefined)({
...opts,
scopePattern: Array.isArray(scopes) ? scopes : [scopes],
type: undefined,
isArray: undefined,
isNestedEntity: undefined,
}));
});
return decorator;
};
return decorator;
}