mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-18 21:47:04 +00:00
Add multiple servers support to JS API client (#1974)
* add multiple servers support to JS ES6 * multiple server support in js es5 * using exports in es5 * fix index check * add oas v3 js es6 client to travis
This commit is contained in:
@@ -594,6 +594,71 @@
|
||||
}
|
||||
};
|
||||
|
||||
{{#emitJSDoc}}
|
||||
/**
|
||||
* Gets an array of host settings
|
||||
* @returns An array of host settings
|
||||
*/
|
||||
{{/emitJSDoc}}
|
||||
exports.hostSettings = function() {
|
||||
return [
|
||||
{{#servers}}
|
||||
{
|
||||
'url': "{{{url}}}",
|
||||
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
{{#variables}}
|
||||
{{#-first}}
|
||||
'variables': {
|
||||
{{/-first}}
|
||||
{{{name}}}: {
|
||||
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
'default_value': "{{{defaultValue}}}",
|
||||
{{#enumValues}}
|
||||
{{#-first}}
|
||||
'enum_values': [
|
||||
{{/-first}}
|
||||
"{{{.}}}"{{^-last}},{{/-last}}
|
||||
{{#-last}}
|
||||
]
|
||||
{{/-last}}
|
||||
{{/enumValues}}
|
||||
}{{^-last}},{{/-last}}
|
||||
{{#-last}}
|
||||
}
|
||||
{{/-last}}
|
||||
{{/variables}}
|
||||
}{{^-last}},{{/-last}}
|
||||
{{/servers}}
|
||||
];
|
||||
};
|
||||
|
||||
exports.getBasePathFromSettings = function(index, variables={}) {
|
||||
var servers = this.hostSettings();
|
||||
|
||||
// check array index out of bound
|
||||
if (index < 0 || index >= servers.length) {
|
||||
throw new Error("Invalid index " + index + " when selecting the host settings. Must be less than " + servers.length);
|
||||
}
|
||||
|
||||
var server = servers[index];
|
||||
var url = server['url'];
|
||||
|
||||
// go through variable and assign a value
|
||||
for (var variable_name in server['variables']) {
|
||||
if (variable_name in variables) {
|
||||
if (server['variables'][variable_name]['enum_values'].includes(variables[variable_name])) {
|
||||
url = url.replace("{" + variable_name + "}", variables[variable_name]);
|
||||
} else {
|
||||
throw new Error("The variable `" + variable_name + "` in the host URL has invalid value " + variables[variable_name] + ". Must be " + server['variables'][variable_name]['enum_values'] + ".");
|
||||
}
|
||||
} else {
|
||||
// use default value
|
||||
url = url.replace("{" + variable_name + "}", server['variables'][variable_name]['default_value'])
|
||||
}
|
||||
}
|
||||
return url;
|
||||
};
|
||||
|
||||
{{#emitJSDoc}} /**
|
||||
* Constructs a new map or array model from REST data.
|
||||
* @param data {Object|Array} The REST data.
|
||||
|
||||
@@ -560,6 +560,71 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
{{#emitJSDoc}}
|
||||
/**
|
||||
* Gets an array of host settings
|
||||
* @returns An array of host settings
|
||||
*/
|
||||
{{/emitJSDoc}}
|
||||
hostSettings() {
|
||||
return [
|
||||
{{#servers}}
|
||||
{
|
||||
'url': "{{{url}}}",
|
||||
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
{{#variables}}
|
||||
{{#-first}}
|
||||
'variables': {
|
||||
{{/-first}}
|
||||
{{{name}}}: {
|
||||
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
'default_value': "{{{defaultValue}}}",
|
||||
{{#enumValues}}
|
||||
{{#-first}}
|
||||
'enum_values': [
|
||||
{{/-first}}
|
||||
"{{{.}}}"{{^-last}},{{/-last}}
|
||||
{{#-last}}
|
||||
]
|
||||
{{/-last}}
|
||||
{{/enumValues}}
|
||||
}{{^-last}},{{/-last}}
|
||||
{{#-last}}
|
||||
}
|
||||
{{/-last}}
|
||||
{{/variables}}
|
||||
}{{^-last}},{{/-last}}
|
||||
{{/servers}}
|
||||
];
|
||||
}
|
||||
|
||||
getBasePathFromSettings(index, variables={}) {
|
||||
var servers = this.hostSettings();
|
||||
|
||||
// check array index out of bound
|
||||
if (index < 0 || index >= servers.length) {
|
||||
throw new Error("Invalid index " + index + " when selecting the host settings. Must be less than " + servers.length);
|
||||
}
|
||||
|
||||
var server = servers[index];
|
||||
var url = server['url'];
|
||||
|
||||
// go through variable and assign a value
|
||||
for (var variable_name in server['variables']) {
|
||||
if (variable_name in variables) {
|
||||
if (server['variables'][variable_name]['enum_values'].includes(variables[variable_name])) {
|
||||
url = url.replace("{" + variable_name + "}", variables[variable_name]);
|
||||
} else {
|
||||
throw new Error("The variable `" + variable_name + "` in the host URL has invalid value " + variables[variable_name] + ". Must be " + server['variables'][variable_name]['enum_values'] + ".");
|
||||
}
|
||||
} else {
|
||||
// use default value
|
||||
url = url.replace("{" + variable_name + "}", server['variables'][variable_name]['default_value'])
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
{{#emitJSDoc}}/**
|
||||
* Constructs a new map or array model from REST data.
|
||||
* @param data {Object|Array} The REST data.
|
||||
|
||||
Reference in New Issue
Block a user