Reactjs客户端用于Swagger/OpenAPI API规范

6

背景

使用Swagger Editor,我为我的API创建了一个规范。我发现Swagger还提供了一个“生成客户端”的选项(可能使用Swagger Codegen)。

目标

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

当前情况

Swagger Editor中的任何选项似乎都不起作用:

  • javascript - 提供一个普通的node模块,您需要将其作为依赖项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-*选项与js不兼容,我不一定知道/想要将TypeScript添加到我的项目中。
  • *-angular选项当然是不相关的。

解决方法

  • 生成.ts文件并将其转换为flow。如何实现?
  • 手动将生成的.js文件添加到我的项目中。当前无法正常工作,因为生成的代码带有自己的依赖项。
  • 在我的项目中添加TypeScript(我应该替换flow吗?)。如何实现?

谢谢


1
如果您在使用JS API客户端时遇到问题,请通过https://github.com/swagger-api/swagger-codegen/issues/new打开一个问题,以便社区能够提供帮助。 - William Cheng
1个回答

0

在生成器可用之前,有一个简单的解决方案,即使用动态实现,例如swagger-js


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接