Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "servers" support to operation, path in the JS client #2060

Merged
merged 3 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,26 @@
return param.toString();
};

{{#emitJSDoc}} /**
{{#emitJSDoc}}
/**
* 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.
*/
{{/emitJSDoc}} exports.prototype.buildUrl = function(path, pathParams) {
{{/emitJSDoc}}
exports.prototype.buildUrl = function(path, pathParams, apiBasePath) {
if (!path.match(/^\//)) {
path = '/' + path;
}
var url = this.basePath + path;

// use API (operation, path) base path if defined
if (apiBasePath !== null && apiBasePath !== undefined) {
url = apiBasePath + path;
}

var _this = this;
url = url.replace(/\{([\w-]+)\}/g, function(fullMatch, key) {
var value;
Expand Down Expand Up @@ -390,10 +398,10 @@
*/
{{/emitJSDoc}} exports.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
returnType{{^usePromises}}, callback{{/usePromises}}) {
returnType, apiBasePath{{^usePromises}}, callback{{/usePromises}}) {

var _this = this;
var url = this.buildUrl(path, pathParams);
var url = this.buildUrl(path, pathParams, apiBasePath);
var request = superagent(httpMethod, url);

if (this.plugins !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@
var contentTypes = [<#consumes>'<& mediaType>'<#hasMore>, </hasMore></consumes>];
var accepts = [<#produces>'<& mediaType>'<#hasMore>, </hasMore></produces>];
var returnType = <#vendorExtensions.x-return-type><&vendorExtensions.x-return-type></vendorExtensions.x-return-type><^vendorExtensions.x-return-type>null</vendorExtensions.x-return-type>;
<#servers.0>
let basePaths = [<#servers>'<url>'<^-last>, </-last></servers>];
let basePath = basePaths[0]; // by default use the first one in "servers" defined in OpenAPI
if (typeof opts['_base_path_index'] !== 'undefined') {
if (opts['_base_path_index'] < 0 || opts['_base_path_index'] >= basePaths.length) {
throw new Error("Invalid index " + opts['_base_path_index'] + " when selecting the host settings. Must be less than " + basePaths.length);
}
basePath = basePaths[opts['_base_path_index']];
}

</servers.0>
return this.apiClient.callApi(
'<&path>', '<httpMethod>',
pathParams, queryParams, collectionQueryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
authNames, contentTypes, accepts, returnType, <#servers.0>basePath</servers.0><^servers.0>null</servers.0><^usePromises>, callback</usePromises>
);
}
<#usePromises>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,28 @@ class ApiClient {
return param.toString();
}

{{#emitJSDoc}}/**
{{#emitJSDoc}}
/**
* 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.
* @param {String} apiBasePath Base path defined in the path, operation level to override the default one
* @returns {String} The encoded path with parameter values substituted.
*/{{/emitJSDoc}}
buildUrl(path, pathParams) {
*/
{{/emitJSDoc}}
buildUrl(path, pathParams, apiBasePath) {
if (!path.match(/^\//)) {
path = '/' + path;
}

var url = this.basePath + path;

// use API (operation, path) base path if defined
if (apiBasePath !== null && apiBasePath !== undefined) {
url = apiBasePath + path;
}

url = url.replace(/\{([\w-]+)\}/g, (fullMatch, key) => {
var value;
if (pathParams.hasOwnProperty(key)) {
Expand Down Expand Up @@ -309,7 +318,8 @@ class ApiClient {
});
}

{{#emitJSDoc}}/**
{{#emitJSDoc}}
/**
* 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
Expand All @@ -335,15 +345,20 @@ class ApiClient {
return ApiClient.convertToType(data, returnType);
}

{{#emitJSDoc}}{{^usePromises}}/**
{{#emitJSDoc}}
{{^usePromises}}
/**
* Callback function to receive the result of the operation.
* @callback module:{{#invokerPackage}}{{invokerPackage}}/{{/invokerPackage}}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.
*/{{/usePromises}}{{/emitJSDoc}}
*/
{{/usePromises}}
{{/emitJSDoc}}

{{#emitJSDoc}}/**
{{#emitJSDoc}}
/**
* 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.
Expand All @@ -356,16 +371,19 @@ class ApiClient {
* @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.{{^usePromises}}
* @param {module:{{#invokerPackage}}{{invokerPackage}}/{{/invokerPackage}}ApiClient~callApiCallback} callback The callback function.{{/usePromises}}
* constructor for a complex type.
* @param {String} apiBasePath base path defined in the operation/path level to override the default one
{{^usePromises}}
* @param {module:{{#invokerPackage}}{{invokerPackage}}/{{/invokerPackage}}ApiClient~callApiCallback} callback The callback function.
{{/usePromises}}
* @returns {{#usePromises}}{Promise} A {@link https://www.promisejs.org/|Promise} object{{/usePromises}}{{^usePromises}}{Object} The SuperAgent request object{{/usePromises}}.
*/
{{/emitJSDoc}}
callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
returnType{{^usePromises}}, callback{{/usePromises}}) {
returnType, apiBasePath{{^usePromises}}, callback{{/usePromises}}) {

var url = this.buildUrl(path, pathParams);
var url = this.buildUrl(path, pathParams, apiBasePath);
var request = superagent(httpMethod, url);

if (this.plugins !== null) {
Expand Down Expand Up @@ -446,7 +464,8 @@ class ApiClient {
}
}

{{#usePromises}}return new Promise((resolve, reject) => {
{{#usePromises}}
return new Promise((resolve, reject) => {
request.end((error, response) => {
if (error) {
var err = {};
Expand All @@ -470,9 +489,11 @@ class ApiClient {
}
}
});
});{{/usePromises}}
});

{{^usePromises}}request.end((error, response) => {
{{/usePromises}}
{{^usePromises}}
request.end((error, response) => {
if (callback) {
var data = null;
if (!error) {
Expand All @@ -490,7 +511,8 @@ class ApiClient {
}
});

return request;{{/usePromises}}
return request;
{{/usePromises}}
}

{{#emitJSDoc}}/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ export default class <classname> {
</emitJSDoc> <operationId><#usePromises>WithHttpInfo</usePromises>(<vendorExtensions.x-codegen-argList>) {<#hasOptionalParams>
opts = opts || {};</hasOptionalParams>
let postBody = <#bodyParam><#required><paramName></required><^required>opts['<paramName>']</required></bodyParam><^bodyParam>null</bodyParam>;
<#allParams><#required>
<#allParams>
<#required>
// verify the required parameter '<paramName>' is set
if (<paramName> === undefined || <paramName> === null) {
throw new Error("Missing the required parameter '<paramName>' when calling <operationId>");
}
</required></allParams>
</required>
</allParams>

let pathParams = {<#pathParams>
'<baseName>': <#required><paramName></required><^required>opts['<paramName>']</required><#hasMore>,</hasMore></pathParams>
Expand All @@ -70,15 +72,25 @@ export default class <classname> {
let contentTypes = [<#consumes>'<& mediaType>'<#hasMore>, </hasMore></consumes>];
let accepts = [<#produces>'<& mediaType>'<#hasMore>, </hasMore></produces>];
let returnType = <#vendorExtensions.x-return-type><&vendorExtensions.x-return-type></vendorExtensions.x-return-type><^vendorExtensions.x-return-type>null</vendorExtensions.x-return-type>;
<#servers.0>
let basePaths = [<#servers>'<url>'<^-last>, </-last></servers>];
let basePath = basePaths[0]; // by default use the first one in "servers" defined in OpenAPI
if (typeof opts['_base_path_index'] !== 'undefined') {
if (opts['_base_path_index'] < 0 || opts['_base_path_index'] >= basePaths.length) {
throw new Error("Invalid index " + opts['_base_path_index'] + " when selecting the host settings. Must be less than " + basePaths.length);
}
basePath = basePaths[opts['_base_path_index']];
}

</servers.0>
return this.apiClient.callApi(
'<&path>', '<httpMethod>',
pathParams, queryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
authNames, contentTypes, accepts, returnType, <#servers.0>basePath</servers.0><^servers.0>null</servers.0><^usePromises>, callback</usePromises>
);
}
<#usePromises>
<#emitJSDoc>
<#emitJSDoc>

/**<#summary>
* <summary></summary><#notes>
Expand All @@ -90,7 +102,8 @@ export default class <classname> {
* data is of type: {@link <&vendorExtensions.x-jsdoc-type>}</returnType></usePromises><#usePromises>
* @return {Promise} a {@link https://www.promisejs.org/|Promise}<#returnType>, with data of type {@link <&vendorExtensions.x-jsdoc-type>}</returnType></usePromises>
*/
</emitJSDoc> <operationId>(<vendorExtensions.x-codegen-argList>) {
</emitJSDoc>
<operationId>(<vendorExtensions.x-codegen-argList>) {
return this.<operationId>WithHttpInfo(<vendorExtensions.x-codegen-argList>)
.then(function(response_and_data) {
return response_and_data.data;
Expand Down
24 changes: 15 additions & 9 deletions samples/client/petstore/javascript-es6/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,26 @@ class ApiClient {
return param.toString();
}

/**
/**
* 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.
* @param {String} apiBasePath Base path defined in the path, operation level to override the default one
* @returns {String} The encoded path with parameter values substituted.
*/
buildUrl(path, pathParams) {
buildUrl(path, pathParams, apiBasePath) {
if (!path.match(/^\//)) {
path = '/' + path;
}

var url = this.basePath + path;

// use API (operation, path) base path if defined
if (apiBasePath !== null && apiBasePath !== undefined) {
url = apiBasePath + path;
}

url = url.replace(/\{([\w-]+)\}/g, (fullMatch, key) => {
var value;
if (pathParams.hasOwnProperty(key)) {
Expand Down Expand Up @@ -308,7 +315,7 @@ class ApiClient {
});
}

/**
/**
* 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
Expand All @@ -333,15 +340,15 @@ class ApiClient {
return ApiClient.convertToType(data, returnType);
}

/**
/**
* 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.
Expand All @@ -355,14 +362,15 @@ class ApiClient {
* @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 {String} apiBasePath base path defined in the operation/path level to override the default one
* @param {module:ApiClient~callApiCallback} callback The callback function.
* @returns {Object} The SuperAgent request object.
*/
callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
returnType, callback) {
returnType, apiBasePath, callback) {

var url = this.buildUrl(path, pathParams);
var url = this.buildUrl(path, pathParams, apiBasePath);
var request = superagent(httpMethod, url);

if (this.plugins !== null) {
Expand Down Expand Up @@ -443,8 +451,6 @@ class ApiClient {
}
}



request.end((error, response) => {
if (callback) {
var data = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ export default class AnotherFakeApi {
*/
call123testSpecialTags(body, callback) {
let postBody = body;

// verify the required parameter 'body' is set
if (body === undefined || body === null) {
throw new Error("Missing the required parameter 'body' when calling call123testSpecialTags");
}


let pathParams = {
};
let queryParams = {
Expand All @@ -71,11 +69,10 @@ export default class AnotherFakeApi {
let contentTypes = ['application/json'];
let accepts = ['application/json'];
let returnType = Client;

return this.apiClient.callApi(
'/another-fake/dummy', 'PATCH',
pathParams, queryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, null, callback
);
}

Expand Down
Loading