【问题标题】:How to document rest api using aws cdk如何使用 aws cdk 记录 rest api
【发布时间】:2021-06-10 23:41:44
【问题描述】:

我正在使用 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"
        }
      `,
    });
  }
}

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

【问题讨论】:

  • 我还没用过,但我一直在推荐 TSDoc。 github.com/microsoft/tsdoc
  • @citadelgrad 谢谢,我更喜欢 CDK 方式而不是 TSDoc

标签: documentation api-gateway aws-cdk


【解决方案1】:

据我所知,如果您使用 CDK 的默认部署选项来代表您创建阶段和部署,则无法在阶段附加文档版本集。

相反,解决方案是将 RESTAPI 的选项对象设置为 deploy:false 并手动定义阶段和部署。

stack.ts 代码

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

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

    // create the API, need to not rely on CFN's automatic deployment because we need to 
    // make our own deployment to set the documentation we create
    const api = new apigateway.RestApi(this, 'books-api',{
      deploy: false
    });

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

    // // create documentation for GET method
    const docpart = 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
    });

    const doc = new CfnDocumentationVersion(this, 'docVersion1', {
      documentationVersion: 'version1',
      restApiId: api.restApiId,
      description: 'this is a test of documentation'
    });
    // not sure if this is necessary but it made sense to me
    doc.addDependsOn(docpart);

    const deployment = api.latestDeployment ? api.latestDeployment: new Deployment(this,'newDeployment',{
      api: api,
      description: 'new deployment, API Gateway did not make one'
    });
    // create stage of api with documentation version
    const stage = new Stage(this, 'books-api-stage1', {
      deployment:  deployment,
      documentationVersion: doc.documentationVersion,
      stageName: 'somethingOtherThanProd'
    });
  }
}

输出

在此处为此选项创建了feature request

【讨论】:

  • 非常感谢。我会试试看告诉你
【解决方案2】:

我已经使用 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'
    });
  }
}

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。 CfnDocumentationVersion 调用必须在您创建所有 CfnDocumentationPart 之后发生。以您的代码为例,它应该如下所示:

    class APISiteDocs extends Stack {
    
      constructor(scope: Construct, id: string, props: APIResourceProps) {
        super(scope, id, props);
    
        new CfnDocumentationPart(this, 'siteDocs', {
          restApiId: props.gateway.restApiId,
          location: {
            type: 'RESOURCE',
            method: '*',
            path: APISiteStack.apiBasePath,
            statusCode: '405',
          },
          properties: JSON.stringify({
            "status": "error",
            "code": 405,
            "message": "Method Not Allowed"
          }),
        });
    
        new CfnDocumentationVersion(this, 'apiDocsVersion', {
          restApiId: props.gateway.restApiId,
          documentationVersion: config.app.name(`API-${config.gateway.api.version}`),
          description: 'Spare-It API Documentation',
        });
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2016-12-06
      • 2020-02-02
      相关资源
      最近更新 更多