UNPKG

6.54 kBJavaScriptView Raw
1function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
2
3function _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); }
4
5function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } 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); }
6
7function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
8
9function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
10
11function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
12
13function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
14
15import 'isomorphic-unfetch';
16import UserAgent from './UserAgent';
17
18var CognitoError = /*#__PURE__*/function (_Error) {
19 _inheritsLoose(CognitoError, _Error);
20
21 function CognitoError(message, code, name, statusCode) {
22 var _this;
23
24 _this = _Error.call(this, message) || this;
25 _this.code = code;
26 _this.name = name;
27 _this.statusCode = statusCode;
28 return _this;
29 }
30
31 return CognitoError;
32}( /*#__PURE__*/_wrapNativeSuper(Error));
33/** @class */
34
35
36var Client = /*#__PURE__*/function () {
37 /**
38 * Constructs a new AWS Cognito Identity Provider client object
39 * @param {string} region AWS region
40 * @param {string} endpoint endpoint
41 * @param {object} fetchOptions options for fetch API (only credentials is supported)
42 */
43 function Client(region, endpoint, fetchOptions) {
44 this.endpoint = endpoint || "https://cognito-idp." + region + ".amazonaws.com/";
45
46 var _ref = fetchOptions || {},
47 credentials = _ref.credentials;
48
49 this.fetchOptions = credentials ? {
50 credentials: credentials
51 } : {};
52 }
53 /**
54 * Makes an unauthenticated request on AWS Cognito Identity Provider API
55 * using fetch
56 * @param {string} operation API operation
57 * @param {object} params Input parameters
58 * @returns Promise<object>
59 */
60
61
62 var _proto = Client.prototype;
63
64 _proto.promisifyRequest = function promisifyRequest(operation, params) {
65 var _this2 = this;
66
67 return new Promise(function (resolve, reject) {
68 _this2.request(operation, params, function (err, data) {
69 if (err) {
70 reject(new CognitoError(err.message, err.code, err.name, err.statusCode));
71 } else {
72 resolve(data);
73 }
74 });
75 });
76 }
77 /**
78 * Makes an unauthenticated request on AWS Cognito Identity Provider API
79 * using fetch
80 * @param {string} operation API operation
81 * @param {object} params Input parameters
82 * @param {function} callback Callback called when a response is returned
83 * @returns {void}
84 */
85 ;
86
87 _proto.request = function request(operation, params, callback) {
88 var headers = {
89 'Content-Type': 'application/x-amz-json-1.1',
90 'X-Amz-Target': "AWSCognitoIdentityProviderService." + operation,
91 'X-Amz-User-Agent': UserAgent.prototype.userAgent
92 };
93 var options = Object.assign({}, this.fetchOptions, {
94 headers: headers,
95 method: 'POST',
96 mode: 'cors',
97 cache: 'no-cache',
98 body: JSON.stringify(params)
99 });
100 var response;
101 var responseJsonData;
102 fetch(this.endpoint, options).then(function (resp) {
103 response = resp;
104 return resp;
105 }, function (err) {
106 // If error happens here, the request failed
107 // if it is TypeError throw network error
108 if (err instanceof TypeError) {
109 throw new Error('Network error');
110 }
111
112 throw err;
113 }).then(function (resp) {
114 return resp.json()["catch"](function () {
115 return {};
116 });
117 }).then(function (data) {
118 // return parsed body stream
119 if (response.ok) return callback(null, data);
120 responseJsonData = data; // Taken from aws-sdk-js/lib/protocol/json.js
121 // eslint-disable-next-line no-underscore-dangle
122
123 var code = (data.__type || data.code).split('#').pop();
124 var error = {
125 code: code,
126 name: code,
127 message: data.message || data.Message || null
128 };
129 return callback(error);
130 })["catch"](function (err) {
131 // first check if we have a service error
132 if (response && response.headers && response.headers.get('x-amzn-errortype')) {
133 try {
134 var code = response.headers.get('x-amzn-errortype').split(':')[0];
135 var error = {
136 code: code,
137 name: code,
138 statusCode: response.status,
139 message: response.status ? response.status.toString() : null
140 };
141 return callback(error);
142 } catch (ex) {
143 return callback(err);
144 } // otherwise check if error is Network error
145
146 } else if (err instanceof Error && err.message === 'Network error') {
147 var _error = {
148 code: 'NetworkError',
149 name: err.name,
150 message: err.message
151 };
152 return callback(_error);
153 } else {
154 return callback(err);
155 }
156 });
157 };
158
159 return Client;
160}();
161
162export { Client as default };
\No newline at end of file