1 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
2 |
|
3 | 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); }
|
4 |
|
5 | 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); }
|
6 |
|
7 | 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; } }
|
8 |
|
9 | function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
10 |
|
11 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
12 |
|
13 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
14 |
|
15 | import 'isomorphic-unfetch';
|
16 | import UserAgent from './UserAgent';
|
17 |
|
18 | var CognitoError = 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 | }( _wrapNativeSuper(Error));
|
33 |
|
34 |
|
35 |
|
36 | var Client = function () {
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
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 |
|
55 |
|
56 |
|
57 |
|
58 |
|
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 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
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 |
|
107 |
|
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 |
|
119 | if (response.ok) return callback(null, data);
|
120 | responseJsonData = data;
|
121 |
|
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 |
|
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 | }
|
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 |
|
162 | export { Client as default }; |
\ | No newline at end of file |