documentation - 如何使用 aws cdk 记录 rest api

标签 documentation api-gateway aws-cdk

我正在使用 AWS CDK 版本 1.22 创建一个 REST API,我也想使用 CDK 记录我的 API,但我没有看到部署后为我的 API 生成的任何文档。

我已经深入研究了 aws 文档、cdk 示例、cdk 引用,但我可以找到具体示例来帮助我理解如何去做。

这是我的代码:

const app = new App();
const api = new APIStack(app, 'APIStack', { env }); // basic api gateway

// API Resources
const resourceProps: APIResourceProps = {
  gateway: api.gateway,
}

// dummy endpoint with some HTTP methods
const siteResource = new APISiteStack(app, 'APISiteStack', {
  env,
  ...resourceProps
});

const siteResourceDocs = new APISiteDocs(app, 'APISiteDocs', {
  env,
  ...resourceProps,
});

// APISiteDocs is defined as follow:
class APISiteDocs extends Stack {

  constructor(scope: Construct, id: string, props: APIResourceProps) {
    super(scope, id, props);

    new CfnDocumentationVersion(this, 'apiDocsVersion', {
      restApiId: props.gateway.restApiId,
      documentationVersion: config.app.name(`API-${config.gateway.api.version}`),
      description: 'Spare-It API Documentation',
    });

    new CfnDocumentationPart(this, 'siteDocs', {
      restApiId: props.gateway.restApiId,
      location: {
        type: 'RESOURCE',
        method: '*',
        path: APISiteStack.apiBasePath,
        statusCode: '405',
      },
      properties: `
        {
          "status": "error",
          "code": 405,
          "message": "Method Not Allowed"
        }
      `,
    });
  }
}

感谢任何帮助/提示,​​谢谢。

最佳答案

我已经使用 CDK 1.31 进行了测试,可以使用 CDK 的默认部署选项,还可以在舞台上添加文档版本。我在 rest api 定义中使用了 deployOptions.documentVersion 来设置 API 文档的版本标识符:

import * as cdk from '@aws-cdk/core';
import * as apigateway from "@aws-cdk/aws-apigateway";
import {CfnDocumentationPart, CfnDocumentationVersion} from "@aws-cdk/aws-apigateway";

export class CdkSftpStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const documentVersion = "v1";

    // create the API
    const api = new apigateway.RestApi(this, 'books-api', {
      deploy: true,
      deployOptions: {
        documentationVersion: documentVersion
      }
    });

    // create GET method on /books resource
    const books = api.root.addResource('books');
    books.addMethod('GET');

    // // create documentation for GET method
    new CfnDocumentationPart(this, 'doc-part1', {
      location: {
        type: 'METHOD',
        method: 'GET',
        path: books.path
      },
      properties: JSON.stringify({
        "status": "successful",
        "code": 200,
        "message": "Get method was succcessful"
      }),
      restApiId: api.restApiId
    });

    new CfnDocumentationVersion(this, 'docVersion1', {
      documentationVersion: documentVersion,
      restApiId: api.restApiId,
      description: 'this is a test of documentation'
    });
  }
}

enter image description here

关于documentation - 如何使用 aws cdk 记录 rest api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60009052/

相关文章:

java - Tyk API Gateway 每 60 秒重试一次请求

microservices - Netflix-Zuul 与 Mashape-Kong

python - 使用 AWS CDK (python) 创建 Glue 作业失败

c# - 如何简化代码文档过程 - visual studio C#

swift - Xcode 8.2.1 不显示自动完成的文档描述

java - 文档 - 它是一个模板吗?

node.js - molecularr-web API 网关。 onError 从未命中

documentation - 在 reStructuredText 中插入相对链接

amazon-web-services - AWS CDK 错误 : bucket policy already exists on bucket

typescript - AWS SecretsManager 值无法解析