Make JavaScript client work in both Node.js and browser

* Replace jQuery with SuperAgent which works in both Node.js and browser
* Use UMD pattern (returnExports.js) to make the module exporting compatible
  with all major systems: AMD, Node.js (CommonJS) and browser
* Implement support of header and form parameters. Closes #1736
* Move HTTP requesting code to `ApiClient` and allow customizing options
  in it, e.g. "basePath"
* Update unit tests accordingly and add some tests for `ApiClient`
This commit is contained in:
xhh 2016-01-19 10:46:20 +08:00 committed by Maelig Nantel
parent a2cb3f7c3c
commit 6efbde5691
22 changed files with 3575 additions and 2106 deletions

View File

@ -134,6 +134,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
typeMapping.put("double", "Number");
typeMapping.put("number", "Number");
typeMapping.put("DateTime", "Date");
// binary not supported in JavaScript client right now, using Object as a workaround
typeMapping.put("binary", "Object");
importMapping.clear();
}
@ -206,6 +208,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("index.mustache", sourceFolder, "index.js"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", sourceFolder, "ApiClient.js"));
}
@Override

View File

@ -0,0 +1,146 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['superagent'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('superagent'));
} else {
// Browser globals (root is window)
if (!root.{{moduleName}}) {
root.{{moduleName}} = {};
}
root.{{moduleName}}.ApiClient = factory(root.superagent);
}
}(this, function(superagent) {
'use strict';
var ApiClient = function ApiClient() {
this.basePath = '{{basePath}}'.replace(/\/+$/, '');
};
ApiClient.prototype.paramToString = function paramToString(param) {
if (param == null) {
// return empty string for null and undefined
return '';
} else {
return param.toString();
}
};
/**
* Build full URL by appending the given path to base path and replacing
* path parameter placeholders with parameter values.
* NOTE: query parameters are not handled here.
*/
ApiClient.prototype.buildUrl = function buildUrl(path, pathParams) {
if (!path.match(/^\//)) {
path = '/' + path;
}
var url = this.basePath + path;
var _this = this;
url = url.replace(/\{([\w-]+)\}/g, function(fullMatch, key) {
var value;
if (pathParams.hasOwnProperty(key)) {
value = _this.paramToString(pathParams[key]);
} else {
value = fullMatch;
}
return encodeURIComponent(value);
});
return url;
};
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
*/
ApiClient.prototype.isJsonMime = function isJsonMime(mime) {
return Boolean(mime != null && mime.match(/^application\/json(;.*)?$/i));
};
/**
* Choose a MIME from the given MIMEs with JSON preferred,
* i.e. return JSON if included, otherwise return the first one.
*/
ApiClient.prototype.jsonPreferredMime = function jsonPreferredMime(mimes) {
var len = mimes.length;
for (var i = 0; i < len; i++) {
if (this.isJsonMime(mimes[i])) {
return mimes[i];
}
}
return mimes[0];
};
/**
* Normalize parameters values:
* remove nils,
* keep files and arrays,
* format to string with `paramToString` for other cases.
*/
ApiClient.prototype.normalizeParams = function normalizeParams(params) {
var newParams = {};
for (var key in params) {
if (params.hasOwnProperty(key) && params[key] != null) {
var value = params[key];
if (value instanceof Blob || Array.isArray(value)) {
newParams[key] = value;
} else {
newParams[key] = this.paramToString(value);
}
}
}
return newParams;
};
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
callback) {
var url = this.buildUrl(path, pathParams);
var request = superagent(httpMethod, url);
// set query parameters
request.query(this.normalizeParams(queryParams));
// set header parameters
request.set(this.normalizeParams(headerParams));
var contentType = this.jsonPreferredMime(contentTypes) || 'application/json';
request.type(contentType);
if (contentType === 'application/x-www-form-urlencoded') {
request.send(this.normalizeParams(formParams));
} else if (contentType == 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
if (_formParams.hasOwnProperty(key)) {
if (_formParams[key] instanceof Blob) {
// file field
request.attach(key, _formParams[key]);
} else {
request.field(key, _formParams[key]);
}
}
}
} else if (bodyParam) {
request.send(bodyParam);
}
request.end(function(error, response) {
if (callback) {
var data = response && response.body;
callback(error, data, response);
}
});
return request;
};
ApiClient.default = new ApiClient();
return ApiClient;
}));

View File

