Skip to main content

HttpMediaType

Package: @opra/common

HttpMediaType is the base class that describes a typed content block — a body payload with an optional MIME type, an OPRA data type for validation, and multipart field declarations. It is extended by both HttpRequestBody content entries and HttpOperationResponse.


Inheritance

DocumentElement
└── HttpMediaType
├── HttpOperationResponse
└── HttpMultipartField

Properties

PropertyTypeDescription
typeDataType | undefinedThe OPRA data type used to decode/encode the body.
contentTypestring | string[] | undefinedExpected MIME type(s) (e.g. 'application/json', ['application/json', 'application/xml']).
contentEncodingstring | undefinedContent encoding (e.g. 'gzip').
descriptionstring | undefinedHuman-readable description.
isArrayboolean | undefinedWhether the body is an array of the declared type.
examplestring | undefinedA single inline example.
examplesRecord<string, string> | undefinedNamed examples.
multipartFieldsHttpMultipartField[]Declared multipart fields (files and form fields).
maxFieldsnumber | undefinedMaximum number of multipart fields.
maxFieldsSizenumber | undefinedMaximum total size of non-file fields in bytes.
maxFilesnumber | undefinedMaximum number of uploaded files.
maxFileSizenumber | undefinedMaximum size per uploaded file in bytes.
maxTotalFileSizenumber | undefinedMaximum total size of all uploaded files in bytes.

Methods

findMultipartField(fieldName, fieldType?)

Finds a declared multipart field by name (case-insensitive string or RegExp) and optional type ('file' or 'field').

mediaType.findMultipartField('avatar', 'file');
mediaType.findMultipartField('description', 'field');
findMultipartField(
fieldName: string,
fieldType?: 'file' | 'field'
): HttpMultipartField | undefined

generateCodec(codec, options?)

Compiles a validation/transformation function for the body's data type. If the body has no declared type but specifies a JSON content type, falls back to an object validator. Wraps in an array validator when isArray is true.

generateCodec(
codec: 'encode' | 'decode',
options?: DataType.GenerateCodecOptions
): Validator

toJSON(options?)

Returns an OpraSchema.HttpMediaType plain object for schema export.

toJSON(options?: ApiDocument.ExportOptions): OpraSchema.HttpMediaType

HttpRequestBody · HttpOperationResponse · HttpMultipartField