@greatapps/common 1.1.656 → 1.1.657

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.
Files changed (121) hide show
  1. package/dist/components/account/sections/TokenSection.mjs +1 -1
  2. package/dist/components/account/sections/TokenSection.mjs.map +1 -1
  3. package/dist/handlers.mjs +33 -0
  4. package/dist/handlers.mjs.map +1 -0
  5. package/dist/http.mjs +5 -0
  6. package/dist/http.mjs.map +1 -0
  7. package/dist/infra/http/api-fetch.mjs +30 -0
  8. package/dist/infra/http/api-fetch.mjs.map +1 -0
  9. package/dist/infra/route/parse-id.mjs +12 -0
  10. package/dist/infra/route/parse-id.mjs.map +1 -0
  11. package/dist/infra/route/safe-route-handler.mjs +18 -0
  12. package/dist/infra/route/safe-route-handler.mjs.map +1 -0
  13. package/dist/infra/route/search-params.mjs +11 -0
  14. package/dist/infra/route/search-params.mjs.map +1 -0
  15. package/dist/modules/cards/handlers/find-card-by-id.handler.mjs +16 -0
  16. package/dist/modules/cards/handlers/find-card-by-id.handler.mjs.map +1 -0
  17. package/dist/modules/cards/handlers/list-cards.handler.mjs +12 -0
  18. package/dist/modules/cards/handlers/list-cards.handler.mjs.map +1 -0
  19. package/dist/modules/cards/hooks/card-by-id.hook.mjs +2 -5
  20. package/dist/modules/cards/hooks/card-by-id.hook.mjs.map +1 -1
  21. package/dist/modules/cards/hooks/cards.hook.mjs +2 -5
  22. package/dist/modules/cards/hooks/cards.hook.mjs.map +1 -1
  23. package/dist/modules/charges/handlers/find-charge-by-id.handler.mjs +15 -0
  24. package/dist/modules/charges/handlers/find-charge-by-id.handler.mjs.map +1 -0
  25. package/dist/modules/charges/handlers/list-charges.handler.mjs +15 -0
  26. package/dist/modules/charges/handlers/list-charges.handler.mjs.map +1 -0
  27. package/dist/modules/charges/hooks/charge-by-id.hook.mjs +2 -3
  28. package/dist/modules/charges/hooks/charge-by-id.hook.mjs.map +1 -1
  29. package/dist/modules/charges/hooks/charges.hook.mjs +2 -3
  30. package/dist/modules/charges/hooks/charges.hook.mjs.map +1 -1
  31. package/dist/modules/ia-credits/handlers/list-ia-credits.handler.mjs +15 -0
  32. package/dist/modules/ia-credits/handlers/list-ia-credits.handler.mjs.map +1 -0
  33. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +5 -3
  34. package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
  35. package/dist/modules/pages/handlers/count-pages.handler.mjs +14 -0
  36. package/dist/modules/pages/handlers/count-pages.handler.mjs.map +1 -0
  37. package/dist/modules/pages/hooks/count-pages.hook.mjs +3 -4
  38. package/dist/modules/pages/hooks/count-pages.hook.mjs.map +1 -1
  39. package/dist/modules/plans/handlers/find-plan-by-id.handler.mjs +15 -0
  40. package/dist/modules/plans/handlers/find-plan-by-id.handler.mjs.map +1 -0
  41. package/dist/modules/plans/handlers/list-plans.handler.mjs +20 -0
  42. package/dist/modules/plans/handlers/list-plans.handler.mjs.map +1 -0
  43. package/dist/modules/plans/hooks/list-plans.hook.mjs +2 -3
  44. package/dist/modules/plans/hooks/list-plans.hook.mjs.map +1 -1
  45. package/dist/modules/plans/hooks/use-plan-by-id.hook.mjs +2 -3
  46. package/dist/modules/plans/hooks/use-plan-by-id.hook.mjs.map +1 -1
  47. package/dist/modules/projects/handlers/find-project.handler.mjs +16 -0
  48. package/dist/modules/projects/handlers/find-project.handler.mjs.map +1 -0
  49. package/dist/modules/projects/handlers/list-all-account-users.handler.mjs +17 -0
  50. package/dist/modules/projects/handlers/list-all-account-users.handler.mjs.map +1 -0
  51. package/dist/modules/projects/handlers/list-available-users.handler.mjs +20 -0
  52. package/dist/modules/projects/handlers/list-available-users.handler.mjs.map +1 -0
  53. package/dist/modules/projects/handlers/list-project-users.handler.mjs +20 -0
  54. package/dist/modules/projects/handlers/list-project-users.handler.mjs.map +1 -0
  55. package/dist/modules/projects/handlers/list-projects.handler.mjs +17 -0
  56. package/dist/modules/projects/handlers/list-projects.handler.mjs.map +1 -0
  57. package/dist/modules/projects/hooks/find-project.hook.mjs +2 -3
  58. package/dist/modules/projects/hooks/find-project.hook.mjs.map +1 -1
  59. package/dist/modules/projects/hooks/list-all-account-users.hook.mjs +6 -6
  60. package/dist/modules/projects/hooks/list-all-account-users.hook.mjs.map +1 -1
  61. package/dist/modules/projects/hooks/list-available-users.hook.mjs +6 -6
  62. package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
  63. package/dist/modules/projects/hooks/list-infinite-projects.hook.mjs +6 -6
  64. package/dist/modules/projects/hooks/list-infinite-projects.hook.mjs.map +1 -1
  65. package/dist/modules/projects/hooks/list-project-users.hook.mjs +6 -6
  66. package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
  67. package/dist/modules/projects/hooks/list-projects.hook.mjs +2 -3
  68. package/dist/modules/projects/hooks/list-projects.hook.mjs.map +1 -1
  69. package/dist/modules/subscriptions/handlers/list-subscriptions.handler.mjs +17 -0
  70. package/dist/modules/subscriptions/handlers/list-subscriptions.handler.mjs.map +1 -0
  71. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +5 -5
  72. package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
  73. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +5 -3
  74. package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
  75. package/dist/modules/users/handlers/list-messages.handler.mjs +36 -0
  76. package/dist/modules/users/handlers/list-messages.handler.mjs.map +1 -0
  77. package/dist/modules/users/hooks/messages.hook.mjs +7 -6
  78. package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
  79. package/dist/route.mjs +9 -0
  80. package/dist/route.mjs.map +1 -0
  81. package/package.json +15 -4
  82. package/src/components/account/sections/TokenSection.tsx +1 -1
  83. package/src/handlers.ts +22 -0
  84. package/src/http.ts +1 -0
  85. package/src/infra/http/api-fetch.ts +39 -0
  86. package/src/infra/route/parse-id.ts +9 -0
  87. package/src/infra/route/safe-route-handler.ts +16 -0
  88. package/src/infra/route/search-params.ts +9 -0
  89. package/src/modules/cards/handlers/find-card-by-id.handler.ts +15 -0
  90. package/src/modules/cards/handlers/list-cards.handler.ts +10 -0
  91. package/src/modules/cards/hooks/card-by-id.hook.ts +4 -5
  92. package/src/modules/cards/hooks/cards.hook.ts +4 -5
  93. package/src/modules/charges/handlers/find-charge-by-id.handler.ts +14 -0
  94. package/src/modules/charges/handlers/list-charges.handler.ts +15 -0
  95. package/src/modules/charges/hooks/charge-by-id.hook.ts +5 -3
  96. package/src/modules/charges/hooks/charges.hook.ts +5 -4
  97. package/src/modules/ia-credits/handlers/list-ia-credits.handler.ts +14 -0
  98. package/src/modules/ia-credits/hooks/ia-credits.hook.ts +7 -3
  99. package/src/modules/pages/handlers/count-pages.handler.ts +12 -0
  100. package/src/modules/pages/hooks/count-pages.hook.ts +3 -4
  101. package/src/modules/plans/handlers/find-plan-by-id.handler.ts +14 -0
  102. package/src/modules/plans/handlers/list-plans.handler.ts +20 -0
  103. package/src/modules/plans/hooks/list-plans.hook.ts +2 -3
  104. package/src/modules/plans/hooks/use-plan-by-id.hook.ts +4 -3
  105. package/src/modules/projects/handlers/find-project.handler.ts +15 -0
  106. package/src/modules/projects/handlers/list-all-account-users.handler.ts +17 -0
  107. package/src/modules/projects/handlers/list-available-users.handler.ts +20 -0
  108. package/src/modules/projects/handlers/list-project-users.handler.ts +20 -0
  109. package/src/modules/projects/handlers/list-projects.handler.ts +17 -0
  110. package/src/modules/projects/hooks/find-project.hook.ts +3 -3
  111. package/src/modules/projects/hooks/list-all-account-users.hook.ts +8 -7
  112. package/src/modules/projects/hooks/list-available-users.hook.ts +8 -7
  113. package/src/modules/projects/hooks/list-infinite-projects.hook.ts +8 -7
  114. package/src/modules/projects/hooks/list-project-users.hook.ts +8 -7
  115. package/src/modules/projects/hooks/list-projects.hook.ts +3 -3
  116. package/src/modules/subscriptions/handlers/list-subscriptions.handler.ts +17 -0
  117. package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +8 -5
  118. package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +8 -5
  119. package/src/modules/users/handlers/list-messages.handler.ts +39 -0
  120. package/src/modules/users/hooks/messages.hook.ts +10 -8
  121. package/src/route.ts +3 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/route.ts"],"sourcesContent":["export { safeRouteHandler } from './infra/route/safe-route-handler';\nexport { searchParamsToParams } from './infra/route/search-params';\nexport { parseId } from './infra/route/parse-id';\n"],"mappings":"AAAA,SAAS,wBAAwB;AACjC,SAAS,4BAA4B;AACrC,SAAS,eAAe;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greatapps/common",
