@bluemarble/bm-components 2.1.4 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -5,9 +5,12 @@ import { TableCellProps, TableCell, TextFieldProps, StandardTextFieldProps, Sele
5
5
  import * as _mui_material_OverridableComponent from '@mui/material/OverridableComponent';
6
6
  import { FactoryOpts } from 'imask';
7
7
  import { IconType } from 'react-icons';
8
+ import * as next from 'next';
8
9
  import { NextApiRequest, NextApiResponse, GetServerSidePropsContext } from 'next';
9
10
  import { ZodSchema } from 'zod';
10
11
  import { AxiosInstance } from 'axios';
12
+ import * as cookie from 'cookie';
13
+ import * as express from 'express';
11
14
 
12
15
  interface ColumnTitleProps {
13
16
  name: string;
@@ -431,25 +434,35 @@ declare class DomainError extends Error {
431
434
  }
432
435
 
433
436
  type PossibleMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'ALL';
434
- type HandlerProps<UserKeyType> = (req: NextApiRequest & {
435
- user?: UserKeyType;
436
- }, res: NextApiResponse) => Promise<any>;
437
+ type NextHandlerApiRequest<T = string> = NextApiRequest & {
438
+ user?: T;
439
+ };
440
+ type HandlerProps<UserKeyType> = (req: NextHandlerApiRequest<UserKeyType>, res: NextApiResponse, options?: TApiOptions) => Promise<any>;
437
441
  type MethodProps<T> = Partial<Record<PossibleMethods, HandlerProps<T>>>;
438
- type MiddlewaresProps<T> = ((handler: HandlerProps<T>) => HandlerProps<T>)[];
442
+ type MiddlewaresProps<T> = ((handler: HandlerProps<T>, options?: TApiOptions) => HandlerProps<T>)[];
439
443
  type ConstructorProps$1<T> = {
444
+ public?: boolean;
440
445
  middlewares?: MiddlewaresProps<T>;
441
446
  onFinally?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
442
447
  onError?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
443
448
  };
449
+ type TApiOptions = {
450
+ public?: boolean;
451
+ middlewares?: MiddlewaresProps<any>;
452
+ };
444
453
  declare class ApiHelper<T> {
445
- middlewares: MiddlewaresProps<T>;
454
+ private middlewares;
455
+ private public;
446
456
  private onFinally;
447
457
  private onError;
448
458
  constructor(props?: ConstructorProps$1<T>);
449
- setMiddlewares(middlewares: any[]): this;
450
- createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<void | NextApiResponse<any>>;
451
- static parse: typeof ApiHelper.parserErrorWrapper;
452
- static parserErrorWrapper<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
459
+ createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<any>;
460
+ private buildFactory;
461
+ static build(factory: (req: NextHandlerApiRequest, res: NextApiResponse) => MethodProps<string>, options?: TApiOptions): (req: NextHandlerApiRequest, res: NextApiResponse) => Promise<any>;
462
+ static parser<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
463
+ /** @deprecated Use {@link ApiHelper.parser} instead. */
464
+ static parserErrorWrapper: typeof ApiHelper.parser;
465
+ /** @deprecated Use {@Link ApiHelper.build} instead. */
453
466
  static create({ onFinally }: {
454
467
  onFinally: any;
455
468
  }): ApiHelper<unknown>;
@@ -643,4 +656,46 @@ declare function CreateAuthProvider<T>({ api, children, sessionTokenName, Provid
643
656
  sessionTokenName: string;
644
657
  }): React__default.JSX.Element;
645
658
 
646
- export { AlertContext, AlertProvider, ApiHelper, type AuthContextProps, AuthHelper, Autocomplete, BaseDialog, BaseDialogBody, BaseDialogContainer, BaseDialogCreate, BaseDialogTrigger, BaseGrid, BaseGridAutoRows, Checkbox, type ColumnTitleProps, type ColumnsProps, CreateAuthProvider, Dialog, DialogConfirm, DomainError, EditableTableCell, type FilterCompareType, type FilterProps, FormHelperContext, FormHelperProvider, GetInputLabel, Grid, HttpError, type IBaseDialogBodyProps, type IBaseDialogContainerProps, type IBaseDialogTriggerProps, type IDialogConfirmProps, type IFilter, Input, InputMask, LargeButton, Modal, Radio, Select, type SortedByProps$1 as SortedByProps, Switch, type TUseDialogConfirm, TabPanel, type TabPanelProps, Td, Tr, UseDialogConfirm, createAuthContext, createFilter, filterData, getTabProps, useAlert, useAsyncGrid, useBaseDialog, useBaseDialogInstance, useEvent, useFilter, useFormHelper, useGrid, useLoading };
659
+ /**
660
+ * Parses cookies.
661
+ *
662
+ * @param ctx NextJS page or API context, express context, null or undefined.
663
+ * @param options Options that we pass down to `cookie` library.
664
+ */
665
+ declare function parseCookies(ctx?: Pick<next.NextPageContext, 'req'> | {
666
+ req: next.NextApiRequest;
667
+ } | {
668
+ req: express.Request;
669
+ } | null | undefined, options?: cookie.ParseOptions): cookie.Cookies;
670
+ /**
671
+ * Sets a cookie.
672
+ *
673
+ * @param ctx NextJS page or API context, express context, null or undefined.
674
+ * @param name The name of your cookie.
675
+ * @param value The value of your cookie.
676
+ * @param options Options that we pass down to `cookie` library.
677
+ */
678
+ declare function setCookie(ctx: Pick<next.NextPageContext, 'res'> | {
679
+ res: next.NextApiResponse;
680
+ } | {
681
+ res: express.Response;
682
+ } | null | undefined, name: string, value: string, options?: cookie.SerializeOptions): {};
683
+ /**
684
+ * Destroys a cookie with a particular name.
685
+ *
686
+ * @param ctx NextJS page or API context, express context, null or undefined.
687
+ * @param name Cookie name.
688
+ * @param options Options that we pass down to `cookie` library.
689
+ */
690
+ declare function destroyCookie(ctx: Pick<next.NextPageContext, 'res'> | {
691
+ res: next.NextApiResponse;
692
+ } | {
693
+ res: express.Response;
694
+ } | null | undefined, name: string, options?: cookie.SerializeOptions): {};
695
+ declare const nookies: {
696
+ set: typeof setCookie;
697
+ get: typeof parseCookies;
698
+ destroy: typeof destroyCookie;
699
+ };
700
+
701
+ export { AlertContext, AlertProvider, ApiHelper, type AuthContextProps, AuthHelper, Autocomplete, BaseDialog, BaseDialogBody, BaseDialogContainer, BaseDialogCreate, BaseDialogTrigger, BaseGrid, BaseGridAutoRows, Checkbox, type ColumnTitleProps, type ColumnsProps, CreateAuthProvider, Dialog, DialogConfirm, DomainError, EditableTableCell, type FilterCompareType, type FilterProps, FormHelperContext, FormHelperProvider, GetInputLabel, Grid, HttpError, type IBaseDialogBodyProps, type IBaseDialogContainerProps, type IBaseDialogTriggerProps, type IDialogConfirmProps, type IFilter, Input, InputMask, LargeButton, Modal, Radio, Select, type SortedByProps$1 as SortedByProps, Switch, type TUseDialogConfirm, TabPanel, type TabPanelProps, Td, Tr, UseDialogConfirm, createAuthContext, createFilter, destroyCookie, filterData, getTabProps, nookies, parseCookies, setCookie, useAlert, useAsyncGrid, useBaseDialog, useBaseDialogInstance, useEvent, useFilter, useFormHelper, useGrid, useLoading };
package/dist/index.d.ts CHANGED
@@ -5,9 +5,12 @@ import { TableCellProps, TableCell, TextFieldProps, StandardTextFieldProps, Sele
5
5
  import * as _mui_material_OverridableComponent from '@mui/material/OverridableComponent';
6
6
  import { FactoryOpts } from 'imask';
7
7
  import { IconType } from 'react-icons';
8
+ import * as next from 'next';
8
9
  import { NextApiRequest, NextApiResponse, GetServerSidePropsContext } from 'next';
9
10
  import { ZodSchema } from 'zod';
10
11
  import { AxiosInstance } from 'axios';
12
+ import * as cookie from 'cookie';
13
+ import * as express from 'express';
11
14
 
12
15
  interface ColumnTitleProps {
13
16
  name: string;
@@ -431,25 +434,35 @@ declare class DomainError extends Error {
431
434
  }
432
435
 
433
436
  type PossibleMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'ALL';
434
- type HandlerProps<UserKeyType> = (req: NextApiRequest & {
435
- user?: UserKeyType;
436
- }, res: NextApiResponse) => Promise<any>;
437
+ type NextHandlerApiRequest<T = string> = NextApiRequest & {
438
+ user?: T;
439
+ };
440
+ type HandlerProps<UserKeyType> = (req: NextHandlerApiRequest<UserKeyType>, res: NextApiResponse, options?: TApiOptions) => Promise<any>;
437
441
  type MethodProps<T> = Partial<Record<PossibleMethods, HandlerProps<T>>>;
438
- type MiddlewaresProps<T> = ((handler: HandlerProps<T>) => HandlerProps<T>)[];
442
+ type MiddlewaresProps<T> = ((handler: HandlerProps<T>, options?: TApiOptions) => HandlerProps<T>)[];
439
443
  type ConstructorProps$1<T> = {
444
+ public?: boolean;
440
445
  middlewares?: MiddlewaresProps<T>;
441
446
  onFinally?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
442
447
  onError?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
443
448
  };
449
+ type TApiOptions = {
450
+ public?: boolean;
451
+ middlewares?: MiddlewaresProps<any>;
452
+ };
444
453
  declare class ApiHelper<T> {
445
- middlewares: MiddlewaresProps<T>;
454
+ private middlewares;
455
+ private public;
446
456
  private onFinally;
447
457
  private onError;
448
458
  constructor(props?: ConstructorProps$1<T>);
449
- setMiddlewares(middlewares: any[]): this;
450
- createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<void | NextApiResponse<any>>;
451
- static parse: typeof ApiHelper.parserErrorWrapper;
452
- static parserErrorWrapper<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
459
+ createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<any>;
460
+ private buildFactory;
461
+ static build(factory: (req: NextHandlerApiRequest, res: NextApiResponse) => MethodProps<string>, options?: TApiOptions): (req: NextHandlerApiRequest, res: NextApiResponse) => Promise<any>;
462
+ static parser<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
463
+ /** @deprecated Use {@link ApiHelper.parser} instead. */
464
+ static parserErrorWrapper: typeof ApiHelper.parser;
465
+ /** @deprecated Use {@Link ApiHelper.build} instead. */
453
466
  static create({ onFinally }: {
454
467
  onFinally: any;
455
468
  }): ApiHelper<unknown>;
@@ -643,4 +656,46 @@ declare function CreateAuthProvider<T>({ api, children, sessionTokenName, Provid
643
656
  sessionTokenName: string;
644
657
  }): React__default.JSX.Element;
645
658
 
646
- export { AlertContext, AlertProvider, ApiHelper, type AuthContextProps, AuthHelper, Autocomplete, BaseDialog, BaseDialogBody, BaseDialogContainer, BaseDialogCreate, BaseDialogTrigger, BaseGrid, BaseGridAutoRows, Checkbox, type ColumnTitleProps, type ColumnsProps, CreateAuthProvider, Dialog, DialogConfirm, DomainError, EditableTableCell, type FilterCompareType, type FilterProps, FormHelperContext, FormHelperProvider, GetInputLabel, Grid, HttpError, type IBaseDialogBodyProps, type IBaseDialogContainerProps, type IBaseDialogTriggerProps, type IDialogConfirmProps, type IFilter, Input, InputMask, LargeButton, Modal, Radio, Select, type SortedByProps$1 as SortedByProps, Switch, type TUseDialogConfirm, TabPanel, type TabPanelProps, Td, Tr, UseDialogConfirm, createAuthContext, createFilter, filterData, getTabProps, useAlert, useAsyncGrid, useBaseDialog, useBaseDialogInstance, useEvent, useFilter, useFormHelper, useGrid, useLoading };
659
+ /**
660
+ * Parses cookies.
661
+ *
662
+ * @param ctx NextJS page or API context, express context, null or undefined.
663
+ * @param options Options that we pass down to `cookie` library.
664
+ */
665
+ declare function parseCookies(ctx?: Pick<next.NextPageContext, 'req'> | {
666
+ req: next.NextApiRequest;
667
+ } | {
668
+ req: express.Request;
669
+ } | null | undefined, options?: cookie.ParseOptions): cookie.Cookies;
670
+ /**
671
+ * Sets a cookie.
672
+ *
673
+ * @param ctx NextJS page or API context, express context, null or undefined.
674
+ * @param name The name of your cookie.
675
+ * @param value The value of your cookie.
676
+ * @param options Options that we pass down to `cookie` library.
677
+ */
678
+ declare function setCookie(ctx: Pick<next.NextPageContext, 'res'> | {
679
+ res: next.NextApiResponse;
680
+ } | {
681
+ res: express.Response;
682
+ } | null | undefined, name: string, value: string, options?: cookie.SerializeOptions): {};
683
+ /**
684
+ * Destroys a cookie with a particular name.
685
+ *
686
+ * @param ctx NextJS page or API context, express context, null or undefined.
687
+ * @param name Cookie name.
688
+ * @param options Options that we pass down to `cookie` library.
689
+ */
690
+ declare function destroyCookie(ctx: Pick<next.NextPageContext, 'res'> | {
691
+ res: next.NextApiResponse;
692
+ } | {
693
+ res: express.Response;
694
+ } | null | undefined, name: string, options?: cookie.SerializeOptions): {};
695
+ declare const nookies: {
696
+ set: typeof setCookie;
697
+ get: typeof parseCookies;
698
+ destroy: typeof destroyCookie;
699
+ };
700
+
701
+ export { AlertContext, AlertProvider, ApiHelper, type AuthContextProps, AuthHelper, Autocomplete, BaseDialog, BaseDialogBody, BaseDialogContainer, BaseDialogCreate, BaseDialogTrigger, BaseGrid, BaseGridAutoRows, Checkbox, type ColumnTitleProps, type ColumnsProps, CreateAuthProvider, Dialog, DialogConfirm, DomainError, EditableTableCell, type FilterCompareType, type FilterProps, FormHelperContext, FormHelperProvider, GetInputLabel, Grid, HttpError, type IBaseDialogBodyProps, type IBaseDialogContainerProps, type IBaseDialogTriggerProps, type IDialogConfirmProps, type IFilter, Input, InputMask, LargeButton, Modal, Radio, Select, type SortedByProps$1 as SortedByProps, Switch, type TUseDialogConfirm, TabPanel, type TabPanelProps, Td, Tr, UseDialogConfirm, createAuthContext, createFilter, destroyCookie, filterData, getTabProps, nookies, parseCookies, setCookie, useAlert, useAsyncGrid, useBaseDialog, useBaseDialogInstance, useEvent, useFilter, useFormHelper, useGrid, useLoading };
package/dist/index.js CHANGED
@@ -4501,55 +4501,62 @@ var VALID_METHODS = [
4501
4501
  "PATCH"
4502
4502
  ];
4503
4503
  var _ApiHelper = class _ApiHelper {
4504
- async onFinally(req, res) {
4504
+ async onFinally(_req, _res) {
4505
4505
  }
4506
- async onError(req, res, error) {
4506
+ async onError(_req, _res, _error) {
4507
4507
  }
4508
4508
  constructor(props) {
4509
+ this.public = props?.public ?? false;
4509
4510
  this.middlewares = (props?.middlewares || []).reverse();
4510
4511
  this.onFinally = props?.onFinally || (async () => {
4511
4512
  });
4512
4513
  this.onError = props?.onError || (async () => {
4513
4514
  });
4514
4515
  }
4515
- setMiddlewares(middlewares) {
4516
- this.middlewares = middlewares.reverse();
4517
- return this;
4518
- }
4519
4516
  createMethods(methods) {
4520
4517
  return async (req, res) => {
4521
4518
  const currentMethod = methods[req.method] || methods.ALL;
4519
+ const options = { public: this.public };
4522
4520
  if (req.method === "OPTIONS") return res.status(200).end();
4523
4521
  try {
4524
4522
  if (!VALID_METHODS.includes(req.method))
4525
4523
  throw new HttpError(405, "M\xE9todo inv\xE1lido");
4526
4524
  if (!currentMethod) throw new HttpError(500, "M\xE9todo n\xE3o encontrado");
4527
4525
  const methodWithMiddlewares = this.middlewares.reduce(
4528
- (acc, fn) => fn(acc),
4526
+ (acc, fn) => fn(acc, options),
4529
4527
  currentMethod
4530
4528
  );
4531
- await methodWithMiddlewares(req, res);
4529
+ return await methodWithMiddlewares(req, res, options);
4532
4530
  } catch (error) {
4533
- if (error instanceof DomainError)
4534
- return res.status(400).json(error.message);
4535
- if (error instanceof HttpError)
4536
- return res.status(error.status).json(error.message);
4537
- if (process.env.NODE_ENV === "production") {
4538
- res.status(500).json({
4539
- code: "internal.error",
4540
- error: "Erro interno do servidor",
4541
- details: error
4542
- });
4543
- return this.onError(req, res, error);
4544
- }
4531
+ if (error instanceof DomainError) return res.status(400).json(error.message);
4532
+ if (error instanceof HttpError) return res.status(error.status).json(error.message);
4545
4533
  this.onError(req, res, error);
4546
- throw new Error(error);
4534
+ throw error;
4547
4535
  } finally {
4548
4536
  await this.onFinally(req, res);
4549
4537
  }
4550
4538
  };
4551
4539
  }
4552
- static parserErrorWrapper(body, parser) {
4540
+ buildFactory(factory) {
4541
+ const options = {
4542
+ public: this.public
4543
+ };
4544
+ return async (req, res) => {
4545
+ const methods = factory(req, res);
4546
+ const handler = methods[req.method];
4547
+ const methodWithMiddlewares = this.middlewares.reduce((acc, fn) => {
4548
+ return fn(acc, options);
4549
+ }, handler);
4550
+ return await methodWithMiddlewares(req, res, options);
4551
+ };
4552
+ }
4553
+ static build(factory, options) {
4554
+ const helper = new _ApiHelper({
4555
+ ...options
4556
+ }).buildFactory(factory);
4557
+ return helper;
4558
+ }
4559
+ static parser(body, parser) {
4553
4560
  try {
4554
4561
  const object = parser.parse(body);
4555
4562
  return object;
@@ -4561,13 +4568,15 @@ var _ApiHelper = class _ApiHelper {
4561
4568
  });
4562
4569
  }
4563
4570
  }
4571
+ /** @deprecated Use {@Link ApiHelper.build} instead. */
4564
4572
  static create({ onFinally }) {
4565
4573
  return new _ApiHelper({
4566
4574
  onFinally
4567
4575
  });
4568
4576
  }
4569
4577
  };
4570
- _ApiHelper.parse = _ApiHelper.parserErrorWrapper;
4578
+ /** @deprecated Use {@link ApiHelper.parser} instead. */
4579
+ _ApiHelper.parserErrorWrapper = _ApiHelper.parser;
4571
4580
  var ApiHelper = _ApiHelper;
4572
4581
 
4573
4582
  // src/hooks/useFormHelper.ts
@@ -4856,6 +4865,14 @@ function setCookie(ctx, name, value, options = {}) {
4856
4865
  }
4857
4866
  return {};
4858
4867
  }
4868
+ function destroyCookie(ctx, name, options) {
4869
+ return setCookie(ctx, name, "", { ...options || {}, maxAge: -1 });
4870
+ }
4871
+ var nookies = {
4872
+ set: setCookie,
4873
+ get: parseCookies,
4874
+ destroy: destroyCookie
4875
+ };
4859
4876
 
4860
4877
  // src/helpers/authHelper.ts
4861
4878
  function decodeSessionToken({
@@ -5705,8 +5722,12 @@ export {
5705
5722
  UseDialogConfirm,
5706
5723
  createAuthContext,
5707
5724
  createFilter,
5725
+ destroyCookie,
5708
5726
  filterData,
5709
5727
  getTabProps,
5728
+ nookies,
5729
+ parseCookies,
5730
+ setCookie,
5710
5731
  useAlert,
5711
5732
  useAsyncGrid,
5712
5733
  useBaseDialog,