@ -1,18 +1,23 @@
// require files in Node.js environment
var ${{#imports}}, {{import}}{{/imports}};
if (typeof module === 'object' && module.exports) {
$ = require('jquery');{{#imports}}
{{import}} = require('../model/{{import}}.js');{{/imports}}
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['../ApiClient'{{#imports}}, '../model/{{import}}'{{/imports}}], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('../ApiClient.js'){{#imports}}, require('../model/{{import}}.js'){{/imports}});
} else {
// Browser globals (root is window)
if (!root.{{moduleName}}) {
root.{{moduleName}} = {};
}
// export module for AMD
if ( typeof define === "function" && define.amd ) {
define(['jquery'{{#imports}}, '{{import}}'{{/imports}}], function(${{#imports}}, {{import}}{{/imports}}) {
return {{classname}};
});
root.{{moduleName}}.{{classname}} = factory(root.{{moduleName}}.ApiClient{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}});
}
}(this, function(ApiClient{{#imports}}, {{import}}{{/imports}}) {
'use strict';
var {{classname}} = function {{classname}}(apiClient) {
this.apiClient = apiClient || ApiClient.default;
var {{classname}} = function {{classname}}() {
var self = this;
{{#operations}}
{{#operation}}
@ -20,106 +25,62 @@ var {{classname}} = function {{classname}}() {
* {{summary}}
* {{notes}}
{{#allParams}} * @param {{=<% %>=}}{<% dataType %>} <%={{ }}=%> {{paramName}} {{description}}
{{/allParams}} * @param {function} callback the callback function
* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
{{/allParams}} * @param {function} callback the callback function, accepting three arguments: error, data, response{{#returnType}}
* data is of type: {{{returnType}}}{{/returnType}}
*/
self.{{nickname}} = function({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}callback) {
var {{localVariablePrefix}}postBody = {{#bodyParam}}{{^isBinary}}JSON.stringify({{paramName}}){{/isBinary}}{{#isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
var {{localVariablePrefix}}postBinaryBody = {{#bodyParam}}{{#isBinary}}{{paramName}}{{/isBinary}}{{^isBinary}}null{{/isBinary}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
var postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
//throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}");
var errorRequiredMsg = "Missing the required parameter '{{paramName}}' when calling {{nickname}}";
throw errorRequiredMsg;
throw "Missing the required parameter '{{paramName}}' when calling {{nickname}}";
}
{{/required}}{{/allParams}}
// create path and map variables
var basePath = '{{basePath}}';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var {{localVariablePrefix}}path = basePath + replaceAll(replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}}
, "\\{" + "{{baseName}}" + "\\}", encodeURIComponent({{{paramName}}}.toString()){{/pathParams}});
{{=< >=}}
var pathParams = {<#pathParams>
'<baseName>': <paramName><#hasMore>,</hasMore></pathParams>
};
var queryParams = {<#queryParams>
'<baseName>': <paramName><#hasMore>,</hasMore></queryParams>
};
var headerParams = {<#headerParams>
'<baseName>': <paramName><#hasMore>,</hasMore></headerParams>
};
var formParams = {<#formParams>
'<baseName>': <paramName><#hasMore>,</hasMore></formParams>
};
var queryParams = {};
var headerParams = {};
var formParams = {};
var contentTypes = [<#consumes>'<mediaType>'<#hasMore>, </hasMore></consumes>];
var accepts = [<#produces>'<mediaType>'<#hasMore>, </hasMore></produces>];
{{#queryParams}}
queryParams.{{baseName}} = {{paramName}};
{{/queryParams}}
{{#headerParams}}if ({{paramName}} != null)
{{localVariablePrefix}}headerParams.put("{{baseName}}", {{paramName}});
{{/headerParams}}
{{#formParams}}if ({{paramName}} != null)
{{localVariablePrefix}}formParams.put("{{baseName}}", {{paramName}});
{{/formParams}}
path += createQueryString(queryParams);
var options = {type: "{{httpMethod}}", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {<#returnType><#returnTypeIsPrimitive>
callback(error, data, response);</returnTypeIsPrimitive><^returnTypeIsPrimitive><#isListContainer>
// TODO: support deserializing array of models
callback(error, data, response);</isListContainer><^isListContainer>
if (!error && data) {
var result = new <&returnType>();
result.constructFromObject(data);
callback(error, result, response);
} else {
callback(error, data, response);
}</isListContainer></returnTypeIsPrimitive></returnType><^returnType>
callback(error, data, response);</returnType>
};
}
});
request.done(function(response, textStatus, jqXHR){
{{#returnType}}
/**
* @returns {{{returnType}}}
*/
{{#returnTypeIsPrimitive}}var myResponse = response;{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}}var myResponse = new {{{returnType}}}();
myResponse.constructFromObject(response);{{/returnTypeIsPrimitive}}
if (callback) {
callback(myResponse, textStatus, jqXHR);
}
{{/returnType}}{{^returnType}}
if (callback) {
callback(response, textStatus, jqXHR);
}
{{/returnType}}
});
return request;
return this.apiClient.callApi(
'<&path>', '<httpMethod>',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
<={{ }}=>
}
{{/operation}}
{{/operations}}
};
function replaceAll (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
}
return result;
}
function createQueryString (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
}
return queryString;
}
}
// export module for Node.js
if (typeof module === 'object' && module.exports) {
module.exports = {{classname}};
}
return {{classname}};
}));

View File

@ -1,10 +1,17 @@
if (typeof module === 'object' && module.exports) {
var {{moduleName}} = {};
{{#models}}
{{moduleName}}.{{importPath}} = require('./model/{{importPath}}.js');
{{/models}}
{{#apiInfo}}{{#apis}}
{{moduleName}}.{{importPath}} = require('./api/{{importPath}}.js');
{{/apis}}{{/apiInfo}}
module.exports = {{moduleName}};
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['./ApiClient'{{#models}}, './model/{{importPath}}'{{/models}}{{#apiInfo}}{{#apis}}, './api/{{importPath}}'{{/apis}}{{/apiInfo}}], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('./ApiClient.js'){{#models}}, require('./model/{{importPath}}.js'){{/models}}{{#apiInfo}}{{#apis}}, require('./api/{{importPath}}.js'){{/apis}}{{/apiInfo}});
}
}(function(ApiClient{{#models}}, {{importPath}}{{/models}}{{#apiInfo}}{{#apis}}, {{importPath}}{{/apis}}{{/apiInfo}}) {
'use strict';
return {
ApiClient: ApiClient{{#models}},
{{importPath}}: {{importPath}}{{/models}}{{#apiInfo}}{{#apis}},
{{importPath}}: {{importPath}}{{/apis}}{{/apiInfo}}
};
}));

View File

@ -1,23 +1,24 @@
// require files in Node.js environment
{{#imports}}
var {{import}};{{/imports}}
if (typeof module === 'object' && module.exports) {
{{#imports}}
{{import}} = require('./{{import}}.js');{{/imports}}
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined{{#imports}}, './{{import}}'{{/imports}}], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined{{#imports}}, require('./{{import}}.js'){{/imports}});
} else {
// Browser globals (root is window)
if (!root.{{moduleName}}) {
root.{{moduleName}} = {};
}
factory(root.{{moduleName}}{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}});
}
}(this, function(module{{#imports}}, {{import}}{{/imports}}) {
'use strict';
{{#models}}{{#model}}
{{#vars}}{{#isEnum}}{{>enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}}
{{>enumClass}}{{/items}}*/{{/items.isEnum}}{{/vars}}
//export module
if ( typeof define === "function" && define.amd ) {
define('{{classname}}', ['jquery'{{#vars}}{{^isPrimitiveType}}{{^-last}}, {{/-last}}'{{datatypeWithEnum}}'{{/isPrimitiveType}}{{/vars}}],
function(${{#vars}}{{^isPrimitiveType}}{{^-last}}, {{/-last}}{{datatypeWithEnum}}{{/isPrimitiveType}}{{/vars}}) {
return {{classname}};
});
}
{{#description}}/**
* {{description}}
**/{{/description}}
@ -66,10 +67,13 @@ var {{classname}} = function {{classname}}({{#mandatory}}{{this}}{{^-last}}, {{/
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.{{classname}} = {{classname}};
}
if (typeof module === 'object' && module.exports) {
module.exports = {{classname}};
}
return {{classname}};
{{/model}}
{{/models}}
}));

View File

@ -8,13 +8,10 @@
"test": "./node_modules/mocha/bin/mocha --recursive"
},
"dependencies": {
"jquery": "~2.1.4"
"superagent": "^1.6.1"
},
"devDependencies": {
"mocha": "~2.3.4",
"expect.js": "~0.3.1",
"mockrequire": "~0.0.5",
"domino": "~1.0.20",
"xmlhttprequest": "~1.8.0"
"expect.js": "~0.3.1"
}
}

View File

@ -8,13 +8,10 @@
"test": "./node_modules/mocha/bin/mocha --recursive"
},
"dependencies": {
"jquery": "~2.1.4"
"superagent": "^1.6.1"
},
"devDependencies": {
"mocha": "~2.3.4",
"expect.js": "~0.3.1",
"mockrequire": "~0.0.5",
"domino": "~1.0.20",
"xmlhttprequest": "~1.8.0"
"expect.js": "~0.3.1"
}
}

View File

@ -0,0 +1,146 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['superagent'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('superagent'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
root.SwaggerPetstore.ApiClient = factory(root.superagent);
}
}(this, function(superagent) {
'use strict';
var ApiClient = function ApiClient() {
this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, '');
};
ApiClient.prototype.paramToString = function paramToString(param) {
if (param == null) {
// return empty string for null and undefined
return '';
} else {
return param.toString();
}
};
/**
* Build full URL by appending the given path to base path and replacing
* path parameter placeholders with parameter values.
* NOTE: query parameters are not handled here.
*/
ApiClient.prototype.buildUrl = function buildUrl(path, pathParams) {
if (!path.match(/^\//)) {
path = '/' + path;
}
var url = this.basePath + path;
var _this = this;
url = url.replace(/\{([\w-]+)\}/g, function(fullMatch, key) {
var value;
if (pathParams.hasOwnProperty(key)) {
value = _this.paramToString(pathParams[key]);
} else {
value = fullMatch;
}
return encodeURIComponent(value);
});
return url;
};
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
*/
ApiClient.prototype.isJsonMime = function isJsonMime(mime) {
return Boolean(mime != null && mime.match(/^application\/json(;.*)?$/i));
};
/**
* Choose a MIME from the given MIMEs with JSON preferred,
* i.e. return JSON if included, otherwise return the first one.
*/
ApiClient.prototype.jsonPreferredMime = function jsonPreferredMime(mimes) {
var len = mimes.length;
for (var i = 0; i < len; i++) {
if (this.isJsonMime(mimes[i])) {
return mimes[i];
}
}
return mimes[0];
};
/**
* Normalize parameters values:
* remove nils,
* keep files and arrays,
* format to string with `paramToString` for other cases.
*/
ApiClient.prototype.normalizeParams = function normalizeParams(params) {
var newParams = {};
for (var key in params) {
if (params.hasOwnProperty(key) && params[key] != null) {
var value = params[key];
if (value instanceof Blob || Array.isArray(value)) {
newParams[key] = value;
} else {
newParams[key] = this.paramToString(value);
}
}
}
return newParams;
};
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
callback) {
var url = this.buildUrl(path, pathParams);
var request = superagent(httpMethod, url);
// set query parameters
request.query(this.normalizeParams(queryParams));
// set header parameters
request.set(this.normalizeParams(headerParams));
var contentType = this.jsonPreferredMime(contentTypes) || 'application/json';
request.type(contentType);
if (contentType === 'application/x-www-form-urlencoded') {
request.send(this.normalizeParams(formParams));
} else if (contentType == 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
if (_formParams.hasOwnProperty(key)) {
if (_formParams[key] instanceof Blob) {
// file field
request.attach(key, _formParams[key]);
} else {
request.field(key, _formParams[key]);
}
}
}
} else if (bodyParam) {
request.send(bodyParam);
}
request.end(function(error, response) {
if (callback) {
var data = response && response.body;
callback(error, data, response);
}
});
return request;
};
ApiClient.default = new ApiClient();
return ApiClient;
}));

View File

@ -1,18 +1,23 @@
// require files in Node.js environment
var $, Pet;
if (typeof module === 'object' && module.exports) {
$ = require('jquery');
Pet = require('../model/Pet.js');
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['../ApiClient', '../model/Pet'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('../ApiClient.js'), require('../model/Pet.js'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
// export module for AMD
if ( typeof define === "function" && define.amd ) {
define(['jquery', 'Pet'], function($, Pet) {
return PetApi;
});
root.SwaggerPetstore.PetApi = factory(root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.Pet);
}
}(this, function(ApiClient, Pet) {
'use strict';
var PetApi = function PetApi(apiClient) {
this.apiClient = apiClient || ApiClient.default;
var PetApi = function PetApi() {
var self = this;
@ -20,285 +25,209 @@ var PetApi = function PetApi() {
* Update an existing pet
*
* @param {Pet} body Pet object that needs to be added to the store
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.updatePet = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet', 'PUT',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Add a new pet to the store
*
* @param {Pet} body Pet object that needs to be added to the store
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.addPet = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Finds Pets by status
* Multiple status values can be provided with comma seperated strings
* @param {Array} status Status values that need to be considered for filter
* @param {function} callback the callback function
* @return Array
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Array
*/
self.findPetsByStatus = function(status, callback) {
var postBody = null;
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/findByStatus", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
queryParams.status = status;
var pathParams = {
};
var queryParams = {
'status': status
};
var headerParams = {
};
var formParams = {
};
path += createQueryString(queryParams);
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Array
*/
var myResponse = new Array();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
handleResponse = function(error, data, response) {
// TODO: support deserializing array of models
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet/findByStatus', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Finds Pets by tags
* Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
* @param {Array} tags Tags to filter by
* @param {function} callback the callback function
* @return Array
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Array
*/
self.findPetsByTags = function(tags, callback) {
var postBody = null;
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/findByTags", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
queryParams.tags = tags;
var pathParams = {
};
var queryParams = {
'tags': tags
};
var headerParams = {
};
var formParams = {
};
path += createQueryString(queryParams);
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Array
*/
var myResponse = new Array();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
handleResponse = function(error, data, response) {
// TODO: support deserializing array of models
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet/findByTags', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Find pet by ID
* Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
* @param {Integer} petId ID of pet that needs to be fetched
* @param {function} callback the callback function
* @return Pet
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Pet
*/
self.getPetById = function(petId, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'petId' is set
if (petId == null) {
//throw new ApiException(400, "Missing the required parameter 'petId' when calling getPetById");
var errorRequiredMsg = "Missing the required parameter 'petId' when calling getPetById";
throw errorRequiredMsg;
throw "Missing the required parameter 'petId' when calling getPetById";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/{petId}", "\\{format\\}","json")
, "\\{" + "petId" + "\\}", encodeURIComponent(petId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'petId': petId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {
if (!error && data) {
var result = new Pet();
result.constructFromObject(data);
callback(error, result, response);
} else {
callback(error, data, response);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Pet
*/
var myResponse = new Pet();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
};
}
});
return this.apiClient.callApi(
'/pet/{petId}', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
@ -307,63 +236,46 @@ var PetApi = function PetApi() {
* @param {String} petId ID of pet that needs to be updated
* @param {String} name Updated name of the pet
* @param {String} status Updated status of the pet
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.updatePetWithForm = function(petId, name, status, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'petId' is set
if (petId == null) {
//throw new ApiException(400, "Missing the required parameter 'petId' when calling updatePetWithForm");
var errorRequiredMsg = "Missing the required parameter 'petId' when calling updatePetWithForm";
throw errorRequiredMsg;
throw "Missing the required parameter 'petId' when calling updatePetWithForm";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/{petId}", "\\{format\\}","json")
, "\\{" + "petId" + "\\}", encodeURIComponent(petId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'petId': petId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
'name': name,
'status': status
};
if (name != null)
formParams.put("name", name);
if (status != null)
formParams.put("status", status);
var contentTypes = ['application/x-www-form-urlencoded'];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet/{petId}', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
@ -371,61 +283,45 @@ var PetApi = function PetApi() {
*
* @param {Integer} petId Pet id to delete
* @param {String} apiKey
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.deletePet = function(petId, apiKey, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'petId' is set
if (petId == null) {
//throw new ApiException(400, "Missing the required parameter 'petId' when calling deletePet");
var errorRequiredMsg = "Missing the required parameter 'petId' when calling deletePet";
throw errorRequiredMsg;
throw "Missing the required parameter 'petId' when calling deletePet";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/{petId}", "\\{format\\}","json")
, "\\{" + "petId" + "\\}", encodeURIComponent(petId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
if (apiKey != null)
headerParams.put("api_key", apiKey);
var pathParams = {
'petId': petId
};
var queryParams = {
};
var headerParams = {
'api_key': apiKey
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet/{petId}', 'DELETE',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
@ -434,94 +330,133 @@ var PetApi = function PetApi() {
* @param {Integer} petId ID of pet to update
* @param {String} additionalMetadata Additional data to pass to server
* @param {File} file file to upload
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.uploadFile = function(petId, additionalMetadata, file, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'petId' is set
if (petId == null) {
//throw new ApiException(400, "Missing the required parameter 'petId' when calling uploadFile");
var errorRequiredMsg = "Missing the required parameter 'petId' when calling uploadFile";
throw errorRequiredMsg;
throw "Missing the required parameter 'petId' when calling uploadFile";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/pet/{petId}/uploadImage", "\\{format\\}","json")
, "\\{" + "petId" + "\\}", encodeURIComponent(petId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'petId': petId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
'additionalMetadata': additionalMetadata,
'file': file
};
if (additionalMetadata != null)
formParams.put("additionalMetadata", additionalMetadata);
if (file != null)
formParams.put("file", file);
var contentTypes = ['multipart/form-data'];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
request.done(function(response, textStatus, jqXHR){
return this.apiClient.callApi(
'/pet/{petId}/uploadImage', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
}
/**
* Fake endpoint to test byte array return by &#39;Find pet by ID&#39;
* Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
* @param {Integer} petId ID of pet that needs to be fetched
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Object
*/
self.getPetByIdWithByteArray = function(petId, callback) {
var postBody = null;
// verify the required parameter 'petId' is set
if (petId == null) {
throw "Missing the required parameter 'petId' when calling getPetByIdWithByteArray";
}
var pathParams = {
'petId': petId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var handleResponse = null;
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/pet/{petId}?testing_byte_array=true', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Fake endpoint to test byte array in body parameter for adding a new pet to the store
*
* @param {Object} body Pet object in the form of byte array
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.addPetUsingByteArray = function(body, callback) {
var postBody = body;
function replaceAll (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
}
return result;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
var handleResponse = null;
if (callback) {
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
function createQueryString (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
return this.apiClient.callApi(
'/pet?testing_byte_array=true', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
}
return queryString;
}
}
};
// export module for Node.js
if (typeof module === 'object' && module.exports) {
module.exports = PetApi;
}
return PetApi;
}));

View File

@ -1,286 +1,206 @@
// require files in Node.js environment
var $, Order;
if (typeof module === 'object' && module.exports) {
$ = require('jquery');
Order = require('../model/Order.js');
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['../ApiClient', '../model/Order'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('../ApiClient.js'), require('../model/Order.js'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
// export module for AMD
if ( typeof define === "function" && define.amd ) {
define(['jquery', 'Order'], function($, Order) {
return StoreApi;
});
root.SwaggerPetstore.StoreApi = factory(root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.Order);
}
}(this, function(ApiClient, Order) {
'use strict';
var StoreApi = function StoreApi(apiClient) {
this.apiClient = apiClient || ApiClient.default;
var StoreApi = function StoreApi() {
var self = this;
/**
* Returns pet inventories by status
* Returns a map of status codes to quantities
* @param {function} callback the callback function
* @return Object<String, Integer>
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Object<String, Integer>
*/
self.getInventory = function(callback) {
var postBody = null;
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/store/inventory", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Object<String, Integer>
*/
var myResponse = response;
if (callback) {
callback(myResponse, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/store/inventory', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Place an order for a pet
*
* @param {Order} body order placed for purchasing the pet
* @param {function} callback the callback function
* @return Order
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Order
*/
self.placeOrder = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/store/order", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {
if (!error && data) {
var result = new Order();
result.constructFromObject(data);
callback(error, result, response);
} else {
callback(error, data, response);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Order
*/
var myResponse = new Order();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
};
}
});
return this.apiClient.callApi(
'/store/order', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Find purchase order by ID
* For valid response try integer IDs with value &lt;= 5 or &gt; 10. Other values will generated exceptions
* @param {String} orderId ID of pet that needs to be fetched
* @param {function} callback the callback function
* @return Order
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: Order
*/
self.getOrderById = function(orderId, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'orderId' is set
if (orderId == null) {
//throw new ApiException(400, "Missing the required parameter 'orderId' when calling getOrderById");
var errorRequiredMsg = "Missing the required parameter 'orderId' when calling getOrderById";
throw errorRequiredMsg;
throw "Missing the required parameter 'orderId' when calling getOrderById";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/store/order/{orderId}", "\\{format\\}","json")
, "\\{" + "orderId" + "\\}", encodeURIComponent(orderId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'orderId': orderId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {
if (!error && data) {
var result = new Order();
result.constructFromObject(data);
callback(error, result, response);
} else {
callback(error, data, response);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns Order
*/
var myResponse = new Order();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
};
}
});
return this.apiClient.callApi(
'/store/order/{orderId}', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Delete purchase order by ID
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
* @param {String} orderId ID of the order that needs to be deleted
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.deleteOrder = function(orderId, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'orderId' is set
if (orderId == null) {
//throw new ApiException(400, "Missing the required parameter 'orderId' when calling deleteOrder");
var errorRequiredMsg = "Missing the required parameter 'orderId' when calling deleteOrder";
throw errorRequiredMsg;
throw "Missing the required parameter 'orderId' when calling deleteOrder";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/store/order/{orderId}", "\\{format\\}","json")
, "\\{" + "orderId" + "\\}", encodeURIComponent(orderId.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'orderId': orderId
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/store/order/{orderId}', 'DELETE',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
};
function replaceAll (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
}
return result;
}
function createQueryString (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
}
return queryString;
}
}
// export module for Node.js
if (typeof module === 'object' && module.exports) {
module.exports = StoreApi;
}
return StoreApi;
}));

View File

@ -1,18 +1,23 @@
// require files in Node.js environment
var $, User;
if (typeof module === 'object' && module.exports) {
$ = require('jquery');
User = require('../model/User.js');
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['../ApiClient', '../model/User'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('../ApiClient.js'), require('../model/User.js'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
// export module for AMD
if ( typeof define === "function" && define.amd ) {
define(['jquery', 'User'], function($, User) {
return UserApi;
});
root.SwaggerPetstore.UserApi = factory(root.SwaggerPetstore.ApiClient, root.SwaggerPetstore.User);
}
}(this, function(ApiClient, User) {
'use strict';
var UserApi = function UserApi(apiClient) {
this.apiClient = apiClient || ApiClient.default;
var UserApi = function UserApi() {
var self = this;
@ -20,153 +25,114 @@ var UserApi = function UserApi() {
* Create user
* This can only be done by the logged in user.
* @param {User} body Created user object
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.createUser = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Creates list of users with given input array
*
* @param {Array} body List of user object
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.createUsersWithArrayInput = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/createWithArray", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/createWithArray', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Creates list of users with given input array
*
* @param {Array} body List of user object
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.createUsersWithListInput = function(body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/createWithList", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var postBody = body;
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/createWithList', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
@ -174,175 +140,129 @@ var UserApi = function UserApi() {
*
* @param {String} username The user name for login
* @param {String} password The password for login in clear text
* @param {function} callback the callback function
* @return String
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: String
*/
self.loginUser = function(username, password, callback) {
var postBody = null;
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/login", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
queryParams.username = username;
queryParams.password = password;
var pathParams = {
};
var queryParams = {
'username': username,
'password': password
};
var headerParams = {
};
var formParams = {
};
path += createQueryString(queryParams);
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns String
*/
var myResponse = response;
if (callback) {
callback(myResponse, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/login', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Logs out current logged in user session
*
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.logoutUser = function(callback) {
var postBody = null;
var postBinaryBody = null;
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/logout", "\\{format\\}","json"));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/logout', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Get user by user name
*
* @param {String} username The name that needs to be fetched. Use user1 for testing.
* @param {function} callback the callback function
* @return User
* @param {function} callback the callback function, accepting three arguments: error, data, response
* data is of type: User
*/
self.getUserByName = function(username, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'username' is set
if (username == null) {
//throw new ApiException(400, "Missing the required parameter 'username' when calling getUserByName");
var errorRequiredMsg = "Missing the required parameter 'username' when calling getUserByName";
throw errorRequiredMsg;
throw "Missing the required parameter 'username' when calling getUserByName";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/{username}", "\\{format\\}","json")
, "\\{" + "username" + "\\}", encodeURIComponent(username.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'username': username
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
handleResponse = function(error, data, response) {
if (!error && data) {
var result = new User();
result.constructFromObject(data);
callback(error, result, response);
} else {
callback(error, data, response);
}
});
request.done(function(response, textStatus, jqXHR){
/**
* @returns User
*/
var myResponse = new User();
myResponse.constructFromObject(response);
if (callback) {
callback(myResponse, textStatus, jqXHR);
};
}
});
return this.apiClient.callApi(
'/user/{username}', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
@ -350,149 +270,92 @@ var UserApi = function UserApi() {
* This can only be done by the logged in user.
* @param {String} username name that need to be deleted
* @param {User} body Updated user object
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.updateUser = function(username, body, callback) {
var postBody = JSON.stringify(body);
var postBinaryBody = null;
var postBody = body;
// verify the required parameter 'username' is set
if (username == null) {
//throw new ApiException(400, "Missing the required parameter 'username' when calling updateUser");
var errorRequiredMsg = "Missing the required parameter 'username' when calling updateUser";
throw errorRequiredMsg;
throw "Missing the required parameter 'username' when calling updateUser";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/{username}", "\\{format\\}","json")
, "\\{" + "username" + "\\}", encodeURIComponent(username.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'username': username
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/{username}', 'PUT',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
/**
* Delete user
* This can only be done by the logged in user.
* @param {String} username The name that needs to be deleted
* @param {function} callback the callback function
* @return void
* @param {function} callback the callback function, accepting three arguments: error, data, response
*/
self.deleteUser = function(username, callback) {
var postBody = null;
var postBinaryBody = null;
// verify the required parameter 'username' is set
if (username == null) {
//throw new ApiException(400, "Missing the required parameter 'username' when calling deleteUser");
var errorRequiredMsg = "Missing the required parameter 'username' when calling deleteUser";
throw errorRequiredMsg;
throw "Missing the required parameter 'username' when calling deleteUser";
}
// create path and map variables
var basePath = 'http://petstore.swagger.io/v2';
// if basePath ends with a /, remove it as path starts with a leading /
if (basePath.substring(basePath.length-1, basePath.length)=='/') {
basePath = basePath.substring(0, basePath.length-1);
}
var path = basePath + replaceAll(replaceAll("/user/{username}", "\\{format\\}","json")
, "\\{" + "username" + "\\}", encodeURIComponent(username.toString()));
var queryParams = {};
var headerParams = {};
var formParams = {};
var pathParams = {
'username': username
};
var queryParams = {
};
var headerParams = {
};
var formParams = {
};
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
path += createQueryString(queryParams);
var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody};
var request = $.ajax(path, options);
request.fail(function(jqXHR, textStatus, errorThrown){
var handleResponse = null;
if (callback) {
var error = errorThrown || textStatus || jqXHR.statusText || 'error';
callback(null, textStatus, jqXHR, error);
}
});
request.done(function(response, textStatus, jqXHR){
if (callback) {
callback(response, textStatus, jqXHR);
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
});
return this.apiClient.callApi(
'/user/{username}', 'DELETE',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
return request;
}
};
function replaceAll (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
}
return result;
}
function createQueryString (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
}
return queryString;
}
}
// export module for Node.js
if (typeof module === 'object' && module.exports) {
module.exports = UserApi;
}
return UserApi;
}));

View File

@ -1,22 +1,23 @@
if (typeof module === 'object' && module.exports) {
var SwaggerPetstore = {};
SwaggerPetstore.User = require('./model/User.js');
SwaggerPetstore.Category = require('./model/Category.js');
SwaggerPetstore.Pet = require('./model/Pet.js');
SwaggerPetstore.Tag = require('./model/Tag.js');
SwaggerPetstore.Order = require('./model/Order.js');
SwaggerPetstore.UserApi = require('./api/UserApi.js');
SwaggerPetstore.StoreApi = require('./api/StoreApi.js');
SwaggerPetstore.PetApi = require('./api/PetApi.js');
module.exports = SwaggerPetstore;
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['./ApiClient', './model/User', './model/Category', './model/Pet', './model/Tag', './model/Order', './api/UserApi', './api/StoreApi', './api/PetApi'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('./ApiClient.js'), require('./model/User.js'), require('./model/Category.js'), require('./model/Pet.js'), require('./model/Tag.js'), require('./model/Order.js'), require('./api/UserApi.js'), require('./api/StoreApi.js'), require('./api/PetApi.js'));
}
}(function(ApiClient, User, Category, Pet, Tag, Order, UserApi, StoreApi, PetApi) {
'use strict';
return {
ApiClient: ApiClient,
User: User,
Category: Category,
Pet: Pet,
Tag: Tag,
Order: Order,
UserApi: UserApi,
StoreApi: StoreApi,
PetApi: PetApi
};
}));

View File

@ -1,19 +1,22 @@
// require files in Node.js environment
if (typeof module === 'object' && module.exports) {
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined);
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
//export module
if ( typeof define === "function" && define.amd ) {
define('Category', ['jquery'],
function($) {
return Category;
});
factory(root.SwaggerPetstore);
}
}(this, function(module) {
'use strict';
var Category = function Category() {
@ -74,8 +77,13 @@ var Category = function Category() {
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.Category = Category;
}
if (typeof module === 'object' && module.exports) {
module.exports = Category;
}
return Category;
}));

View File

@ -1,8 +1,19 @@
// require files in Node.js environment
if (typeof module === 'object' && module.exports) {
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined);
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore);
}
}(this, function(module) {
'use strict';
@ -35,14 +46,6 @@ var StatusEnum = function StatusEnum() {
}
//export module
if ( typeof define === "function" && define.amd ) {
define('Order', ['jquery'],
function($) {
return Order;
});
}
var Order = function Order() {
var self = this;
@ -189,8 +192,13 @@ var Order = function Order() {
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.Order = Order;
}
if (typeof module === 'object' && module.exports) {
module.exports = Order;
}
return Order;
}));

View File

@ -1,10 +1,19 @@
// require files in Node.js environment
var Category;var Tag;
if (typeof module === 'object' && module.exports) {
Category = require('./Category.js');
Tag = require('./Tag.js');
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined, './Category', './Tag'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined, require('./Category.js'), require('./Tag.js'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
factory(root.SwaggerPetstore, root.SwaggerPetstore.Category, root.SwaggerPetstore.Tag);
}
}(this, function(module, Category, Tag) {
'use strict';
@ -37,14 +46,6 @@ var StatusEnum = function StatusEnum() {
}
//export module
if ( typeof define === "function" && define.amd ) {
define('Pet', ['jquery', 'Category', 'Array'],
function($, Category, Array) {
return Pet;
});
}
var Pet = function Pet(photoUrls, name) {
var self = this;
@ -193,8 +194,13 @@ var Pet = function Pet(photoUrls, name) {
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.Pet = Pet;
}
if (typeof module === 'object' && module.exports) {
module.exports = Pet;
}
return Pet;
}));

View File

@ -1,19 +1,22 @@
// require files in Node.js environment
if (typeof module === 'object' && module.exports) {
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined);
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
//export module
if ( typeof define === "function" && define.amd ) {
define('Tag', ['jquery'],
function($) {
return Tag;
});
factory(root.SwaggerPetstore);
}
}(this, function(module) {
'use strict';
var Tag = function Tag() {
@ -74,8 +77,13 @@ var Tag = function Tag() {
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.Tag = Tag;
}
if (typeof module === 'object' && module.exports) {
module.exports = Tag;
}
return Tag;
}));

View File

@ -1,19 +1,22 @@
// require files in Node.js environment
if (typeof module === 'object' && module.exports) {
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([undefined], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(undefined);
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
//export module
if ( typeof define === "function" && define.amd ) {
define('User', ['jquery'],
function($) {
return User;
});
factory(root.SwaggerPetstore);
}
}(this, function(module) {
'use strict';
var User = function User() {
@ -203,8 +206,13 @@ var User = function User() {
self.toJson = function () {
return JSON.stringify(self);
}
};
if (module) {
module.User = User;
}
if (typeof module === 'object' && module.exports) {
module.exports = User;
}
return User;
}));

View File

@ -0,0 +1,69 @@
if (typeof module === 'object' && module.exports) {
var expect = require('expect.js');
var SwaggerPetstore = require('../src/index');
}
var apiClient = SwaggerPetstore.ApiClient.default;
describe('ApiClient', function() {
describe('defaults', function() {
it('should have correct default values with the default API client', function() {
expect(apiClient).to.be.ok();
expect(apiClient.basePath).to.be('http://petstore.swagger.io/v2');
});
it('should have correct default values with new API client and can customize it', function() {
var newClient = new SwaggerPetstore.ApiClient;
expect(newClient.basePath).to.be('http://petstore.swagger.io/v2');
expect(newClient.buildUrl('/abc', {})).to.be('http://petstore.swagger.io/v2/abc');
newClient.basePath = 'http://example.com';
expect(newClient.basePath).to.be('http://example.com');
expect(newClient.buildUrl('/abc', {})).to.be('http://example.com/abc');
});
});
describe('#paramToString', function() {
it('should return empty string for null and undefined', function() {
expect(apiClient.paramToString(null)).to.be('');
expect(apiClient.paramToString(undefined)).to.be('');
});
it('should return string', function() {
expect(apiClient.paramToString('')).to.be('');
expect(apiClient.paramToString('abc')).to.be('abc');
expect(apiClient.paramToString(123)).to.be('123');
});
});
describe('#buildUrl', function() {
it('should work without path parameters in the path', function() {
expect(apiClient.buildUrl('/abc', {})).to
.be('http://petstore.swagger.io/v2/abc');
expect(apiClient.buildUrl('/abc/def?ok', {id: 123})).to
.be('http://petstore.swagger.io/v2/abc/def?ok');
});
it('should work with path parameters in the path', function() {
expect(apiClient.buildUrl('/{id}', {id: 123})).to
.be('http://petstore.swagger.io/v2/123');
expect(apiClient.buildUrl('/abc/{id}/{name}?ok', {id: 456, name: 'a b'})).to.
be('http://petstore.swagger.io/v2/abc/456/a%20b?ok');
});
});
describe('#isJsonMime', function() {
it('should return true for JSON MIME', function() {
expect(apiClient.isJsonMime('application/json')).to.be(true);
expect(apiClient.isJsonMime('application/json; charset=UTF8')).to.be(true);
expect(apiClient.isJsonMime('APPLICATION/JSON')).to.be(true);
});
it('should return false for non-JSON MIME', function() {
expect(apiClient.isJsonMime('')).to.be(false);
expect(apiClient.isJsonMime('text/plain')).to.be(false);
expect(apiClient.isJsonMime('application/xml')).to.be(false);
expect(apiClient.isJsonMime('application/jsonp')).to.be(false);
});
});
});

View File

@ -1,25 +1,21 @@
if (typeof module === 'object' && module.exports) {
var expect = require('expect.js');
var requireApiWithMocks = require('../helper.js').requireApiWithMocks;
var PetApi = requireApiWithMocks('PetApi');
var Pet = require('../../src/model/Pet');
var Category = require('../../src/model/Category');
var Tag = require('../../src/model/Tag');
var SwaggerPetstore = require('../../src/index');
}
var api;
beforeEach(function() {
api = new PetApi();
api = new SwaggerPetstore.PetApi();
});
var createRandomPet = function() {
var id = new Date().getTime();
var pet = new Pet();
var pet = new SwaggerPetstore.Pet();
pet.setId(id);
pet.setName("gorilla" + id);
var category = new Category();
var category = new SwaggerPetstore.Category();
category.setName("really-happy");
pet.setCategory(category);
@ -33,11 +29,15 @@ var createRandomPet = function() {
describe('PetApi', function() {
it('should create and get pet', function(done) {
var pet = createRandomPet();
api.addPet(pet).then(function() {
api.getPetById(pet.id, function(fetched, textStatus, jqXHR, error) {
api.addPet(pet, function(error) {
if (error) throw error;
expect(textStatus).to.be('success');
api.getPetById(pet.id, function(error, fetched, response) {
if (error) throw error;
expect(response.status).to.be(200);
expect(response.ok).to.be(true);
expect(response.get('Content-Type')).to.be('application/json');
expect(fetched).to.be.ok();
expect(fetched.id).to.be(pet.id);
expect(fetched.getCategory()).to.be.ok();
@ -46,8 +46,6 @@ describe('PetApi', function() {
api.deletePet(pet.id);
done();
});
}, function(jqXHR, textStatus, errorThrown) {
throw errorThrown || textStatus;
});
});
});

View File

@ -1,19 +0,0 @@
var mockrequire = require('mockrequire');
var jquery = require('jquery');
var domino = require('domino');
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var window = domino.createWindow();
var $ = jquery(window);
$.support.cors = true;
$.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
};
var requireApiWithMocks = function(path) {
return mockrequire('../src/api/' + path, {
'jquery': $
});
};
exports.requireApiWithMocks = requireApiWithMocks;

View File

@ -10,7 +10,6 @@
<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script>
mocha.setup({
ui: 'bdd',
@ -18,12 +17,20 @@
});
</script>
<script src="https://cdn.rawgit.com/stephanebachelier/superagent-dist/1.6.1/superagent.js"></script>
<script src="../src/model/Category.js"></script>
<script src="../src/model/Tag.js"></script>
<script src="../src/model/Pet.js"></script>
<script src="../src/model/User.js"></script>
<script src="../src/ApiClient.js"></script>
<script src="../src/api/PetApi.js"></script>
<script src="ApiClientTest.js"></script>
<script src="api/PetApiTest.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);

File diff suppressed because it is too large Load Diff