3
- "version": "1.1.656",
3
+ "version": "1.1.657",
4
4
  "description": "Shared library for GreatApps frontend applications",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./src/index.ts",
@@ -16,6 +16,18 @@
16
16
  "./middleware": {
17
17
  "import": "./dist/middleware.mjs",
18
18
  "types": "./src/middleware.ts"
19
+ },
20
+ "./route": {
21
+ "import": "./dist/route.mjs",
22
+ "types": "./src/route.ts"
23
+ },
24
+ "./http": {
25
+ "import": "./dist/http.mjs",
26
+ "types": "./src/http.ts"
27
+ },
28
+ "./handlers": {
29
+ "import": "./dist/handlers.mjs",
30
+ "types": "./src/handlers.ts"
19
31
  }
20
32
  },
21
33
  "files": [
@@ -61,7 +73,8 @@
61
73
  "react-image-crop": "^11.0.10",
62
74
  "server-only": "^0.0.1",
63
75
  "sonner": ">=2.0.0",
64
- "tailwind-merge": "^3.5.0"
76
+ "tailwind-merge": "^3.5.0",
77
+ "zod": "^4.3.6"
65
78
  },
66
79
  "devDependencies": {
67
80
  "@hookform/resolvers": "^5.2.2",
@@ -77,7 +90,6 @@
77
90
  "react-hook-form": "^7.72.0",
78
91
  "tsup": "^8.5.1",
79
92
  "typescript": "^5.9.3",
80
- "zod": "^4.3.6",
81
93
  "zustand": "^5.0.12"
82
94
  },
