问题描述
使用 Swagger编辑器,我为我的API创建了一个规范.我看到Swagger在其Swagger编辑器中还提供了生成客户端"选项(可能使用 Swagger Codegen ).
Using Swagger Editor, I created a spec for my API.I see that Swagger also provide a "Generate a client" option in their Swagger Editor (probably using Swagger Codegen).
我想生成一个客户端并将其用于我的React应用程序(通过 create-react-创建应用),并且最好享受一些静态输入功能(当前使用流).
I want to generate a client and use it in my React app (created with create-react-app), and preferably enjoy some static typing capabilities (currently using flow).
Swagger编辑器中的所有选项似乎都不起作用:
None of the options in Swagger Editor there seem to work:
-
javascript
-提供一个香草节点模块,您需要将其作为项目的依赖项.现在,不仅没有任何类/类型,而且还失败了: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:295at measureLifeCyclePerf (ReactCompositeComponent.js:75)at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294)at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
-
typescript-*
选项与js不兼容,我不必知道如何将TypeScript添加到我的项目中. -
*-angular
选项当然是不相关的.
javascript
- provides a vanilla node module that you need tonpm install
it as a dependency to your project. Now, not only this doesn't have any classes / types, but also fails with: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:295at measureLifeCyclePerf (ReactCompositeComponent.js:75)at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294)at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
typescript-*
options are not compatible with js, and I don't necessarilty know how / want to add TypeScript to my project.*-angular
options are of course irrelevant.
- 生成
.ts
文件并将其隐藏到flow
.怎么样? - 手动将生成的
.js
文件添加到我的项目中.当前不起作用,因为生成的代码带有其自己的依赖项. - 将TypeScript添加到我的项目中(那我应该替换
flow
吗?).怎么样?
- Generate
.ts
file and covert it toflow
. How?? - Manually adding the generated
.js
files to my project. Currently doesn't work as the generated code comes with its own dependencies. - Add TypeScript to my project (should I replace
flow
then?). How??
谢谢
推荐答案
有一个简单的解决方案,直到生成器无法使用动态实现,例如 swagger-js
There is a simple solution until a generator is out to use dynamic implementation such as swagger-js
这篇关于Swagger/OpenAPI API规范的Reactjs客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!