背景
使用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/