I am currently in the process of developing a TypeScript AWS CDK to set up an API Gateway along with its own Swagger documentation. One of the requirements is to create a simple endpoint that returns a list of "Supplier", but I am facing challenges in specifying this in the CDK.
Below is the code snippet:
export function CreateSupplierMethods(apigw: apigateway.Resource,restApiId: string, scope: cdk.Construct, api: apigateway.RestApi) {
let suppliers = apigw.addResource('suppliers')
let supplierModel = new apigateway.Model(scope, "supplier-model", {
modelName: "supplier",
restApi: api,
contentType: 'application/json',
schema: {
description: "Supplier data",
title: "Supplier",
properties: {
code: { type: apigateway.JsonSchemaType.STRING, minLength: 4, maxLength: 6},
name: { type: apigateway.JsonSchemaType.STRING, maxLength: 81},
}
},
})
let getSuppliers = suppliers.addMethod('GET', new apigateway.MockIntegration(), {
methodResponses: [{
statusCode: "200",
responseModels: {
"application/json": supplierModel,
}
},
{
statusCode: "401",
}]
})
}
The challenge lies in defining how the GET method should return a list of supplierModel. I also need to use this model for both lists of suppliers and individual supplier instances (e.g., a GET method with ID as input).
Is it possible to achieve this? If so, how can it be done?
Upon reviewing the generated JSON, my goal is to have an output similar to the following:
https://i.sstatic.net/FURgp.png
However, the current result looks quite different:
https://i.sstatic.net/xFg8H.png
How can I modify the configuration to obtain a result similar to the first image?