dce-reactkit 3.8.7 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -7,6 +7,7 @@ var freeSolidSvgIcons = require('@fortawesome/free-solid-svg-icons');
7
7
  var reactFontawesome = require('@fortawesome/react-fontawesome');
8
8
  var ReactDOM = require('react-dom');
9
9
  var freeRegularSvgIcons = require('@fortawesome/free-regular-svg-icons');
10
+ var qs = require('qs');
10
11
 
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
13
 
@@ -31,6 +32,7 @@ function _interopNamespace(e) {
31
32
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
32
33
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
33
34
  var ReactDOM__default = /*#__PURE__*/_interopDefaultLegacy(ReactDOM);
35
+ var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
34
36
 
35
37
  /******************************************************************************
36
38
  Copyright (c) Microsoft Corporation.
@@ -15969,6 +15971,180 @@ const shuffleArray = (arr) => {
15969
15971
  return newArr;
15970
15972
  };
15971
15973
 
15974
+ /**
15975
+ * Sends and retries an http request
15976
+ * @author Gabriel Abrams
15977
+ * @param opts object containing all arguments
15978
+ * @param opts.path path to send request to
15979
+ * @param [opts.host] host to send request to
15980
+ * @param [opts.method=GET] http method to use
15981
+ * @param [opts.params] body/data to include in the request
15982
+ * @param [opts.headers] headers to include in the request
15983
+ * @param [opts.sendCrossDomainCredentials=true if in development mode] if true,
15984
+ * send cross-domain credentials even if not in dev mode
15985
+ * @param [opts.responseType=JSON] expected response type
15986
+ * @returns { body, status, headers } on success
15987
+ */
15988
+ const sendServerToServerRequest = (opts) => __awaiter(void 0, void 0, void 0, function* () {
15989
+ var _a;
15990
+ // Process method
15991
+ const method = (opts.method || 'GET');
15992
+ // Encode objects within params
15993
+ let params;
15994
+ if (opts.params) {
15995
+ params = {};
15996
+ Object.entries(opts.params).forEach(([key, val]) => {
15997
+ if (typeof val === 'object' && !Array.isArray(val)) {
15998
+ params[key] = JSON.stringify(val);
15999
+ }
16000
+ else {
16001
+ params[key] = val;
16002
+ }
16003
+ });
16004
+ }
16005
+ // Stringify parameters
16006
+ const stringifiedParams = qs__default["default"].stringify(params || {}, {
16007
+ encodeValuesOnly: true,
16008
+ arrayFormat: 'brackets',
16009
+ });
16010
+ // Create url (include query if GET)
16011
+ const query = (method === 'GET' ? `?${stringifiedParams}` : '');
16012
+ let url;
16013
+ if (!opts.host) {
16014
+ // No host included at all. Just send to a path
16015
+ url = `${opts.path}${query}`;
16016
+ }
16017
+ else {
16018
+ url = `https://${opts.host}${opts.path}${query}`;
16019
+ }
16020
+ // Update headers
16021
+ const headers = opts.headers || {};
16022
+ let data = null;
16023
+ if (!headers['Content-Type']) {
16024
+ // Form encoded
16025
+ headers['Content-Type'] = 'application/x-www-form-urlencoded';
16026
+ // Add data if applicable
16027
+ data = (method !== 'GET' ? stringifiedParams : null);
16028
+ }
16029
+ else {
16030
+ // JSON encode
16031
+ data = params;
16032
+ }
16033
+ // Encode data
16034
+ let encodedData;
16035
+ if (data) {
16036
+ if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
16037
+ encodedData = new URLSearchParams(params);
16038
+ }
16039
+ else {
16040
+ encodedData = JSON.stringify(data);
16041
+ }
16042
+ }
16043
+ // Send request
16044
+ try {
16045
+ const response = yield fetch(url, {
16046
+ method,
16047
+ mode: 'cors',
16048
+ headers: headers !== null && headers !== void 0 ? headers : {},
16049
+ body: ((method !== 'GET' && encodedData)
16050
+ ? encodedData
16051
+ : undefined),
16052
+ redirect: 'follow',
16053
+ });
16054
+ // Get headers map
16055
+ const responseHeaders = {};
16056
+ response.headers.forEach((value, key) => {
16057
+ responseHeaders[key] = value;
16058
+ });
16059
+ // Process response based on responseType
16060
+ try {
16061
+ // Parse response
16062
+ let responseBody;
16063
+ if (opts.responseType
16064
+ && opts.responseType === 'Text') {
16065
+ // Response type is text
16066
+ responseBody = yield response.text();
16067
+ }
16068
+ else {
16069
+ // Response type is JSON
16070
+ responseBody = yield response.json();
16071
+ }
16072
+ // Return response
16073
+ return {
16074
+ body: responseBody,
16075
+ status: response.status,
16076
+ headers: responseHeaders,
16077
+ };
16078
+ }
16079
+ catch (err) {
16080
+ throw new ErrorWithCode(`Failed to parse response as ${opts.responseType}: ${err === null || err === void 0 ? void 0 : err.message}`, ReactKitErrorCode$1.ResponseParseError);
16081
+ }
16082
+ }
16083
+ catch (err) {
16084
+ // Self-signed certificate error:
16085
+ if ((_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.includes('self signed certificate')) {
16086
+ throw new ErrorWithCode('We refused to send a request because the receiver has self-signed certificates.', ReactKitErrorCode$1.SelfSigned);
16087
+ }
16088
+ // No tries left
16089
+ throw new ErrorWithCode(`We encountered an error when trying to send a network request. If this issue persists, contact an admin. Error: ${err === null || err === void 0 ? void 0 : err.message}`, ReactKitErrorCode$1.NotConnected);
16090
+ }
16091
+ });
16092
+
16093
+ /**
16094
+ * Send a server-to-server request from this sever to another server that uses
16095
+ * dce-reactkit [for server only]
16096
+ * @author Gabe Abrams
16097
+ * @param opts object containing all arguments
16098
+ * @param opts.path - the path of the other server's endpoint
16099
+ * @param [opts.method=GET] - the method of the endpoint
16100
+ * @param [opts.params] - query/body parameters to include
16101
+ * @param [opts.headers] - headers to include
16102
+ * @returns response from server
16103
+ */
16104
+ const visitEndpointOnAnotherServer = (opts) => __awaiter(void 0, void 0, void 0, function* () {
16105
+ var _a;
16106
+ // Remove properties with undefined values
16107
+ let params;
16108
+ if (opts.params) {
16109
+ params = Object.fromEntries(Object
16110
+ .entries(opts.params)
16111
+ .filter(([, value]) => {
16112
+ return value !== undefined;
16113
+ }));
16114
+ }
16115
+ // Automatically JSONify arrays and objects
16116
+ if (params) {
16117
+ params = Object.fromEntries(Object
16118
+ .entries(params)
16119
+ .map(([key, value]) => {
16120
+ if (Array.isArray(value) || typeof value === 'object') {
16121
+ return [key, JSON.stringify(value)];
16122
+ }
16123
+ return [key, value];
16124
+ }));
16125
+ }
16126
+ // Send the request
16127
+ const response = yield sendServerToServerRequest({
16128
+ path: opts.path,
16129
+ method: (_a = opts.method) !== null && _a !== void 0 ? _a : 'GET',
16130
+ params,
16131
+ });
16132
+ // Check for failure
16133
+ if (!response || !response.body) {
16134
+ throw new ErrorWithCode('We didn\'t get a response from the server. Please check your internet connection.', ReactKitErrorCode$1.NoResponse);
16135
+ }
16136
+ if (!response.body.success) {
16137
+ // Other errors
16138
+ throw new ErrorWithCode((response.body.message
16139
+ || 'An unknown error occurred. Please contact an admin.'), (response.body.code
16140
+ || ReactKitErrorCode$1.NoCode));
16141
+ }
16142
+ // Success! Extract the body
16143
+ const { body } = response.body;
16144
+ // Return
16145
+ return body;
16146
+ });
16147
+
15972
16148
  /**
15973
16149
  * Days of the week
15974
16150
  * @author Gabe Abrams
@@ -16082,6 +16258,7 @@ exports.useForceRender = useForceRender;
16082
16258
  exports.validateEmail = validateEmail;
16083
16259
  exports.validatePhoneNumber = validatePhoneNumber;
16084
16260
  exports.validateString = validateString;
16261
+ exports.visitEndpointOnAnotherServer = visitEndpointOnAnotherServer;
16085
16262
  exports.visitServerEndpoint = visitServerEndpoint;
16086
16263
  exports.waitMs = waitMs;
16087
16264
  //# sourceMappingURL=index.js.map