@bluemarble/bm-components 2.4.1 → 2.4.3

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/index.cjs CHANGED
@@ -1179,6 +1179,114 @@ var require_react_is2 = __commonJS({
1179
1179
  }
1180
1180
  });
1181
1181
 
1182
+ // packages/nookies/index.ts
1183
+ var _cookie = require('cookie'); var cookie = _interopRequireWildcard(_cookie);
1184
+ var _setcookieparser = require('set-cookie-parser'); var setCookieParser = _interopRequireWildcard(_setcookieparser);
1185
+
1186
+ // packages/nookies/util.ts
1187
+ function isBrowser() {
1188
+ return typeof window !== "undefined";
1189
+ }
1190
+ function createCookie(name, value, options) {
1191
+ let sameSite = options.sameSite;
1192
+ if (sameSite === true) {
1193
+ sameSite = "strict";
1194
+ }
1195
+ if (sameSite === void 0 || sameSite === false) {
1196
+ sameSite = "lax";
1197
+ }
1198
+ const cookieToSet = { ...options, sameSite };
1199
+ delete cookieToSet.encode;
1200
+ return {
1201
+ name,
1202
+ value,
1203
+ ...cookieToSet
1204
+ };
1205
+ }
1206
+ function hasSameProperties(a, b) {
1207
+ const aProps = Object.getOwnPropertyNames(a);
1208
+ const bProps = Object.getOwnPropertyNames(b);
1209
+ if (aProps.length !== bProps.length) {
1210
+ return false;
1211
+ }
1212
+ for (let i = 0; i < aProps.length; i++) {
1213
+ const propName = aProps[i];
1214
+ if (a[propName] !== b[propName]) {
1215
+ return false;
1216
+ }
1217
+ }
1218
+ return true;
1219
+ }
1220
+ function areCookiesEqual(a, b) {
1221
+ let sameSiteSame = a.sameSite === b.sameSite;
1222
+ if (typeof a.sameSite === "string" && typeof b.sameSite === "string") {
1223
+ sameSiteSame = a.sameSite.toLowerCase() === b.sameSite.toLowerCase();
1224
+ }
1225
+ return hasSameProperties(
1226
+ { ...a, sameSite: void 0 },
1227
+ { ...b, sameSite: void 0 }
1228
+ ) && sameSiteSame;
1229
+ }
1230
+
1231
+ // packages/nookies/index.ts
1232
+ function parseCookies(ctx, options) {
1233
+ if (_optionalChain([ctx, 'optionalAccess', _2 => _2.req, 'optionalAccess', _3 => _3.headers, 'optionalAccess', _4 => _4.cookie])) {
1234
+ return cookie.parse(ctx.req.headers.cookie, options);
1235
+ }
1236
+ if (isBrowser()) {
1237
+ return cookie.parse(document.cookie, options);
1238
+ }
1239
+ return {};
1240
+ }
1241
+ function setCookie(ctx, name, value, options = {}) {
1242
+ if (_optionalChain([ctx, 'optionalAccess', _5 => _5.res, 'optionalAccess', _6 => _6.getHeader]) && ctx.res.setHeader) {
1243
+ if (_optionalChain([ctx, 'optionalAccess', _7 => _7.res, 'optionalAccess', _8 => _8.finished])) {
1244
+ console.warn(`Not setting "${name}" cookie. Response has finished.`);
1245
+ console.warn(`You should set cookie before res.send()`);
1246
+ return {};
1247
+ }
1248
+ let cookies = ctx.res.getHeader("Set-Cookie") || [];
1249
+ if (typeof cookies === "string") cookies = [cookies];
1250
+ if (typeof cookies === "number") cookies = [];
1251
+ const parsedCookies = setCookieParser.parse(cookies, {
1252
+ decodeValues: false
1253
+ });
1254
+ const newCookie = createCookie(name, value, options);
1255
+ let cookiesToSet = [];
1256
+ parsedCookies.forEach((parsedCookie) => {
1257
+ if (!areCookiesEqual(parsedCookie, newCookie)) {
1258
+ const serializedCookie = cookie.serialize(
1259
+ parsedCookie.name,
1260
+ parsedCookie.value,
1261
+ {
1262
+ // we prevent reencoding by default, but you might override it
1263
+ encode: (val) => val,
1264
+ ...parsedCookie
1265
+ }
1266
+ );
1267
+ cookiesToSet.push(serializedCookie);
1268
+ }
1269
+ });
1270
+ cookiesToSet.push(cookie.serialize(name, value, options));
1271
+ ctx.res.setHeader("Set-Cookie", cookiesToSet);
1272
+ }
1273
+ if (isBrowser()) {
1274
+ if (options && options.httpOnly) {
1275
+ throw new Error("Can not set a httpOnly cookie in the browser.");
1276
+ }
1277
+ document.cookie = cookie.serialize(name, value, options);
1278
+ }
1279
+ return {};
1280
+ }
1281
+ function destroyCookie(ctx, name, options) {
1282
+ return setCookie(ctx, name, "", { ...options || {}, maxAge: -1 });
1283
+ }
1284
+ var nookies = {
1285
+ set: setCookie,
1286
+ get: parseCookies,
1287
+ destroy: destroyCookie
1288
+ };
1289
+
1182
1290
  // src/components/Grid/Grid.tsx
1183
1291
 
1184
1292
 
@@ -1413,7 +1521,7 @@ function sortContainerQueries(theme, css2) {
1413
1521
  }
1414
1522
  const sorted = Object.keys(css2).filter((key) => key.startsWith("@container")).sort((a, b) => {
1415
1523
  const regex = /min-width:\s*([0-9.]+)/;
1416
- return +(_optionalChain([a, 'access', _2 => _2.match, 'call', _3 => _3(regex), 'optionalAccess', _4 => _4[1]]) || 0) - +(_optionalChain([b, 'access', _5 => _5.match, 'call', _6 => _6(regex), 'optionalAccess', _7 => _7[1]]) || 0);
1524
+ return +(_optionalChain([a, 'access', _9 => _9.match, 'call', _10 => _10(regex), 'optionalAccess', _11 => _11[1]]) || 0) - +(_optionalChain([b, 'access', _12 => _12.match, 'call', _13 => _13(regex), 'optionalAccess', _14 => _14[1]]) || 0);
1417
1525
  });
