MCPcopy Create free account
hub / github.com/APIs-guru/aws2openapi / buildServers

Function buildServers

index.js:81–176  ·  view source on GitHub ↗
(endpointPrefix, serviceName, awsRegionConfig)

Source from the content-addressed store, hash-verified

79// Build a OpenAPI v3-compatible 'servers' object for this endpoint, for all regions.
80// For now this will be attached as x-servers, for v2 compatibility (see swaggerplusplus)
81function buildServers(endpointPrefix, serviceName, awsRegionConfig) {
82 // This uses the same logic for config lookup as aws-sdk/lib/region_config.js
83
84 // Build a map of URL -> regions covered by that URL
85 const regionsByEndpoint = awsRegions.list().reduce((regionsByEndpoint, region) => {
86 // From the AWS SDK. Defines a list of keys for the config of this service, from
87 // most to least specific. We'll use the most specific that exists.
88 const regionPrefix = generateRegionPrefix(region.code);
89 const endpointKeys = [
90 [region.code, endpointPrefix],
91 [regionPrefix, endpointPrefix],
92 [region.code, '*'],
93 [regionPrefix, '*'],
94 ['*', endpointPrefix],
95 ['*', '*']
96 ].map((item) => item[0] && item[1] ? item.join('/') : null);
97
98 const endpointConfigKey = _.find(endpointKeys, (k) => awsRegionConfig.rules[k]);
99
100 // Config is either a config, or a key for a config in the 'patterns' object
101 const endpointConfig = typeof awsRegionConfig.rules[endpointConfigKey] === 'string'
102 ? awsRegionConfig.patterns[awsRegionConfig.rules[endpointConfigKey]]
103 : awsRegionConfig.rules[endpointConfigKey];
104
105 let endpoints = [];
106
107 // If no protocol is specified, both HTTP & HTTPS are allowed
108 if (endpointConfig.endpoint.match(/^https?:\/\//)) {
109 endpoints.push(endpointConfig.endpoint);
110 } else {
111 endpoints.push('http://' + endpointConfig.endpoint);
112 endpoints.push('https://' + endpointConfig.endpoint);
113 }
114
115 // If we have both region & general endpoints, add the general endpoint(s) too
116 if (endpointConfig.generalEndpoint) {
117 if (endpointConfig.generalEndpoint.match(/^https?:\/\//)) {
118 endpoints.push(endpointConfig.generalEndpoint);
119 } else {
120 endpoints.push('http://' + endpointConfig.generalEndpoint);
121 endpoints.push('https://' + endpointConfig.generalEndpoint);
122 }
123 }
124
125 endpoints.forEach((endpoint) => {
126 if (!regionsByEndpoint[endpoint]) {
127 regionsByEndpoint[endpoint] = [region];
128 } else {
129 regionsByEndpoint[endpoint].push(region);
130 }
131 });
132
133 return regionsByEndpoint;
134 }, {});
135
136 // Turn the endpoint URLs + regions list into nice url + description + vars objects
137 return Object.keys(regionsByEndpoint).map((endpoint) => {
138 const validRegions = regionsByEndpoint[endpoint];

Callers 1

index.jsFile · 0.85

Calls 1

generateRegionPrefixFunction · 0.85

Tested by

no test coverage detected