forked from loafle/openapi-generator-original
[JavaScript] Recommission integration tests for Issue#2258.
This commit is contained in:
@@ -15,50 +15,78 @@
|
||||
}(this, function(superagent) {
|
||||
'use strict';
|
||||
|
||||
var ApiClient = function ApiClient() {
|
||||
/**
|
||||
* @module ApiClient
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manages low level client-server communications, parameter marshalling, etc. There should not be any need for an
|
||||
* application to use this class directly - the *Api and model classes provide the public API for the service. The
|
||||
* contents of this file should be regarded as internal but are documented for completeness.
|
||||
* @alias module:ApiClient
|
||||
* @class
|
||||
*/
|
||||
var exports = function() {
|
||||
/**
|
||||
* The base path to put in front of every API call's (relative) path.
|
||||
* The base URL against which to resolve every API call's (relative) path.
|
||||
* @type {String}
|
||||
* @default http://petstore.swagger.io/v2
|
||||
*/
|
||||
this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, '');
|
||||
|
||||
/**
|
||||
* The authentication methods to be included for all API calls.
|
||||
* @type {Array.<String>}
|
||||
*/
|
||||
this.authentications = {
|
||||
'petstore_auth': {type: 'oauth2'},
|
||||
'test_api_client_id': {type: 'apiKey', in: 'header', name: 'x-test_api_client_id'},
|
||||
'test_api_client_secret': {type: 'apiKey', in: 'header', name: 'x-test_api_client_secret'},
|
||||
'api_key': {type: 'apiKey', in: 'header', name: 'api_key'},
|
||||
'test_api_key_header': {type: 'apiKey', 'in': 'header', name: 'test_api_key_header'},
|
||||
'api_key': {type: 'apiKey', 'in': 'header', name: 'api_key'},
|
||||
'test_http_basic': {type: 'basic'},
|
||||
'test_api_key_query': {type: 'apiKey', in: 'query', name: 'test_api_key_query'},
|
||||
'test_api_key_header': {type: 'apiKey', in: 'header', name: 'test_api_key_header'}
|
||||
'test_api_client_secret': {type: 'apiKey', 'in': 'header', name: 'x-test_api_client_secret'},
|
||||
'test_api_client_id': {type: 'apiKey', 'in': 'header', name: 'x-test_api_client_id'},
|
||||
'test_api_key_query': {type: 'apiKey', 'in': 'query', name: 'test_api_key_query'},
|
||||
'petstore_auth': {type: 'oauth2'}
|
||||
};
|
||||
|
||||
/**
|
||||
* The default HTTP headers to be included for all API calls.
|
||||
* @type {Array.<String>}
|
||||
* @default {}
|
||||
*/
|
||||
this.defaultHeaders = {};
|
||||
|
||||
/**
|
||||
* The default HTTP timeout for all API calls.
|
||||
* @type {Number}
|
||||
* @default 60000
|
||||
*/
|
||||
this.timeout = 60000;
|
||||
};
|
||||
|
||||
ApiClient.prototype.paramToString = function paramToString(param) {
|
||||
if (param == null) {
|
||||
// return empty string for null and undefined
|
||||
/**
|
||||
* Returns a string representation for an actual parameter.
|
||||
* @param param The actual parameter.
|
||||
* @returns {String} The string representation of <code>param</code>.
|
||||
*/
|
||||
exports.prototype.paramToString = function(param) {
|
||||
if (param == undefined || param == null) {
|
||||
return '';
|
||||
} else if (param instanceof Date) {
|
||||
return param.toJSON();
|
||||
} else {
|
||||
return param.toString();
|
||||
}
|
||||
if (param instanceof Date) {
|
||||
return param.toJSON();
|
||||
}
|
||||
return param.toString();
|
||||
};
|
||||
|
||||
/**
|
||||
* Build full URL by appending the given path to base path and replacing
|
||||
* path parameter placeholders with parameter values.
|
||||
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
|
||||
* NOTE: query parameters are not handled here.
|
||||
* @param {String} path The path to append to the base URL.
|
||||
* @param {Object} pathParams The parameter values to append.
|
||||
* @returns {String} The encoded path with parameter values substituted.
|
||||
*/
|
||||
ApiClient.prototype.buildUrl = function buildUrl(path, pathParams) {
|
||||
exports.prototype.buildUrl = function(path, pathParams) {
|
||||
if (!path.match(/^\//)) {
|
||||
path = '/' + path;
|
||||
}
|
||||
@@ -77,34 +105,40 @@
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the given MIME is a JSON MIME.
|
||||
* JSON MIME examples:
|
||||
* application/json
|
||||
* application/json; charset=UTF8
|
||||
* APPLICATION/JSON
|
||||
* Checks whether the given content type represents JSON.<br>
|
||||
* JSON content type examples:<br>
|
||||
* <ul>
|
||||
* <li>application/json</li>
|
||||
* <li>application/json; charset=UTF8</li>
|
||||
* <li>APPLICATION/JSON</li>
|
||||
* </ul>
|
||||
* @param {String} contentType The MIME content type to check.
|
||||
* @returns {Boolean} <code>true</code> if <code>contentType</code> represents JSON, otherwise <code>false</code>.
|
||||
*/
|
||||
ApiClient.prototype.isJsonMime = function isJsonMime(mime) {
|
||||
return Boolean(mime != null && mime.match(/^application\/json(;.*)?$/i));
|
||||
exports.prototype.isJsonMime = function(contentType) {
|
||||
return Boolean(contentType != null && contentType.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.
|
||||
* Chooses a content type from the given array, with JSON preferred; i.e. return JSON if included, otherwise return the first.
|
||||
* @param {Array.<String>} contentTypes
|
||||
* @returns {String} The chosen content type, preferring JSON.
|
||||
*/
|
||||
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];
|
||||
exports.prototype.jsonPreferredMime = function(contentTypes) {
|
||||
for (var i = 0; i < contentTypes.length; i++) {
|
||||
if (this.isJsonMime(contentTypes[i])) {
|
||||
return contentTypes[i];
|
||||
}
|
||||
}
|
||||
return mimes[0];
|
||||
return contentTypes[0];
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the given parameter value is like file content.
|
||||
* Checks whether the given parameter value represents file-like content.
|
||||
* @param param The parameter to check.
|
||||
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
|
||||
*/
|
||||
ApiClient.prototype.isFileParam = function isFileParam(param) {
|
||||
exports.prototype.isFileParam = function(param) {
|
||||
// fs.ReadStream in Node.js (but not in runtime like browserify)
|
||||
if (typeof window === 'undefined' &&
|
||||
typeof require === 'function' &&
|
||||
@@ -128,15 +162,19 @@
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalize parameters values:
|
||||
* remove nils,
|
||||
* keep files and arrays,
|
||||
* format to string with `paramToString` for other cases.
|
||||
* Normalizes parameter values:
|
||||
* <ul>
|
||||
* <li>remove nils</li>
|
||||
* <li>keep files and arrays</li>
|
||||
* <li>format to string with `paramToString` for other cases</li>
|
||||
* </ul>
|
||||
* @param {Object.<String, Object>} params The parameters as object properties.
|
||||
* @returns {Object.<String, Object>} normalized parameters.
|
||||
*/
|
||||
ApiClient.prototype.normalizeParams = function normalizeParams(params) {
|
||||
exports.prototype.normalizeParams = function(params) {
|
||||
var newParams = {};
|
||||
for (var key in params) {
|
||||
if (params.hasOwnProperty(key) && params[key] != null) {
|
||||
if (params.hasOwnProperty(key) && params[key] != undefined && params[key] != null) {
|
||||
var value = params[key];
|
||||
if (this.isFileParam(value) || Array.isArray(value)) {
|
||||
newParams[key] = value;
|
||||
@@ -149,10 +187,46 @@
|
||||
};
|
||||
|
||||
/**
|
||||
* Build parameter value according to the given collection format.
|
||||
* @param {String} collectionFormat one of 'csv', 'ssv', 'tsv', 'pipes' and 'multi'
|
||||
* Enumeration of collection format separator strategies.
|
||||
* @enum {String}
|
||||
* @readonly
|
||||
*/
|
||||
ApiClient.prototype.buildCollectionParam = function buildCollectionParam(param, collectionFormat) {
|
||||
exports.CollectionFormatEnum = {
|
||||
/**
|
||||
* Comma-separated values. Value: <code>csv</code>
|
||||
* @const
|
||||
*/
|
||||
CSV: ',',
|
||||
/**
|
||||
* Space-separated values. Value: <code>ssv</code>
|
||||
* @const
|
||||
*/
|
||||
SSV: ' ',
|
||||
/**
|
||||
* Tab-separated values. Value: <code>tsv</code>
|
||||
* @const
|
||||
*/
|
||||
TSV: '\t',
|
||||
/**
|
||||
* Pipe(|)-separated values. Value: <code>pipes</code>
|
||||
* @const
|
||||
*/
|
||||
PIPES: '|',
|
||||
/**
|
||||
* Native array. Value: <code>multi</code>
|
||||
* @const
|
||||
*/
|
||||
MULTI: 'multi'
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds a string representation of an array-type actual parameter, according to the given collection format.
|
||||
* @param {Array} param An array parameter.
|
||||
* @param {module:ApiClient.CollectionFormatEnum} collectionFormat The array element separator strategy.
|
||||
* @returns {String|Array} A string representation of the supplied collection, using the specified delimiter. Returns
|
||||
* <code>param</code> as is if <code>collectionFormat</code> is <code>multi</code>.
|
||||
*/
|
||||
exports.prototype.buildCollectionParam = function buildCollectionParam(param, collectionFormat) {
|
||||
if (param == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -166,14 +240,19 @@
|
||||
case 'pipes':
|
||||
return param.map(this.paramToString).join('|');
|
||||
case 'multi':
|
||||
// return the array directly as Superagent will handle it as expected
|
||||
// return the array directly as SuperAgent will handle it as expected
|
||||
return param.map(this.paramToString);
|
||||
default:
|
||||
throw new Error('Unknown collection format: ' + collectionFormat);
|
||||
}
|
||||
};
|
||||
|
||||
ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
|
||||
/**
|
||||
* Applies authentication headers to the request.
|
||||
* @param {Object} request The request object created by a <code>superagent()</code> call.
|
||||
* @param {Array.<String>} authNames An array of authentication method names.
|
||||
*/
|
||||
exports.prototype.applyAuthToRequest = function(request, authNames) {
|
||||
var _this = this;
|
||||
authNames.forEach(function(authName) {
|
||||
var auth = _this.authentications[authName];
|
||||
@@ -191,7 +270,7 @@
|
||||
} else {
|
||||
data[auth.name] = auth.apiKey;
|
||||
}
|
||||
if (auth.in === 'header') {
|
||||
if (auth['in'] === 'header') {
|
||||
request.set(data);
|
||||
} else {
|
||||
request.query(data);
|
||||
@@ -209,24 +288,58 @@
|
||||
});
|
||||
};
|
||||
|
||||
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
|
||||
/**
|
||||
* Deserializes an HTTP response body into a value of the specified type.
|
||||
* @param {Object} response A SuperAgent response object.
|
||||
* @param {(String|Array.<String>|Object.<String, Object>|Function)} returnType The type to return. Pass a string for simple types
|
||||
* or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
|
||||
* return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
|
||||
* all properties on <code>data<code> will be converted to this type.
|
||||
* @returns A value of the specified type.
|
||||
*/
|
||||
exports.prototype.deserialize = function deserialize(response, returnType) {
|
||||
if (response == null || returnType == null) {
|
||||
return null;
|
||||
}
|
||||
// Rely on Superagent for parsing response body.
|
||||
// Rely on SuperAgent for parsing response body.
|
||||
// See http://visionmedia.github.io/superagent/#parsing-response-bodies
|
||||
var data = response.body;
|
||||
if (data == null) {
|
||||
// Superagent does not always produce a body; use the unparsed response
|
||||
// as a fallback
|
||||
// SuperAgent does not always produce a body; use the unparsed response as a fallback
|
||||
data = response.text;
|
||||
}
|
||||
return ApiClient.convertToType(data, returnType);
|
||||
return exports.convertToType(data, returnType);
|
||||
};
|
||||
|
||||
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
|
||||
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
|
||||
accepts, returnType, callback) {
|
||||
/**
|
||||
* Callback function to receive the result of the operation.
|
||||
* @callback module:ApiClient~callApiCallback
|
||||
* @param {String} error Error message, if any.
|
||||
* @param data The data returned by the service call.
|
||||
* @param {String} response The complete HTTP response.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invokes the REST service using the supplied settings and parameters.
|
||||
* @param {String} path The base URL to invoke.
|
||||
* @param {String} httpMethod The HTTP method to use.
|
||||
* @param {Object.<String, String>} pathParams A map of path parameters and their values.
|
||||
* @param {Object.<String, Object>} queryParams A map of query parameters and their values.
|
||||
* @param {Object.<String, Object>} headerParams A map of header parameters and their values.
|
||||
* @param {Object.<String, Object>} formParams A map of form parameters and their values.
|
||||
* @param {Object} bodyParam The value to pass as the request body.
|
||||
* @param {Array.<String>} authNames An array of authentication type names.
|
||||
* @param {Array.<String>} contentTypes An array of request MIME types.
|
||||
* @param {Array.<String>} accepts An array of acceptable response MIME types.
|
||||
* @param {(String|Array|ObjectFunction)} returnType The required type to return; can be a string for simple types or the
|
||||
* constructor for a complex type.
|
||||
* @param {module:ApiClient~callApiCallback} callback The callback function.
|
||||
* @returns {Object} The SuperAgent request object.
|
||||
*/
|
||||
exports.prototype.callApi = function callApi(path, httpMethod, pathParams,
|
||||
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
|
||||
returnType, callback) {
|
||||
|
||||
var _this = this;
|
||||
var url = this.buildUrl(path, pathParams);
|
||||
var request = superagent(httpMethod, url);
|
||||
@@ -240,7 +353,7 @@
|
||||
// set header parameters
|
||||
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
|
||||
|
||||
//set request timeout
|
||||
// set request timeout
|
||||
request.timeout(this.timeout);
|
||||
|
||||
var contentType = this.jsonPreferredMime(contentTypes);
|
||||
@@ -273,8 +386,7 @@
|
||||
request.accept(accept);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
request.end(function(error, response) {
|
||||
if (callback) {
|
||||
var data = null;
|
||||
@@ -286,15 +398,27 @@
|
||||
});
|
||||
|
||||
return request;
|
||||
|
||||
};
|
||||
|
||||
ApiClient.parseDate = function parseDate(str) {
|
||||
str = str.replace(/T/i, ' ');
|
||||
return new Date(str);
|
||||
/**
|
||||
* Parses an ISO-8601 string representation of a date value.
|
||||
* @param {String} str The date value as a string.
|
||||
* @returns {Date} The parsed date object.
|
||||
*/
|
||||
exports.parseDate = function(str) {
|
||||
return new Date(str.replace(/T/i, ' '));
|
||||
};
|
||||
|
||||
ApiClient.convertToType = function convertToType(data, type) {
|
||||
/**
|
||||
* Converts a value to the specified type.
|
||||
* @param {(String|Object)} data The data to convert, as a string or object.
|
||||
* @param {(String|Array.<String>|Object.<String, Object>|Function)} type The type to return. Pass a string for simple types
|
||||
* or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
|
||||
* return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
|
||||
* all properties on <code>data<code> will be converted to this type.
|
||||
* @returns An instance of the specified type.
|
||||
*/
|
||||
exports.convertToType = function(data, type) {
|
||||
switch (type) {
|
||||
case 'Boolean':
|
||||
return Boolean(data);
|
||||
@@ -312,13 +436,12 @@
|
||||
return data;
|
||||
} else if (typeof type === 'function') {
|
||||
// for model type like: User
|
||||
var model = type.constructFromObject(data);
|
||||
return model;
|
||||
return type.constructFromObject(data);
|
||||
} else if (Array.isArray(type)) {
|
||||
// for array type like: ['String']
|
||||
var itemType = type[0];
|
||||
return data.map(function(item) {
|
||||
return ApiClient.convertToType(item, itemType);
|
||||
return exports.convertToType(item, itemType);
|
||||
});
|
||||
} else if (typeof type === 'object') {
|
||||
// for plain object type like: {'String': 'Integer'}
|
||||
@@ -333,8 +456,8 @@
|
||||
var result = {};
|
||||
for (var k in data) {
|
||||
if (data.hasOwnProperty(k)) {
|
||||
var key = ApiClient.convertToType(k, keyType);
|
||||
var value = ApiClient.convertToType(data[k], valueType);
|
||||
var key = exports.convertToType(k, keyType);
|
||||
var value = exports.convertToType(data[k], valueType);
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
@@ -346,7 +469,11 @@
|
||||
}
|
||||
};
|
||||
|
||||
ApiClient.default = new ApiClient();
|
||||
/**
|
||||
* The default API client implementation.
|
||||
* @type {module:ApiClient}
|
||||
*/
|
||||
exports.instance = new exports();
|
||||
|
||||
return ApiClient;
|
||||
return exports;
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user