UNPKG

@opra/common

Version:
101 lines (100 loc) 3.76 kB
import typeIs from '@browsery/type-is'; import { omitUndefined } from '@jsopen/objects'; import { asMutable } from 'ts-gems'; import { isAny, vg } from 'valgen'; import { DocumentElement } from '../common/document-element.js'; import { ArrayType } from '../data-type/array-type.js'; import { DataType } from '../data-type/data-type.js'; export const HttpMediaType = function (owner, initArgs) { if (!this) throw new TypeError('"this" should be passed to call class constructor'); DocumentElement.call(this, owner); const _this = 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 DataType ? initArgs.type : _this.owner.node.getDataType(initArgs.type); } _this.isArray = initArgs.isArray; _this.designType = initArgs.designType; }; /** * @class HttpMediaType */ class HttpMediaTypeClass extends 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 = 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, properties) { let fn; if (this.type) { fn = this.type.generateCodec(codec, options, { designType: this.designType, }); } else if (this.contentType) { const arr = Array.isArray(this.contentType) ? this.contentType : [this.contentType]; if (arr.find(ct => typeIs.is(ct, ['json']))) { fn = this.node.findDataType('object').generateCodec(codec, options, { ...properties, designType: this.designType, }); } } fn = fn || isAny; return this.isArray && !(this.type instanceof ArrayType) ? vg.isArray(fn) : fn; } } HttpMediaType.prototype = HttpMediaTypeClass.prototype;