如何使用 Node sdk 在 azure 函数的代码中生成 openAPI 规范?

ave*_*mia 3 node.js swagger typescript azure-functions openapi

我在 azure 函数中定义了一个 api,我想以可以生成 openAPI 规范的方式对其进行注释。

我尝试过使用 tsoa,但它似乎与无服务器不兼容。目前,openAPI 规范是使用 swagger-jsdoc 生成的,但在注释中包含描述符是不可维护的。我想要类似于 .NET 端 swagger 的工作方式,其中我可以使用路由信息注释函数,并且库将生成 openAPI 规范。这对于打字稿存在吗?我还查看了 Azure 的 API 管理来生成规范,但这些函数目前不是函数应用程序的一部分(它们作为静态站点 api 的一部分部署),并且我不确定 api 管理是否能够处理打字稿类型。

这是我当前使用 swagger-jsdoc 定义规范的设置示例。

/**
 * @swagger
 *  /api/user:
 *    get:
 *      tags:
 *      - user
 *      summary: Get the user by Id
 *      description: "Returns a single user"
 *      parameters:
 *      - in: "query"
 *        name: "id"
 *        description: "ID of the user to return"
 *        required: true
 *        schema:
 *          type: "string"
 *      responses:
 *        '200':
 *          description: OK
 *          content:
 *            application/json:
 *              schema:
 *                $ref: "#/components/schemas/User"
 *    post:
 *      tags:
 *        - user
 *      summary: POST a user to the database
 *      description: User that needs to be added to the database
 *      requestBody:
 *        description: User that needs to be added to the database
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/User'
 *        required: true
 *      responses:
 *        '200':
 *          description: OK
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/User'
 *
 * components:
 *   schemas:
 *    User:
 *      type: object
 *      properties:
 *        id:
 *          type: string
 *        name:
 *          type: string
 *
 * @param context The context
 * @param req The request
 */

const props: HandlerProperties<User> = {
    containerType: config.userContainer
};

export const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.res = await handleRequest(toRequest<User>(req), props);
};

export default httpTrigger;
Run Code Online (Sandbox Code Playgroud)

小智 6

根据我的调查,仍然没有插件\方式可以开箱即用地支持它。也是同一主题的答案。我现在所做的是将我的 azure 函数应用程序与 azure 中的 APIM 链接起来,然后从那里下载生成的架构。在我将手动更新架构之后以及在部署过程中重新部署到 APIM 之后。

  • 我还没有看到这个问题的答案,所以我+1至少分享你的解决方案。我挑战某人想出比使用整个 APIM 实例为节点功能应用程序生成 openapi 规范更便宜的方法。 (3认同)