amazon-cognito-identity-js
Version:
Amazon Cognito Identity Provider JavaScript SDK
215 lines (211 loc) • 9.18 kB
JavaScript
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
import 'isomorphic-unfetch';
import { getAmplifyUserAgent } from './UserAgent';
var CognitoError = /*#__PURE__*/function (_Error) {
_inheritsLoose(CognitoError, _Error);
function CognitoError(message, code, name, statusCode) {
var _this;
_this = _Error.call(this, message) || this;
_this.code = code;
_this.name = name;
_this.statusCode = statusCode;
return _this;
}
return CognitoError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
/** @class */
var Client = /*#__PURE__*/function () {
/**
* Constructs a new AWS Cognito Identity Provider client object
* @param {string} region AWS region
* @param {string} endpoint endpoint
* @param {object} fetchOptions options for fetch API (only credentials is supported)
*/
function Client(region, endpoint, fetchOptions) {
this.endpoint = endpoint || "https://cognito-idp." + region + ".amazonaws.com/";
var _ref = fetchOptions || {},
credentials = _ref.credentials;
this.fetchOptions = credentials ? {
credentials: credentials
} : {};
}
/**
* Makes an unauthenticated request on AWS Cognito Identity Provider API
* using fetch
* @param {string} operation API operation
* @param {object} params Input parameters
* @returns Promise<object>
*/
var _proto = Client.prototype;
_proto.promisifyRequest = function promisifyRequest(operation, params) {
var _this2 = this;
return new Promise(function (resolve, reject) {
_this2.request(operation, params, function (err, data) {
if (err) {
reject(new CognitoError(err.message, err.code, err.name, err.statusCode));
} else {
resolve(data);
}
});
});
};
_proto.requestWithRetry = function requestWithRetry(operation, params, callback) {
var _this3 = this;
var MAX_DELAY_IN_MILLIS = 5 * 1000;
jitteredExponentialRetry(function (p) {
return new Promise(function (res, rej) {
_this3.request(operation, p, function (error, result) {
if (error) {
rej(error);
} else {
res(result);
}
});
});
}, [params], MAX_DELAY_IN_MILLIS).then(function (result) {
return callback(null, result);
})["catch"](function (error) {
return callback(error);
});
}
/**
* Makes an unauthenticated request on AWS Cognito Identity Provider API
* using fetch
* @param {string} operation API operation
* @param {object} params Input parameters
* @param {function} callback Callback called when a response is returned
* @returns {void}
*/;
_proto.request = function request(operation, params, callback) {
var headers = {
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': "AWSCognitoIdentityProviderService." + operation,
'X-Amz-User-Agent': getAmplifyUserAgent(),
'Cache-Control': 'no-store'
};
var options = Object.assign({}, this.fetchOptions, {
headers: headers,
method: 'POST',
mode: 'cors',
body: JSON.stringify(params)
});
var response;
var responseJsonData;
fetch(this.endpoint, options).then(function (resp) {
response = resp;
return resp;
}, function (err) {
// If error happens here, the request failed
// if it is TypeError throw network error
if (err instanceof TypeError) {
throw new Error('Network error');
}
throw err;
}).then(function (resp) {
return resp.json()["catch"](function () {
return {};
});
}).then(function (data) {
// return parsed body stream
if (response.ok) return callback(null, data);
responseJsonData = data;
// Taken from aws-sdk-js/lib/protocol/json.js
// eslint-disable-next-line no-underscore-dangle
var code = (data.__type || data.code).split('#').pop();
var error = new Error(data.message || data.Message || null);
error.name = code;
error.code = code;
return callback(error);
})["catch"](function (err) {
// first check if we have a service error
if (response && response.headers && response.headers.get('x-amzn-errortype')) {
try {
var code = response.headers.get('x-amzn-errortype').split(':')[0];
var error = new Error(response.status ? response.status.toString() : null);
error.code = code;
error.name = code;
error.statusCode = response.status;
return callback(error);
} catch (ex) {
return callback(err);
}
// otherwise check if error is Network error
} else if (err instanceof Error && err.message === 'Network error') {
err.code = 'NetworkError';
}
return callback(err);
});
};
return Client;
}();
export { Client as default };
var logger = {
debug: function debug() {
// Intentionally blank. This package doesn't have logging
}
};
/**
* For now, all errors are retryable.
*/
var NonRetryableError = /*#__PURE__*/function (_Error2) {
_inheritsLoose(NonRetryableError, _Error2);
function NonRetryableError(message) {
var _this4;
_this4 = _Error2.call(this, message) || this;
_this4.nonRetryable = true;
return _this4;
}
return NonRetryableError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
var isNonRetryableError = function isNonRetryableError(obj) {
var key = 'nonRetryable';
return obj && obj[key];
};
function retry(functionToRetry, args, delayFn, attempt) {
if (attempt === void 0) {
attempt = 1;
}
if (typeof functionToRetry !== 'function') {
throw Error('functionToRetry must be a function');
}
logger.debug(functionToRetry.name + " attempt #" + attempt + " with args: " + JSON.stringify(args));
return functionToRetry.apply(void 0, args)["catch"](function (err) {
logger.debug("error on " + functionToRetry.name, err);
if (isNonRetryableError(err)) {
logger.debug(functionToRetry.name + " non retryable error", err);
throw err;
}
var retryIn = delayFn(attempt, args, err);
logger.debug(functionToRetry.name + " retrying in " + retryIn + " ms");
if (retryIn !== false) {
return new Promise(function (res) {
return setTimeout(res, retryIn);
}).then(function () {
return retry(functionToRetry, args, delayFn, attempt + 1);
});
} else {
throw err;
}
});
}
function jitteredBackoff(maxDelayMs) {
var BASE_TIME_MS = 100;
var JITTER_FACTOR = 100;
return function (attempt) {
var delay = Math.pow(2, attempt) * BASE_TIME_MS + JITTER_FACTOR * Math.random();
return delay > maxDelayMs ? false : delay;
};
}
var MAX_DELAY_MS = 5 * 60 * 1000;
function jitteredExponentialRetry(functionToRetry, args, maxDelayMs) {
if (maxDelayMs === void 0) {
maxDelayMs = MAX_DELAY_MS;
}
return retry(functionToRetry, args, jitteredBackoff(maxDelayMs));
}