OpenAPI document
OasResponse
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 OasBaseExamples
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' })
}
})
})
}
});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' })
})
})
}
});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'
})
})
}
});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']
})
})
}
});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'
})
})
}
});const noContentResponse = new OasResponse({
description: 'Operation completed successfully, no content returned'
// No content or headers - represents HTTP 204 No Content
});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
Methods
isRef
isRef(): this is OasRef<'response'>isReadOnly
isReadOnly(): booleanAlways false: readOnly is a schema keyword.
isWriteOnly
isWriteOnly(): booleanAlways false: writeOnly is a schema keyword.
resolve
resolve(): OasResponseresolveOnce
resolveOnce(): OasResponsetoSchema
toSchema(mediaType?: string): OasSchema | OasRef<'schema'> | undefinedThe 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 | undefinedThe media type this response is read from (JSON-like, or *\/*), and its schema. See toJsonContent.
toJsonSchema
toJsonSchema(
options: ToJsonSchemaOptions
): OpenAPIV3.ResponseObject & { summary?: string }