1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
14 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
15 | return new (P || (P = Promise))(function (resolve, reject) {
|
16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
19 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20 | });
|
21 | };
|
22 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
23 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
24 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
25 | function verb(n) { return function (v) { return step([n, v]); }; }
|
26 | function step(op) {
|
27 | if (f) throw new TypeError("Generator is already executing.");
|
28 | while (_) try {
|
29 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
30 | if (y = 0, t) op = [op[0] & 2, t.value];
|
31 | switch (op[0]) {
|
32 | case 0: case 1: t = op; break;
|
33 | case 4: _.label++; return { value: op[1], done: false };
|
34 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
35 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
36 | default:
|
37 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
38 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
39 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
40 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
41 | if (t[2]) _.ops.pop();
|
42 | _.trys.pop(); continue;
|
43 | }
|
44 | op = body.call(thisArg, _);
|
45 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
46 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
47 | }
|
48 | };
|
49 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
50 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
51 | };
|
52 | Object.defineProperty(exports, "__esModule", { value: true });
|
53 | exports.fetchDestination = exports.fetchSubaccountDestinations = exports.fetchInstanceDestinations = void 0;
|
54 | var util_1 = require("@sap-cloud-sdk/util");
|
55 | var opossum_1 = __importDefault(require("opossum"));
|
56 | var axios_1 = __importDefault(require("axios"));
|
57 | var jwt_1 = require("../jwt");
|
58 | var resilience_options_1 = require("../resilience-options");
|
59 | var http_agent_1 = require("../../../http-agent");
|
60 | var destination_1 = require("./destination");
|
61 | var destination_service_types_1 = require("./destination-service-types");
|
62 | var destination_service_cache_1 = require("./destination-service-cache");
|
63 | var logger = (0, util_1.createLogger)({
|
64 | package: 'core',
|
65 | messageContext: 'destination-service'
|
66 | });
|
67 | var circuitBreaker;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function fetchInstanceDestinations(destinationServiceUri, jwt, options) {
|
76 | return fetchDestinations(destinationServiceUri, jwt, destination_service_types_1.DestinationType.Instance, options);
|
77 | }
|
78 | exports.fetchInstanceDestinations = fetchInstanceDestinations;
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function fetchSubaccountDestinations(destinationServiceUri, jwt, options) {
|
87 | return fetchDestinations(destinationServiceUri, jwt, destination_service_types_1.DestinationType.Subaccount, options);
|
88 | }
|
89 | exports.fetchSubaccountDestinations = fetchSubaccountDestinations;
|
90 | function fetchDestinations(destinationServiceUri, jwt, type, options) {
|
91 | return __awaiter(this, void 0, void 0, function () {
|
92 | var targetUri, destinationsFromCache, headers;
|
93 | return __generator(this, function (_a) {
|
94 | targetUri = "".concat(destinationServiceUri.replace(/\/$/, ''), "/destination-configuration/v1/").concat(type, "Destinations");
|
95 | if (options === null || options === void 0 ? void 0 : options.useCache) {
|
96 | destinationsFromCache = destination_service_cache_1.destinationServiceCache.retrieveDestinationsFromCache(targetUri, (0, jwt_1.decodeJwt)(jwt), options.isolationStrategy);
|
97 | if (destinationsFromCache) {
|
98 | return [2 , destinationsFromCache];
|
99 | }
|
100 | }
|
101 | headers = (0, jwt_1.wrapJwtInHeader)(jwt).headers;
|
102 | return [2 , callDestinationService(targetUri, headers, options)
|
103 | .then(function (response) {
|
104 | var destinations = response.data.map(function (d) {
|
105 | return (0, destination_1.parseDestination)(d);
|
106 | });
|
107 | if (options === null || options === void 0 ? void 0 : options.useCache) {
|
108 | destination_service_cache_1.destinationServiceCache.cacheRetrievedDestinations(targetUri, (0, jwt_1.decodeJwt)(jwt), destinations, options.isolationStrategy);
|
109 | }
|
110 | return destinations;
|
111 | })
|
112 | .catch(function (error) {
|
113 | throw new util_1.ErrorWithCause("Failed to fetch ".concat(type, " destinations.").concat(errorMessageFromResponse(error)), error);
|
114 | })];
|
115 | });
|
116 | });
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function fetchDestination(destinationServiceUri, token, destinationName, options) {
|
131 | return __awaiter(this, void 0, void 0, function () {
|
132 | return __generator(this, function (_a) {
|
133 | return [2 , fetchDestinationByTokens(destinationServiceUri, typeof token === 'string' ? { authHeaderJwt: token } : token, destinationName, options)];
|
134 | });
|
135 | });
|
136 | }
|
137 | exports.fetchDestination = fetchDestination;
|
138 | function fetchDestinationByTokens(destinationServiceUri, tokens, destinationName, options) {
|
139 | return __awaiter(this, void 0, void 0, function () {
|
140 | var targetUri, destinationsFromCache, authHeader;
|
141 | return __generator(this, function (_a) {
|
142 | targetUri = "".concat(destinationServiceUri.replace(/\/$/, ''), "/destination-configuration/v1/destinations/").concat(destinationName);
|
143 | if (options === null || options === void 0 ? void 0 : options.useCache) {
|
144 | destinationsFromCache = destination_service_cache_1.destinationServiceCache.retrieveDestinationsFromCache(targetUri, (0, jwt_1.decodeJwt)(tokens.authHeaderJwt), options.isolationStrategy);
|
145 | if (destinationsFromCache) {
|
146 | if (destinationsFromCache.length > 1) {
|
147 | logger.warn('More than one destination found in the cache. This should not happen. First element used.');
|
148 | }
|
149 | return [2 , destinationsFromCache[0]];
|
150 | }
|
151 | }
|
152 | authHeader = (0, jwt_1.wrapJwtInHeader)(tokens.authHeaderJwt).headers;
|
153 | authHeader = tokens.exchangeHeaderJwt
|
154 | ? __assign(__assign({}, authHeader), { 'X-user-token': tokens.exchangeHeaderJwt }) : authHeader;
|
155 | authHeader = tokens.exchangeTenant
|
156 | ? __assign(__assign({}, authHeader), { 'X-tenant': tokens.exchangeTenant }) : authHeader;
|
157 | return [2 , callDestinationService(targetUri, authHeader, options)
|
158 | .then(function (response) {
|
159 | var destination = (0, destination_1.parseDestination)(response.data);
|
160 | if (options === null || options === void 0 ? void 0 : options.useCache) {
|
161 | destination_service_cache_1.destinationServiceCache.cacheRetrievedDestinations(targetUri, (0, jwt_1.decodeJwt)(tokens.authHeaderJwt), [destination], options.isolationStrategy);
|
162 | }
|
163 | return destination;
|
164 | })
|
165 | .catch(function (error) {
|
166 | {
|
167 | throw new util_1.ErrorWithCause("Failed to fetch destination ".concat(destinationName, ".").concat(errorMessageFromResponse(error)), error);
|
168 | }
|
169 | })];
|
170 | });
|
171 | });
|
172 | }
|
173 | function errorMessageFromResponse(error) {
|
174 | return (0, util_1.propertyExists)(error, 'response', 'data', 'ErrorMessage')
|
175 | ? " ".concat(error.response.data.ErrorMessage)
|
176 | : '';
|
177 | }
|
178 | function callDestinationService(uri, headers, options) {
|
179 | if (options === void 0) { options = { enableCircuitBreaker: true }; }
|
180 | var config = __assign(__assign({}, (0, http_agent_1.urlAndAgent)(uri)), { proxy: false, method: 'get', headers: headers });
|
181 | if (options.enableCircuitBreaker) {
|
182 | return getCircuitBreaker().fire(config);
|
183 | }
|
184 | return axios_1.default.request(config);
|
185 | }
|
186 | function getCircuitBreaker() {
|
187 | var request = axios_1.default.request;
|
188 | if (!circuitBreaker) {
|
189 | circuitBreaker = new opossum_1.default(request, resilience_options_1.circuitBreakerDefaultOptions);
|
190 | }
|
191 | return circuitBreaker;
|
192 | }
|
193 |
|
\ | No newline at end of file |