Swagger/OpenAPI API 规范的 Reactjs 客户端

标签 reactjs typescript swagger flowtype swagger-codegen

背景

使用Swagger Editor ,我为我的 API 创建了一个规范。 我看到 Swagger 还在其 Swagger 编辑器中提供了“生成客户端”选项(可能使用 Swagger Codegen )。

目标

我想生成一个客户端并在我的 React 应用程序中使用它(使用 create-react-app 创建),并且最好享受一些静态类型功能(当前使用 flow )。

目前

Swagger Editor 中的所有选项似乎都不起作用:

  • javascript - 提供您需要的普通节点模块npm install它作为您项目的依赖项。现在,这不仅没有任何类/类型,而且还会失败: Uncaught Error: Cannot find module "ApiClient" at webpackMissingModule (index.js:17) at exports.ApiClient (index.js:17) at Object.<anonymous> (index.js:22) at __webpack_require__ (bootstrap 75f547b…:555) at fn (bootstrap 75f547b…:86) at new Index (index.js:87) at ReactCompositeComponent.js:295 at measureLifeCyclePerf (ReactCompositeComponent.js:75) at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294) at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
  • typescript-* options 与 js 不兼容,我不需要知道如何/想要将 TypeScript 添加到我的项目中。
  • *-angular选项当然无关紧要。

方法

  • 生成.ts文件并将其隐藏为 flow 。怎么办?
  • 手动添加生成的 .js文件到我的项目。目前不起作用,因为生成的代码带有自己的依赖项。
  • 将 TypeScript 添加到我的项目中(那么我应该替换 flow 吗?)。怎么办?

谢谢

最佳答案

有一个简单的解决方案,直到生成器使用动态实现,例如 swagger-js

关于Swagger/OpenAPI API 规范的 Reactjs 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44051247/

相关文章:

css - 如何在 React Native 中使用 Flex 居中组件?

javascript - React 中用于图像模态的动态 CSS 类

javascript - 在 Angular 2 应用程序中使用 Microsoft BotFramework-WebChat 时遇到问题

java - Swagger 递归地解决类型的依赖关系(无限循环)

java - 如何设置 swagger 文档以使用 dd/MM/yyyy 格式?

javascript - react.js 中 props 和 state 的混淆

node.js - 获取类型错误 : Cannot read property 'prototype' of undefined after modifying form

javascript - typescript 类型 boolean 不能分配给 void

typescript - DeepMerge<> 通用不支持深度可选

Python转义变量名中的 '$'字符