@bigbinary/neeto-commons-frontend 2.0.1 → 2.0.2

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/initializers.js CHANGED
@@ -1,17 +1,19 @@
1
- 'use strict';
1
+ import { curry, isNil, values, evolve, omit, modify, either, isEmpty, mergeDeepLeft } from 'ramda';
2
+ import { Toastr } from '@bigbinary/neetoui';
3
+ import axios from 'axios';
4
+ import i18next from 'i18next';
5
+ import mixpanel from 'mixpanel-browser';
6
+ import { initReactI18next } from 'react-i18next';
2
7
 
3
- var ramda = require('ramda');
4
- var neetoui = require('@bigbinary/neetoui');
5
- var axios = require('axios');
6
- var i18next = require('i18next');
7
- var mixpanel = require('mixpanel-browser');
8
- var reactI18next = require('react-i18next');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+ function _typeof(obj) {
9
+ "@babel/helpers - typeof";
11
10
 
12
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
13
- var i18next__default = /*#__PURE__*/_interopDefaultLegacy(i18next);
14
- var mixpanel__default = /*#__PURE__*/_interopDefaultLegacy(mixpanel);
11
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
12
+ return typeof obj;
13
+ } : function (obj) {
14
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
15
+ }, _typeof(obj);
16
+ }
15
17
 
