|
| 1 | + |
| 2 | +//Create a sample system Property called x_my_scope.api.endpoints having below object as example. make sure your company instance includes those key such as dev,prod,test or modify it with your instance name |
| 3 | + |
| 4 | +// { |
| 5 | +// "dev": "https://dev-instance.example.com/api", |
| 6 | +// "test": "https://test-instance.example.com/api", |
| 7 | +// "prod": "https://prod-instance.example.com/api" |
| 8 | +// } |
| 9 | + |
| 10 | +var EndpointConfig = Class.create(); |
| 11 | +EndpointConfig.prototype = { |
| 12 | + initialize: function() { |
| 13 | + // No hardcoded object here. It will be fetched from the System Property. |
| 14 | + }, |
| 15 | + |
| 16 | + getEndpoint: function() { |
| 17 | + var propertyName = 'x_my_scope.api.endpoints'; |
| 18 | + var endpointObjectStr = gs.getProperty(propertyName); |
| 19 | + if (gs.nil(endpointObjectStr)) { |
| 20 | + gs.error("EndpointConfig: System property '" + propertyName + "' not found or is empty."); |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + var endpoints = JSON.parse(endpointObjectStr); |
| 26 | + var instanceName = gs.getProperty('instance_name'); |
| 27 | + var environmentKey; |
| 28 | + |
| 29 | + if (instanceName.includes('dev')) { |
| 30 | + environmentKey = 'dev'; |
| 31 | + } else if (instanceName.includes('test') || instanceName.includes('uat')) { |
| 32 | + environmentKey = 'test'; |
| 33 | + } else if (instanceName.includes('prod')) { |
| 34 | + environmentKey = 'prod'; |
| 35 | + } else { |
| 36 | + gs.error("EndpointConfig: Could not determine environment for instance '" + instanceName + "'."); |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + if (endpoints.hasOwnProperty(environmentKey)) { |
| 41 | + return endpoints[environmentKey]; |
| 42 | + } else { |
| 43 | + gs.error("EndpointConfig: Configuration not found for environment '" + environmentKey + "'."); |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + } catch (ex) { |
| 48 | + gs.error("EndpointConfig: Failed to parse JSON from system property '" + propertyName + "'. Exception: " + ex); |
| 49 | + return null; |
| 50 | + } |
| 51 | + }, |
| 52 | + |
| 53 | + type: 'EndpointConfig' |
| 54 | +}; |
0 commit comments