1418
1526
  if (!sorted.length) {
1419
1527
  return css2;
@@ -1557,7 +1665,7 @@ function handleBreakpoints(props, propValue, styleFromPropValue) {
1557
1665
  return output;
1558
1666
  }
1559
1667
  function createEmptyBreakpointObject(breakpointsInput = {}) {
1560
- const breakpointsInOrder = _optionalChain([breakpointsInput, 'access', _8 => _8.keys, 'optionalAccess', _9 => _9.reduce, 'call', _10 => _10((acc, key) => {
1668
+ const breakpointsInOrder = _optionalChain([breakpointsInput, 'access', _15 => _15.keys, 'optionalAccess', _16 => _16.reduce, 'call', _17 => _17((acc, key) => {
1561
1669
  const breakpointStyleKey = breakpointsInput.up(key);
1562
1670
  acc[breakpointStyleKey] = {};
1563
1671
  return acc;
@@ -2003,13 +2111,13 @@ var width = style_default({
2003
2111
  var maxWidth = (props) => {
2004
2112
  if (props.maxWidth !== void 0 && props.maxWidth !== null) {
2005
2113
  const styleFromPropValue = (propValue) => {
2006
- const breakpoint = _optionalChain([props, 'access', _11 => _11.theme, 'optionalAccess', _12 => _12.breakpoints, 'optionalAccess', _13 => _13.values, 'optionalAccess', _14 => _14[propValue]]) || values[propValue];
2114
+ const breakpoint = _optionalChain([props, 'access', _18 => _18.theme, 'optionalAccess', _19 => _19.breakpoints, 'optionalAccess', _20 => _20.values, 'optionalAccess', _21 => _21[propValue]]) || values[propValue];
2007
2115
  if (!breakpoint) {
2008
2116
  return {
2009
2117
  maxWidth: sizingTransform(propValue)
2010
2118
  };
2011
2119
  }
2012
- if (_optionalChain([props, 'access', _15 => _15.theme, 'optionalAccess', _16 => _16.breakpoints, 'optionalAccess', _17 => _17.unit]) !== "px") {
2120
+ if (_optionalChain([props, 'access', _22 => _22.theme, 'optionalAccess', _23 => _23.breakpoints, 'optionalAccess', _24 => _24.unit]) !== "px") {
2013
2121
  return {
2014
2122
  maxWidth: `${breakpoint}${props.theme.breakpoints.unit}`
2015
2123
  };
@@ -2467,7 +2575,7 @@ var styleFunctionSx_default = styleFunctionSx;
2467
2575
  function applyStyles(key, styles2) {
2468
2576
  const theme = this;
2469
2577
  if (theme.vars) {
2470
- if (!_optionalChain([theme, 'access', _18 => _18.colorSchemes, 'optionalAccess', _19 => _19[key]]) || typeof theme.getColorSchemeSelector !== "function") {
2578
+ if (!_optionalChain([theme, 'access', _25 => _25.colorSchemes, 'optionalAccess', _26 => _26[key]]) || typeof theme.getColorSchemeSelector !== "function") {
2471
2579
  return {};
2472
2580
  }
2473
2581
  let selector = theme.getColorSchemeSelector(key);
@@ -2518,7 +2626,7 @@ function createTheme(options = {}, ...args) {
2518
2626
  muiTheme = args.reduce((acc, argument) => deepmerge(acc, argument), muiTheme);
2519
2627
  muiTheme.unstable_sxConfig = {
2520
2628
  ...defaultSxConfig_default,
2521
- ..._optionalChain([other, 'optionalAccess', _20 => _20.unstable_sxConfig])
2629
+ ..._optionalChain([other, 'optionalAccess', _27 => _27.unstable_sxConfig])
2522
2630
  };
2523
2631
  muiTheme.unstable_sx = function sx(props) {
2524
2632
  return styleFunctionSx_default({
@@ -2554,7 +2662,7 @@ var splitProps = (props) => {
2554
2662
  systemProps: {},
2555
2663
  otherProps: {}
2556
2664
  };
2557
- const config = _nullishCoalesce(_optionalChain([props, 'optionalAccess', _21 => _21.theme, 'optionalAccess', _22 => _22.unstable_sxConfig]), () => ( defaultSxConfig_default));
2665
+ const config = _nullishCoalesce(_optionalChain([props, 'optionalAccess', _28 => _28.theme, 'optionalAccess', _29 => _29.unstable_sxConfig]), () => ( defaultSxConfig_default));
2558
2666
  Object.keys(props).forEach((prop) => {
2559
2667
  if (config[prop]) {
2560
2668
  result.systemProps[prop] = props[prop];
@@ -3155,7 +3263,7 @@ var Grid_default = Grid;
3155
3263
  // src/components/Grid/EditableTableCell/DefaultInput.tsx
3156
3264
 
3157
3265
 
3158
- var _moment = require('moment'); var _moment2 = _interopRequireDefault(_moment);
3266
+ var _dayjs = require('dayjs'); var _dayjs2 = _interopRequireDefault(_dayjs);
3159
3267
  var DefaultInput = (allProps) => {
3160
3268
  const {
3161
3269
  TextFieldProps: TextFieldProps3,
@@ -3174,21 +3282,21 @@ var DefaultInput = (allProps) => {
3174
3282
  if (formatInputDefautvalue) return formatInputDefautvalue(value);
3175
3283
  switch (type) {
3176
3284
  case "date":
3177
- return _moment2.default.call(void 0, value).format("YYYY-MM-DD");
3285
+ return _dayjs2.default.call(void 0, value).format("YYYY-MM-DD");
3178
3286
  case "datetime-local":
3179
- return _moment2.default.call(void 0, value).format("YYYY-MM-DDTHH:mm:ss");
3287
+ return _dayjs2.default.call(void 0, value).format("YYYY-MM-DDTHH:mm:ss");
3180
3288
  default:
3181
3289
  return String(value);
3182
3290
  }
3183
3291
  };
3184
3292
  const handleSave = (event) => {
3185
- onUpdateValue(String(_optionalChain([event, 'access', _23 => _23.target, 'optionalAccess', _24 => _24.value])));
3293
+ onUpdateValue(String(_optionalChain([event, 'access', _30 => _30.target, 'optionalAccess', _31 => _31.value])));
3186
3294
  setIsEditing(false);
3187
3295
  const response = {
3188
3296
  name,
3189
3297
  event,
3190
- value: _optionalChain([event, 'access', _25 => _25.target, 'optionalAccess', _26 => _26.value]),
3191
- data: { ...rowData, [name]: _optionalChain([event, 'access', _27 => _27.target, 'optionalAccess', _28 => _28.value]) }
3298
+ value: _optionalChain([event, 'access', _32 => _32.target, 'optionalAccess', _33 => _33.value]),
3299
+ data: { ...rowData, [name]: _optionalChain([event, 'access', _34 => _34.target, 'optionalAccess', _35 => _35.value]) }
3192
3300
  };
3193
3301
  onSave(response);
3194
3302
  };
@@ -3205,10 +3313,10 @@ var DefaultInput = (allProps) => {
3205
3313
  autoFocus: true,
3206
3314
  type,
3207
3315
  size: "small",
3208
- defaultValue: formatDefaultValue(_optionalChain([rowData, 'optionalAccess', _29 => _29[name]])),
3316
+ defaultValue: formatDefaultValue(_optionalChain([rowData, 'optionalAccess', _36 => _36[name]])),
3209
3317
  sx: {
3210
3318
  width: fullWidth ? "100%" : "intial",
3211
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _30 => _30.sx])
3319
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _37 => _37.sx])
3212
3320
  },
3213
3321
  inputProps: {
3214
3322
  style: {
@@ -3218,11 +3326,11 @@ var DefaultInput = (allProps) => {
3218
3326
  onKeyDown
3219
3327
  },
3220
3328
  InputProps: {
3221
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _31 => _31.InputProps]),
3329
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _38 => _38.InputProps]),
3222
3330
  sx: {
3223
3331
  fontSize: 14,
3224
3332
  pl: 0.2,
3225
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _32 => _32.InputProps, 'optionalAccess', _33 => _33.sx])
3333
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _39 => _39.InputProps, 'optionalAccess', _40 => _40.sx])
3226
3334
  }
3227
3335
  },
3228
3336
  ...TextFieldProps3
@@ -3246,18 +3354,16 @@ var InputMask = (allProps) => {
3246
3354
  handleCancelEditing,
3247
3355
  setIsEditing
3248
3356
  } = allProps;
3249
- const { ref, unmaskedValue, setValue } = _reactimask.useIMask.call(void 0,
3250
- mask
3251
- );
3357
+ const { ref, unmaskedValue, setValue } = _reactimask.useIMask.call(void 0, mask);
3252
3358
  const handleSave = (event) => {
3253
3359
  setIsEditing(false);
3254
- setValue(String(_optionalChain([event, 'access', _34 => _34.target, 'optionalAccess', _35 => _35.value])));
3360
+ setValue(String(_optionalChain([event, 'access', _41 => _41.target, 'optionalAccess', _42 => _42.value])));
3255
3361
  const response = {
3256
3362
  name,
3257
3363
  event,
3258
- value: _optionalChain([event, 'access', _36 => _36.target, 'optionalAccess', _37 => _37.value]),
3364
+ value: _optionalChain([event, 'access', _43 => _43.target, 'optionalAccess', _44 => _44.value]),
3259
3365
  unmaskedValue,
3260
- data: { ...rowData, [name]: _optionalChain([event, 'access', _38 => _38.target, 'optionalAccess', _39 => _39.value]) }
3366
+ data: { ...rowData, [name]: _optionalChain([event, 'access', _45 => _45.target, 'optionalAccess', _46 => _46.value]) }
3261
3367
  };
3262
3368
  onSave(response);
3263
3369
  };
@@ -3266,8 +3372,8 @@ var InputMask = (allProps) => {
3266
3372
  if (ev.code === "Escape") handleCancelEditing();
3267
3373
  };
3268
3374
  _react.useEffect.call(void 0, () => {
3269
- if (_optionalChain([rowData, 'optionalAccess', _40 => _40[name]])) setValue(String(_optionalChain([rowData, 'optionalAccess', _41 => _41[name]])));
3270
- }, [_optionalChain([rowData, 'optionalAccess', _42 => _42[name]])]);
3375
+ if (_optionalChain([rowData, 'optionalAccess', _47 => _47[name]])) setValue(String(_optionalChain([rowData, 'optionalAccess', _48 => _48[name]])));
3376
+ }, [_optionalChain([rowData, 'optionalAccess', _49 => _49[name]])]);
3271
3377
  return /* @__PURE__ */ React2.default.createElement(
3272
3378
  _material.TextField,
3273
3379
  {
@@ -3279,7 +3385,7 @@ var InputMask = (allProps) => {
3279
3385
  size: "small",
3280
3386
  sx: {
3281
3387
  width: fullWidth ? "100%" : "intial",
3282
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _43 => _43.sx])
3388
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _50 => _50.sx])
3283
3389
  },
3284
3390
  inputProps: {
3285
3391
  style: {
@@ -3289,11 +3395,11 @@ var InputMask = (allProps) => {
3289
3395
  onKeyDown
3290
3396
  },
3291
3397
  InputProps: {
3292
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _44 => _44.InputProps]),
3398
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _51 => _51.InputProps]),
3293
3399
  sx: {
3294
3400
  fontSize: 14,
3295
3401
  pl: 0.2,
3296
- ..._optionalChain([TextFieldProps3, 'optionalAccess', _45 => _45.InputProps, 'optionalAccess', _46 => _46.sx])
3402
+ ..._optionalChain([TextFieldProps3, 'optionalAccess', _52 => _52.InputProps, 'optionalAccess', _53 => _53.sx])
3297
3403
  }
3298
3404
  },
3299
3405
  ...TextFieldProps3
@@ -3320,7 +3426,7 @@ var EditableTableCell = (allProps) => {
3320
3426
  ...props
3321
3427
  } = allProps;
3322
3428
  const [isEditing, setIsEditing] = _react.useState.call(void 0, false);
3323
- const [value, setValue] = _react.useState.call(void 0, _optionalChain([rowData, 'optionalAccess', _47 => _47[name]]));
3429
+ const [value, setValue] = _react.useState.call(void 0, _optionalChain([rowData, 'optionalAccess', _54 => _54[name]]));
3324
3430
  const handleCancelEditing = () => {
3325
3431
  setIsEditing(false);
3326
3432
  if (onCancel) onCancel();
@@ -3484,8 +3590,8 @@ function FormikInputMask({
3484
3590
 
3485
3591
 
3486
3592
  var CustomInputLabel = _material.InputLabel;
3487
- function Select({ withFormik = true, ...rest }) {
3488
- if (withFormik) return /* @__PURE__ */ React2.default.createElement(FormikSelect, { ...rest });
3593
+ function Select({ withFormik = true, helperText, ...rest }) {
3594
+ if (withFormik) return /* @__PURE__ */ React2.default.createElement(FormikSelect, { helperText, ...rest });
3489
3595
  else return /* @__PURE__ */ React2.default.createElement(BaseSelect, { ...rest });
3490
3596
  }
3491
3597
  function BaseSelect({
@@ -3522,7 +3628,7 @@ function FormikSelect({
3522
3628
  const onChange = (_, { props: { value: value2 } }) => {
3523
3629
  setValue(value2);
3524
3630
  };
3525
- return /* @__PURE__ */ React2.default.createElement(_material.FormControl, { fullWidth: true, ...FormControlProps2, error: Boolean(_optionalChain([meta, 'optionalAccess', _48 => _48.error])) }, /* @__PURE__ */ React2.default.createElement(CustomInputLabel, { ...InputLabelProps2 }, label), /* @__PURE__ */ React2.default.createElement(
3631
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, { fullWidth: true, ...FormControlProps2, error: Boolean(_optionalChain([meta, 'optionalAccess', _55 => _55.error])) }, /* @__PURE__ */ React2.default.createElement(CustomInputLabel, { ...InputLabelProps2 }, label), /* @__PURE__ */ React2.default.createElement(
3526
3632
  _material.Select,
3527
3633
  {
3528
3634
  inputProps: {
@@ -3535,7 +3641,7 @@ function FormikSelect({
3535
3641
  onChange
3536
3642
  },
3537
3643
  children
3538
- ), /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText || _optionalChain([meta, 'optionalAccess', _49 => _49.error])));
3644
+ ), /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText || _optionalChain([meta, 'optionalAccess', _56 => _56.error])));
3539
3645
  }
3540
3646
 
3541
3647
  // src/components/Autocomplete/index.tsx
@@ -3560,9 +3666,9 @@ function Autocomplete2({
3560
3666
  if (withFormik) {
3561
3667
  const theme = _styles.useTheme.call(void 0, );
3562
3668
  const isLegacyBehaviorDisabledTheme = _react.useMemo.call(void 0, () => {
3563
- return _optionalChain([theme, 'access', _50 => _50.components, 'optionalAccess', _51 => _51.MuiAutocomplete, 'optionalAccess', _52 => _52.defaultProps, 'optionalAccess', _53 => _53["data-legacy-behavior"]]) === "disabled";
3669
+ return _optionalChain([theme, 'access', _57 => _57.components, 'optionalAccess', _58 => _58.MuiAutocomplete, 'optionalAccess', _59 => _59.defaultProps, 'optionalAccess', _60 => _60["data-legacy-behavior"]]) === "disabled";
3564
3670
  }, [theme]);
3565
- const isLegacyBehaviorDisabled = typeof _optionalChain([rest, 'optionalAccess', _54 => _54["data-legacy-behavior"]]) !== "undefined" ? rest["data-legacy-behavior"] === "disabled" : isLegacyBehaviorDisabledTheme;
3671
+ const isLegacyBehaviorDisabled = typeof _optionalChain([rest, 'optionalAccess', _61 => _61["data-legacy-behavior"]]) !== "undefined" ? rest["data-legacy-behavior"] === "disabled" : isLegacyBehaviorDisabledTheme;
3566
3672
  if (isLegacyBehaviorDisabled)
3567
3673
  return /* @__PURE__ */ React2.default.createElement(
3568
3674
  FormikAutocomplete,
@@ -3602,14 +3708,14 @@ function FormikAutocompleteLegacy({
3602
3708
  name: props.name
3603
3709
  });
3604
3710
  const [defaultOption] = _react.useState.call(void 0, () => {
3605
- const key = _optionalChain([option, 'optionalAccess', _55 => _55.key]);
3711
+ const key = _optionalChain([option, 'optionalAccess', _62 => _62.key]);
3606
3712
  if (key) return props.options.find((option2) => option2[key] === value);
3607
3713
  return props.options.find(
3608
3714
  (option2) => Object.values(option2)[0] === value
3609
3715
  );
3610
3716
  });
3611
3717
  const onChange = (_, newValue) => {
3612
- const value2 = _optionalChain([option, 'optionalAccess', _56 => _56.value]);
3718
+ const value2 = _optionalChain([option, 'optionalAccess', _63 => _63.value]);
3613
3719
  if (getOptionValue) {
3614
3720
  setValue(getOptionValue(newValue));
3615
3721
  } else if (value2) {
@@ -3617,13 +3723,13 @@ function FormikAutocompleteLegacy({
3617
3723
  } else setValue(newValue);
3618
3724
  };
3619
3725
  const getOptionLabel = (item) => {
3620
- if (_optionalChain([props, 'optionalAccess', _57 => _57.getOptionLabel])) return props.getOptionLabel(item);
3621
- if (_optionalChain([option, 'optionalAccess', _58 => _58.label])) return String(item[option.label]);
3726
+ if (_optionalChain([props, 'optionalAccess', _64 => _64.getOptionLabel])) return props.getOptionLabel(item);
3727
+ if (_optionalChain([option, 'optionalAccess', _65 => _65.label])) return String(item[option.label]);
3622
3728
  return "[getOptionLabel] error";
3623
3729
  };
3624
3730
  const isOptionEqualToValue = (a, b) => {
3625
- const key = _optionalChain([option, 'optionalAccess', _59 => _59.key]);
3626
- if (_optionalChain([props, 'optionalAccess', _60 => _60.isOptionEqualToValue])) return props.isOptionEqualToValue(a, b);
3731
+ const key = _optionalChain([option, 'optionalAccess', _66 => _66.key]);
3732
+ if (_optionalChain([props, 'optionalAccess', _67 => _67.isOptionEqualToValue])) return props.isOptionEqualToValue(a, b);
3627
3733
  if (key) return a[key] === b[key];
3628
3734
  return Object.values(a)[0] === Object.values(b)[0];
3629
3735
  };
@@ -3634,8 +3740,8 @@ function FormikAutocompleteLegacy({
3634
3740
  renderInput: (params) => /* @__PURE__ */ React2.default.createElement(
3635
3741
  _material.TextField,
3636
3742
  {
3637
- error: Boolean(_optionalChain([meta, 'optionalAccess', _61 => _61.error])),
3638
- helperText: _optionalChain([meta, 'optionalAccess', _62 => _62.error]),
3743
+ error: Boolean(_optionalChain([meta, 'optionalAccess', _68 => _68.error])),
3744
+ helperText: _optionalChain([meta, 'optionalAccess', _69 => _69.error]),
3639
3745
  ...params,
3640
3746
  ...field,
3641
3747
  InputProps: {
@@ -3665,7 +3771,7 @@ function FormikAutocomplete({
3665
3771
  });
3666
3772
  const [defaultValue] = _react.useState.call(void 0, value);
3667
3773
  const onChange = (_, newValue) => {
3668
- const value2 = _optionalChain([option, 'optionalAccess', _63 => _63.value]);
3774
+ const value2 = _optionalChain([option, 'optionalAccess', _70 => _70.value]);
3669
3775
  if (getOptionValue) {
3670
3776
  setValue(getOptionValue(newValue));
3671
3777
  } else if (value2) {
@@ -3678,8 +3784,8 @@ function FormikAutocomplete({
3678
3784
  renderInput: (params) => /* @__PURE__ */ React2.default.createElement(
3679
3785
  _material.TextField,
3680
3786
  {
3681
- error: Boolean(_optionalChain([meta, 'optionalAccess', _64 => _64.error])),
3682
- helperText: _optionalChain([meta, 'optionalAccess', _65 => _65.error]),
3787
+ error: Boolean(_optionalChain([meta, 'optionalAccess', _71 => _71.error])),
3788
+ helperText: _optionalChain([meta, 'optionalAccess', _72 => _72.error]),
3683
3789
  ...params,
3684
3790
  ...field,
3685
3791
  InputProps: {
@@ -3706,6 +3812,8 @@ function FormikAutocomplete({
3706
3812
 
3707
3813
 
3708
3814
 
3815
+
3816
+
3709
3817
  var Checkbox = ({
3710
3818
  withFormik = true,
3711
3819
  name,
@@ -3716,40 +3824,40 @@ var Checkbox = ({
3716
3824
  };
3717
3825
  var BaseCheckbox = ({
3718
3826
  label,
3827
+ helperText,
3719
3828
  FormControlLabelProps: FormControlLabelProps3,
3720
3829
  ...props
3721
3830
  }) => {
3722
- return /* @__PURE__ */ React2.default.createElement(
3831
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, null, /* @__PURE__ */ React2.default.createElement(
3723
3832
  _material.FormControlLabel,
3724
3833
  {
3725
3834
  label,
3726
3835
  control: /* @__PURE__ */ React2.default.createElement(_material.Checkbox, { ...props }),
3727
3836
  ...FormControlLabelProps3
3728
3837
  }
3729
- );
3838
+ ), helperText && /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText));
3730
3839
  };
3731
3840
  var FormikCheckbox = ({
3732
3841
  label,
3733
3842
  name,
3843
+ helperText,
3734
3844
  FormControlLabelProps: FormControlLabelProps3,
3735
3845
  ...props
3736
3846
  }) => {
3737
- const [{ value, ...field }, , { setValue }] = _formik.useField.call(void 0, {
3738
- name
3739
- });
3847
+ const [{ value, ...field }, { error }, { setValue }] = _formik.useField.call(void 0, { name });
3740
3848
  const onChange = (_, value2) => {
3741
3849
  setValue(value2);
3742
3850
  };
3743
- return /* @__PURE__ */ React2.default.createElement(
3851
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, { error: Boolean(error) }, /* @__PURE__ */ React2.default.createElement(
3744
3852
  _material.FormControlLabel,
3745
3853
  {
3746
3854
  label,
3747
- control: /* @__PURE__ */ React2.default.createElement(_material.Checkbox, { ...props, defaultChecked: Boolean(value) }),
3855
+ control: /* @__PURE__ */ React2.default.createElement(_material.Checkbox, { ...props, checked: Boolean(value) }),
3748
3856
  ...field,
3749
3857
  onChange,
3750
3858
  ...FormControlLabelProps3
3751
3859
  }
3752
- );
3860
+ ), (error || helperText) && /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, _nullishCoalesce(error, () => ( helperText))));
3753
3861
  };
3754
3862
 
3755
3863
  // src/components/Switch/index.tsx
@@ -3759,46 +3867,48 @@ var FormikCheckbox = ({
3759
3867
 
3760
3868
 
3761
3869
 
3870
+
3871
+
3762
3872
  var Switch = ({ withFormik = true, name, ...props }) => {
3763
3873
  if (withFormik && name) return /* @__PURE__ */ React2.default.createElement(FormikSwitch, { name, ...props });
3764
3874
  else return /* @__PURE__ */ React2.default.createElement(BaseSwitch, { ...props });
3765
3875
  };
3766
3876
  var BaseSwitch = ({
3767
3877
  label,
3878
+ helperText,
3768
3879
  FormControlLabelProps: FormControlLabelProps3,
3769
3880
  ...props
3770
3881
  }) => {
3771
- return /* @__PURE__ */ React2.default.createElement(
3882
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, null, /* @__PURE__ */ React2.default.createElement(
3772
3883
  _material.FormControlLabel,
3773
3884
  {
3774
3885
  label,
3775
3886
  control: /* @__PURE__ */ React2.default.createElement(_material.Switch, { ...props }),
3776
3887
  ...FormControlLabelProps3
3777
3888
  }
3778
- );
3889
+ ), helperText && /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText));
3779
3890
  };
3780
3891
  var FormikSwitch = ({
3781
3892
  label,
3782
3893
  name,
3894
+ helperText,
3783
3895
  FormControlLabelProps: FormControlLabelProps3,
3784
3896
  ...props
3785
3897
  }) => {
3786
- const [{ value, onChange: unused, ...field }, , { setValue }] = _formik.useField.call(void 0, {
3787
- name
3788
- });
3898
+ const [{ value, onChange: unused, ...field }, { error }, { setValue }] = _formik.useField.call(void 0, { name });
3789
3899
  const onChange = (_, value2) => {
3790
3900
  setValue(value2);
3791
3901
  };
3792
- return /* @__PURE__ */ React2.default.createElement(
3902
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, { error: Boolean(error) }, /* @__PURE__ */ React2.default.createElement(
3793
3903
  _material.FormControlLabel,
3794
3904
  {
3795
3905
  label,
3796
3906
  onChange,
3797
- control: /* @__PURE__ */ React2.default.createElement(_material.Switch, { defaultChecked: value, ...props }),
3907
+ control: /* @__PURE__ */ React2.default.createElement(_material.Switch, { checked: value, ...props }),
3798
3908
  ...field,
3799
3909
  ...FormControlLabelProps3
3800
3910
  }
3801
- );
3911
+ ), (error || helperText) && /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, _nullishCoalesce(error, () => ( helperText))));
3802
3912
  };
3803
3913
 
3804
3914
  // src/components/Radio/index.tsx
@@ -3811,6 +3921,7 @@ var FormikSwitch = ({
3811
3921
 
3812
3922
 
3813
3923
 
3924
+
3814
3925
  var Radio = ({
3815
3926
  name,
3816
3927
  withFormik = true,
@@ -3822,6 +3933,7 @@ var Radio = ({
3822
3933
  var BaseRadio = ({
3823
3934
  label,
3824
3935
  options,
3936
+ helperText,
3825
3937
  ...rest
3826
3938
  }) => {
3827
3939
  return /* @__PURE__ */ React2.default.createElement(_material.FormControl, null, label && /* @__PURE__ */ React2.default.createElement(_material.FormLabel, null, label), /* @__PURE__ */ React2.default.createElement(_material.RadioGroup, { ...rest }, options.map((option) => /* @__PURE__ */ React2.default.createElement(
@@ -3832,22 +3944,21 @@ var BaseRadio = ({
3832
3944
  label: option.label,
3833
3945
  control: /* @__PURE__ */ React2.default.createElement(_material.Radio, null)
3834
3946
  }
3835
- ))));
3947
+ ))), helperText && /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText));
3836
3948
  };
3837
3949
  var FormikRadio = ({
3838
3950
  label,
3839
3951
  name,
3840
3952
  options,
3953
+ helperText,
3841
3954
  ...rest
3842
3955
  }) => {
3843
- const onChange = (setFieldValue, value) => {
3844
- setFieldValue(name, value);
3845
- };
3846
- return /* @__PURE__ */ React2.default.createElement(_formik.Field, null, ({ field: { value }, form: { setFieldValue } }) => /* @__PURE__ */ React2.default.createElement(_material.FormControl, null, label && /* @__PURE__ */ React2.default.createElement(_material.FormLabel, null, label), /* @__PURE__ */ React2.default.createElement(
3956
+ const [{ value }, meta, { setValue }] = _formik.useField.call(void 0, name);
3957
+ return /* @__PURE__ */ React2.default.createElement(_material.FormControl, { error: Boolean(meta.error) }, label && /* @__PURE__ */ React2.default.createElement(_material.FormLabel, null, label), /* @__PURE__ */ React2.default.createElement(
3847
3958
  _material.RadioGroup,
3848
3959
  {
3849
- defaultValue: value,
3850
- onChange: (_, value2) => onChange(setFieldValue, value2),
3960
+ value: _nullishCoalesce(value, () => ( "")),
3961
+ onChange: (_, val) => setValue(val),
3851
3962
  ...rest
3852
3963
  },
3853
3964
  options.map((option) => /* @__PURE__ */ React2.default.createElement(
@@ -3859,7 +3970,7 @@ var FormikRadio = ({
3859
3970
  control: /* @__PURE__ */ React2.default.createElement(_material.Radio, null)
3860
3971
  }
3861
3972
  ))
3862
- )));
3973
+ ), /* @__PURE__ */ React2.default.createElement(_material.FormHelperText, null, helperText || meta.error));
3863
3974
  };
3864
3975
 
3865
3976
  // src/components/LargeButton/index.tsx
@@ -4066,14 +4177,14 @@ function BaseGrid({
4066
4177
  ...styles.bordered(bordered),
4067
4178
  ...styles.striped(striped),
4068
4179
  ...styles.lastRowBorder(hideFooter),
4069
- ..._optionalChain([paperProps, 'optionalAccess', _66 => _66.sx])
4180
+ ..._optionalChain([paperProps, 'optionalAccess', _73 => _73.sx])
4070
4181
  }
4071
4182
  },
4072
4183
  /* @__PURE__ */ React2.default.createElement(
4073
4184
  _material.Box,
4074
4185
  {
4075
4186
  ...boxContainerProps,
4076
- sx: { overflowX: "auto", ..._optionalChain([boxContainerProps, 'optionalAccess', _67 => _67.sx]) }
4187
+ sx: { overflowX: "auto", ..._optionalChain([boxContainerProps, 'optionalAccess', _74 => _74.sx]) }
4077
4188
  },
4078
4189
  /* @__PURE__ */ React2.default.createElement(
4079
4190
  _material.Table,
@@ -4082,7 +4193,7 @@ function BaseGrid({
4082
4193
  stickyHeader: true,
4083
4194
  ...tableProps,
4084
4195
  sx: {
4085
- ..._optionalChain([tableProps, 'optionalAccess', _68 => _68.sx])
4196
+ ..._optionalChain([tableProps, 'optionalAccess', _75 => _75.sx])
4086
4197
  }
4087
4198
  },
4088
4199
  /* @__PURE__ */ React2.default.createElement(_material.TableHead, { ...tableHeadProps }, /* @__PURE__ */ React2.default.createElement(_material.TableRow, null, prependColumn, columns.map((column) => /* @__PURE__ */ React2.default.createElement(
@@ -4093,18 +4204,18 @@ function BaseGrid({
4093
4204
  ...column.props,
4094
4205
  sx: {
4095
4206
  pl: 2,
4096
- ..._optionalChain([column, 'optionalAccess', _69 => _69.sx])
4207
+ ..._optionalChain([column, 'optionalAccess', _76 => _76.sx])
4097
4208
  }
4098
4209
  },
4099
4210
  column.children ? column.children : /* @__PURE__ */ React2.default.createElement(
4100
4211
  _material.TableSortLabel,
4101
4212
  {
4102
4213
  active: sortedBy.some((p) => p.prop === column.name),
4103
- direction: _optionalChain([sortedBy, 'access', _70 => _70.find, 'call', _71 => _71((p) => p.prop === column.name), 'optionalAccess', _72 => _72.direction]) || "desc",
4214
+ direction: _optionalChain([sortedBy, 'access', _77 => _77.find, 'call', _78 => _78((p) => p.prop === column.name), 'optionalAccess', _79 => _79.direction]) || "desc",
4104
4215
  onClick: () => onSortBy(column.name),
4105
4216
  disabled: column.canSort === false,
4106
4217
  ...tableSortLabelProps,
4107
- sx: { ..._optionalChain([tableSortLabelProps, 'optionalAccess', _73 => _73.sx]) }
4218
+ sx: { ..._optionalChain([tableSortLabelProps, 'optionalAccess', _80 => _80.sx]) }
4108
4219
  },
4109
4220
  column.label
4110
4221
  )
@@ -4153,6 +4264,11 @@ function BaseGridAutoRows({
4153
4264
 
4154
4265
 
4155
4266
  var Modal = ({ open, onClose, BoxProps: BoxProps3, ...rest }) => {
4267
+ if (process.env.NODE_ENV !== "production") {
4268
+ console.warn(
4269
+ "[Modal] is deprecated. Use `BaseDialog` instead. `Modal` will be removed in a future release."
4270
+ );
4271
+ }
4156
4272
  return /* @__PURE__ */ React2.default.createElement(_material.Modal, { open, onClose, ...rest }, /* @__PURE__ */ React2.default.createElement(
4157
4273
  _material.Box,
4158
4274
  {
@@ -4165,7 +4281,7 @@ var Modal = ({ open, onClose, BoxProps: BoxProps3, ...rest }) => {
4165
4281
  left: "50%",
4166
4282
  transform: "translate(-50%, -50%)",
4167
4283
  borderRadius: 1,
4168
- ..._optionalChain([BoxProps3, 'optionalAccess', _74 => _74.sx])
4284
+ ..._optionalChain([BoxProps3, 'optionalAccess', _81 => _81.sx])
4169
4285
  }
4170
4286
  },
4171
4287
  rest.children
@@ -4240,8 +4356,8 @@ function UseDialogConfirm() {
4240
4356
  };
4241
4357
  };
4242
4358
  const onProceed = async (event) => {
4243
- _optionalChain([event, 'optionalAccess', _75 => _75.preventDefault, 'call', _76 => _76()]);
4244
- _optionalChain([event, 'optionalAccess', _77 => _77.stopPropagation, 'call', _78 => _78()]);
4359
+ _optionalChain([event, 'optionalAccess', _82 => _82.preventDefault, 'call', _83 => _83()]);
4360
+ _optionalChain([event, 'optionalAccess', _84 => _84.stopPropagation, 'call', _85 => _85()]);
4245
4361
  setLoading(true);
4246
4362
  try {
4247
4363
  if (!onConfirmFn.current) return;
@@ -4252,8 +4368,8 @@ function UseDialogConfirm() {
4252
4368
  }
4253
4369
  };
4254
4370
  const onCancel = (event) => {
4255
- _optionalChain([event, 'optionalAccess', _79 => _79.preventDefault, 'call', _80 => _80()]);
4256
- _optionalChain([event, 'optionalAccess', _81 => _81.stopPropagation, 'call', _82 => _82()]);
4371
+ _optionalChain([event, 'optionalAccess', _86 => _86.preventDefault, 'call', _87 => _87()]);
4372
+ _optionalChain([event, 'optionalAccess', _88 => _88.stopPropagation, 'call', _89 => _89()]);
4257
4373
  setOpened(false);
4258
4374
  };
4259
4375
  const onCloseModal = () => {
@@ -4472,121 +4588,13 @@ var BaseDialog = {
4472
4588
  Body: BaseDialogBody
4473
4589
  };
4474
4590
 
4475
- // src/errors/HttpError.ts
4476
- var HttpError = class extends Error {
4477
- constructor(status, message) {
4478
- super(message);
4479
- this.message = message;
4480
- this.stack = `HttpError: ${message}`;
4481
- this.status = status;
4482
- }
4483
- };
4591
+ // src/contexts/FormHelperProvider.tsx
4484
4592
 
4485
- // src/errors/DomainError.ts
4486
- var DomainError = class extends Error {
4487
- constructor(message) {
4488
- super(message);
4489
- this.message = message;
4490
- this.stack = `DomainError: ${message}`;
4491
- }
4492
- };
4493
4593
 
4494
- // src/helpers/apiHelper/index.ts
4495
- var VALID_METHODS = [
4496
- "GET",
4497
- "HEAD",
4498
- "POST",
4499
- "PUT",
4500
- "DELETE",
4501
- "CONNECT",
4502
- "OPTIONS",
4503
- "TRACE",
4504
- "PATCH"
4505
- ];
4506
- var _ApiHelper = class _ApiHelper {
4507
- async onFinally(_req, _res) {
4508
- }
4509
- async onError(_req, _res, _error) {
4510
- }
4511
- constructor(props) {
4512
- this.public = _nullishCoalesce(_optionalChain([props, 'optionalAccess', _83 => _83.public]), () => ( false));
4513
- this.middlewares = (_optionalChain([props, 'optionalAccess', _84 => _84.middlewares]) || []).reverse();
4514
- this.onFinally = _optionalChain([props, 'optionalAccess', _85 => _85.onFinally]) || (async () => {
4515
- });
4516
- this.onError = _optionalChain([props, 'optionalAccess', _86 => _86.onError]) || (async () => {
4517
- });
4518
- }
4519
- createMethods(methods) {
4520
- return async (req, res) => {
4521
- const currentMethod = methods[req.method] || methods.ALL;
4522
- const options = { public: this.public };
4523
- if (req.method === "OPTIONS") return res.status(200).end();
4524
- try {
4525
- if (!VALID_METHODS.includes(req.method))
4526
- throw new HttpError(405, "M\xE9todo inv\xE1lido");
4527
- if (!currentMethod) throw new HttpError(500, "M\xE9todo n\xE3o encontrado");
4528
- const methodWithMiddlewares = this.middlewares.reduce(
4529
- (acc, fn) => fn(acc, options),
4530
- currentMethod
4531
- );
4532
- return await methodWithMiddlewares(req, res, options);
4533
- } catch (error) {
4534
- if (error instanceof DomainError) return res.status(400).json(error.message);
4535
- if (error instanceof HttpError) return res.status(error.status).json(error.message);
4536
- this.onError(req, res, error);
4537
- throw error;
4538
- } finally {
4539
- await this.onFinally(req, res);
4540
- }
4541
- };
4542
- }
4543
- buildFactory(factory) {
4544
- const options = {
4545
- public: this.public
4546
- };
4547
- return async (req, res) => {
4548
- const methods = factory(req, res);
4549
- const handler = methods[req.method];
4550
- const methodWithMiddlewares = this.middlewares.reduce((acc, fn) => {
4551
- return fn(acc, options);
4552
- }, handler);
4553
- return await methodWithMiddlewares(req, res, options);
4554
- };
4555
- }
4556
- static build(factory, options) {
4557
- const helper = new _ApiHelper({
4558
- ...options
4559
- }).buildFactory(factory);
4560
- return helper;
4561
- }
4562
- static parse(body, parser) {
4563
- try {
4564
- const object = parser.parse(body);
4565
- return object;
4566
- } catch (error) {
4567
- throw new HttpError(400, {
4568
- code: "invalid.body",
4569
- error: "Dados inv\xE1lidos",
4570
- details: error
4571
- });
4572
- }
4573
- }
4574
- /** @deprecated Use {@Link ApiHelper.build} instead. */
4575
- static create({ onFinally }) {
4576
- return new _ApiHelper({
4577
- onFinally
4578
- });
4579
- }
4594
+ var FormHelperContext = _react.createContext.call(void 0, {});
4595
+ var FormHelperProvider = ({ formatErrorMessage, api, children }) => {
4596
+ return /* @__PURE__ */ React2.default.createElement(FormHelperContext.Provider, { value: { formatErrorMessage, api } }, children);
4580
4597
  };
4581
- /** @deprecated Use {@link ApiHelper.parser} instead. */
4582
- _ApiHelper.parserErrorWrapper = _ApiHelper.parse;
4583
- var ApiHelper = _ApiHelper;
4584
-
4585
- // src/hooks/useFormHelper.ts
4586
-
4587
-
4588
- // src/hooks/useAlert.ts
4589
-
4590
4598
 
4591
4599
  // src/contexts/AlertContext.tsx
4592
4600
 
@@ -4655,611 +4663,324 @@ var AlertProvider = ({ children }) => {
4655
4663
  ));
4656
4664
  };
4657
4665
 
4658
- // src/hooks/useAlert.ts
4659
- var useAlert = () => {
4660
- return _react.useContext.call(void 0, AlertContext);
4661
- };
4666
+ // src/contexts/AuthContext.tsx
4662
4667
 
4663
- // src/hooks/useLoading.ts
4664
4668
 
4665
- function useLoading() {
4666
- const [state, setState] = _react.useState.call(void 0, []);
4667
- const isLoading = _react.useCallback.call(void 0, (prop) => state.includes(prop), [state]);
4668
- const setLoading = _react.useCallback.call(void 0, (prop, remove) => {
4669
- if (remove)
4670
- setState((prevState) => prevState.filter((state2) => state2 !== prop));
4671
- else setState((prevState) => [...prevState, prop]);
4672
- }, []);
4673
- return { isLoading, setLoading };
4674
- }
4675
4669
 
4676
- // src/contexts/FormHelperProvider.tsx
4677
4670
 
4678
4671
 
4679
- var FormHelperContext = _react.createContext.call(void 0, {});
4680
- var FormHelperProvider = ({ formatErrorMessage, api, children }) => {
4681
- return /* @__PURE__ */ React2.default.createElement(FormHelperContext.Provider, { value: { formatErrorMessage, api } }, children);
4682
- };
4683
4672
 
4684
- // src/hooks/useFormHelper.ts
4685
- function useFormHelper() {
4686
- const alertProps = useAlert();
4687
- const loadingProps = useLoading();
4688
- const { api, formatErrorMessage } = _react.useContext.call(void 0, FormHelperContext);
4689
- const { createAlert } = alertProps;
4690
- const { setLoading } = loadingProps;
4691
- const sourceRef = _react.useRef.call(void 0, new AbortController());
4692
- const onSubmitWrapper = _react.useCallback.call(void 0,
4693
- (fn, { name }) => {
4694
- return async (fields, methods) => {
4695
- const LOADING_NAME = name;
4696
- setLoading(LOADING_NAME);
4697
- try {
4698
- await fn(fields, methods);
4699
- } catch (error) {
4700
- errorHandler(error, methods.setErrors);
4701
- } finally {
4702
- setLoading(LOADING_NAME, true);
4703
- }
4704
- };
4705
- },
4706
- [setLoading]
4707
- );
4708
- const onRequestWrapper = _react.useCallback.call(void 0,
4709
- (fn, { name }) => {
4710
- return async (...params) => {
4711
- const LOADING_NAME = name;
4712
- setLoading(LOADING_NAME);
4713
- api.interceptors.request.use(
4714
- (config) => {
4715
- if (!config.signal && sourceRef.current && config.method === "get") {
4716
- config.signal = sourceRef.current.signal;
4717
- }
4718
- return config;
4719
- },
4720
- (error) => {
4721
- return Promise.reject(error);
4722
- }
4723
- );
4724
- try {
4725
- const response = await fn(...params);
4726
- return response;
4727
- } catch (error) {
4728
- errorHandler(error);
4729
- } finally {
4730
- setLoading(LOADING_NAME, true);
4731
- }
4732
- };
4733
- },
4734
- [setLoading, api]
4735
- );
4736
- const errorHandler = _react.useCallback.call(void 0,
4737
- (error, callback) => {
4738
- if (_optionalChain([error, 'optionalAccess', _87 => _87.message]) === "cancel.navigation") return;
4739
- if (callback) {
4740
- if (error.response.data.code === "invalid.body") {
4741
- const errors = error.response.data.details.issues;
4742
- const currentErrors = errors.reduce((acc, item) => {
4743
- acc[item.path.join(".")] = item.message;
4744
- return acc;
4745
- }, {});
4746
- callback(currentErrors);
4747
- }
4748
- }
4749
- createAlert(formatErrorMessage(error), "error");
4750
- },
4751
- [formatErrorMessage, createAlert]
4752
- );
4753
- _react.useEffect.call(void 0, () => {
4754
- return () => {
4755
- sourceRef.current.abort();
4756
- sourceRef.current = new AbortController();
4757
- };
4758
- }, []);
4759
- return {
4760
- ...alertProps,
4761
- ...loadingProps,
4762
- onSubmitWrapper,
4763
- onRequestWrapper
4764
- };
4765
- }
4766
4673
 
4767
- // src/helpers/authHelper.ts
4768
- var _cookie = require('cookie'); var cookie = _interopRequireWildcard(_cookie);
4769
- var _uuid = require('uuid');
4770
- var _jsonwebtoken = require('jsonwebtoken'); var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);
4674
+ // src/hooks/useGrid.ts
4771
4675
 
4772
- // packages/nookies/index.ts
4773
4676
 
4774
- var _setcookieparser = require('set-cookie-parser'); var setCookieParser = _interopRequireWildcard(_setcookieparser);
4677
+ // src/hooks/useFilter.ts
4775
4678
 
4776
- // packages/nookies/util.ts
4777
- function isBrowser() {
4778
- return typeof window !== "undefined";
4679
+
4680
+ // src/components/utils/getObjectValue.ts
4681
+ function getObjectValue(obj) {
4682
+ return (prop) => {
4683
+ try {
4684
+ return prop.split(".").reduce((o, k) => o[k], obj);
4685
+ } catch (_) {
4686
+ return void 0;
4687
+ }
4688
+ };
4779
4689
  }
4780
- function createCookie(name, value, options) {
4781
- let sameSite = options.sameSite;
4782
- if (sameSite === true) {
4783
- sameSite = "strict";
4784
- }
4785
- if (sameSite === void 0 || sameSite === false) {
4786
- sameSite = "lax";
4690
+
4691
+ // src/hooks/useFilter.ts
4692
+ function useFilter(props = { defaultFilters: [] }) {
4693
+ const [selectedFilters, setSelectedFilters] = _react.useState.call(void 0, () => {
4694
+ const { defaultFilters } = props;
4695
+ return defaultFilters || [];
4696
+ });
4697
+ const filterBy = _react.useCallback.call(void 0, (newFilter) => {
4698
+ const propToCompare = _optionalChain([newFilter, 'optionalAccess', _90 => _90.id]) ? "id" : "prop";
4699
+ function removeRepeatedFilters(filter) {
4700
+ return filter[propToCompare] !== newFilter[propToCompare];
4701
+ }
4702
+ setSelectedFilters((filters) => [
4703
+ ...filters.filter(removeRepeatedFilters),
4704
+ newFilter
4705
+ ]);
4706
+ }, []);
4707
+ const removeFilter = _react.useCallback.call(void 0,
4708
+ (prop, isId) => {
4709
+ const propToCompare = isId ? "id" : "prop";
4710
+ setSelectedFilters(
4711
+ selectedFilters.filter((filter) => filter[propToCompare] !== prop)
4712
+ );
4713
+ },
4714
+ [selectedFilters]
4715
+ );
4716
+ function clearAllFilters() {
4717
+ setSelectedFilters([]);
4787
4718
  }
4788
- const cookieToSet = { ...options, sameSite };
4789
- delete cookieToSet.encode;
4790
4719
  return {
4791
- name,
4792
- value,
4793
- ...cookieToSet
4720
+ filters: selectedFilters,
4721
+ filterBy,
4722
+ removeFilter,
4723
+ createFilter,
4724
+ clearAllFilters
4794
4725
  };
4795
4726
  }
4796
- function hasSameProperties(a, b) {
4797
- const aProps = Object.getOwnPropertyNames(a);
4798
- const bProps = Object.getOwnPropertyNames(b);
4799
- if (aProps.length !== bProps.length) {
4800
- return false;
4801
- }
4802
- for (let i = 0; i < aProps.length; i++) {
4803
- const propName = aProps[i];
4804
- if (a[propName] !== b[propName]) {
4805
- return false;
4806
- }
4807
- }
4808
- return true;
4727
+ function isDate(date) {
4728
+ if (date instanceof Date) return true;
4729
+ else if (String(date).endsWith("Z")) return true;
4730
+ return false;
4809
4731
  }
4810
- function areCookiesEqual(a, b) {
4811
- let sameSiteSame = a.sameSite === b.sameSite;
4812
- if (typeof a.sameSite === "string" && typeof b.sameSite === "string") {
4813
- sameSiteSame = a.sameSite.toLowerCase() === b.sameSite.toLowerCase();
4732
+ function compareFilter(item, filter) {
4733
+ const itemValue = getObjectValue(item)(filter.prop);
4734
+ switch (filter.compareType) {
4735
+ case "equal":
4736
+ return itemValue === filter.value;
4737
+ case "notEqual":
4738
+ return itemValue !== filter.value;
4739
+ case "in":
4740
+ return filter.value.includes(itemValue);
4741
+ case "notIn":
4742
+ return !filter.value.includes(itemValue);
4743
+ case "valueIn":
4744
+ return itemValue.includes(filter.value);
4745
+ case "valueNotIn":
4746
+ return !itemValue.includes(filter.value);
4747
+ case "gte":
4748
+ return isDate(itemValue) ? new Date(String(itemValue)) >= filter.value : itemValue >= filter.value;
4749
+ case "gt":
4750
+ return isDate(itemValue) ? new Date(String(itemValue)) > filter.value : itemValue > filter.value;
4751
+ case "lte":
4752
+ return isDate(itemValue) ? new Date(String(itemValue)) <= filter.value : itemValue <= filter.value;
4753
+ case "lt":
4754
+ return isDate(itemValue) ? new Date(String(itemValue)) < filter.value : itemValue < filter.value;
4814
4755
  }
4815
- return hasSameProperties(
4816
- { ...a, sameSite: void 0 },
4817
- { ...b, sameSite: void 0 }
4818
- ) && sameSiteSame;
4819
4756
  }
4820
-
4821
- // packages/nookies/index.ts
4822
- function parseCookies(ctx, options) {
4823
- if (_optionalChain([ctx, 'optionalAccess', _88 => _88.req, 'optionalAccess', _89 => _89.headers, 'optionalAccess', _90 => _90.cookie])) {
4824
- return cookie.parse(ctx.req.headers.cookie, options);
4825
- }
4826
- if (isBrowser()) {
4827
- return cookie.parse(document.cookie, options);
4757
+ function createFilter(filters) {
4758
+ function apply(item) {
4759
+ const satisfiedFilters = filters.reduce((acc, filter) => {
4760
+ if (compareFilter(item, filter)) acc += 1;
4761
+ return acc;
4762
+ }, 0);
4763
+ return satisfiedFilters === filters.length;
4828
4764
  }
4829
- return {};
4765
+ return {
4766
+ apply
4767
+ };
4830
4768
  }
4831
- function setCookie(ctx, name, value, options = {}) {
4832
- if (_optionalChain([ctx, 'optionalAccess', _91 => _91.res, 'optionalAccess', _92 => _92.getHeader]) && ctx.res.setHeader) {
4833
- if (_optionalChain([ctx, 'optionalAccess', _93 => _93.res, 'optionalAccess', _94 => _94.finished])) {
4834
- console.warn(`Not setting "${name}" cookie. Response has finished.`);
4835
- console.warn(`You should set cookie before res.send()`);
4836
- return {};
4837
- }
4838
- let cookies = ctx.res.getHeader("Set-Cookie") || [];
4839
- if (typeof cookies === "string") cookies = [cookies];
4840
- if (typeof cookies === "number") cookies = [];
4841
- const parsedCookies = setCookieParser.parse(cookies, {
4842
- decodeValues: false
4843
- });
4844
- const newCookie = createCookie(name, value, options);
4845
- let cookiesToSet = [];
4846
- parsedCookies.forEach((parsedCookie) => {
4847
- if (!areCookiesEqual(parsedCookie, newCookie)) {
4848
- const serializedCookie = cookie.serialize(
4849
- parsedCookie.name,
4850
- parsedCookie.value,
4851
- {
4852
- // we prevent reencoding by default, but you might override it
4853
- encode: (val) => val,
4854
- ...parsedCookie
4855
- }
4856
- );
4857
- cookiesToSet.push(serializedCookie);
4769
+
4770
+ // src/helpers/sortHelper.ts
4771
+ function SortHelper(...fields) {
4772
+ return (a, b) => {
4773
+ for (const field of fields) {
4774
+ let direction = 1;
4775
+ let key = field;
4776
+ if (field.startsWith("-")) {
4777
+ direction = -1;
4778
+ key = field.slice(1);
4858
4779
  }
4859
- });
4860
- cookiesToSet.push(cookie.serialize(name, value, options));
4861
- ctx.res.setHeader("Set-Cookie", cookiesToSet);
4862
- }
4863
- if (isBrowser()) {
4864
- if (options && options.httpOnly) {
4865
- throw new Error("Can not set a httpOnly cookie in the browser.");
4780
+ const aVal = a[key];
4781
+ const bVal = b[key];
4782
+ if (aVal > bVal) return direction;
4783
+ if (aVal < bVal) return -direction;
4866
4784
  }
4867
- document.cookie = cookie.serialize(name, value, options);
4868
- }
4869
- return {};
4870
- }
4871
- function destroyCookie(ctx, name, options) {
4872
- return setCookie(ctx, name, "", { ...options || {}, maxAge: -1 });
4785
+ return 0;
4786
+ };
4873
4787
  }
4874
- var nookies = {
4875
- set: setCookie,
4876
- get: parseCookies,
4877
- destroy: destroyCookie
4878
- };
4879
4788
 
4880
- // src/helpers/authHelper.ts
4881
- function decodeSessionToken({
4882
- req,
4883
- res,
4884
- sessionTokenName,
4885
- validate
4789
+ // src/hooks/useGrid.ts
4790
+ function useGrid({
4791
+ columns,
4792
+ filters,
4793
+ search,
4794
+ rowsPerPageOptions = [30, 60, 100],
4795
+ defaultData: externalDefaultData,
4796
+ defaultCurrentPage,
4797
+ defaultSortedBy
4886
4798
  }) {
4887
- const token = _optionalChain([req, 'access', _95 => _95.headers, 'access', _96 => _96.authorization, 'optionalAccess', _97 => _97.split, 'call', _98 => _98(" "), 'access', _99 => _99[1]]) || req.cookies[sessionTokenName];
4888
- if (!token) {
4889
- res.status(401).json({ error: "Token inv\xE1lido", code: "token.invalid" });
4890
- return true;
4891
- }
4892
- const jwtDecode = (token2) => {
4893
- if (validate) {
4894
- return _jsonwebtoken2.default.verify(token2, process.env.JWT_SECRET);
4799
+ const [defaultData, setDefaultData] = _react.useState.call(void 0, externalDefaultData || []);
4800
+ const [sortedBy, setSortedBy] = _react.useState.call(void 0,
4801
+ defaultSortedBy || []
4802
+ );
4803
+ const [currentPage, setCurrentPage] = _react.useState.call(void 0, defaultCurrentPage || 0);
4804
+ const [rowsPerPage, setRowsPerPage] = _react.useState.call(void 0, rowsPerPageOptions[0]);
4805
+ const toggleSortedDirection = _react.useCallback.call(void 0,
4806
+ (direction) => {
4807
+ if (direction === "asc") return "desc";
4808
+ return "asc";
4809
+ },
4810
+ []
4811
+ );
4812
+ const clearSort = _react.useCallback.call(void 0, () => {
4813
+ setSortedBy([]);
4814
+ }, []);
4815
+ const appendSort = _react.useCallback.call(void 0, (prop, direction) => {
4816
+ setSortedBy((prev) => [...prev, { prop, direction }]);
4817
+ }, []);
4818
+ const setSort = _react.useCallback.call(void 0, (prop, direction) => {
4819
+ setSortedBy([{ prop, direction }]);
4820
+ }, []);
4821
+ const onSortBy = _react.useCallback.call(void 0,
4822
+ (prop) => {
4823
+ if (!prop) return;
4824
+ const currentSorted = sortedBy.find((p) => p.prop === prop);
4825
+ if (currentSorted) {
4826
+ if (currentSorted.direction === "asc") {
4827
+ setSortedBy((prev) => prev.filter((p) => p.prop !== prop));
4828
+ } else {
4829
+ setSortedBy((prev) => {
4830
+ const newArr = prev.map((p) => {
4831
+ if (p.prop !== prop) return p;
4832
+ return {
4833
+ prop: p.prop,
4834
+ direction: toggleSortedDirection(p.direction)
4835
+ };
4836
+ });
4837
+ return [...newArr].slice(0);
4838
+ });
4839
+ }
4840
+ } else {
4841
+ setSortedBy((prev) => [...prev, { prop, direction: "desc" }]);
4842
+ }
4843
+ },
4844
+ [toggleSortedDirection, sortedBy]
4845
+ );
4846
+ const set = _react.useCallback.call(void 0, (data) => {
4847
+ setDefaultData(data);
4848
+ }, []);
4849
+ const sortData = _react.useCallback.call(void 0,
4850
+ (data) => {
4851
+ if (sortedBy.length > 0) {
4852
+ const symbolDir = {
4853
+ asc: "",
4854
+ desc: "-"
4855
+ };
4856
+ const formattedKeys = sortedBy.map(
4857
+ ({ prop, direction }) => `${symbolDir[direction]}${prop}`
4858
+ );
4859
+ return data.sort(SortHelper(...formattedKeys));
4860
+ } else return data;
4861
+ },
4862
+ [sortedBy]
4863
+ );
4864
+ const onPageChange = (pageNumber) => {
4865
+ if (pageNumber < 0) return;
4866
+ if (pageNumber > totalNumberOfPages) return;
4867
+ setCurrentPage(pageNumber);
4868
+ };
4869
+ const onChangeRowsPerPage = _react.useCallback.call(void 0,
4870
+ (rows) => {
4871
+ let totalNumberOfPages2 = Math.round(filteredData.length / rows) - 1;
4872
+ totalNumberOfPages2 = totalNumberOfPages2 <= 0 ? 0 : 1;
4873
+ if (currentPage > totalNumberOfPages2) setCurrentPage(totalNumberOfPages2);
4874
+ setRowsPerPage(rows);
4875
+ },
4876
+ [currentPage]
4877
+ );
4878
+ const orderedData = _react.useMemo.call(void 0, () => {
4879
+ if (sortedBy.length === 0) return defaultData;
4880
+ const newData = defaultData.slice(0);
4881
+ const sortedData = sortData(newData);
4882
+ return sortedData;
4883
+ }, [defaultData, sortData, sortedBy]);
4884
+ const filteredData = _react.useMemo.call(void 0, () => {
4885
+ let newData = orderedData.slice(0);
4886
+ if (search && search.value !== "") {
4887
+ const searchBy = createSearch(search);
4888
+ newData = newData.filter(searchBy);
4895
4889
  }
4896
- return _jsonwebtoken2.default.decode(token2);
4890
+ if (!filters) return newData;
4891
+ const newFilter = createFilter(filters);
4892
+ return newData.filter(newFilter.apply);
4893
+ }, [orderedData, search, filters]);
4894
+ const paginatedData = _react.useMemo.call(void 0, () => {
4895
+ const startPage = currentPage * rowsPerPage;
4896
+ const endPage = startPage + rowsPerPage;
4897
+ return filteredData.slice(startPage, endPage);
4898
+ }, [currentPage, rowsPerPage, filteredData]);
4899
+ const totalNumberOfPages = Math.ceil(filteredData.length / rowsPerPage) - 1;
4900
+ _react.useEffect.call(void 0, () => {
4901
+ if (externalDefaultData) setDefaultData(externalDefaultData);
4902
+ }, [externalDefaultData]);
4903
+ return {
4904
+ data: paginatedData,
4905
+ orderedData,
4906
+ filteredData,
4907
+ defaultData,
4908
+ sortedBy,
4909
+ columns,
4910
+ currentPage,
4911
+ totalNumberOfPages: totalNumberOfPages < 0 ? 0 : totalNumberOfPages,
4912
+ rowsPerPageOptions,
4913
+ rowsPerPage,
4914
+ set,
4915
+ onSortBy,
4916
+ onPageChange,
4917
+ setRowsPerPage: onChangeRowsPerPage,
4918
+ appendSort,
4919
+ setSort,
4920
+ clearSort
4897
4921
  };
4898
- try {
4899
- const decoded = jwtDecode(token);
4900
- req.user = decoded.sub;
4901
- } catch (_) {
4902
- res.status(401).json({ error: "Token inv\xE1lido", code: "token.expired" });
4903
- return true;
4922
+ }
4923
+ var isNumberOrString = (value) => ["number", "string"].includes(typeof value);
4924
+ var concatenateKey = (key, prev) => {
4925
+ if (!prev) return key;
4926
+ return `${prev}.${key}`;
4927
+ };
4928
+ function searchKeysForValue(row, accKey, compare) {
4929
+ if (typeof row === "undefined") return false;
4930
+ const rowKeys = Object.keys(row);
4931
+ let match = false;
4932
+ for (const key of rowKeys) {
4933
+ if (match) break;
4934
+ const objValue = row[key];
4935
+ if (!objValue) continue;
4936
+ const concatenatedKey = concatenateKey(key, accKey);
4937
+ if (Array.isArray(objValue)) {
4938
+ match = objValue.some(
4939
+ (obj) => isNumberOrString(obj) ? compare(concatenatedKey, obj) : searchKeysForValue(obj, concatenatedKey, compare)
4940
+ );
4941
+ continue;
4942
+ }
4943
+ if (typeof objValue === "object") {
4944
+ match = searchKeysForValue(objValue, concatenatedKey, compare);
4945
+ continue;
4946
+ }
4947
+ match = compare(concatenatedKey, row[key]);
4904
4948
  }
4949
+ return match;
4905
4950
  }
4906
- var AuthHelper = class {
4907
- constructor({
4908
- cookies,
4909
- oauth,
4910
- tokenExpTimeInSeconds,
4911
- onLogin,
4912
- onValidateRefreshToken,
4913
- onInvalidateRefreshToken,
4914
- onCreateRefreshToken,
4915
- onGetUserData
4916
- }) {
4917
- this.generateJwtAndRefreshToken = async (userId, payload = {}) => {
4918
- const token = _jsonwebtoken2.default.sign(payload, process.env.JWT_SECRET, {
4919
- subject: String(userId),
4920
- expiresIn: this.tokenExpTimeInSeconds || 60 * 15
4921
- // 15 minutos
4922
- });
4923
- const uniqueToken = _uuid.v4.call(void 0, );
4924
- await this.onCreateRefreshToken(userId, uniqueToken);
4925
- return {
4926
- token,
4927
- refreshToken: uniqueToken
4928
- };
4929
- };
4930
- this.invalidateCookies = (res) => {
4931
- return res.setHeader("Set-Cookie", [
4932
- _cookie.serialize.call(void 0, this.cookies.sessionToken, "", {
4933
- maxAge: -1,
4934
- path: "/"
4935
- }),
4936
- _cookie.serialize.call(void 0, this.cookies.refreshToken, "", {
4937
- maxAge: -1,
4938
- path: "/"
4939
- })
4940
- ]);
4941
- };
4942
- this.cookies = cookies;
4943
- this.oauth = oauth;
4944
- this.tokenExpTimeInSeconds = tokenExpTimeInSeconds;
4945
- this.onLogin = onLogin;
4946
- this.onValidateRefreshToken = onValidateRefreshToken;
4947
- this.onInvalidateRefreshToken = onInvalidateRefreshToken;
4948
- this.onCreateRefreshToken = onCreateRefreshToken;
4949
- this.onGetUserData = onGetUserData;
4951
+ var normalize = (str) => str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
4952
+ function createSearch(options) {
4953
+ const searchValue = options.caseSensitive ? options.value : String(options.value).toLowerCase();
4954
+ function getValue2(value) {
4955
+ if (options.caseSensitive) return String(value);
4956
+ return String(value).toLowerCase();
4950
4957
  }
4951
- async handler(req, res) {
4952
- if (!req.url) return res.status(400).json({ error: "url not sent" });
4953
- if (req.url.endsWith("/login")) {
4954
- const loginResult = await this.onLogin(req.body);
4955
- if (loginResult.status === "sucess") {
4956
- const { refreshToken, token } = await this.generateJwtAndRefreshToken(
4957
- loginResult.userId,
4958
- {}
4959
- );
4960
- setCookie({ res }, this.cookies.sessionToken, token, {
4961
- secure: true,
4962
- maxAge: 60 * 60 * 24 * 30,
4963
- // 30 days
4964
- path: "/",
4965
- sameSite: true
4966
- });
4967
- setCookie({ res }, this.cookies.refreshToken, refreshToken, {
4968
- secure: true,
4969
- maxAge: 60 * 60 * 24 * 30,
4970
- // 30 days
4971
- path: "/",
4972
- sameSite: true,
4973
- httpOnly: true
4974
- });
4975
- return res.json({ token, refreshToken });
4976
- }
4977
- throw new HttpError(400, loginResult.response);
4978
- }
4979
- if (req.url.endsWith("/logout")) {
4980
- this.invalidateCookies(res).end();
4981
- }
4982
- if (req.url.endsWith("/refresh")) {
4983
- const error = decodeSessionToken({
4984
- req,
4985
- res,
4986
- sessionTokenName: this.cookies.sessionToken,
4987
- validate: false
4988
- });
4989
- if (error) return;
4990
- const userId = String(req.user);
4991
- const refreshToken = parseCookies({ req })[this.cookies.refreshToken];
4992
- if (!refreshToken) {
4993
- this.invalidateCookies(res);
4994
- return res.status(400).json({
4995
- error: "Refresh Token inv\xE1lido"
4996
- });
4997
- }
4998
- const isValidRefreshToken = await this.onValidateRefreshToken(
4999
- userId,
5000
- refreshToken
5001
- );
5002
- if (!isValidRefreshToken) {
5003
- this.invalidateCookies(res);
5004
- return res.status(400).json({
5005
- error: "Refresh Token inv\xE1lido"
5006
- });
5007
- }
5008
- await this.onInvalidateRefreshToken(userId, refreshToken);
5009
- const { token, refreshToken: newRefreshToken } = await this.generateJwtAndRefreshToken(userId, {});
5010
- setCookie({ res }, this.cookies.sessionToken, token, {
5011
- secure: true,
5012
- maxAge: 60 * 60 * 24 * 30,
5013
- // 30 days
5014
- path: "/",
5015
- sameSite: true
5016
- });
5017
- setCookie({ res }, this.cookies.refreshToken, newRefreshToken, {
5018
- secure: true,
5019
- maxAge: 60 * 60 * 24 * 30,
5020
- // 30 days
5021
- path: "/",
5022
- sameSite: true,
5023
- httpOnly: true
5024
- });
5025
- return res.json({
5026
- token,
5027
- refreshToken: newRefreshToken
5028
- });
5029
- }
5030
- if (req.url.endsWith("/me")) {
5031
- const error = decodeSessionToken({
5032
- req,
5033
- res,
5034
- sessionTokenName: this.cookies.sessionToken,
5035
- validate: true
5036
- });
5037
- if (error) return;
5038
- if (!req.user)
5039
- return res.status(400).json({ error: "Usu\xE1rio n\xE3o encontrado" });
5040
- const userData = await this.onGetUserData(req.user);
5041
- if (!userData)
5042
- return res.status(400).json({ error: "Usu\xE1rio n\xE3o encontrado" });
5043
- return res.json(userData);
5044
- }
5045
- if (req.url.endsWith("/oauth-url") && this.oauth) {
5046
- const params = {
5047
- client_id: this.oauth.client_id,
5048
- redirect_uri: this.oauth.redirect_uri,
5049
- scope: this.oauth.scope,
5050
- response_type: "code",
5051
- response_mode: "query"
5052
- };
5053
- const url = `https://login.microsoftonline.com/${this.oauth.tenant_id}/oauth2/v2.0/authorize?${new URLSearchParams(params)}`;
5054
- return res.json({
5055
- url
5056
- });
5057
- }
5058
- return res.status(404).json({ error: "Route not found" });
5059
- }
5060
- async oauthSignInCallback(code) {
5061
- if (!this.oauth) throw new Error("OAUTH variables is not defined");
5062
- const body = {
5063
- client_id: this.oauth.client_id,
5064
- scope: this.oauth.scope,
5065
- code,
5066
- session_state: this.oauth.client_id,
5067
- redirect_uri: this.oauth.redirect_uri,
5068
- grant_type: "authorization_code",
5069
- client_secret: this.oauth.client_secret
5070
- };
5071
- const response = await fetch(
5072
- `https://login.microsoftonline.com/${this.oauth.tenant_id}/oauth2/v2.0/token`,
5073
- {
5074
- method: "POST",
5075
- body: new URLSearchParams(body),
5076
- headers: { "Content-Type": "application/x-www-form-urlencoded" }
5077
- }
5078
- );
5079
- const data = await response.json();
5080
- const decodedToken = _jsonwebtoken2.default.decode(data.access_token);
5081
- const email = decodedToken.upn;
5082
- const fullName = `${_optionalChain([decodedToken, 'optionalAccess', _100 => _100.given_name])} ${_optionalChain([decodedToken, 'optionalAccess', _101 => _101.family_name])}`;
5083
- return { decodedToken, email, fullName };
5084
- }
5085
- createOauthCallbackGetServerSideProps({
5086
- onSuccessDestination,
5087
- onFailedDestination
5088
- }) {
5089
- return async (ctx) => {
5090
- if (!this.oauth) throw new Error("Oauth env variables are not defined");
5091
- const code = ctx.query.code;
5092
- if (!code)
5093
- return {
5094
- redirect: {
5095
- permanent: false,
5096
- destination: onFailedDestination || "/"
5097
- }
5098
- };
5099
- try {
5100
- const { fullName, email } = await this.oauthSignInCallback(code);
5101
- const userExists = await this.onGetUserData(email);
5102
- if (!userExists && !this.oauth.onCreateUser)
5103
- throw new Error("User does not exists");
5104
- if (!userExists && this.oauth.onCreateUser) {
5105
- await this.oauth.onCreateUser({ fullname: fullName, email });
5106
- }
5107
- const { token, refreshToken } = await this.generateJwtAndRefreshToken(
5108
- email,
5109
- {}
5110
- );
5111
- setCookie(ctx, this.cookies.sessionToken, token, {
5112
- secure: true,
5113
- maxAge: 60 * 60 * 24 * 30,
5114
- // 30 days
5115
- path: "/"
5116
- });
5117
- setCookie(ctx, this.cookies.refreshToken, refreshToken, {
5118
- secure: true,
5119
- maxAge: 60 * 60 * 24 * 30,
5120
- // 30 days
5121
- path: "/",
5122
- httpOnly: true
5123
- });
5124
- return {
5125
- redirect: {
5126
- destination: onSuccessDestination,
5127
- permanent: false
5128
- }
5129
- };
5130
- } catch (error) {
5131
- return {
5132
- props: {
5133
- error: JSON.stringify(error)
5134
- }
5135
- };
5136
- }
5137
- };
5138
- }
5139
- };
5140
-
5141
- // src/hooks/useGrid.ts
5142
-
5143
-
5144
- // src/hooks/useFilter.ts
5145
-
5146
-
5147
-
5148
- // src/components/utils/getObjectValue.ts
5149
- function getObjectValue(obj) {
5150
- return (prop) => {
5151
- try {
5152
- return prop.split(".").reduce((o, k) => o[k], obj);
5153
- } catch (_) {
5154
- return void 0;
5155
- }
5156
- };
5157
- }
5158
-
5159
- // src/hooks/useFilter.ts
5160
- function useFilter(props = { defaultFilters: [] }) {
5161
- const [selectedFilters, setSelectedFilters] = _react.useState.call(void 0, () => {
5162
- const { defaultFilters } = props;
5163
- return defaultFilters || [];
5164
- });
5165
- const filterBy = _react.useCallback.call(void 0, (newFilter) => {
5166
- const propToCompare = _optionalChain([newFilter, 'optionalAccess', _102 => _102.id]) ? "id" : "prop";
5167
- function removeRepeatedFilters(filter) {
5168
- return filter[propToCompare] !== newFilter[propToCompare];
5169
- }
5170
- setSelectedFilters((filters) => [
5171
- ...filters.filter(removeRepeatedFilters),
5172
- newFilter
5173
- ]);
5174
- }, []);
5175
- const removeFilter = _react.useCallback.call(void 0,
5176
- (prop, isId) => {
5177
- const propToCompare = isId ? "id" : "prop";
5178
- setSelectedFilters(
5179
- selectedFilters.filter((filter) => filter[propToCompare] !== prop)
5180
- );
5181
- },
5182
- [selectedFilters]
5183
- );
5184
- function clearAllFilters() {
5185
- setSelectedFilters([]);
5186
- }
5187
- return {
5188
- filters: selectedFilters,
5189
- filterBy,
5190
- removeFilter,
5191
- createFilter,
5192
- clearAllFilters
5193
- };
5194
- }
5195
- function isDate(date) {
5196
- if (date instanceof Date) return true;
5197
- else if (String(date).endsWith("Z")) return true;
5198
- return false;
5199
- }
5200
- function compareFilter(item, filter) {
5201
- const itemValue = getObjectValue(item)(filter.prop);
5202
- switch (filter.compareType) {
5203
- case "equal":
5204
- return itemValue === filter.value;
5205
- case "notEqual":
5206
- return itemValue !== filter.value;
5207
- case "in":
5208
- return filter.value.includes(itemValue);
5209
- case "notIn":
5210
- return !filter.value.includes(itemValue);
5211
- case "valueIn":
5212
- return itemValue.includes(filter.value);
5213
- case "valueNotIn":
5214
- return !itemValue.includes(filter.value);
5215
- case "gte":
5216
- return isDate(itemValue) ? _moment2.default.call(void 0, String(itemValue)).isSameOrAfter(filter.value) : itemValue >= filter.value;
5217
- case "gt":
5218
- return isDate(itemValue) ? _moment2.default.call(void 0, String(itemValue)).isAfter(filter.value) : itemValue > filter.value;
5219
- case "lte":
5220
- return isDate(itemValue) ? _moment2.default.call(void 0, String(itemValue)).isSameOrBefore(filter.value) : itemValue <= filter.value;
5221
- case "lt":
5222
- return isDate(itemValue) ? _moment2.default.call(void 0, String(itemValue)).isBefore(filter.value) : itemValue < filter.value;
5223
- }
5224
- }
5225
- function createFilter(filters) {
5226
- function apply(item) {
5227
- const satisfiedFilters = filters.reduce((acc, filter) => {
5228
- if (compareFilter(item, filter)) acc += 1;
5229
- return acc;
5230
- }, 0);
5231
- return satisfiedFilters === filters.length;
5232
- }
5233
- return {
5234
- apply
5235
- };
5236
- }
5237
-
5238
- // src/helpers/sortHelper.ts
5239
- function SortHelper(...fields) {
5240
- return (a, b) => {
5241
- for (const field of fields) {
5242
- let direction = 1;
5243
- let key = field;
5244
- if (field.startsWith("-")) {
5245
- direction = -1;
5246
- key = field.slice(1);
5247
- }
5248
- const aVal = a[key];
5249
- const bVal = b[key];
5250
- if (aVal > bVal) return direction;
5251
- if (aVal < bVal) return -direction;
4958
+ function compare(key, objValue) {
4959
+ if (options.ignoredKeys) {
4960
+ const isIgnoredKey = options.ignoredKeys.includes(key);
4961
+ if (isIgnoredKey) return false;
5252
4962
  }
5253
- return 0;
4963
+ const value = getValue2(objValue);
4964
+ if (options.exact) return value === searchValue;
4965
+ if (options.ignoreAccentMark) return normalize(value).includes(normalize(searchValue));
4966
+ return value.includes(searchValue);
4967
+ }
4968
+ return (row) => {
4969
+ const match = searchKeysForValue(row, "", compare);
4970
+ return match;
5254
4971
  };
5255
4972
  }
5256
4973
 
5257
- // src/hooks/useGrid.ts
5258
- function useGrid({
4974
+ // src/hooks/useAsyncGrid.ts
4975
+
4976
+ function useAsyncGrid({
5259
4977
  columns,
5260
- filters,
4978
+ filters = [],
5261
4979
  search,
5262
4980
  rowsPerPageOptions = [30, 60, 100],
4981
+ onRequest,
4982
+ axiosInstance,
4983
+ url,
5263
4984
  defaultData: externalDefaultData,
5264
4985
  defaultCurrentPage,
5265
4986
  defaultSortedBy
@@ -5268,8 +4989,11 @@ function useGrid({
5268
4989
  const [sortedBy, setSortedBy] = _react.useState.call(void 0,
5269
4990
  defaultSortedBy || []
5270
4991
  );
4992
+ const [totalNumberOfItems, setTotalNumberOfItems] = _react.useState.call(void 0, 0);
4993
+ const [isLoading, setIsLoading] = _react.useState.call(void 0, false);
5271
4994
  const [currentPage, setCurrentPage] = _react.useState.call(void 0, defaultCurrentPage || 0);
5272
4995
  const [rowsPerPage, setRowsPerPage] = _react.useState.call(void 0, rowsPerPageOptions[0]);
4996
+ const totalNumberOfPages = Math.ceil(totalNumberOfItems / rowsPerPage) - 1;
5273
4997
  const toggleSortedDirection = _react.useCallback.call(void 0,
5274
4998
  (direction) => {
5275
4999
  if (direction === "asc") return "desc";
@@ -5277,417 +5001,697 @@ function useGrid({
5277
5001
  },
5278
5002
  []
5279
5003
  );
5280
- const clearSort = _react.useCallback.call(void 0, () => {
5281
- setSortedBy([]);
5282
- }, []);
5283
- const appendSort = _react.useCallback.call(void 0, (prop, direction) => {
5284
- setSortedBy((prev) => [...prev, { prop, direction }]);
5285
- }, []);
5286
5004
  const setSort = _react.useCallback.call(void 0, (prop, direction) => {
5287
- setSortedBy([{ prop, direction }]);
5005
+ setSortedBy((prev) => [...prev, { prop, direction }]);
5288
5006
  }, []);
5289
5007
  const onSortBy = _react.useCallback.call(void 0,
5290
- (prop) => {
5008
+ async (prop) => {
5291
5009
  if (!prop) return;
5010
+ let finalArr = [];
5292
5011
  const currentSorted = sortedBy.find((p) => p.prop === prop);
5293
5012
  if (currentSorted) {
5294
5013
  if (currentSorted.direction === "asc") {
5295
- setSortedBy((prev) => prev.filter((p) => p.prop !== prop));
5014
+ finalArr = sortedBy.filter((p) => p.prop !== prop);
5296
5015
  } else {
5297
- setSortedBy((prev) => {
5298
- const newArr = prev.map((p) => {
5299
- if (p.prop !== prop) return p;
5300
- return {
5301
- prop: p.prop,
5302
- direction: toggleSortedDirection(p.direction)
5303
- };
5304
- });
5305
- return [...newArr].slice(0);
5016
+ finalArr = sortedBy.map((p) => {
5017
+ if (p.prop !== prop) return p;
5018
+ return {
5019
+ prop: p.prop,
5020
+ direction: toggleSortedDirection(p.direction)
5021
+ };
5306
5022
  });
5307
5023
  }
5308
5024
  } else {
5309
- setSortedBy((prev) => [...prev, { prop, direction: "desc" }]);
5025
+ finalArr = [...sortedBy, { prop, direction: "desc" }];
5310
5026
  }
5027
+ await updateGridContent({
5028
+ page: currentPage,
5029
+ sortedBy: finalArr,
5030
+ rowsPerPage
5031
+ });
5311
5032
  },
5312
- [toggleSortedDirection, sortedBy]
5033
+ [sortedBy, toggleSortedDirection, rowsPerPage, currentPage]
5313
5034
  );
5314
5035
  const set = _react.useCallback.call(void 0, (data) => {
5315
5036
  setDefaultData(data);
5316
5037
  }, []);
5317
- const sortData = _react.useCallback.call(void 0,
5318
- (data) => {
5319
- if (sortedBy.length > 0) {
5320
- const symbolDir = {
5321
- asc: "",
5322
- desc: "-"
5038
+ const baseRequest = _react.useCallback.call(void 0,
5039
+ async ({
5040
+ page,
5041
+ search: search2,
5042
+ filters: filters2,
5043
+ sortedBy: sortedBy2,
5044
+ rowsPerPage: rowsPerPage2
5045
+ }) => {
5046
+ if (!axiosInstance) throw new Error("Axios instance not provided");
5047
+ try {
5048
+ const params = new URLSearchParams({
5049
+ page: String(page),
5050
+ rowsPerPage: String(rowsPerPage2),
5051
+ searchText: _optionalChain([search2, 'optionalAccess', _91 => _91.value]) || "",
5052
+ sort: sortedBy2.map(({ prop, direction }) => `${prop}:${direction}`).join(","),
5053
+ filters: filters2.map(
5054
+ (filter) => `${filter.prop}:${filter.compareType}:${filter.value}`
5055
+ ).join(",")
5056
+ });
5057
+ const pathWithParams = `${url}?${params.toString()}`;
5058
+ const { data } = await axiosInstance.get(pathWithParams);
5059
+ setTotalNumberOfItems(data.totalNumberOfItems);
5060
+ return data.rows;
5061
+ } catch (_) {
5062
+ return [];
5063
+ }
5064
+ },
5065
+ [axiosInstance, url]
5066
+ );
5067
+ const updateGridContent = _react.useCallback.call(void 0,
5068
+ async ({
5069
+ page,
5070
+ sortedBy: sortedBy2,
5071
+ rowsPerPage: rowsPerPage2
5072
+ }) => {
5073
+ setIsLoading(true);
5074
+ try {
5075
+ const props = {
5076
+ page,
5077
+ rowsPerPage: rowsPerPage2,
5078
+ sortedBy: sortedBy2,
5079
+ search,
5080
+ filters
5323
5081
  };
5324
- const formattedKeys = sortedBy.map(
5325
- ({ prop, direction }) => `${symbolDir[direction]}${prop}`
5082
+ const result = !onRequest ? await baseRequest(props) : await onRequest(props);
5083
+ setSortedBy(sortedBy2);
5084
+ setRowsPerPage(rowsPerPage2);
5085
+ set(result);
5086
+ setCurrentPage(page);
5087
+ } finally {
5088
+ setIsLoading(false);
5089
+ }
5090
+ },
5091
+ [set, search, filters, onRequest, baseRequest]
5092
+ );
5093
+ const onPageChange = _react.useCallback.call(void 0,
5094
+ (pageNumber) => {
5095
+ if (pageNumber < 0) return;
5096
+ if (pageNumber > totalNumberOfPages) return;
5097
+ updateGridContent({ page: pageNumber, sortedBy, rowsPerPage });
5098
+ },
5099
+ [updateGridContent, totalNumberOfPages, sortedBy, rowsPerPage]
5100
+ );
5101
+ const onChangeRowsPerPage = _react.useCallback.call(void 0,
5102
+ (rows) => {
5103
+ let totalNumberOfPages2 = Math.round(totalNumberOfItems / rows) - 1;
5104
+ totalNumberOfPages2 = totalNumberOfPages2 <= 0 ? 0 : 1;
5105
+ if (currentPage > totalNumberOfPages2)
5106
+ updateGridContent({
5107
+ page: totalNumberOfPages2,
5108
+ sortedBy,
5109
+ rowsPerPage: rows
5110
+ });
5111
+ updateGridContent({
5112
+ page: currentPage,
5113
+ sortedBy,
5114
+ rowsPerPage: rows
5115
+ });
5116
+ },
5117
+ [updateGridContent, totalNumberOfItems, sortedBy, currentPage]
5118
+ );
5119
+ const displayData = defaultData;
5120
+ _react.useEffect.call(void 0, () => {
5121
+ updateGridContent({ page: 0, sortedBy: [], rowsPerPage });
5122
+ }, [updateGridContent, rowsPerPage]);
5123
+ return {
5124
+ data: displayData,
5125
+ set,
5126
+ onSortBy,
5127
+ sortedBy,
5128
+ defaultData,
5129
+ columns,
5130
+ currentPage,
5131
+ totalNumberOfPages: totalNumberOfPages < 0 ? 0 : totalNumberOfPages,
5132
+ onPageChange,
5133
+ setRowsPerPage: onChangeRowsPerPage,
5134
+ rowsPerPageOptions,
5135
+ rowsPerPage,
5136
+ setSort,
5137
+ isLoading,
5138
+ setIsLoading
5139
+ };
5140
+ }
5141
+
5142
+ // src/hooks/useEvent.ts
5143
+
5144
+ function useEvent(event, handler, passive = false) {
5145
+ _react.useEffect.call(void 0, () => {
5146
+ window.addEventListener(event, handler, passive);
5147
+ return function cleanup() {
5148
+ window.removeEventListener(event, handler);
5149
+ };
5150
+ });
5151
+ }
5152
+
5153
+ // src/hooks/useLoading.ts
5154
+
5155
+ function useLoading() {
5156
+ const [state, setState] = _react.useState.call(void 0, []);
5157
+ const isLoading = _react.useCallback.call(void 0, (prop) => state.includes(prop), [state]);
5158
+ const setLoading = _react.useCallback.call(void 0, (prop, remove) => {
5159
+ if (remove)
5160
+ setState((prevState) => prevState.filter((state2) => state2 !== prop));
5161
+ else setState((prevState) => [...prevState, prop]);
5162
+ }, []);
5163
+ return { isLoading, setLoading };
5164
+ }
5165
+
5166
+ // src/hooks/useAlert.ts
5167
+
5168
+ var useAlert = () => {
5169
+ return _react.useContext.call(void 0, AlertContext);
5170
+ };
5171
+
5172
+ // src/hooks/useFormHelper.ts
5173
+
5174
+ function useFormHelper() {
5175
+ const alertProps = useAlert();
5176
+ const loadingProps = useLoading();
5177
+ const { api, formatErrorMessage } = _react.useContext.call(void 0, FormHelperContext);
5178
+ const { createAlert } = alertProps;
5179
+ const { setLoading } = loadingProps;
5180
+ const sourceRef = _react.useRef.call(void 0, new AbortController());
5181
+ const onSubmitWrapper = _react.useCallback.call(void 0,
5182
+ (fn, { name }) => {
5183
+ return async (fields, methods) => {
5184
+ const LOADING_NAME = name;
5185
+ setLoading(LOADING_NAME);
5186
+ try {
5187
+ await fn(fields, methods);
5188
+ } catch (error) {
5189
+ errorHandler(error, methods.setErrors);
5190
+ } finally {
5191
+ setLoading(LOADING_NAME, true);
5192
+ }
5193
+ };
5194
+ },
5195
+ [setLoading]
5196
+ );
5197
+ const onRequestWrapper = _react.useCallback.call(void 0,
5198
+ (fn, { name }) => {
5199
+ return async (...params) => {
5200
+ const LOADING_NAME = name;
5201
+ setLoading(LOADING_NAME);
5202
+ api.interceptors.request.use(
5203
+ (config) => {
5204
+ if (!config.signal && sourceRef.current && config.method === "get") {
5205
+ config.signal = sourceRef.current.signal;
5206
+ }
5207
+ return config;
5208
+ },
5209
+ (error) => {
5210
+ return Promise.reject(error);
5211
+ }
5326
5212
  );
5327
- return data.sort(SortHelper(...formattedKeys));
5328
- } else return data;
5213
+ try {
5214
+ const response = await fn(...params);
5215
+ return response;
5216
+ } catch (error) {
5217
+ errorHandler(error);
5218
+ } finally {
5219
+ setLoading(LOADING_NAME, true);
5220
+ }
5221
+ };
5329
5222
  },
5330
- [sortedBy]
5223
+ [setLoading, api]
5331
5224
  );
5332
- const onPageChange = (pageNumber) => {
5333
- if (pageNumber < 0) return;
5334
- if (pageNumber > totalNumberOfPages) return;
5335
- setCurrentPage(pageNumber);
5336
- };
5337
- const onChangeRowsPerPage = _react.useCallback.call(void 0,
5338
- (rows) => {
5339
- let totalNumberOfPages2 = Math.round(filteredData.length / rows) - 1;
5340
- totalNumberOfPages2 = totalNumberOfPages2 <= 0 ? 0 : 1;
5341
- if (currentPage > totalNumberOfPages2) setCurrentPage(totalNumberOfPages2);
5342
- setRowsPerPage(rows);
5225
+ const errorHandler = _react.useCallback.call(void 0,
5226
+ (error, callback) => {
5227
+ if (_optionalChain([error, 'optionalAccess', _92 => _92.message]) === "cancel.navigation") return;
5228
+ if (callback) {
5229
+ if (error.response.data.code === "invalid.body") {
5230
+ const errors = error.response.data.details.issues;
5231
+ const currentErrors = errors.reduce((acc, item) => {
5232
+ acc[item.path.join(".")] = item.message;
5233
+ return acc;
5234
+ }, {});
5235
+ callback(currentErrors);
5236
+ }
5237
+ }
5238
+ createAlert(formatErrorMessage(error), "error");
5343
5239
  },
5344
- [currentPage]
5240
+ [formatErrorMessage, createAlert]
5345
5241
  );
5346
- const orderedData = _react.useMemo.call(void 0, () => {
5347
- if (sortedBy.length === 0) return defaultData;
5348
- const newData = defaultData.slice(0);
5349
- const sortedData = sortData(newData);
5350
- return sortedData;
5351
- }, [defaultData, sortData, sortedBy]);
5352
- const filteredData = _react.useMemo.call(void 0, () => {
5353
- let newData = orderedData.slice(0);
5354
- if (search && search.value !== "") {
5355
- const searchBy = createSearch(search);
5356
- newData = newData.filter(searchBy);
5357
- }
5358
- if (!filters) return newData;
5359
- const newFilter = createFilter(filters);
5360
- return newData.filter(newFilter.apply);
5361
- }, [orderedData, search, filters]);
5362
- const paginatedData = _react.useMemo.call(void 0, () => {
5363
- const startPage = currentPage * rowsPerPage;
5364
- const endPage = startPage + rowsPerPage;
5365
- return filteredData.slice(startPage, endPage);
5366
- }, [currentPage, rowsPerPage, filteredData]);
5367
- const totalNumberOfPages = Math.ceil(filteredData.length / rowsPerPage) - 1;
5368
5242
  _react.useEffect.call(void 0, () => {
5369
- if (externalDefaultData) setDefaultData(externalDefaultData);
5370
- }, [externalDefaultData]);
5243
+ return () => {
5244
+ sourceRef.current.abort();
5245
+ sourceRef.current = new AbortController();
5246
+ };
5247
+ }, []);
5371
5248
  return {
5372
- data: paginatedData,
5373
- orderedData,
5374
- filteredData,
5375
- defaultData,
5376
- sortedBy,
5377
- columns,
5378
- currentPage,
5379
- totalNumberOfPages: totalNumberOfPages < 0 ? 0 : totalNumberOfPages,
5380
- rowsPerPageOptions,
5381
- rowsPerPage,
5382
- set,
5383
- onSortBy,
5384
- onPageChange,
5385
- setRowsPerPage: onChangeRowsPerPage,
5386
- appendSort,
5387
- setSort,
5388
- clearSort
5249
+ ...alertProps,
5250
+ ...loadingProps,
5251
+ onSubmitWrapper,
5252
+ onRequestWrapper
5389
5253
  };
5390
5254
  }
5391
- var isNumberOrString = (value) => ["number", "string"].includes(typeof value);
5392
- var concatenateKey = (key, prev) => {
5393
- if (!prev) return key;
5394
- return `${prev}.${key}`;
5395
- };
5396
- function searchKeysForValue(row, accKey, compare) {
5397
- if (typeof row === "undefined") return false;
5398
- const rowKeys = Object.keys(row);
5399
- let match = false;
5400
- for (const key of rowKeys) {
5401
- if (match) break;
5402
- const objValue = row[key];
5403
- if (!objValue) continue;
5404
- const concatenatedKey = concatenateKey(key, accKey);
5405
- if (Array.isArray(objValue)) {
5406
- match = objValue.some(
5407
- (obj) => isNumberOrString(obj) ? compare(concatenatedKey, obj) : searchKeysForValue(obj, concatenatedKey, compare)
5408
- );
5409
- continue;
5410
- }
5411
- if (typeof objValue === "object") {
5412
- match = searchKeysForValue(objValue, concatenatedKey, compare);
5413
- continue;
5255
+
5256
+ // src/contexts/AuthContext.tsx
5257
+ function createAuthContext() {
5258
+ return _react.createContext.call(void 0, {});
5259
+ }
5260
+ function CreateAuthProvider({
5261
+ api,
5262
+ children,
5263
+ sessionTokenName,
5264
+ Provider
5265
+ }) {
5266
+ const [user, setUser] = _react.useState.call(void 0, );
5267
+ const [status, setStatus] = _react.useState.call(void 0, "unauthenticated");
5268
+ const { createAlert } = useAlert();
5269
+ const signIn = _react.useCallback.call(void 0,
5270
+ async ({ email, password }) => {
5271
+ setStatus("loading");
5272
+ try {
5273
+ const response = await api.post("/auth/login", {
5274
+ email,
5275
+ password
5276
+ });
5277
+ const { token } = response.data;
5278
+ api.defaults.headers.common.Authorization = `Bearer ${token}`;
5279
+ const { data } = await api.get("/auth/me");
5280
+ setUser(data);
5281
+ setStatus("autenticated");
5282
+ return true;
5283
+ } catch (error) {
5284
+ createAlert(_optionalChain([error, 'optionalAccess', _93 => _93.response, 'optionalAccess', _94 => _94.data, 'optionalAccess', _95 => _95.error]), "error");
5285
+ setStatus("unauthenticated");
5286
+ throw error;
5287
+ }
5288
+ },
5289
+ [createAlert, api]
5290
+ );
5291
+ const ClientSignOut = _react.useCallback.call(void 0, async () => {
5292
+ await api.get("/auth/logout");
5293
+ setUser(void 0);
5294
+ }, [api]);
5295
+ _react.useEffect.call(void 0, () => {
5296
+ const token = parseCookies()[sessionTokenName];
5297
+ if (token) {
5298
+ setStatus("loading");
5299
+ api.get("/auth/me").then((response) => {
5300
+ setStatus("autenticated");
5301
+ setUser(response.data);
5302
+ }).catch(() => {
5303
+ setStatus("unauthenticated");
5304
+ });
5414
5305
  }
5415
- match = compare(concatenatedKey, row[key]);
5416
- }
5417
- return match;
5306
+ }, [api, sessionTokenName]);
5307
+ return /* @__PURE__ */ React2.default.createElement(
5308
+ Provider,
5309
+ {
5310
+ value: {
5311
+ user,
5312
+ signOut: ClientSignOut,
5313
+ signIn,
5314
+ status
5315
+ }
5316
+ },
5317
+ children
5318
+ );
5418
5319
  }
5419
- var normalize = (str) => str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
5420
- function createSearch(options) {
5421
- const searchValue = options.caseSensitive ? options.value : String(options.value).toLowerCase();
5422
- function getValue2(value) {
5423
- if (options.caseSensitive) return String(value);
5424
- return String(value).toLowerCase();
5320
+
5321
+ // src/errors/HttpError.ts
5322
+ var HttpError = class extends Error {
5323
+ constructor(status, message) {
5324
+ super(message);
5325
+ this.message = message;
5326
+ this.stack = `HttpError: ${message}`;
5327
+ this.status = status;
5328
+ }
5329
+ };
5330
+
5331
+ // src/errors/DomainError.ts
5332
+ var DomainError = class extends Error {
5333
+ constructor(message) {
5334
+ super(message);
5335
+ this.message = message;
5336
+ this.stack = `DomainError: ${message}`;
5337
+ }
5338
+ };
5339
+
5340
+ // src/helpers/apiHelper/index.ts
5341
+ var VALID_METHODS = [
5342
+ "GET",
5343
+ "HEAD",
5344
+ "POST",
5345
+ "PUT",
5346
+ "DELETE",
5347
+ "CONNECT",
5348
+ "OPTIONS",
5349
+ "TRACE",
5350
+ "PATCH"
5351
+ ];
5352
+ var _ApiHelper = class _ApiHelper {
5353
+ async onFinally(_req, _res) {
5354
+ }
5355
+ async onError(_req, _res, _error) {
5356
+ }
5357
+ constructor(props) {
5358
+ this.public = _nullishCoalesce(_optionalChain([props, 'optionalAccess', _96 => _96.public]), () => ( false));
5359
+ this.middlewares = (_optionalChain([props, 'optionalAccess', _97 => _97.middlewares]) || []).reverse();
5360
+ this.onFinally = _optionalChain([props, 'optionalAccess', _98 => _98.onFinally]) || (async () => {
5361
+ });
5362
+ this.onError = _optionalChain([props, 'optionalAccess', _99 => _99.onError]) || (async () => {
5363
+ });
5364
+ }
5365
+ createMethods(methods) {
5366
+ return async (req, res) => {
5367
+ const currentMethod = methods[req.method] || methods.ALL;
5368
+ const options = { public: this.public };
5369
+ if (req.method === "OPTIONS") return res.status(200).end();
5370
+ try {
5371
+ if (!VALID_METHODS.includes(req.method))
5372
+ throw new HttpError(405, "M\xE9todo inv\xE1lido");
5373
+ if (!currentMethod) throw new HttpError(500, "M\xE9todo n\xE3o encontrado");
5374
+ const methodWithMiddlewares = this.middlewares.reduce(
5375
+ (acc, fn) => fn(acc, options),
5376
+ currentMethod
5377
+ );
5378
+ return await methodWithMiddlewares(req, res, options);
5379
+ } catch (error) {
5380
+ if (error instanceof DomainError) return res.status(400).json(error.message);
5381
+ if (error instanceof HttpError) return res.status(error.status).json(error.message);
5382
+ this.onError(req, res, error);
5383
+ throw error;
5384
+ } finally {
5385
+ await this.onFinally(req, res);
5386
+ }
5387
+ };
5388
+ }
5389
+ buildFactory(factory) {
5390
+ const options = {
5391
+ public: this.public
5392
+ };
5393
+ return async (req, res) => {
5394
+ const methods = factory(req, res);
5395
+ const handler = methods[req.method];
5396
+ if (!handler) throw new HttpError(405, "M\xE9todo inv\xE1lido");
5397
+ const methodWithMiddlewares = this.middlewares.reduce((acc, fn) => {
5398
+ return fn(acc, options);
5399
+ }, handler);
5400
+ return await methodWithMiddlewares(req, res, options);
5401
+ };
5402
+ }
5403
+ static build(factory, options) {
5404
+ const helper = new _ApiHelper({
5405
+ ...options
5406
+ }).buildFactory(factory);
5407
+ return helper;
5425
5408
  }
5426
- function compare(key, objValue) {
5427
- if (options.ignoredKeys) {
5428
- const isIgnoredKey = options.ignoredKeys.includes(key);
5429
- if (isIgnoredKey) return false;
5409
+ static parse(body, parser) {
5410
+ try {
5411
+ const object = parser.parse(body);
5412
+ return object;
5413
+ } catch (error) {
5414
+ throw new HttpError(400, {
5415
+ code: "invalid.body",
5416
+ error: "Dados inv\xE1lidos",
5417
+ details: error
5418
+ });
5430
5419
  }
5431
- const value = getValue2(objValue);
5432
- if (options.exact) return value === searchValue;
5433
- if (options.ignoreAccentMark) return normalize(value).includes(normalize(searchValue));
5434
- return value.includes(searchValue);
5435
5420
  }
5436
- return (row) => {
5437
- const match = searchKeysForValue(row, "", compare);
5438
- return match;
5439
- };
5440
- }
5421
+ /** @deprecated Use {@Link ApiHelper.build} instead. */
5422
+ static create({ onFinally }) {
5423
+ return new _ApiHelper({
5424
+ onFinally
5425
+ });
5426
+ }
5427
+ };
5428
+ /** @deprecated Use {@link ApiHelper.parser} instead. */
5429
+ _ApiHelper.parserErrorWrapper = _ApiHelper.parse;
5430
+ var ApiHelper = _ApiHelper;
5441
5431
 
5442
- // src/hooks/useAsyncGrid.ts
5432
+ // src/helpers/authHelper.ts
5443
5433
 
5444
- function useAsyncGrid({
5445
- columns,
5446
- filters = [],
5447
- search,
5448
- rowsPerPageOptions = [30, 60, 100],
5449
- onRequest,
5450
- axiosInstance,
5451
- url,
5452
- defaultData: externalDefaultData,
5453
- defaultCurrentPage,
5454
- defaultSortedBy
5434
+ var _jsonwebtoken = require('jsonwebtoken'); var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);
5435
+ var _crypto = require('crypto');
5436
+ function decodeSessionToken({
5437
+ req,
5438
+ res,
5439
+ sessionTokenName,
5440
+ validate
5455
5441
  }) {
5456
- const [defaultData, setDefaultData] = _react.useState.call(void 0, externalDefaultData || []);
5457
- const [sortedBy, setSortedBy] = _react.useState.call(void 0,
5458
- defaultSortedBy || []
5459
- );
5460
- const [totalNumberOfItems, setTotalNumberOfItems] = _react.useState.call(void 0, 0);
5461
- const [isLoading, setIsLoading] = _react.useState.call(void 0, false);
5462
- const [currentPage, setCurrentPage] = _react.useState.call(void 0, defaultCurrentPage || 0);
5463
- const [rowsPerPage, setRowsPerPage] = _react.useState.call(void 0, rowsPerPageOptions[0]);
5464
- const totalNumberOfPages = Math.ceil(totalNumberOfItems / rowsPerPage) - 1;
5465
- const toggleSortedDirection = _react.useCallback.call(void 0,
5466
- (direction) => {
5467
- if (direction === "asc") return "desc";
5468
- return "asc";
5469
- },
5470
- []
5471
- );
5472
- const setSort = _react.useCallback.call(void 0, (prop, direction) => {
5473
- setSortedBy((prev) => [...prev, { prop, direction }]);
5474
- }, []);
5475
- const onSortBy = _react.useCallback.call(void 0,
5476
- async (prop) => {
5477
- if (!prop) return;
5478
- let finalArr = [];
5479
- const currentSorted = sortedBy.find((p) => p.prop === prop);
5480
- if (currentSorted) {
5481
- if (currentSorted.direction === "asc") {
5482
- finalArr = sortedBy.filter((p) => p.prop !== prop);
5483
- } else {
5484
- finalArr = sortedBy.map((p) => {
5485
- if (p.prop !== prop) return p;
5486
- return {
5487
- prop: p.prop,
5488
- direction: toggleSortedDirection(p.direction)
5489
- };
5490
- });
5491
- }
5492
- } else {
5493
- finalArr = [...sortedBy, { prop, direction: "desc" }];
5494
- }
5495
- await updateGridContent({
5496
- page: currentPage,
5497
- sortedBy: finalArr,
5498
- rowsPerPage
5442
+ const token = _optionalChain([req, 'access', _100 => _100.headers, 'access', _101 => _101.authorization, 'optionalAccess', _102 => _102.split, 'call', _103 => _103(" "), 'access', _104 => _104[1]]) || req.cookies[sessionTokenName];
5443
+ if (!token) {
5444
+ res.status(401).json({ error: "Token inv\xE1lido", code: "token.invalid" });
5445
+ return true;
5446
+ }
5447
+ const jwtDecode = (token2) => {
5448
+ if (validate) {
5449
+ return _jsonwebtoken2.default.verify(token2, process.env.JWT_SECRET);
5450
+ }
5451
+ return _jsonwebtoken2.default.decode(token2);
5452
+ };
5453
+ try {
5454
+ const decoded = jwtDecode(token);
5455
+ req.user = decoded.sub;
5456
+ } catch (_) {
5457
+ res.status(401).json({ error: "Token inv\xE1lido", code: "token.expired" });
5458
+ return true;
5459
+ }
5460
+ }
5461
+ var AuthHelper = class {
5462
+ constructor({
5463
+ cookies,
5464
+ oauth,
5465
+ tokenExpTimeInSeconds,
5466
+ onLogin,
5467
+ onValidateRefreshToken,
5468
+ onInvalidateRefreshToken,
5469
+ onCreateRefreshToken,
5470
+ onGetUserData
5471
+ }) {
5472
+ this.generateJwtAndRefreshToken = async (userId, payload = {}) => {
5473
+ const token = _jsonwebtoken2.default.sign(payload, process.env.JWT_SECRET, {
5474
+ subject: String(userId),
5475
+ expiresIn: this.tokenExpTimeInSeconds || 60 * 15
5476
+ // 15 minutos
5499
5477
  });
5500
- },
5501
- [sortedBy, toggleSortedDirection, rowsPerPage, currentPage]
5502
- );
5503
- const set = _react.useCallback.call(void 0, (data) => {
5504
- setDefaultData(data);
5505
- }, []);
5506
- const baseRequest = _react.useCallback.call(void 0,
5507
- async ({
5508
- page,
5509
- search: search2,
5510
- filters: filters2,
5511
- sortedBy: sortedBy2,
5512
- rowsPerPage: rowsPerPage2
5513
- }) => {
5514
- if (!axiosInstance) throw new Error("Axios instance not provided");
5515
- try {
5516
- const params = new URLSearchParams({
5517
- page: String(page),
5518
- rowsPerPage: String(rowsPerPage2),
5519
- searchText: _optionalChain([search2, 'optionalAccess', _103 => _103.value]) || "",
5520
- sort: sortedBy2.map(({ prop, direction }) => `${prop}:${direction}`).join(","),
5521
- filters: filters2.map(
5522
- (filter) => `${filter.prop}:${filter.compareType}:${filter.value}`
5523
- ).join(",")
5478
+ const uniqueToken = _crypto.randomUUID.call(void 0, );
5479
+ await this.onCreateRefreshToken(userId, uniqueToken);
5480
+ return {
5481
+ token,
5482
+ refreshToken: uniqueToken
5483
+ };
5484
+ };
5485
+ this.invalidateCookies = (res) => {
5486
+ return res.setHeader("Set-Cookie", [
5487
+ _cookie.serialize.call(void 0, this.cookies.sessionToken, "", {
5488
+ maxAge: -1,
5489
+ path: "/"
5490
+ }),
5491
+ _cookie.serialize.call(void 0, this.cookies.refreshToken, "", {
5492
+ maxAge: -1,
5493
+ path: "/"
5494
+ })
5495
+ ]);
5496
+ };
5497
+ this.cookies = cookies;
5498
+ this.oauth = oauth;
5499
+ this.tokenExpTimeInSeconds = tokenExpTimeInSeconds;
5500
+ this.onLogin = onLogin;
5501
+ this.onValidateRefreshToken = onValidateRefreshToken;
5502
+ this.onInvalidateRefreshToken = onInvalidateRefreshToken;
5503
+ this.onCreateRefreshToken = onCreateRefreshToken;
5504
+ this.onGetUserData = onGetUserData;
5505
+ }
5506
+ async handler(req, res) {
5507
+ if (!req.url) return res.status(400).json({ error: "url not sent" });
5508
+ if (req.url.endsWith("/login")) {
5509
+ const loginResult = await this.onLogin(req.body);
5510
+ if (loginResult.status === "success") {
5511
+ const { refreshToken, token } = await this.generateJwtAndRefreshToken(
5512
+ loginResult.userId,
5513
+ {}
5514
+ );
5515
+ setCookie({ res }, this.cookies.sessionToken, token, {
5516
+ secure: true,
5517
+ maxAge: 60 * 60 * 24 * 30,
5518
+ // 30 days
5519
+ path: "/",
5520
+ sameSite: true
5524
5521
  });
5525
- const pathWithParams = `${url}?${params.toString()}`;
5526
- const { data } = await axiosInstance.get(pathWithParams);
5527
- setTotalNumberOfItems(data.totalNumberOfItems);
5528
- return data.rows;
5529
- } catch (_) {
5530
- return [];
5531
- }
5532
- },
5533
- [axiosInstance, url]
5534
- );
5535
- const updateGridContent = _react.useCallback.call(void 0,
5536
- async ({
5537
- page,
5538
- sortedBy: sortedBy2,
5539
- rowsPerPage: rowsPerPage2
5540
- }) => {
5541
- setIsLoading(true);
5542
- try {
5543
- const props = {
5544
- page,
5545
- rowsPerPage: rowsPerPage2,
5546
- sortedBy: sortedBy2,
5547
- search,
5548
- filters
5549
- };
5550
- const result = !onRequest ? await baseRequest(props) : await onRequest(props);
5551
- setSortedBy(sortedBy2);
5552
- setRowsPerPage(rowsPerPage2);
5553
- set(result);
5554
- setCurrentPage(page);
5555
- } finally {
5556
- setIsLoading(false);
5522
+ setCookie({ res }, this.cookies.refreshToken, refreshToken, {
5523
+ secure: true,
5524
+ maxAge: 60 * 60 * 24 * 30,
5525
+ // 30 days
5526
+ path: "/",
5527
+ sameSite: true,
5528
+ httpOnly: true
5529
+ });
5530
+ return res.json({ token, refreshToken });
5557
5531
  }
5558
- },
5559
- [set, search, filters, onRequest, baseRequest]
5560
- );
5561
- const onPageChange = _react.useCallback.call(void 0,
5562
- (pageNumber) => {
5563
- if (pageNumber < 0) return;
5564
- if (pageNumber > totalNumberOfPages) return;
5565
- updateGridContent({ page: pageNumber, sortedBy, rowsPerPage });
5566
- },
5567
- [updateGridContent, totalNumberOfPages, sortedBy, rowsPerPage]
5568
- );
5569
- const onChangeRowsPerPage = _react.useCallback.call(void 0,
5570
- (rows) => {
5571
- let totalNumberOfPages2 = Math.round(totalNumberOfItems / rows) - 1;
5572
- totalNumberOfPages2 = totalNumberOfPages2 <= 0 ? 0 : 1;
5573
- if (currentPage > totalNumberOfPages2)
5574
- updateGridContent({
5575
- page: totalNumberOfPages2,
5576
- sortedBy,
5577
- rowsPerPage: rows
5532
+ throw new HttpError(400, loginResult.response);
5533
+ }
5534
+ if (req.url.endsWith("/logout")) {
5535
+ this.invalidateCookies(res).end();
5536
+ }
5537
+ if (req.url.endsWith("/refresh")) {
5538
+ const error = decodeSessionToken({
5539
+ req,
5540
+ res,
5541
+ sessionTokenName: this.cookies.sessionToken,
5542
+ validate: false
5543
+ });
5544
+ if (error) return;
5545
+ const userId = String(req.user);
5546
+ const refreshToken = parseCookies({ req })[this.cookies.refreshToken];
5547
+ if (!refreshToken) {
5548
+ this.invalidateCookies(res);
5549
+ return res.status(400).json({
5550
+ error: "Refresh Token inv\xE1lido"
5578
5551
  });
5579
- updateGridContent({
5580
- page: currentPage,
5581
- sortedBy,
5582
- rowsPerPage: rows
5552
+ }
5553
+ const isValidRefreshToken = await this.onValidateRefreshToken(
5554
+ userId,
5555
+ refreshToken
5556
+ );
5557
+ if (!isValidRefreshToken) {
5558
+ this.invalidateCookies(res);
5559
+ return res.status(400).json({
5560
+ error: "Refresh Token inv\xE1lido"
5561
+ });
5562
+ }
5563
+ await this.onInvalidateRefreshToken(userId, refreshToken);
5564
+ const { token, refreshToken: newRefreshToken } = await this.generateJwtAndRefreshToken(userId, {});
5565
+ setCookie({ res }, this.cookies.sessionToken, token, {
5566
+ secure: true,
5567
+ maxAge: 60 * 60 * 24 * 30,
5568
+ // 30 days
5569
+ path: "/",
5570
+ sameSite: true
5583
5571
  });
5584
- },
5585
- [updateGridContent, totalNumberOfItems, sortedBy, currentPage]
5586
- );
5587
- const displayData = defaultData;
5588
- _react.useEffect.call(void 0, () => {
5589
- updateGridContent({ page: 0, sortedBy: [], rowsPerPage });
5590
- }, [updateGridContent, rowsPerPage]);
5591
- return {
5592
- data: displayData,
5593
- set,
5594
- onSortBy,
5595
- sortedBy,
5596
- defaultData,
5597
- columns,
5598
- currentPage,
5599
- totalNumberOfPages: totalNumberOfPages < 0 ? 0 : totalNumberOfPages,
5600
- onPageChange,
5601
- setRowsPerPage: onChangeRowsPerPage,
5602
- rowsPerPageOptions,
5603
- rowsPerPage,
5604
- setSort,
5605
- isLoading,
5606
- setIsLoading
5607
- };
5608
- }
5609
-
5610
- // src/hooks/useEvent.ts
5611
-
5612
- function useEvent(event, handler, passive = false) {
5613
- _react.useEffect.call(void 0, () => {
5614
- window.addEventListener(event, handler, passive);
5615
- return function cleanup() {
5616
- window.removeEventListener(event, handler);
5572
+ setCookie({ res }, this.cookies.refreshToken, newRefreshToken, {
5573
+ secure: true,
5574
+ maxAge: 60 * 60 * 24 * 30,
5575
+ // 30 days
5576
+ path: "/",
5577
+ sameSite: true,
5578
+ httpOnly: true
5579
+ });
5580
+ return res.json({
5581
+ token,
5582
+ refreshToken: newRefreshToken
5583
+ });
5584
+ }
5585
+ if (req.url.endsWith("/me")) {
5586
+ const error = decodeSessionToken({
5587
+ req,
5588
+ res,
5589
+ sessionTokenName: this.cookies.sessionToken,
5590
+ validate: true
5591
+ });
5592
+ if (error) return;
5593
+ if (!req.user)
5594
+ return res.status(400).json({ error: "Usu\xE1rio n\xE3o encontrado" });
5595
+ const userData = await this.onGetUserData(req.user);
5596
+ if (!userData)
5597
+ return res.status(400).json({ error: "Usu\xE1rio n\xE3o encontrado" });
5598
+ return res.json(userData);
5599
+ }
5600
+ if (req.url.endsWith("/oauth-url") && this.oauth) {
5601
+ const params = {
5602
+ client_id: this.oauth.client_id,
5603
+ redirect_uri: this.oauth.redirect_uri,
5604
+ scope: this.oauth.scope,
5605
+ response_type: "code",
5606
+ response_mode: "query"
5607
+ };
5608
+ const url = `https://login.microsoftonline.com/${this.oauth.tenant_id}/oauth2/v2.0/authorize?${new URLSearchParams(params)}`;
5609
+ return res.json({
5610
+ url
5611
+ });
5612
+ }
5613
+ return res.status(404).json({ error: "Route not found" });
5614
+ }
5615
+ async oauthSignInCallback(code) {
5616
+ if (!this.oauth) throw new Error("OAUTH variables is not defined");
5617
+ const body = {
5618
+ client_id: this.oauth.client_id,
5619
+ scope: this.oauth.scope,
5620
+ code,
5621
+ session_state: this.oauth.client_id,
5622
+ redirect_uri: this.oauth.redirect_uri,
5623
+ grant_type: "authorization_code",
5624
+ client_secret: this.oauth.client_secret
5617
5625
  };
5618
- });
5619
- }
5620
-
5621
- // src/contexts/AuthContext.tsx
5622
-
5623
-
5624
-
5625
-
5626
-
5627
-
5628
- function createAuthContext() {
5629
- return _react.createContext.call(void 0, {});
5630
- }
5631
- function CreateAuthProvider({
5632
- api,
5633
- children,
5634
- sessionTokenName,
5635
- Provider
5636
- }) {
5637
- const [user, setUser] = _react.useState.call(void 0, );
5638
- const [status, setStatus] = _react.useState.call(void 0, "unauthenticated");
5639
- const { createAlert } = useAlert();
5640
- const signIn = _react.useCallback.call(void 0,
5641
- async ({ email, password }) => {
5642
- setStatus("loading");
5626
+ const response = await fetch(
5627
+ `https://login.microsoftonline.com/${this.oauth.tenant_id}/oauth2/v2.0/token`,
5628
+ {
5629
+ method: "POST",
5630
+ body: new URLSearchParams(body),
5631
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
5632
+ }
5633
+ );
5634
+ const data = await response.json();
5635
+ const decodedToken = _jsonwebtoken2.default.decode(data.access_token);
5636
+ const email = decodedToken.upn;
5637
+ const fullName = `${_optionalChain([decodedToken, 'optionalAccess', _105 => _105.given_name])} ${_optionalChain([decodedToken, 'optionalAccess', _106 => _106.family_name])}`;
5638
+ return { decodedToken, email, fullName };
5639
+ }
5640
+ createOauthCallbackGetServerSideProps({
5641
+ onSuccessDestination,
5642
+ onFailedDestination
5643
+ }) {
5644
+ return async (ctx) => {
5645
+ if (!this.oauth) throw new Error("Oauth env variables are not defined");
5646
+ const code = ctx.query.code;
5647
+ if (!code)
5648
+ return {
5649
+ redirect: {
5650
+ permanent: false,
5651
+ destination: onFailedDestination || "/"
5652
+ }
5653
+ };
5643
5654
  try {
5644
- const response = await api.post("/auth/login", {
5655
+ const { fullName, email } = await this.oauthSignInCallback(code);
5656
+ const userExists = await this.onGetUserData(email);
5657
+ if (!userExists && !this.oauth.onCreateUser)
5658
+ throw new Error("User does not exists");
5659
+ if (!userExists && this.oauth.onCreateUser) {
5660
+ await this.oauth.onCreateUser({ fullname: fullName, email });
5661
+ }
5662
+ const { token, refreshToken } = await this.generateJwtAndRefreshToken(
5645
5663
  email,
5646
- password
5664
+ {}
5665
+ );
5666
+ setCookie(ctx, this.cookies.sessionToken, token, {
5667
+ secure: true,
5668
+ maxAge: 60 * 60 * 24 * 30,
5669
+ // 30 days
5670
+ path: "/"
5647
5671
  });
5648
- const { token } = response.data;
5649
- api.defaults.headers.common.Authorization = `Bearer ${token}`;
5650
- const { data } = await api.get("/auth/me");
5651
- setUser(data);
5652
- setStatus("autenticated");
5653
- return true;
5672
+ setCookie(ctx, this.cookies.refreshToken, refreshToken, {
5673
+ secure: true,
5674
+ maxAge: 60 * 60 * 24 * 30,
5675
+ // 30 days
5676
+ path: "/",
5677
+ httpOnly: true
5678
+ });
5679
+ return {
5680
+ redirect: {
5681
+ destination: onSuccessDestination,
5682
+ permanent: false
5683
+ }
5684
+ };
5654
5685
  } catch (error) {
5655
- createAlert(_optionalChain([error, 'optionalAccess', _104 => _104.response, 'optionalAccess', _105 => _105.data, 'optionalAccess', _106 => _106.error]), "error");
5656
- setStatus("unauthenticated");
5657
- throw error;
5658
- }
5659
- },
5660
- [createAlert, api]
5661
- );
5662
- const ClientSignOut = _react.useCallback.call(void 0, async () => {
5663
- await api.get("/auth/logout");
5664
- setUser(void 0);
5665
- }, [api]);
5666
- _react.useEffect.call(void 0, () => {
5667
- const token = parseCookies()[sessionTokenName];
5668
- if (token) {
5669
- setStatus("loading");
5670
- api.get("/auth/me").then((response) => {
5671
- setStatus("autenticated");
5672
- setUser(response.data);
5673
- }).catch(() => {
5674
- setStatus("unauthenticated");
5675
- });
5676
- }
5677
- }, [api, sessionTokenName]);
5678
- return /* @__PURE__ */ React2.default.createElement(
5679
- Provider,
5680
- {
5681
- value: {
5682
- user,
5683
- signOut: ClientSignOut,
5684
- signIn,
5685
- status
5686
+ return {
5687
+ props: {
5688
+ error: JSON.stringify(error)
5689
+ }
5690
+ };
5686
5691
  }
5687
- },
5688
- children
5689
- );
5690
- }
5692
+ };
5693
+ }
5694
+ };
5691
5695
 
5692
5696
 
5693
5697