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:
William Cheng
2019-01-29 11:19:21 +08:00
committed by GitHub
parent 046db19a85
commit 83d34bd8d7
222 changed files with 24380 additions and 6 deletions

View File

@@ -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.

View File

@@ -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.