1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = void 0;
|
5 |
|
6 | require("isomorphic-unfetch");
|
7 |
|
8 | var _UserAgent = _interopRequireDefault(require("./UserAgent"));
|
9 |
|
10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
11 |
|
12 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
13 |
|
14 | 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); }
|
15 |
|
16 | function _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); }
|
17 |
|
18 | function _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; } }
|
19 |
|
20 | function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
21 |
|
22 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
23 |
|
24 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
25 |
|
26 | var CognitoError = function (_Error) {
|
27 | _inheritsLoose(CognitoError, _Error);
|
28 |
|
29 | function CognitoError(message, code, name, statusCode) {
|
30 | var _this;
|
31 |
|
32 | _this = _Error.call(this, message) || this;
|
33 | _this.code = code;
|
34 | _this.name = name;
|
35 | _this.statusCode = statusCode;
|
36 | return _this;
|
37 | }
|
38 |
|
39 | return CognitoError;
|
40 | }( _wrapNativeSuper(Error));
|
41 |
|
42 |
|
43 |
|
44 | var Client = function () {
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function Client(region, endpoint, fetchOptions) {
|
52 | this.endpoint = endpoint || "https://cognito-idp." + region + ".amazonaws.com/";
|
53 |
|
54 | var _ref = fetchOptions || {},
|
55 | credentials = _ref.credentials;
|
56 |
|
57 | this.fetchOptions = credentials ? {
|
58 | credentials: credentials
|
59 | } : {};
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | var _proto = Client.prototype;
|
71 |
|
72 | _proto.promisifyRequest = function promisifyRequest(operation, params) {
|
73 | var _this2 = this;
|
74 |
|
75 | return new Promise(function (resolve, reject) {
|
76 | _this2.request(operation, params, function (err, data) {
|
77 | if (err) {
|
78 | reject(new CognitoError(err.message, err.code, err.name, err.statusCode));
|
79 | } else {
|
80 | resolve(data);
|
81 | }
|
82 | });
|
83 | });
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | ;
|
94 |
|
95 | _proto.request = function request(operation, params, callback) {
|
96 | var headers = {
|
97 | 'Content-Type': 'application/x-amz-json-1.1',
|
98 | 'X-Amz-Target': "AWSCognitoIdentityProviderService." + operation,
|
99 | 'X-Amz-User-Agent': _UserAgent["default"].prototype.userAgent
|
100 | };
|
101 | var options = Object.assign({}, this.fetchOptions, {
|
102 | headers: headers,
|
103 | method: 'POST',
|
104 | mode: 'cors',
|
105 | cache: 'no-cache',
|
106 | body: JSON.stringify(params)
|
107 | });
|
108 | var response;
|
109 | var responseJsonData;
|
110 | fetch(this.endpoint, options).then(function (resp) {
|
111 | response = resp;
|
112 | return resp;
|
113 | }, function (err) {
|
114 |
|
115 |
|
116 | if (err instanceof TypeError) {
|
117 | throw new Error('Network error');
|
118 | }
|
119 |
|
120 | throw err;
|
121 | }).then(function (resp) {
|
122 | return resp.json()["catch"](function () {
|
123 | return {};
|
124 | });
|
125 | }).then(function (data) {
|
126 |
|
127 | if (response.ok) return callback(null, data);
|
128 | responseJsonData = data;
|
129 |
|
130 |
|
131 | var code = (data.__type || data.code).split('#').pop();
|
132 | var error = {
|
133 | code: code,
|
134 | name: code,
|
135 | message: data.message || data.Message || null
|
136 | };
|
137 | return callback(error);
|
138 | })["catch"](function (err) {
|
139 |
|
140 | if (response && response.headers && response.headers.get('x-amzn-errortype')) {
|
141 | try {
|
142 | var code = response.headers.get('x-amzn-errortype').split(':')[0];
|
143 | var error = {
|
144 | code: code,
|
145 | name: code,
|
146 | statusCode: response.status,
|
147 | message: response.status ? response.status.toString() : null
|
148 | };
|
149 | return callback(error);
|
150 | } catch (ex) {
|
151 | return callback(err);
|
152 | }
|
153 |
|
154 | } else if (err instanceof Error && err.message === 'Network error') {
|
155 | var _error = {
|
156 | code: 'NetworkError',
|
157 | name: err.name,
|
158 | message: err.message
|
159 | };
|
160 | return callback(_error);
|
161 | } else {
|
162 | return callback(err);
|
163 | }
|
164 | });
|
165 | };
|
166 |
|
167 | return Client;
|
168 | }();
|
169 |
|
170 | exports["default"] = Client; |
\ | No newline at end of file |