class Model (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.APIGateway.Model |
Java | software.amazon.awscdk.services.apigateway.Model |
Python | aws_cdk.aws_apigateway.Model |
TypeScript (source) | @aws-cdk/aws-apigateway » Model |
Implements
IConstruct
, IConstruct
, IDependable
, IResource
, IModel
Example
declare const api: apigateway.RestApi;
// We define the JSON Schema for the transformed valid response
const responseModel = api.addModel('ResponseModel', {
contentType: 'application/json',
modelName: 'ResponseModel',
schema: {
schema: apigateway.JsonSchemaVersion.DRAFT4,
title: 'pollResponse',
type: apigateway.JsonSchemaType.OBJECT,
properties: {
state: { type: apigateway.JsonSchemaType.STRING },
greeting: { type: apigateway.JsonSchemaType.STRING }
}
}
});
// We define the JSON Schema for the transformed error response
const errorResponseModel = api.addModel('ErrorResponseModel', {
contentType: 'application/json',
modelName: 'ErrorResponseModel',
schema: {
schema: apigateway.JsonSchemaVersion.DRAFT4,
title: 'errorResponse',
type: apigateway.JsonSchemaType.OBJECT,
properties: {
state: { type: apigateway.JsonSchemaType.STRING },
message: { type: apigateway.JsonSchemaType.STRING }
}
}
});
Initializer
new Model(scope: Construct, id: string, props: ModelProps)
Parameters
- scope
Construct
- id
string
- props
Model
Props
Construct Props
Name | Type | Description |
---|---|---|
rest | IRest | The rest API that this model is part of. |
schema | Json | The schema to use to transform data to one or more output formats. |
content | string | The content type for the model. |
description? | string | A description that identifies this model. |
model | string | A name for the model. |
restApi
Type:
IRest
The rest API that this model is part of.
The reason we need the RestApi object itself and not just the ID is because the model is being tracked by the top-level RestApi object for the purpose of calculating it's hash to determine the ID of the deployment. This allows us to automatically update the deployment when the model of the REST API changes.
schema
Type:
Json
The schema to use to transform data to one or more output formats.
Specify null ({}) if you don't want to specify a schema.
contentType?
Type:
string
(optional, default: 'application/json')
The content type for the model.
You can also force a content type in the request or response model mapping.
description?
Type:
string
(optional, default: None)
A description that identifies this model.
modelName?
Type:
string
(optional, default:
A name for the model.
Important If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.
Properties
Name | Type | Description |
---|---|---|
env | Resource | The environment this resource belongs to. |
model | string | Returns the model name, such as 'myModel'. |
node | Construct | The construct tree node associated with this construct. |
stack | Stack | The stack in which this resource is defined. |
static EMPTY_MODEL | IModel | Represents a reference to a REST API's Empty model, which is available as part of the model collection by default. |
static ERROR_MODEL | IModel | Represents a reference to a REST API's Error model, which is available as part of the model collection by default. |
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.
modelId
Type:
string
Returns the model name, such as 'myModel'.
node
Type:
Construct
The construct tree node associated with this construct.
stack
Type:
Stack
The stack in which this resource is defined.
static EMPTY_MODEL
Type:
IModel
Represents a reference to a REST API's Empty model, which is available as part of the model collection by default.
This can be used for mapping JSON responses from an integration to what is returned to a client, where strong typing is not required. In the absence of any defined model, the Empty model will be used to return the response payload unmapped.
Definition { "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "Empty Schema", "type" : "object" }
static ERROR_MODEL
Type:
IModel
Represents a reference to a REST API's Error model, which is available as part of the model collection by default.
This can be used for mapping error JSON responses from an integration to a client, where a simple generic message field is sufficient to map and return an error payload.
Definition { "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "Error Schema", "type" : "object", "properties" : { "message" : { "type" : "string" } } }
Methods
Name | Description |
---|---|
apply | Apply the given removal policy to this resource. |
to | Returns a string representation of this construct. |
static from |
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
Policy
Apply the given removal policy to this resource.
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY
), or left in your AWS
account for data recovery and cleanup later (RemovalPolicy.RETAIN
).
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
ModelName(scope, id, modelName)
static frompublic static fromModelName(scope: Construct, id: string, modelName: string): IModel
Parameters
- scope
Construct
- id
string
- modelName
string
Returns