@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
70 lines (69 loc) • 2.02 kB
JavaScript
import { mapHeaders } from "../utils/mapHeaders.js";
import { toJsonMapCollection } from "../utils/toJsonMapCollection.js";
import { JsonMap } from "./JsonMap.js";
import { JsonMedia } from "./JsonMedia.js";
/**
* Represents an HTTP response definition for OpenAPI specifications.
*
* JsonResponse defines the structure of an HTTP response including status code,
* description, headers, and content types. It provides a fluent API for building
* response metadata compatible with OpenAPI 3 specifications.
*
* ### Usage
*
* ```typescript
* const response = new JsonResponse()
* .description("User created successfully")
* .headers({"X-Rate-Limit": "100"})
* .content({
* "application/json": {
* schema: userSchema
* }
* });
* ```
*
* ### Key Features
*
* - **Status Codes**: Associated HTTP status code
* - **Headers**: Response header definitions
* - **Content Types**: Multiple media type support
* - **Schema Integration**: JSON schemas for response bodies
*
* @public
*/
export class JsonResponse extends JsonMap {
constructor(obj = {}) {
super(obj);
this.$kind = "operationResponse";
this.content(obj.content || {});
}
description(description) {
this.set("description", description);
return this;
}
headers(headers) {
this.set("headers", mapHeaders(headers));
return this;
}
content(content) {
this.set("content", toJsonMapCollection(content, JsonMedia));
return this;
}
getContent() {
return this.get("content");
}
getMedia(mediaType, create = true) {
create && this.addMedia(mediaType);
return this.getContent()?.get(mediaType);
}
addMedia(mediaType) {
const content = this.get("content");
if (!content.has(mediaType)) {
content.set(mediaType, new JsonMedia());
}
return this;
}
isBinary() {
return this.getContent().has("application/octet-stream");
}
}