83
95
  "peerDependencies": {
@@ -88,7 +100,6 @@
88
100
  "react-dom": ">=19.0.0",
89
101
  "react-hook-form": ">=7.0.0",
90
102
  "sonner": ">=2.0.0",
91
- "zod": ">=4.0.0",
92
103
  "zustand": ">=4.0.0"
93
104
  }
94
105
  }
@@ -60,7 +60,7 @@ export function TokenSection() {
60
60
  </div>
61
61
 
62
62
  <a
63
- href="https://documenter.getpostman.com/view/4861938/T1Djiz8H"
63
+ href="https://documenter.getpostman.com/view/4861938/2sAYdoDmj4"
64
64
  target="_blank"
65
65
  rel="noopener noreferrer"
66
66
  className="paragraph-small-medium text-gray-500 underline hover:text-gray-700 transition-colors w-fit"
@@ -0,0 +1,22 @@
1
+ export { createListCardsHandler } from './modules/cards/handlers/list-cards.handler';
2
+ export { createFindCardByIdHandler } from './modules/cards/handlers/find-card-by-id.handler';
3
+
4
+ export { createListChargesHandler } from './modules/charges/handlers/list-charges.handler';
5
+ export { createFindChargeByIdHandler } from './modules/charges/handlers/find-charge-by-id.handler';
6
+
7
+ export { createListIaCreditsHandler } from './modules/ia-credits/handlers/list-ia-credits.handler';
8
+
9
+ export { createCountPagesHandler } from './modules/pages/handlers/count-pages.handler';
10
+
11
+ export { createListPlansHandler } from './modules/plans/handlers/list-plans.handler';
12
+ export { createFindPlanByIdHandler } from './modules/plans/handlers/find-plan-by-id.handler';
13
+
14
+ export { createListProjectsHandler } from './modules/projects/handlers/list-projects.handler';
15
+ export { createFindProjectHandler } from './modules/projects/handlers/find-project.handler';
16
+ export { createListProjectUsersHandler } from './modules/projects/handlers/list-project-users.handler';
17
+ export { createListAvailableUsersHandler } from './modules/projects/handlers/list-available-users.handler';
18
+ export { createListAllAccountUsersHandler } from './modules/projects/handlers/list-all-account-users.handler';
19
+
20
+ export { createListSubscriptionsHandler } from './modules/subscriptions/handlers/list-subscriptions.handler';
21
+
22
+ export { createListMessagesHandler } from './modules/users/handlers/list-messages.handler';
package/src/http.ts ADDED
@@ -0,0 +1 @@
1
+ export { apiFetch } from './infra/http/api-fetch';
@@ -0,0 +1,39 @@
1
+ import { buildQueryParams } from '../utils/params';
2
+ import type { ActionResult } from '../api/types';
3
+
4
+ type QueryParams = Parameters<typeof buildQueryParams>[0];
5
+
6
+ interface ApiFetchOptions {
7
+ params?: object;
8
+ signal?: AbortSignal;
9
+ }
10
+
11
+ export async function apiFetch<T>(path: string, options: ApiFetchOptions = {}): Promise<T> {
12
+ const { params, signal } = options;
13
+ const qs = params ? buildQueryParams(params as QueryParams) : '';
14
+ const url = qs ? `${path}?${qs}` : path;
15
+
16
+ const response = await fetch(url, {
17
+ method: 'GET',
18
+ credentials: 'same-origin',
19
+ headers: { Accept: 'application/json' },
20
+ signal,
21
+ });
22
+
23
+ if (!response.ok) {
24
+ let message = `Erro ${response.status}`;
25
+ try {
26
+ const body = (await response.json()) as ActionResult<unknown>;
27
+ if (body && !body.success && body.error) message = body.error;
28
+ } catch {
29
+ // corpo não-JSON — mantém a mensagem genérica
30
+ }
31
+ throw new Error(message);
32
+ }
33
+
34
+ const body = (await response.json()) as ActionResult<T>;
35
+ if (!body.success) {
36
+ throw new Error(body.error ?? 'Erro desconhecido');
37
+ }
38
+ return body.data as T;
39
+ }
@@ -0,0 +1,9 @@
1
+ import { ApiError } from '../api/types';
2
+
3
+ export function parseId(value: string): number {
4
+ const id = Number(value);
5
+ if (!Number.isFinite(id) || id <= 0) {
6
+ throw new ApiError('Identificador inválido', 'INVALID_ID', 400);
7
+ }
8
+ return id;
9
+ }
@@ -0,0 +1,16 @@
1
+ import 'server-only';
2
+
3
+ import { ApiError } from '../api/types';
4
+
5
+ export async function safeRouteHandler<T>(fn: () => Promise<T>): Promise<Response> {
6
+ try {
7
+ const data = await fn();
8
+ return Response.json({ success: true, data });
9
+ } catch (error) {
10
+ if (error instanceof ApiError) {
11
+ return Response.json({ success: false, error: error.message }, { status: error.statusCode });
12
+ }
13
+ console.error('[safeRouteHandler] erro inesperado', error);
14
+ return Response.json({ success: false, error: 'Erro interno do servidor' }, { status: 500 });
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ export function searchParamsToParams<T extends object>(
2
+ searchParams: URLSearchParams
3
+ ): Partial<T> {
4
+ const params = Object.create(null) as Partial<Record<keyof T, string>>;
5
+ for (const [key, value] of searchParams.entries()) {
6
+ params[key as keyof T] = value;
7
+ }
8
+ return params as Partial<T>;
9
+ }
@@ -0,0 +1,15 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { parseId } from '../../../infra/route/parse-id';
5
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
6
+ import { cardsService } from '../services/cards.service';
7
+
8
+ export function createFindCardByIdHandler() {
9
+ return async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
10
+ return safeRouteHandler(async () => {
11
+ const { id } = await params;
12
+ return cardsService.findCardById(parseId(id));
13
+ });
14
+ };
15
+ }
@@ -0,0 +1,10 @@
1
+ import 'server-only';
2
+
3
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
4
+ import { cardsService } from '../services/cards.service';
5
+
6
+ export function createListCardsHandler() {
7
+ return async function GET() {
8
+ return safeRouteHandler(() => cardsService.listCards());
9
+ };
10
+ }
@@ -1,7 +1,9 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { SuccessResult } from '../../../infra/api/types';
6
+ import type { Card } from '../types';
5
7
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
6
8
 
7
9
  export function useCardById(id: number | undefined) {
@@ -9,10 +11,7 @@ export function useCardById(id: number | undefined) {
9
11
 
10
12
  return useQuery({
11
13
  queryKey: cardKey,
12
- queryFn: async () => {
13
- const { findCardByIdAction } = await import('../actions/find-card-by-id.action');
14
- return withAction(findCardByIdAction)(id!);
15
- },
14
+ queryFn: ({ signal }) => apiFetch<SuccessResult<Card>>(`/api/cards/${id}`, { signal }),
16
15
  enabled: id != null,
17
16
  });
18
17
  }
@@ -1,7 +1,9 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { PaginatedSuccessResult } from '../../../infra/api/types';
6
+ import type { Card } from '../types';
5
7
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
6
8
 
7
9
  export const CARDS_QUERY_KEY = ['cards'] as const;
@@ -11,10 +13,7 @@ export function useCards() {
11
13
 
12
14
  return useQuery({
13
15
  queryKey,
14
- queryFn: async () => {
15
- const { listCardsAction } = await import('../actions/list-cards.action');
16
- return withAction(listCardsAction)();
17
- },
16
+ queryFn: ({ signal }) => apiFetch<PaginatedSuccessResult<Card>>('/api/cards', { signal }),
18
17
  select: (result) => ({
19
18
  ...result,
20
19
  data: result.data.filter((card) => !card.deleted),
@@ -0,0 +1,14 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { chargesService } from '../services/charges.service';
6
+
7
+ export function createFindChargeByIdHandler() {
8
+ return async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
9
+ return safeRouteHandler(async () => {
10
+ const { id } = await params;
11
+ return chargesService.findChargeById(id);
12
+ });
13
+ };
14
+ }
@@ -0,0 +1,15 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { searchParamsToParams } from '../../../infra/route/search-params';
6
+ import { chargesService } from '../services/charges.service';
7
+ import type { FindChargesParams } from '../types/charge.type';
8
+
9
+ export function createListChargesHandler() {
10
+ return async function GET(req: NextRequest) {
11
+ return safeRouteHandler(() =>
12
+ chargesService.listCharges(searchParamsToParams<FindChargesParams>(req.nextUrl.searchParams))
13
+ );
14
+ };
15
+ }
@@ -1,9 +1,10 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { SuccessResult } from '../../../infra/api/types';
5
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
6
- import { findChargeByIdAction } from '../actions/find-charge-by-id.action';
7
+ import { Charge } from '../types/charge.type';
7
8
  import { CHARGES_QUERY_KEY } from '../../subscriptions/constants/query-keys.constants';
8
9
 
9
10
  export function useChargeById(chargeId: string | number | null | undefined) {
@@ -11,7 +12,8 @@ export function useChargeById(chargeId: string | number | null | undefined) {
11
12
 
12
13
  return useQuery({
13
14
  queryKey: chargesKey,
14
- queryFn: () => withAction(findChargeByIdAction)(chargeId!),
15
+ queryFn: ({ signal }) =>
16
+ apiFetch<SuccessResult<Charge>>(`/api/charges/${chargeId}`, { signal }),
15
17
  enabled: !!chargeId,
16
18
  });
17
19
  }
@@ -1,10 +1,10 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { PaginatedSuccessResult } from '../../../infra/api/types';
5
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
6
- import { listChargesAction } from '../actions/list-charges.action';
7
- import { FindChargesParams } from '../types/charge.type';
7
+ import { Charge, FindChargesParams } from '../types/charge.type';
8
8
  import { CHARGES_QUERY_KEY } from '../../subscriptions/constants/query-keys.constants';
9
9
 
10
10
  export function useCharges(params?: Partial<FindChargesParams>) {
@@ -12,7 +12,8 @@ export function useCharges(params?: Partial<FindChargesParams>) {
12
12
 
13
13
  return useQuery({
14
14
  queryKey: chargesKey,
15
- queryFn: () => withAction(listChargesAction)(params),
15
+ queryFn: ({ signal }) =>
16
+ apiFetch<PaginatedSuccessResult<Charge>>('/api/charges', { params, signal }),
16
17
  staleTime: 10_000,
17
18
  });
18
19
  }
@@ -0,0 +1,14 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { iaCreditsService } from '../services/ia-credits.service';
6
+
7
+ export function createListIaCreditsHandler() {
8
+ return async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
9
+ return safeRouteHandler(async () => {
10
+ const { id } = await params;
11
+ return iaCreditsService.listOperations(id);
12
+ });
13
+ };
14
+ }
@@ -2,10 +2,10 @@
2
2
 
3
3
  import { useMemo } from 'react';
4
4
  import { useQuery } from '@tanstack/react-query';
5
- import { listIaCreditsAction } from '../actions/list-ia-credits.action';
5
+ import { apiFetch } from '../../../infra/http/api-fetch';
6
+ import type { PaginatedSuccessResult } from '../../../infra/api/types';
6
7
  import type { IaCreditOperation, IaCreditsSummary } from '../types';
7
8
  import { useActiveSubscription } from '../../subscriptions/hooks/find-active-subscription.hook';
8
- import { withAction } from '../../../utils/withAction';
9
9
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
10
10
 
11
11
  function calculateSummary(operations: IaCreditOperation[]): IaCreditsSummary {
@@ -77,7 +77,11 @@ export function useIaCredits() {
77
77
 
78
78
  const query = useQuery({
79
79
  queryKey,
80
- queryFn: withAction(() => listIaCreditsAction(subscriptionId!)),
80
+ queryFn: ({ signal }) =>
81
+ apiFetch<PaginatedSuccessResult<IaCreditOperation>>(
82
+ `/api/subscriptions/${subscriptionId}/ia-credits`,
83
+ { signal }
84
+ ),
81
85
  enabled: subscriptionId != null,
82
86
  });
83
87
 
@@ -0,0 +1,12 @@
1
+ import 'server-only';
2
+
3
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
4
+ import { pagesService } from '../services/pages.service';
5
+
6
+ export function createCountPagesHandler() {
7
+ return async function GET() {
8
+ return safeRouteHandler(async () => ({
9
+ total: await pagesService.countPages(),
10
+ }));
11
+ };
12
+ }
@@ -1,9 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { countPagesAction } from '../actions/count-pages.action';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
5
  import { PAGES_QUERY_KEY } from '../constants/query-keys.constants';
6
- import { withAction } from '../../../utils/withAction';
7
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
8
7
 
9
8
  export const COUNT_PAGES_QUERY_KEY = [...PAGES_QUERY_KEY, 'count'] as const;
@@ -13,8 +12,8 @@ export function useCountPages() {
13
12
 
14
13
  return useQuery({
15
14
  queryKey,
16
- queryFn: async () => {
17
- const result = await withAction(countPagesAction)();
15
+ queryFn: async ({ signal }) => {
16
+ const result = await apiFetch<{ total: number }>('/api/pages/count', { signal });
18
17
  return result.total;
19
18
  },
20
19
  staleTime: 30_000,
@@ -0,0 +1,14 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { plansService } from '../services/plans.service';
6
+
7
+ export function createFindPlanByIdHandler() {
8
+ return async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
9
+ return safeRouteHandler(async () => {
10
+ const { id } = await params;
11
+ return plansService.findById(id);
12
+ });
13
+ };
14
+ }
@@ -0,0 +1,20 @@
1
+ import 'server-only';
2
+
3
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
4
+ import { plansService } from '../services/plans.service';
5
+
6
+ export function createListPlansHandler() {
7
+ return async function GET() {
8
+ return safeRouteHandler(async () => {
9
+ const { data: apiPlans } = await plansService.listPlans({
10
+ active: true,
11
+ search: 'client',
12
+ sort: 'id:ASC',
13
+ });
14
+
15
+ if (!apiPlans?.length) return [];
16
+
17
+ return [...apiPlans].sort((a, b) => a.value - b.value);
18
+ });
19
+ };
20
+ }
@@ -1,8 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { withAction } from '@greatapps/common';
5
- import { listPlansAction } from '../actions/list-plans.action';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
6
5
  import type { Plan } from '../types/plan.type';
7
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
8
7
 
@@ -13,7 +12,7 @@ export function usePlans() {
13
12
 
14
13
  return useQuery<Plan[]>({
15
14
  queryKey,
16
- queryFn: withAction(listPlansAction),
15
+ queryFn: ({ signal }) => apiFetch<Plan[]>('/api/plans', { signal }),
17
16
  staleTime: Infinity,
18
17
  gcTime: Infinity,
19
18
  refetchOnWindowFocus: false,
@@ -1,8 +1,8 @@
1
1
  "use client";
2
2
 
3
3
  import { useQuery, useQueryClient } from "@tanstack/react-query";
4
- import { withAction } from "../../../utils/withAction";
5
- import { findPlanByIdAction } from "../actions/find-plan-by-id.action";
4
+ import { apiFetch } from "../../../infra/http/api-fetch";
5
+ import type { SuccessResult } from "../../../infra/api/types";
6
6
  import type { Plan } from "../types/plan.type";
7
7
  import { PLANS_QUERY_KEY } from "./list-plans.hook";
8
8
  import { useAuthQueryKey } from "../../../hooks/useAuthQueryKey";
@@ -13,7 +13,8 @@ export function usePlanById(idPlan?: number | string | null) {
13
13
 
14
14
  return useQuery({
15
15
  queryKey: [...plansKey, idPlan],
16
- queryFn: withAction(() => findPlanByIdAction(idPlan!)),
16
+ queryFn: ({ signal }) =>
17
+ apiFetch<SuccessResult<Plan>>(`/api/plans/${idPlan}`, { signal }),
17
18
  initialData: () => {
18
19
  const queries = queryClient.getQueriesData<Plan[]>({
19
20
  queryKey: plansKey,
@@ -0,0 +1,15 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { parseId } from '../../../infra/route/parse-id';
5
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
6
+ import { projectsService } from '../services/projects.service';
7
+
8
+ export function createFindProjectHandler() {
9
+ return async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
10
+ return safeRouteHandler(async () => {
11
+ const { id } = await params;
12
+ return projectsService.findById(parseId(id));
13
+ });
14
+ };
15
+ }
@@ -0,0 +1,17 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { searchParamsToParams } from '../../../infra/route/search-params';
6
+ import { projectUsersService } from '../services/project-users.service';
7
+ import type { ListUsersParams } from '../types';
8
+
9
+ export function createListAllAccountUsersHandler() {
10
+ return async function GET(req: NextRequest) {
11
+ return safeRouteHandler(() =>
12
+ projectUsersService.listAllAccountUsers(
13
+ searchParamsToParams<ListUsersParams>(req.nextUrl.searchParams)
14
+ )
15
+ );
16
+ };
17
+ }
@@ -0,0 +1,20 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { parseId } from '../../../infra/route/parse-id';
5
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
6
+ import { searchParamsToParams } from '../../../infra/route/search-params';
7
+ import { projectUsersService } from '../services/project-users.service';
8
+ import type { ListUsersParams } from '../types';
9
+
10
+ export function createListAvailableUsersHandler() {
11
+ return async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
12
+ return safeRouteHandler(async () => {
13
+ const { id } = await params;
14
+ return projectUsersService.listNotInProject(
15
+ parseId(id),
16
+ searchParamsToParams<ListUsersParams>(req.nextUrl.searchParams)
17
+ );
18
+ });
19
+ };
20
+ }
@@ -0,0 +1,20 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { parseId } from '../../../infra/route/parse-id';
5
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
6
+ import { searchParamsToParams } from '../../../infra/route/search-params';
7
+ import { projectUsersService } from '../services/project-users.service';
8
+ import type { ListUsersParams } from '../types';
9
+
10
+ export function createListProjectUsersHandler() {
11
+ return async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
12
+ return safeRouteHandler(async () => {
13
+ const { id } = await params;
14
+ return projectUsersService.listInProject(
15
+ parseId(id),
16
+ searchParamsToParams<ListUsersParams>(req.nextUrl.searchParams)
17
+ );
18
+ });
19
+ };
20
+ }
@@ -0,0 +1,17 @@
1
+ import 'server-only';
2
+
3
+ import type { NextRequest } from 'next/server';
4
+ import { safeRouteHandler } from '../../../infra/route/safe-route-handler';
5
+ import { searchParamsToParams } from '../../../infra/route/search-params';
6
+ import { projectsService } from '../services/projects.service';
7
+ import type { ListProjectsActionParams } from '../actions/list-projects.action';
8
+
9
+ export function createListProjectsHandler() {
10
+ return async function GET(req: NextRequest) {
11
+ return safeRouteHandler(() =>
12
+ projectsService.list(
13
+ searchParamsToParams<ListProjectsActionParams>(req.nextUrl.searchParams)
14
+ )
15
+ );
16
+ };
17
+ }
@@ -1,8 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import { useQuery } from '@tanstack/react-query';
4
- import { findProjectAction } from '../actions/find-project.action';
5
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { Project } from '../types';
6
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
7
7
  import { PROJECTS_BASE_KEY } from './list-projects.hook';
8
8
 
@@ -11,7 +11,7 @@ export function useProject(projectId: number | null) {
11
11
 
12
12
  return useQuery({
13
13
  queryKey,
14
- queryFn: () => withAction(() => findProjectAction(projectId!))(),
14
+ queryFn: ({ signal }) => apiFetch<Project>(`/api/projects/${projectId}`, { signal }),
15
15
  enabled: projectId != null && projectId > 0,
16
16
  staleTime: 10_000,
17
17
  });
@@ -1,8 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import { useInfiniteQuery } from '@tanstack/react-query';
4
- import { listAllAccountUsersAction } from '../actions/list-all-account-users.action';
5
- import { withAction } from '../../../utils/withAction';
4
+ import { apiFetch } from '../../../infra/http/api-fetch';
5
+ import type { AccountUser, UsersPage } from '../types';
6
6
  import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
7
7
 
8
8
  const PAGE_SIZE = 20;
@@ -21,15 +21,16 @@ export function useListAllAccountUsers(search: string = '', projectId?: number)
21
21
 
22
22
  return useInfiniteQuery({
23
23
  queryKey,
24
- queryFn: ({ pageParam }) =>
25
- withAction(() =>
26
- listAllAccountUsersAction({
24
+ queryFn: ({ pageParam, signal }) =>
25
+ apiFetch<UsersPage<AccountUser>>('/api/account/users', {
26
+ params: {
27
27
  search: search || undefined,
28
28
  page: pageParam as number,
29
29
  limit: PAGE_SIZE,
30
30
  inProject: projectId,
31
- })
32
- )(),
31
+ },
32
+ signal,
33
+ }),
33
34
  getNextPageParam: (lastPage, allPages) => {
34
35
  const loaded = allPages.reduce((sum, p) => sum + p.data.length, 0);
35
36
  return loaded < lastPage.total ? allPages.length + 1 : undefined;