UNPKG

6.75 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6require("isomorphic-unfetch");
7
8var _UserAgent = _interopRequireDefault(require("./UserAgent"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
12function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
13
14function _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
16function _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
18function _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
20function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
21
22function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
23
24function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
25
26var CognitoError = /*#__PURE__*/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}( /*#__PURE__*/_wrapNativeSuper(Error));
41/** @class */
42
43
44var Client = /*#__PURE__*/function () {
45 /**
46 * Constructs a new AWS Cognito Identity Provider client object
47 * @param {string} region AWS region
48 * @param {string} endpoint endpoint
49 * @param {object} fetchOptions options for fetch API (only credentials is supported)
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 * Makes an unauthenticated request on AWS Cognito Identity Provider API
63 * using fetch
64 * @param {string} operation API operation
65 * @param {object} params Input parameters
66 * @returns Promise<object>
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 * Makes an unauthenticated request on AWS Cognito Identity Provider API
87 * using fetch
88 * @param {string} operation API operation
89 * @param {object} params Input parameters
90 * @param {function} callback Callback called when a response is returned
91 * @returns {void}
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 // If error happens here, the request failed
115 // if it is TypeError throw network error
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 // return parsed body stream
127 if (response.ok) return callback(null, data);
128 responseJsonData = data; // Taken from aws-sdk-js/lib/protocol/json.js
129 // eslint-disable-next-line no-underscore-dangle
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 // first check if we have a service error
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 } // otherwise check if error is Network error
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
170exports["default"] = Client;
\No newline at end of file