Docs@skmtc/core

OpenAPI document

CLASS

OasResponse

@skmtc/core 0.63.4

Represents a Response Object in the OpenAPI Specification.

The OasResponse class encapsulates the definition of a single HTTP response, including its description, headers, and content for different media types. Responses describe the possible outcomes of API operations and can include both successful and error scenarios.

This class provides comprehensive support for response definitions with typed content, custom headers, and multiple media type representations.

Key Features

  • Content Types: Support for multiple media types (JSON, XML, HTML, etc.)
  • Header Definitions: Custom response headers with validation
  • Schema Integration: Typed response bodies through schema definitions
  • Reference Support: Can reference reusable response components
  • Documentation: Rich description support for API documentation
  • JSON Schema: Converts to standard JSON Schema format for validation

Signature

import { OasResponse } from '@skmtc/core'

class OasResponse extends OasBase

Examples

Basic JSON response
import { OasResponse, OasMediaType, OasObject, OasString } from '@skmtc/core';

const successResponse = new OasResponse({
  description: 'User retrieved successfully',
  content: {
    'application/json': new OasMediaType({
      schema: new OasObject({
        properties: {
          id: new OasString(),
          name: new OasString(),
          email: new OasString({ format: 'email' })
        }
      })
    })
  }
});
Response with custom headers
const paginatedResponse = new OasResponse({
  description: 'Paginated list of items',
  headers: {
    'X-Total-Count': new OasHeader({
      description: 'Total number of items',
      schema: new OasInteger({ minimum: 0 })
    }),
    'X-Page-Number': new OasHeader({
      description: 'Current page number',
      schema: new OasInteger({ minimum: 1 })
    }),
    'Link': new OasHeader({
      description: 'Pagination links',
      schema: new OasString()
    })
  },
  content: {
    'application/json': new OasMediaType({
      schema: new OasArray({
        items: new OasRef({ $ref: '#/components/schemas/Item' })
      })
    })
  }
});
Multiple content types
const multiFormatResponse = new OasResponse({
  description: 'User data in multiple formats',
  content: {
    'application/json': new OasMediaType({
      schema: new OasRef({ $ref: '#/components/schemas/User' })
    }),
    'application/xml': new OasMediaType({
      schema: new OasObject({
        // XML-specific schema definition
        properties: {
          user: new OasRef({ $ref: '#/components/schemas/User' })
        }
      })
    }),
    'text/csv': new OasMediaType({
      schema: new OasString({
        description: 'User data in CSV format'
      })
    })
  }
});
Error responses
const errorResponse = new OasResponse({
  description: 'Validation error occurred',
  content: {
    'application/json': new OasMediaType({
      schema: new OasObject({
        properties: {
          error: new OasString({ description: 'Error message' }),
          code: new OasString({ description: 'Error code' }),
          details: new OasArray({
            items: new OasObject({
              properties: {
                field: new OasString(),
                message: new OasString()
              }
            })
          })
        },
        required: ['error', 'code']
      })
    })
  }
});
File download response
const fileDownloadResponse = new OasResponse({
  description: 'File download',
  headers: {
    'Content-Disposition': new OasHeader({
      description: 'Attachment filename',
      schema: new OasString({ example: 'attachment; filename="report.pdf"' })
    }),
    'Content-Length': new OasHeader({
      description: 'File size in bytes',
      schema: new OasInteger({ minimum: 0 })
    })
  },
  content: {
    'application/pdf': new OasMediaType({
      schema: new OasString({
        format: 'binary',
        description: 'PDF file content'
      })
    }),
    'application/octet-stream': new OasMediaType({
      schema: new OasString({
        format: 'binary',
        description: 'Generic binary content'
      })
    })
  }
});
No content response
const noContentResponse = new OasResponse({
  description: 'Operation completed successfully, no content returned'
  // No content or headers - represents HTTP 204 No Content
});
Using in operation definitions
const getUserOperation = new OasOperation({
  path: '/users/{id}',
  method: 'get',
  responses: {
    '200': successResponse,
    '404': new OasResponse({
      description: 'User not found',
      content: {
        'application/json': new OasMediaType({
          schema: new OasObject({
            properties: {
              error: new OasString({ example: 'User not found' }),
              code: new OasString({ example: 'USER_NOT_FOUND' })
            }
          })
        })
      }
    }),
    '500': new OasRef({ $ref: '#/components/responses/InternalServerError' })
  }
});

Constructor

constructor

constructor(fields: ResponseFields, context?: ParseContextType)

Properties

oasType, 'response'
summary, string | undefined
description, string | undefined
headers, Record<string, OasHeader | OasRef<'header'>> | undefined
links, Record<string, OasLink | OasRef<'link'>> | undefined
content, Record<string, OasMediaType> | undefined
extensionFields, Record<string, unknown> | undefined

Methods

isRef

isRef(): this is OasRef<'response'>

isReadOnly

isReadOnly(): boolean

Always false: readOnly is a schema keyword.

isWriteOnly

isWriteOnly(): boolean

Always false: writeOnly is a schema keyword.

resolve

resolve(): OasResponse

resolveOnce

resolveOnce(): OasResponse

toSchema

toSchema(mediaType?: string): OasSchema | OasRef<'schema'> | undefined

The body schema for mediaType, an exact key lookup. Without mediaType, the schema of the JSON media type that toJsonContent picks: application/json, else the first JSON-like type such as application/json;v=1.0 or application/vnd.api+json, else *\/* when its schema is an object, array, union, intersection or $ref. undefined when there is no such media type or it has no schema.

toJsonContent

toJsonContent(): JsonContent | undefined

The media type this response is read from (JSON-like, or *\/*), and its schema. See toJsonContent.

toJsonSchema

toJsonSchema(
  options: ToJsonSchemaOptions
): OpenAPIV3.ResponseObject & { summary?: string }