UNPKG

@opra/common

Version:
96 lines (95 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpMediaType = void 0; const tslib_1 = require("tslib"); const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is")); const objects_1 = require("@jsopen/objects"); const ts_gems_1 = require("ts-gems"); const valgen_1 = require("valgen"); const document_element_js_1 = require("../common/document-element.js"); const data_type_js_1 = require("../data-type/data-type.js"); exports.HttpMediaType = function (owner, initArgs) { if (!this) throw new TypeError('"this" should be passed to call class constructor'); document_element_js_1.DocumentElement.call(this, owner); const _this = (0, ts_gems_1.asMutable)(this); if (initArgs.contentType) { let arr = Array.isArray(initArgs.contentType) ? initArgs.contentType : [initArgs.contentType]; arr = arr.map(x => x.split(/\s*,\s*/)).flat(); _this.contentType = arr.length > 1 ? arr : arr[0]; } _this.description = initArgs.description; _this.contentEncoding = initArgs.contentEncoding; _this.examples = initArgs.examples; _this.multipartFields = []; _this.maxFieldsSize = initArgs.maxFieldsSize; _this.maxFields = initArgs.maxFields; _this.maxFiles = initArgs.maxFiles; _this.maxFileSize = initArgs.maxFileSize; _this.maxTotalFileSize = initArgs.maxTotalFileSize; if (initArgs?.type) { _this.type = initArgs?.type instanceof data_type_js_1.DataType ? initArgs.type : _this.owner.node.getDataType(initArgs.type); } _this.isArray = initArgs.isArray; }; /** * @class HttpMediaType */ class HttpMediaTypeClass extends document_element_js_1.DocumentElement { findMultipartField(fieldName, fieldType) { if (!this.multipartFields) return; for (const f of this.multipartFields) { if ((!fieldType || fieldType === f.fieldType) && ((f.fieldName instanceof RegExp && f.fieldName.test(fieldName)) || f.fieldName === fieldName)) { return f; } } } toJSON(options) { const typeName = this.type ? this.node.getDataTypeNameWithNs(this.type) : undefined; const out = (0, objects_1.omitUndefined)({ description: this.description, contentType: this.contentType, contentEncoding: this.contentEncoding, type: typeName ? typeName : this.type?.toJSON(options), isArray: this.isArray, example: this.example, examples: this.examples, maxFields: this.maxFields, maxFieldsSize: this.maxFieldsSize, maxFiles: this.maxFiles, maxFileSize: this.maxFileSize, maxTotalFileSize: this.maxTotalFileSize, }); if (this.multipartFields?.length) { out.multipartFields = this.multipartFields.map(x => x.toJSON(options)); } return out; } generateCodec(codec, options) { let fn; if (this.type) { fn = this.type.generateCodec(codec, options); } else if (this.contentType) { const arr = Array.isArray(this.contentType) ? this.contentType : [this.contentType]; if (arr.find(ct => type_is_1.default.is(ct, ['json']))) { fn = this.node.findDataType('object').generateCodec(codec); } } fn = fn || valgen_1.isAny; return this.isArray ? valgen_1.vg.isArray(fn) : fn; } } exports.HttpMediaType.prototype = HttpMediaTypeClass.prototype;