Javascript中的API客户端

时间:2019-07-15 15:49:45

标签: javascript vue.js swagger swagger-codegen

我需要一些帮助来解决项目中的问题。

场景:

首先:我有一个SPA网站,该网站正在Vue.js中开发。

第二:我还想使用Swagger中的Web API规范来生成Java客户端代码。

最后:我正在为此使用swagger-codegen-cli.jar。

我到目前为止所做的事情

1-下载具有JavaScript支持的最新swagger-codegen-cli.jar稳定版本:

curl http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.7/swagger-codegen-cli-2.4.7.jar -o swagger-codegen-cli.jar

2-使用以下方法生成客户端代码:

java -jar swagger-codegen-cli.jar generate -i http://192.168.0.85:32839/api/swagger/v1/swagger.json -l javascript -o ./web_api_client/

3-将生成的模块添加到我的项目中:

  "dependencies": {
    // ...
    "vue": "^2.6.10",
    "vue-router": "^3.0.3",
    "web_api_client": "file:./web_api_client"
  },

4-执行npm安装。显然,它工作正常。

5-这时我遇到了问题。由于某种原因,生成的模块未完全加载。

export default {
  name: 'home',
  components: {
    HelloWorld
  },
  mounted() {
    var WebApiClient = require("web_api_client");
    var defaultClient = WebApiClient.ApiClient.instance;

    var oauth2 = defaultClient.authentications["oauth2"];
    oauth2.accessToken = "YOUR ACCESS TOKEN";

    var apiInstance = new WebApiClient.VersaoApi();
    var callback = function(error, data, response) {
      if (error) {
        console.error(error);
      } else {
        console.log('API called successfully. Returned data: ' + data);
      }
    };
    apiInstance.apiVersaoGet(callback);
  }
}

6-第var WebApiClient = require("web_api_client");行可以正常工作,但是不能100%运行。模块的实例已创建但为空。例如,WebApiClient.ApiClient始终是未定义的。

7-我查看了生成的代码,我认为问题与模块的加载方式有关。

(function(factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['ApiClient', 'api/VersaoApi'], factory);
  } else if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('./ApiClient'),  require('./api/VersaoApi'));
  }
}(function(ApiClient, VersaoApi) {
  'use strict';
 // ...

在此代码中,都不执行ifs块。

有人遇到过这样的问题吗? 有什么建议吗?

非常感谢,伙计们。

1 个答案:

答案 0 :(得分:0)

解决方案

尝试解决require("web_api_client");的问题后,我决定使用ES6代替ES5。

我在swagger-codegen-cli.jar中找到了一个使用ES6生成客户端代码的选项,如下所示:

java -jar swagger-codegen-cli.jar generate -i http://192.168.0.85:32839/api/swagger/v1/swagger.json -l javascript --additional-properties useES6=true -o ./web_api_client/

使用ES6,我可以直接从生成的源导入javascript模块,如下面的代码所示。

import WebApiClient from "./web_api_client/src/index";

let defaultClient = WebApiClient.ApiClient.instance;
defaultClient.basePath = 'http://192.168.0.85:32839';

// Configure OAuth2 access token for authorization: oauth2
let oauth2 = defaultClient.authentications["oauth2"];
oauth2.accessToken = "YOUR ACCESS TOKEN";

let apiInstance = new WebApiClient.VersaoApi();

apiInstance.apiVersaoGet((error, data, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log("API called successfully. Returned data: " + data + response);
  }
});

当我第一次运行代码时,由于生成的模块WebApiClient在导出块中没有关键字default,我遇到了一个错误。

原始生成的代码

export {
    /**
     * The ApiClient constructor.
     * @property {module:ApiClient}
     */
    ApiClient,
    // ...

更改了

export default {
    /**
     * The ApiClient constructor.
     * @property {module:ApiClient}
     */
    ApiClient,
    // ...

现在一切正常。