在没有 NodeJs 的客户端站点上使用带有 javascript 的 Swagger
Using Swagger with javascript on client-site without NodeJs
如何在客户端(没有 NodeJs 的普通浏览器应用程序)上使用 Swagger 生成的 API 客户端源?
在第一次测试中,我为 Swaggers 的宠物店 API 生成了一个 javascript 客户端(https://petstore.swagger.io/v2) using editor.swagger.io
生成的代码包含一个 index.js,它提供对 public API 类 的构造函数的访问,我尝试在我的 Web 应用程序中嵌入和使用它。
文档描述了 API 的用法,如下所示:
var SwaggerPetstore = require('swagger_petstore');
var defaultClient = SwaggerPetstore.ApiClient.instance;
// Configure API key authorization: api_key
var api_key = defaultClient.authentications['api_key'];
api_key.apiKey = 'YOUR API KEY';
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//api_key.apiKeyPrefix = 'Token';
var apiInstance = new SwaggerPetstore.PetApi();
var petId = 789; // Number | ID of pet to return
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.getPetById(petId, callback);
这适用于 NodeJs 应用程序。但是我如何将 API 用于浏览器内的传统客户端 Web 应用程序?对于此类应用程序,nodejs 函数 require 不起作用。
来自https://github.com/swagger-api/swagger-js
该示例存在跨源问题,但它应该适用于您自己的项目
<html>
<head>
<script src='//unpkg.com/swagger-client' type='text/javascript'></script>
<script>
var specUrl = '//petstore.swagger.io/v2/swagger.json'; // data urls are OK too 'data:application/json;base64,abc...'
SwaggerClient.http.withCredentials = true; // this activates CORS, if necessary
var swaggerClient = new SwaggerClient(specUrl)
.then(function (swaggerClient) {
return swaggerClient.apis.pet.addPet({id: 1, name: "bobby"}); // chaining promises
}, function (reason) {
console.error("failed to load the spec" + reason);
})
.then(function(addPetResult) {
console.log(addPetResult.obj);
// you may return more promises, if necessary
}, function (reason) {
console.error("failed on API call " + reason);
});
</script>
</head>
<body>
check console in browser's dev. tools
</body>
</html>
如何在客户端(没有 NodeJs 的普通浏览器应用程序)上使用 Swagger 生成的 API 客户端源?
在第一次测试中,我为 Swaggers 的宠物店 API 生成了一个 javascript 客户端(https://petstore.swagger.io/v2) using editor.swagger.io
生成的代码包含一个 index.js,它提供对 public API 类 的构造函数的访问,我尝试在我的 Web 应用程序中嵌入和使用它。
文档描述了 API 的用法,如下所示:
var SwaggerPetstore = require('swagger_petstore');
var defaultClient = SwaggerPetstore.ApiClient.instance;
// Configure API key authorization: api_key
var api_key = defaultClient.authentications['api_key'];
api_key.apiKey = 'YOUR API KEY';
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//api_key.apiKeyPrefix = 'Token';
var apiInstance = new SwaggerPetstore.PetApi();
var petId = 789; // Number | ID of pet to return
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.getPetById(petId, callback);
这适用于 NodeJs 应用程序。但是我如何将 API 用于浏览器内的传统客户端 Web 应用程序?对于此类应用程序,nodejs 函数 require 不起作用。
来自https://github.com/swagger-api/swagger-js
该示例存在跨源问题,但它应该适用于您自己的项目
<html>
<head>
<script src='//unpkg.com/swagger-client' type='text/javascript'></script>
<script>
var specUrl = '//petstore.swagger.io/v2/swagger.json'; // data urls are OK too 'data:application/json;base64,abc...'
SwaggerClient.http.withCredentials = true; // this activates CORS, if necessary
var swaggerClient = new SwaggerClient(specUrl)
.then(function (swaggerClient) {
return swaggerClient.apis.pet.addPet({id: 1, name: "bobby"}); // chaining promises
}, function (reason) {
console.error("failed to load the spec" + reason);
})
.then(function(addPetResult) {
console.log(addPetResult.obj);
// you may return more promises, if necessary
}, function (reason) {
console.error("failed on API call " + reason);
});
</script>
</head>
<body>
check console in browser's dev. tools
</body>
</html>