cimpress-fulfiller-identity
Version:
Thin client library for Cimpress' Fulfiller Identity service
63 lines (51 loc) • 2.89 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var AWSXRayMock = require("./aws_xray_mock");
var XRayProxy = function () {
function XRayProxy(authenticator, xray) {
_classCallCheck(this, XRayProxy);
this.authenticator = authenticator ? authenticator : { getAuthorization: function getAuthorization() {
return Promise.resolve();
} };
this.xray = xray ? xray : AWSXRayMock;
}
/**
* This method captures the promise pased by parameter within xRay, the promise will receive
* by parameter the result of the authenticator passed in the constructor as first argument, a subsegment
* for adding further annotations and all the rest of arguments passed to this function
*
* @param {*} segmentName String, // X-Ray segment name
* @param {*} promise Wrapped function
* @param {*} annotations Array<Object> // Annotations to be included, {key, value}
*/
_createClass(XRayProxy, [{
key: "capturePromise",
value: function capturePromise(segmentName, promise, annotations) {
var _this = this,
_arguments = arguments;
return new Promise(function (resolve) {
_this.xray.captureAsyncFunc(segmentName, function (subsegment) {
resolve(_this.authenticator.getAuthorization().then(function (authorization) {
var args = Object.keys(_arguments).map(function (a) {
return _arguments[a];
});
annotations.forEach(function (annotation) {
return subsegment.addAnnotation(annotation.key, annotation.value);
});
return promise.apply(undefined, [authorization, subsegment].concat(_toConsumableArray(args.slice(3))));
}).then(function (result) {
subsegment.close();
return result;
}).catch(function (err) {
subsegment.close(err);
return Promise.reject(err);
}));
});
});
}
}]);
return XRayProxy;
}();
module.exports = XRayProxy;