16
18
  function _arrayWithHoles(arr) {
17
19
  if (Array.isArray(arr)) return arr;
@@ -74,16 +76,6 @@ function _slicedToArray(arr, i) {
74
76
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
75
77
  }
76
78
 
77
- function _typeof(obj) {
78
- "@babel/helpers - typeof";
79
-
80
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
81
- return typeof obj;
82
- } : function (obj) {
83
- return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
84
- }, _typeof(obj);
85
- }
86
-
87
79
  var snakeToCamelCase = function snakeToCamelCase(string) {
88
80
  return string.replace(/(_\w)/g, function (letter) {
89
81
  return letter[1].toUpperCase();
@@ -127,12 +119,12 @@ var deepFreezeObject = function deepFreezeObject(object) {
127
119
 
128
120
  return object;
129
121
  };
130
- var matches = ramda.curry(function (pattern, object) {
122
+ var matches = curry(function (pattern, object) {
131
123
  var __parent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : object;
132
124
 
133
125
  if (object === pattern) return true;
134
126
  if (typeof pattern === "function" && pattern(object, __parent)) return true;
135
- if (ramda.isNil(pattern) || ramda.isNil(object)) return false;
127
+ if (isNil(pattern) || isNil(object)) return false;
136
128
  if (_typeof(pattern) !== "object") return false;
137
129
  return Object.entries(pattern).every(function (_ref3) {
138
130
  var _ref4 = _slicedToArray(_ref3, 2),
@@ -143,9 +135,12 @@ var matches = ramda.curry(function (pattern, object) {
143
135
  });
144
136
  });
145
137
 
146
- var _document$getElements, _document$getElements2;
147
- window.globalProps = keysToCamelCase(JSON.parse(((_document$getElements = document.getElementsByClassName("root-container")[0]) === null || _document$getElements === void 0 ? void 0 : (_document$getElements2 = _document$getElements.dataset) === null || _document$getElements2 === void 0 ? void 0 : _document$getElements2.reactProps) || "{}"));
148
- deepFreezeObject(window.globalProps);
138
+ function initializeGlobalProps() {
139
+ var _document$getElements, _document$getElements2;
140
+
141
+ window.globalProps = keysToCamelCase(JSON.parse(((_document$getElements = document.getElementsByClassName("root-container")[0]) === null || _document$getElements === void 0 ? void 0 : (_document$getElements2 = _document$getElements.dataset) === null || _document$getElements2 === void 0 ? void 0 : _document$getElements2.reactProps) || "{}"));
142
+ deepFreezeObject(window.globalProps);
143
+ }
149
144
 
150
145
  function _defineProperty(obj, key, value) {
151
146
  if (key in obj) {
@@ -169,21 +164,19 @@ var HEADERS_KEYS = {
169
164
  };
170
165
 
171
166
  var resetAuthTokens = function resetAuthTokens() {
172
- ramda.values(HEADERS_KEYS).forEach(function (header) {
173
- delete axios__default["default"].defaults.headers[header];
167
+ values(HEADERS_KEYS).forEach(function (header) {
168
+ delete axios.defaults.headers[header];
174
169
  });
175
170
  };
176
171
 
177
- var setAxiosDefaults = function setAxiosDefaults() {
178
- axios__default["default"].defaults.baseURL = "/";
179
- setAuthHeaders();
180
- registerIntercepts();
172
+ var shouldNot = function shouldNot(skip) {
173
+ return _typeof(skip) === "object" || !skip;
181
174
  };
182
175
 
183
176
  var setAuthHeaders = function setAuthHeaders() {
184
177
  var _globalProps$user, _globalProps$user2;
185
178
 
186
- axios__default["default"].defaults.headers = _defineProperty({
179
+ axios.defaults.headers = _defineProperty({
187
180
  Accept: "application/json",
188
181
  "Content-Type": "application/json"
189
182
  }, HEADERS_KEYS.xCsrfToken, document.querySelector('[name="csrf-token"]').getAttribute("content"));
@@ -191,71 +184,102 @@ var setAuthHeaders = function setAuthHeaders() {
191
184
  var email = (_globalProps$user2 = globalProps.user) === null || _globalProps$user2 === void 0 ? void 0 : _globalProps$user2.email;
192
185
 
193
186
  if (token && email) {
194
- axios__default["default"].defaults.headers[HEADERS_KEYS.xAuthEmail] = email;
195
- axios__default["default"].defaults.headers[HEADERS_KEYS.xAuthToken] = token;
187
+ axios.defaults.headers[HEADERS_KEYS.xAuthEmail] = email;
188
+ axios.defaults.headers[HEADERS_KEYS.xAuthToken] = token;
196
189
  }
190
+ }; // pipe function from ramda doesn't accept array of functions.
191
+ // We can't use spread operator too. So this is a workaround.
192
+
193
+
194
+ var createPipe = function createPipe(functions) {
195
+ return function (data) {
196
+ return functions.reduce(function (acc, fn) {
197
+ return fn(acc);
198
+ }, data);
199
+ };
197
200
  };
198
201
 
199
- var handleSuccessResponse = function handleSuccessResponse(response) {
200
- var _response$config = response.config,
201
- _response$config$tran = _response$config.transformResponseCase,
202
- transformResponseCase = _response$config$tran === void 0 ? true : _response$config$tran,
203
- _response$config$incl = _response$config.includeMetadataInResponse,
204
- includeMetadataInResponse = _response$config$incl === void 0 ? false : _response$config$incl,
205
- _response$config$show = _response$config.showToastr,
206
- showToastr = _response$config$show === void 0 ? true : _response$config$show;
202
+ var transformKeysToCamelCase = function transformKeysToCamelCase(response) {
203
+ var _response$config$tran = response.config.transformResponseCase,
204
+ transformResponseCase = _response$config$tran === void 0 ? true : _response$config$tran;
207
205
 
208
- if (response.data) {
209
- if (transformResponseCase) response.data = keysToCamelCase(response.data);
206
+ if (response.data && transformResponseCase) {
207
+ response.data = keysToCamelCase(response.data);
208
+ }
210
209
 
211
- if (showToastr && typeof response.data.notice === "string") {
212
- neetoui.Toastr.success(response.data.notice);
213
- }
210
+ return response;
211
+ };
212
+
213
+ var showSuccessToastr = function showSuccessToastr(response) {
214
+ var _response$config$show = response.config.showToastr,
215
+ showToastr = _response$config$show === void 0 ? true : _response$config$show;
216
+
217
+ if (showToastr && typeof response.data.notice === "string") {
218
+ Toastr.success(response.data.notice);
214
219
  }
215
220
 
221
+ return response;
222
+ };
223
+
224
+ var pullDataFromResponse = function pullDataFromResponse(response) {
225
+ var _response$config$incl = response.config.includeMetadataInResponse,
226
+ includeMetadataInResponse = _response$config$incl === void 0 ? false : _response$config$incl;
216
227
  return includeMetadataInResponse ? response : response.data;
217
228
  };
218
229
 
219
- var handleUnauthorizedErrorResponse = function handleUnauthorizedErrorResponse() {
230
+ var buildSuccessResponseHandler = function buildSuccessResponseHandler(skip) {
231
+ var interceptors = [];
232
+ if (!(skip !== null && skip !== void 0 && skip.transformCase)) interceptors.push(transformKeysToCamelCase);
233
+ if (!(skip !== null && skip !== void 0 && skip.showToastr)) interceptors.push(showSuccessToastr);
234
+ if (!(skip !== null && skip !== void 0 && skip.pullDataFromResponse)) interceptors.push(pullDataFromResponse);
235
+ return createPipe(interceptors);
236
+ };
237
+
238
+ var handleUnauthorizedErrorResponse = function handleUnauthorizedErrorResponse(error) {
220
239
  resetAuthTokens();
221
240
  setTimeout(function () {
222
241
  var redirectTo = window.location.pathname === "/login" ? "/login" : "/login?redirect_uri=".concat(encodeURIComponent(window.location.href));
223
242
  window.location.href = redirectTo;
224
243
  }, 300);
244
+ return error;
225
245
  };
226
246
 
227
247
  var showErrorToastr = function showErrorToastr(error) {
228
- if (axios__default["default"].isCancel(error)) {
229
- neetoui.Toastr.error(i18next__default["default"].t("neetoCommons.toastr.error.requestCanceled"));
248
+ if (axios.isCancel(error)) {
249
+ Toastr.error(i18next.t("neetoCommons.toastr.error.requestCanceled"));
230
250
  } else if (error.message === "Network Error") {
231
- neetoui.Toastr.error(i18next__default["default"].t("neetoCommons.toastr.error.networkError"));
251
+ Toastr.error(i18next.t("neetoCommons.toastr.error.networkError"));
232
252
  } else {
233
- neetoui.Toastr.error(error);
253
+ Toastr.error(error);
234
254
  }
235
- };
236
255
 
237
- var handleErrorResponse = function handleErrorResponse(error) {
238
- var _error$response, _error$response2;
256
+ return error;
257
+ };
239
258
 
240
- var _error$config$showToa = error.config.showToastr,
241
- showToastr = _error$config$showToa === void 0 ? true : _error$config$showToa;
259
+ var redirect404 = function redirect404(error) {
260
+ var _error$response;
242
261
 
243
262
  if (((_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.status) === 404) {
244
263
  window.location.href = "/page-not-found";
245
- } else if (((_error$response2 = error.response) === null || _error$response2 === void 0 ? void 0 : _error$response2.status) === 401) {
246
- handleUnauthorizedErrorResponse();
247
- } else if (showToastr) {
248
- showErrorToastr(error);
249
264
  }
250
265
 
251
- return Promise.reject(error);
266
+ return error;
267
+ };
268
+
269
+ var buildErrorResponseHandler = function buildErrorResponseHandler(skip) {
270
+ var interceptors = [];
271
+ if (!(skip !== null && skip !== void 0 && skip.redirectOn404)) interceptors.push(redirect404);
272
+ if (!(skip !== null && skip !== void 0 && skip.logoutOn401)) interceptors.push(handleUnauthorizedErrorResponse);
273
+ if (!(skip !== null && skip !== void 0 && skip.showToastr)) interceptors.push(showErrorToastr);
274
+ interceptors.push(Promise.reject);
275
+ return createPipe(interceptors);
252
276
  };
253
277
 
254
278
  var cleanupCredentialsForCrossOrigin = function cleanupCredentialsForCrossOrigin(request) {
255
279
  if (!request.url.includes("://")) return request;
256
280
  if (request.url.includes(window.location.hostname)) return request;
257
- return ramda.evolve({
258
- headers: ramda.omit(ramda.values(HEADERS_KEYS))
281
+ return evolve({
282
+ headers: omit(values(HEADERS_KEYS))
259
283
  })(request);
260
284
  };
261
285
 
@@ -280,34 +304,54 @@ var transformDataToSnakeCase = function transformDataToSnakeCase(request) {
280
304
  var _request$transformReq = request.transformRequestCase,
281
305
  transformRequestCase = _request$transformReq === void 0 ? true : _request$transformReq;
282
306
  if (!transformRequestCase || !request.data) return request;
283
- return ramda.modify("data", serializeKeysToSnakeCase, request);
307
+ return modify("data", serializeKeysToSnakeCase, request);
284
308
  };
285
309
 
286
- var registerIntercepts = function registerIntercepts() {
287
- axios__default["default"].interceptors.request.use(ramda.pipe(cleanupCredentialsForCrossOrigin, transformDataToSnakeCase));
288
- axios__default["default"].interceptors.response.use(handleSuccessResponse, handleErrorResponse);
310
+ var addRequestInterceptors = function addRequestInterceptors(skip) {
311
+ if (!(skip !== null && skip !== void 0 && skip.cleanCredentialsForCrossOrigin)) {
312
+ axios.interceptors.request.use(cleanupCredentialsForCrossOrigin);
313
+ }
314
+
315
+ if (!(skip !== null && skip !== void 0 && skip.transformCase)) {
316
+ axios.interceptors.request.use(transformDataToSnakeCase);
317
+ }
289
318
  };
290
319
 
291
- setAxiosDefaults();
320
+ var addResponseInterceptors = function addResponseInterceptors(skip) {
321
+ axios.interceptors.response.use(buildSuccessResponseHandler(skip), buildErrorResponseHandler(skip));
322
+ };
292
323
 
293
- var isProduction = process.env.NODE_ENV === "production";
294
- var isTokenPresent = !!process.env.MIXPANEL_TOKEN;
295
- var isUserLoggedIn = !ramda.either(ramda.isEmpty, ramda.isNil)(globalProps.user);
324
+ var registerIntercepts = function registerIntercepts(skip) {
325
+ if (shouldNot(skip === null || skip === void 0 ? void 0 : skip.request)) addRequestInterceptors(skip === null || skip === void 0 ? void 0 : skip.request);
326
+ if (shouldNot(skip === null || skip === void 0 ? void 0 : skip.response)) addResponseInterceptors(skip === null || skip === void 0 ? void 0 : skip.response);
327
+ };
296
328
 
297
- if (isProduction && isTokenPresent && isUserLoggedIn) {
298
- mixpanel__default["default"].init(process.env.MIXPANEL_TOKEN);
299
- mixpanel__default["default"].people.set({
300
- $email: globalProps.user.email,
301
- $fist_name: globalProps.user.firstName,
302
- $last_name: globalProps.user.lastName
303
- });
304
- mixpanel__default["default"].identify(globalProps.user.email);
305
- } else {
306
- /*
307
- We need to initialize mixpanel with a bogus token in development and test environment to
308
- prevent mixpanel library from throwing an error when we use mixpanel.track() method in react components.
309
- */
310
- mixpanel__default["default"].init("TEST_TOKEN");
329
+ function initializeAxios(skip) {
330
+ if (!(skip !== null && skip !== void 0 && skip.baseURL)) axios.defaults.baseURL = "/";
331
+ if (!(skip !== null && skip !== void 0 && skip.authHeaders)) setAuthHeaders();
332
+ if (shouldNot(skip === null || skip === void 0 ? void 0 : skip.interceptors)) registerIntercepts(skip === null || skip === void 0 ? void 0 : skip.interceptors);
333
+ }
334
+
335
+ function initializeMixPanel() {
336
+ var isProduction = process.env.NODE_ENV === "production";
337
+ var isTokenPresent = !!process.env.MIXPANEL_TOKEN;
338
+ var isUserLoggedIn = !either(isEmpty, isNil)(globalProps.user);
339
+
340
+ if (isProduction && isTokenPresent && isUserLoggedIn) {
341
+ mixpanel.init(process.env.MIXPANEL_TOKEN || "");
342
+ mixpanel.people.set({
343
+ $email: globalProps.user.email,
344
+ $fist_name: globalProps.user.firstName,
345
+ $last_name: globalProps.user.lastName
346
+ });
347
+ mixpanel.identify(globalProps.user.email);
348
+ } else {
349
+ /*
350
+ We need to initialize mixpanel with a bogus token in development and test environment to
351
+ prevent mixpanel library from throwing an error when we use mixpanel.track() method in react components.
352
+ */
353
+ mixpanel.init("TEST_TOKEN");
354
+ }
311
355
  }
312
356
 
313
357
  var neetoCommons = {
@@ -350,8 +394,8 @@ var en = {
350
394
  };
351
395
 
352
396
  var initializeI18n = function initializeI18n(resources) {
353
- i18next__default["default"].use(reactI18next.initReactI18next).init({
354
- resources: ramda.mergeDeepLeft({
397
+ i18next.use(initReactI18next).init({
398
+ resources: mergeDeepLeft({
355
399
  en: {
356
400
  translation: en
357
401
  }
@@ -365,10 +409,23 @@ var initializeI18n = function initializeI18n(resources) {
365
409
  });
366
410
  };
367
411
 
368
- /* eslint-disable import/order */
412
+ var globalProps$1 = {};
369
413
  function initializeApplication(_ref) {
370
- var translationResources = _ref.translationResources;
371
- initializeI18n(translationResources);
414
+ var translationResources = _ref.translationResources,
415
+ skip = _ref.skip;
416
+
417
+ if (!(skip !== null && skip !== void 0 && skip.globalProps)) {
418
+ initializeGlobalProps();
419
+ globalProps$1 = window.globalProps;
420
+ }
421
+
422
+ if (!(skip !== null && skip !== void 0 && skip.mixpanel)) initializeMixPanel();
423
+
424
+ if (_typeof(skip === null || skip === void 0 ? void 0 : skip.axios) === "object" || !(skip !== null && skip !== void 0 && skip.axios)) {
425
+ initializeAxios(skip === null || skip === void 0 ? void 0 : skip.axios);
426
+ }
427
+
428
+ if (!(skip !== null && skip !== void 0 && skip.i18n)) initializeI18n(translationResources);
372
429
  }
373
430
 
374
- module.exports = initializeApplication;
431
+ export { initializeApplication as default, globalProps$1 as globalProps };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/neeto-commons-frontend",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.",
5
5
  "repository": "git@github.com:bigbinary/neeto-commons-frontend.git",
6
6
  "author": "Amaljith K <amaljith.k@bigbinary.com>",
@@ -23,6 +23,24 @@
23
23
  ".scripts/fix-lints.sh"
24
24
  ]
25
25
  },
26
+ "exports": {
27
+ "./react-utils": {
28
+ "import": "./react-utils.js",
29
+ "require": "./react-utils.cjs.js"
30
+ },
31
+ "./utils": {
32
+ "import": "./utils.js",
33
+ "require": "./utils.cjs.js"
34
+ },
35
+ "./pure": {
36
+ "import": "./pure.js",
37
+ "require": "./pure.cjs.js"
38
+ },
39
+ "./initializers": {
40
+ "import": "./initializers.js",
41
+ "require": "./initializers.cjs.js"
42
+ }
43
+ },
26
44
  "devDependencies": {
27
45
  "@babel/eslint-parser": "^7.18.2",
28
46
  "@babel/plugin-transform-runtime": "^7.18.5",