我正在创建一个需要使用REST API与服务器通信的新webApp。我想使用Swagger来定义REST API并为服务器和Javascript客户端(在浏览器中运行的简单webapp)生成代码。
我能够实现服务器(使用Java servlet)并且它可以工作。
我已经为Javascript客户端生成了代码,但我无法使其正常工作(我是Javascript,webpack,nom ......的初学者)。
我想使用webpack生成一个将由webapp加载的.js文件,这个.js将包含swagger和我的代码生成的代码,用于使用Swagger发送查询(...和UI!)
这是我的项目组织:
/
- package.json
- node_modules (modules installed with npm)
- src
---> /main.js (code I have written to send the REST API query using Swagger generated code)
---> /index.js (code generated by Swagger)
---> /ApiClient.js (code generated by Swagger)
---> /api (folder that contains code generated by Swagger)
---> /model (folder that contains code generated by Swagger)
- webpack.config.js
- runTest ---> - bundle.js (generated by webpack) ---> - index.html
执行webpack命令时出现错误:
./ node_modules / .bin / webpack --config webpack.config.js
哈希: a77095b323ec225f9b17版本:webpack 2.6.1时间:396ms 资产大小块块名称bundle.js 6.48 kB 0 [emit] main [0] ./src/index.js 2.61 kB {0} [built] [1] ./src/main.js 212字节{0} [已建立]
> ./src/index.js中的错误找不到模块:错误:无法解决 ' ApiClient'在 ' /用户/塞巴斯蒂安/文档/使用Javascript /#2 / superagenttest / SRC' @ ./src/index.js 17:4-86 @ ./src/main.js > ./src/index.js中的错误找不到模块:错误:无法解决 '模型/错误'在 ' /用户/塞巴斯蒂安/文档/使用Javascript /#2 / superagenttest / SRC' @ ./src/index.js 17:4-86 @ ./src/main.js
我的main.js包含以下内容:
var MyMetadataApi = require('./index'); // See note below*.
var xxxSvc = new MyMetadataApi.defaultApi(); // Allocate the API class we're going to use.
var zzz = xxxSvc.myFieldsGet(); // Invoke the service.
index.js(由Swagger生成)
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['ApiClient', 'model/Error', 'model/MyField', 'api/DefaultApi'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('./ApiClient'), require('./model/Error'), require('./model/MyField'), require('./api/DefaultApi'));
}
}(function(ApiClient, Error, MyField, DefaultApi) {
'use strict';
/**
* get_My_fields.<br>
* The <code>index</code> module provides access to constructors for all the classes which comprise the public API.
* <p>
* An AMD (recommended!) or CommonJS application will generally do something equivalent to the following:
* <pre>
* var MyMetadataApi = require('index'); // See note below*.
* var xxxSvc = new MyMetadataApi.XxxApi(); // Allocate the API class we're going to use.
* var yyyModel = new MyMetadataApi.Yyy(); // Construct a model instance.
* yyyModel.someProperty = 'someValue';
* ...
* var zzz = xxxSvc.doSomething(yyyModel); // Invoke the service.
* ...
* </pre>
* <em>*NOTE: For a top-level AMD script, use require(['index'], function(){...})
* and put the application logic within the callback function.</em>
* </p>
* <p>
* A non-AMD browser application (discouraged) might do something like this:
* <pre>
* var xxxSvc = new MyMetadataApi.XxxApi(); // Allocate the API class we're going to use.
* var yyy = new MyMetadataApi.Yyy(); // Construct a model instance.
* yyyModel.someProperty = 'someValue';
* ...
* var zzz = xxxSvc.doSomething(yyyModel); // Invoke the service.
* ...
* </pre>
* </p>
* @module index
* @version 1.0.0
*/
var exports = {
/**
* The ApiClient constructor.
* @property {module:ApiClient}
*/
ApiClient: ApiClient,
/**
* The Error model constructor.
* @property {module:model/Error}
*/
Error: Error,
/**
* The MyField model constructor.
* @property {module:model/MyField}
*/
MyField: MyField,
/**
* The DefaultApi service constructor.
* @property {module:api/DefaultApi}
*/
DefaultApi: DefaultApi
};
return exports;
}));
如何让webpack生成一个bundle.js,它将成功包含Swagger和我的main.js生成的代码?
我迷失了导入/需要...我试图用一些导入替换require()但没有任何成功(或者它可以读取index.js然后在index.js中找到问题,因为它无法导入ApiClient.js)。 什么是AMD和CommonJS应用程序由Swagger生成的代码提到?
任何帮助将不胜感激;-) 谢谢,
的Sebastien
答案 0 :(得分:3)
我找到了一个解决方案,我需要在webpack.config.js中添加以下内容
module: {
rules: [
{
test: /my_client\/.*\.js$/,
use: 'imports-loader?define=>false'
}
]
},
node: {
fs: 'empty'
}
我在swagger-codegen GitHub存储库中找到了此修复程序:https://github.com/swagger-api/swagger-codegen/issues/3336