From cae25e2270f3919620492f11118ffd01bf8f20bf Mon Sep 17 00:00:00 2001 From: Wellington Braga Date: Sun, 26 Jan 2025 01:15:23 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20Adiciona=20novas=20interfa?= =?UTF-8?q?ces=20e=20refatora=20APIs,=20incluindo=20a=20implementa=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20SectorApi=20e=20ajustes=20na=20estrutura=20de=20?= =?UTF-8?q?URLs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/public/register/getRoles/route.ts | 0 .../api/public/register/getSectors/route.ts | 5 +++ src/app/api/public/register/route.ts | 0 src/app/api/tickets/[id]/route.ts | 2 +- src/app/api/url/index.ts | 6 --- src/app/api/urls/index.ts | 10 +++++ .../client/abstractions/httpClient/index.tsx | 42 ++++-------------- .../client/interfaces/HttpClient/index.ts | 26 +++++++++++ .../client/interfaces/index.ts | 1 + .../UI/components/Form/WhatYouDo/index.tsx | 5 +++ src/server/controllers/Issue.ts | 30 ++++++------- src/server/controllers/Search.ts | 22 +++++----- src/server/controllers/Session.ts | 2 - src/server/controllers/Ticket.ts | 14 +++--- src/server/controllers/User.ts | 31 +++---------- src/server/functions/getAuthToken/index.ts | 2 + src/types/Interfaces/Apis.ts | 8 ++++ src/types/Interfaces/HttpResponse.ts | 22 ++++++++++ src/types/Interfaces/index.ts | 1 + src/utils/apis/Issue.api.ts | 43 ++++++++++--------- src/utils/apis/Search.api.ts | 19 ++++---- src/utils/apis/Sector.api.ts | 28 +++++++++++- src/utils/apis/Session.api.ts | 16 ++++--- src/utils/apis/Ticket.api.ts | 27 ++++++------ src/utils/apis/User.api.ts | 15 ++++--- src/utils/apis/index.ts | 13 +++--- 26 files changed, 228 insertions(+), 162 deletions(-) create mode 100644 src/app/api/public/register/getRoles/route.ts create mode 100644 src/app/api/public/register/getSectors/route.ts create mode 100644 src/app/api/public/register/route.ts delete mode 100644 src/app/api/url/index.ts create mode 100644 src/app/api/urls/index.ts create mode 100644 src/implementations/client/interfaces/HttpClient/index.ts create mode 100644 src/types/Interfaces/Apis.ts diff --git a/src/app/api/public/register/getRoles/route.ts b/src/app/api/public/register/getRoles/route.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/app/api/public/register/getSectors/route.ts b/src/app/api/public/register/getSectors/route.ts new file mode 100644 index 00000000..907c7486 --- /dev/null +++ b/src/app/api/public/register/getSectors/route.ts @@ -0,0 +1,5 @@ +import { NextRequest } from "next/server"; + +export async function GET(req: NextRequest) { + return SearchController.searchTickets(req); + } \ No newline at end of file diff --git a/src/app/api/public/register/route.ts b/src/app/api/public/register/route.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/app/api/tickets/[id]/route.ts b/src/app/api/tickets/[id]/route.ts index fa2701c7..52d3dcb1 100644 --- a/src/app/api/tickets/[id]/route.ts +++ b/src/app/api/tickets/[id]/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from "next/server"; -import { ticketUrl } from "../../url"; +import { ticketUrl } from "../../urls"; type Ticket = { id: string; diff --git a/src/app/api/url/index.ts b/src/app/api/url/index.ts deleted file mode 100644 index 35b2532d..00000000 --- a/src/app/api/url/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -const baseUrl = process.env.BASE_URL ?? ""; - -const apiBaseUrl = `${baseUrl}api/`; - -export const issueUrl = `${apiBaseUrl}issues/`; -export const ticketUrl = `${apiBaseUrl}tickets/`; diff --git a/src/app/api/urls/index.ts b/src/app/api/urls/index.ts new file mode 100644 index 00000000..5ab6f965 --- /dev/null +++ b/src/app/api/urls/index.ts @@ -0,0 +1,10 @@ +const baseUrl = process.env.APIS_BASE_URL ?? ""; + +export const issueUrl = `${baseUrl}issues/`; +export const ticketUrl = `${baseUrl}tickets/`; +export const accountUrl = `${baseUrl}account/`; +export const userUrl = `${baseUrl}users/`; +export const sectorUrl = `${baseUrl}sector/`; +export const roleUrl = `${baseUrl}role/`; +export const sessionUrl = `${baseUrl}sessions/`; +export const searchUrl = `${baseUrl}search/`; diff --git a/src/implementations/client/abstractions/httpClient/index.tsx b/src/implementations/client/abstractions/httpClient/index.tsx index 900b4d9c..101911e1 100644 --- a/src/implementations/client/abstractions/httpClient/index.tsx +++ b/src/implementations/client/abstractions/httpClient/index.tsx @@ -1,7 +1,9 @@ /* eslint-disable react-hooks/rules-of-hooks */ +import { HttpClientProps, IHttpResponse } from "@/types"; import axios, { AxiosResponse } from "axios"; import useSWR from "swr"; import useSWRImmutable from "swr/immutable"; +import { IHttpClient, IHttpClientResponse } from "../../interfaces/HttpClient"; /** * A generic HTTP client that fetches data from an API endpoint. @@ -13,35 +15,7 @@ import useSWRImmutable from "swr/immutable"; * @returns The data fetched from the API endpoint. */ -type HttpClientOptions = { - revalidateOnFocus?: boolean; - revalidateOnReconnect?: boolean; - refreshInterval?: boolean; - dontRefresh?: boolean; -}; - -export type HttpClientProps = { - url: string; - type?: "GET" | "POST" | "PUT" | "DELETE"; - body?: Record; - headers?: Record; - shouldFetch?: boolean; - options?: HttpClientOptions; -}; - -type HttpClientResponse = { - result?: { - data?: T; - error?: { - message?: string; - title?: string; - }; - }; - statusCode: number; - headers?: Record; -}; - -export class HTTPClient { +export class HTTPClient implements IHttpClient { get(props: Omit) { return this.httpClient({ ...props, @@ -77,7 +51,7 @@ export class HTTPClient { headers, shouldFetch = true, options, - }: HttpClientProps) { + }: HttpClientProps): IHttpResponse { const fetcher = async () => { let res: AxiosResponse; switch (type) { @@ -102,12 +76,12 @@ export class HTTPClient { result: res.data, statusCode: res.status, headers: res.headers, - } as HttpClientResponse; + } as IHttpClientResponse; }; const newRes = !options?.dontRefresh || typeof window === "undefined" - ? useSWR>(shouldFetch ? url : null, fetcher, { + ? useSWR>(shouldFetch ? url : null, fetcher, { refreshInterval: options?.refreshInterval ? 1000 * 60 * 5 : 0, revalidateOnFocus: true, revalidateOnReconnect: true, @@ -116,7 +90,7 @@ export class HTTPClient { refreshWhenHidden: true, refreshWhenOffline: true, }) - : useSWRImmutable>( + : useSWRImmutable>( shouldFetch ? url : null, fetcher, ); @@ -125,7 +99,7 @@ export class HTTPClient { data: newRes.data?.result as unknown as T, error: newRes.data?.result?.error || newRes.error, isLoading: (!newRes.data && !newRes.error) || newRes.isLoading, - statusCode: newRes.data?.statusCode, + status: newRes.data?.statusCode, headers: newRes.data?.headers, mutate: newRes.mutate, isValidating: newRes.isValidating, diff --git a/src/implementations/client/interfaces/HttpClient/index.ts b/src/implementations/client/interfaces/HttpClient/index.ts new file mode 100644 index 00000000..7d815cc6 --- /dev/null +++ b/src/implementations/client/interfaces/HttpClient/index.ts @@ -0,0 +1,26 @@ +/* eslint-disable no-unused-vars */ +import { HttpClientProps, IHttpResponse } from "@/types"; + +export interface IHttpClientResponse { + result?: { + data?: T; + error?: { + message?: string; + title?: string; + }; + }; + statusCode: number; + headers?: Record; +} + +export interface IHttpClient { + get(props: Omit): IHttpResponse; + + post(props: Omit): IHttpResponse; + + put(props: Omit): IHttpResponse; + + delete(props: Omit): IHttpResponse; + + // patch(props: Omit): IHttpResponse; +} diff --git a/src/implementations/client/interfaces/index.ts b/src/implementations/client/interfaces/index.ts index e69de29b..c8ac5797 100644 --- a/src/implementations/client/interfaces/index.ts +++ b/src/implementations/client/interfaces/index.ts @@ -0,0 +1 @@ +export * from "./HttpClient"; diff --git a/src/screens/Register/UI/components/Form/WhatYouDo/index.tsx b/src/screens/Register/UI/components/Form/WhatYouDo/index.tsx index 7e64bd31..b71c6dc4 100644 --- a/src/screens/Register/UI/components/Form/WhatYouDo/index.tsx +++ b/src/screens/Register/UI/components/Form/WhatYouDo/index.tsx @@ -1,10 +1,15 @@ import { CustomSelect } from "@/components"; import { theme } from "@/styles"; +import { sectorApi } from "@/utils"; import { Control, useFormContext } from "react-hook-form"; import { Section } from "../components"; export const WhatYouDo = () => { const { control } = useFormContext(); + + const res = sectorApi.getSectors(); + + console.log({ res }); return (
(req); const { userId, isAuthenticated } = await getAuthToken(req); @@ -105,7 +102,8 @@ export class IssueController { ); } - const issue = await IssueServices.createIssue(userId, body); + // @todo call the api + const issue = {}; return NextResponse.json(TicketView.getTicketId(issue), { status: 201, @@ -128,7 +126,6 @@ export class IssueController { static async getInProgressIssues(req: NextRequest) { try { - await startDBConnection(); const { userId, isAuthenticated } = await getAuthToken(req); if (!isAuthenticated || !userId) { @@ -140,7 +137,8 @@ export class IssueController { ); } - const tickets = await IssueServices.getInProgressIssues(userId); + // @todo call the api + const tickets = []; if (!tickets?.length) { return NextResponse.json( @@ -172,7 +170,6 @@ export class IssueController { static async closeIssue(req: NextRequest, params: { id: string }) { try { - await startDBConnection(); const ticketId = params.id; const { userId, isAuthenticated } = await getAuthToken(req); @@ -194,7 +191,8 @@ export class IssueController { ); } - const ticket = await IssueServices.closeIssue(userId, ticketId); + // @todo call the api + const ticket = {}; if (!ticket) { return NextResponse.json( @@ -228,7 +226,6 @@ export class IssueController { static async startIssue(req: NextRequest, params: { id: string }) { try { - await startDBConnection(); const ticketId = params.id; const { userId, isAuthenticated } = await getAuthToken(req); @@ -250,7 +247,8 @@ export class IssueController { ); } - const ticket = await IssueServices.startIssue(userId, ticketId); + // @todo call the api + const ticket = {}; if (!ticket) { return NextResponse.json( @@ -284,7 +282,6 @@ export class IssueController { static async reopenIssue(req: NextRequest, params: { id: string }) { try { - await startDBConnection(); const ticketId = params.id; const { userId, isAuthenticated } = await getAuthToken(req); @@ -306,7 +303,8 @@ export class IssueController { ); } - const ticket = await IssueServices.reopenIssue(userId, ticketId); + // @todo call the api + const ticket = {}; if (!ticket) { return NextResponse.json( diff --git a/src/server/controllers/Search.ts b/src/server/controllers/Search.ts index 69b24e70..b85c46d6 100644 --- a/src/server/controllers/Search.ts +++ b/src/server/controllers/Search.ts @@ -1,13 +1,10 @@ import { getAuthToken } from "@/server/functions/getAuthToken"; -import { SearchServices } from "@/server/services"; -import { NextRequest, NextResponse } from "next/server"; -import { startDBConnection } from "@/database"; import { captureException } from "@sentry/nextjs"; +import { NextRequest, NextResponse } from "next/server"; export class SearchController { static async searchTickets(req: NextRequest) { try { - await startDBConnection(); const searchTerm = req.nextUrl.searchParams.get("searchTerm"); const { userId, isAuthenticated } = await getAuthToken(req); @@ -36,7 +33,10 @@ export class SearchController { ); } - const tickets = await SearchServices.searchTickets(userId, searchTerm); + // @todo call the api + // const tickets = await SearchServices.searchTickets(userId, searchTerm); + + const tickets = []; if (!tickets.length) { return NextResponse.json( @@ -54,12 +54,12 @@ export class SearchController { }, ); } catch (error) { - captureException(error, { - tags: { - controller: "SearchController", - method: "searchTickets", - }, - }); + captureException(error, { + tags: { + controller: "SearchController", + method: "searchTickets", + }, + }); return NextResponse.json({ error }); } } diff --git a/src/server/controllers/Session.ts b/src/server/controllers/Session.ts index 55016410..f01a3158 100644 --- a/src/server/controllers/Session.ts +++ b/src/server/controllers/Session.ts @@ -68,8 +68,6 @@ export class SessionController { const resBody = await res.json(); - console.log({ resBody }); - const { data, error, status } = resBody as IHttpResponse< ISessionResponse, { message?: string; title?: string } diff --git a/src/server/controllers/Ticket.ts b/src/server/controllers/Ticket.ts index aacb1e51..663e63e7 100644 --- a/src/server/controllers/Ticket.ts +++ b/src/server/controllers/Ticket.ts @@ -1,13 +1,10 @@ -import { startDBConnection } from "@/database"; import { getAuthToken } from "@/server/functions/getAuthToken"; import { captureException } from "@sentry/nextjs"; import { NextRequest, NextResponse } from "next/server"; -import { TicketServices } from "../services"; class TicketController { static async getAllTickets(req: NextRequest) { try { - await startDBConnection(); const { userId, isAuthenticated } = await getAuthToken(req); if (!isAuthenticated || !userId) { @@ -19,7 +16,8 @@ class TicketController { ); } - const issues = await TicketServices.getAllTickets(userId); + // @todo call the api + const issues = []; if (!issues?.length) { return NextResponse.json( @@ -58,9 +56,10 @@ class TicketController { ); } - const issue = await TicketServices.getTicketById(userId, issueId); + // @todo call the api + const issue = {}; - if (!issue?.length) { + if (!issue) { return NextResponse.json( { error: { message: "No issue found" } }, { @@ -106,7 +105,8 @@ class TicketController { ); } - const ticket = await TicketServices.startTicket(userId, issueId); + // @todo call the api + const ticket = {}; if (!ticket) { return NextResponse.json( diff --git a/src/server/controllers/User.ts b/src/server/controllers/User.ts index cb7c82fa..7ac3d52c 100644 --- a/src/server/controllers/User.ts +++ b/src/server/controllers/User.ts @@ -1,17 +1,13 @@ -import { startDBConnection } from "@/database"; import { UserView } from "@/server/views"; import { IRegisterUser, RegisterUserSchema } from "@/types"; import { AuthErrorMessage } from "@/types/Interfaces/Auth"; import { captureException } from "@sentry/nextjs"; import { NextRequest, NextResponse } from "next/server"; import { getAuthToken } from "../functions/getAuthToken"; -import { userModel } from "../models"; -import { UserServices } from "../services"; export class UserController { static async authUser(req: NextRequest) { try { - await startDBConnection(); const { isAuthenticated, userId } = await getAuthToken(req); if (!isAuthenticated || !userId) { @@ -23,24 +19,8 @@ export class UserController { ); } - await userModel.init({ - register: userId, - }); - - if (!userModel.exists({ safe: true })) { - return NextResponse.json( - UserView.getUser({ - user: null, - error: { message: AuthErrorMessage.UserNotExist }, - status: 404, - }), - { - status: 404, - }, - ); - } - - const userData = userModel.getData(); + // @todo call the api + const userData = {}; return NextResponse.json( UserView.getUser({ user: userData, status: 200 }), @@ -69,7 +49,6 @@ export class UserController { static async createUser(req: NextRequest) { try { - await startDBConnection(); const data: IRegisterUser = await req.json(); const { email, password, register } = data; @@ -87,8 +66,10 @@ export class UserController { }, ); } + // @todo call the api + const existingUser = {}; - const existingUser = await UserServices.exits(register, email); + const user = {}; if (existingUser) { return NextResponse.json( @@ -103,8 +84,6 @@ export class UserController { ); } - const { user } = await userModel.createUser(data); - return NextResponse.json(UserView.getUser({ user, status: 201 }), { status: 201, }); diff --git a/src/server/functions/getAuthToken/index.ts b/src/server/functions/getAuthToken/index.ts index aead1cba..14a3c7e9 100644 --- a/src/server/functions/getAuthToken/index.ts +++ b/src/server/functions/getAuthToken/index.ts @@ -34,12 +34,14 @@ const getAuthToken = async (req: NextRequest) => { accessToken, userData, userId: String(userData.register), + isAuthenticated: true, }; } catch { return { accessToken: "", userData: null, userId: "", + isAuthenticated: false, }; } }; diff --git a/src/types/Interfaces/Apis.ts b/src/types/Interfaces/Apis.ts new file mode 100644 index 00000000..d6a61cbe --- /dev/null +++ b/src/types/Interfaces/Apis.ts @@ -0,0 +1,8 @@ +import { IHttpClient } from "@/implementations/client/interfaces"; + +export interface IApi { + httpClient: IHttpClient; + baseUrl?: string; + apiUrl: string; +// appMonitoringClient?: unknown; +} diff --git a/src/types/Interfaces/HttpResponse.ts b/src/types/Interfaces/HttpResponse.ts index 97df236e..cf6760e9 100644 --- a/src/types/Interfaces/HttpResponse.ts +++ b/src/types/Interfaces/HttpResponse.ts @@ -1,11 +1,33 @@ +import { IHttpClientResponse } from "@/implementations/client/interfaces"; +import { KeyedMutator } from "swr"; + type ErrorLevel = "low" | "medium" | "critical"; +export type HttpClientOptions = { + revalidateOnFocus?: boolean; + revalidateOnReconnect?: boolean; + refreshInterval?: boolean; + dontRefresh?: boolean; +}; + +export interface HttpClientProps { + url: string; + type?: "GET" | "POST" | "PUT" | "DELETE"; + body?: Record; + headers?: Record; + shouldFetch?: boolean; + options?: HttpClientOptions; +} + export interface IHttpResponse { data?: T; message?: string; isLoading?: boolean; error?: K; status?: number; + headers?: Record; + mutate?: KeyedMutator>; + isValidating?: boolean; } export interface IHttpError { diff --git a/src/types/Interfaces/index.ts b/src/types/Interfaces/index.ts index 6d502056..8e59f45f 100644 --- a/src/types/Interfaces/index.ts +++ b/src/types/Interfaces/index.ts @@ -1,3 +1,4 @@ +export * from "./Apis"; export * from "./Auth"; export * from "./Event"; export * from "./HttpResponse"; diff --git a/src/utils/apis/Issue.api.ts b/src/utils/apis/Issue.api.ts index f8d4f714..d3e7fe22 100644 --- a/src/utils/apis/Issue.api.ts +++ b/src/utils/apis/Issue.api.ts @@ -1,17 +1,20 @@ -import { httpClient } from "@/implementations/client"; +import { IHttpClient } from "@/implementations/client/interfaces"; import { IOpenIssueForm, IssueDto } from "@/types"; /** * The `IssueApi` class provides methods to interact with the issues API, including fetching and creating issues. */ export class IssueApi { - private readonly base_url: string | undefined; + private readonly baseUrl: string | undefined; - private readonly api_url: string; + private readonly apiUrl: string; - constructor() { - this.base_url = process.env.NEXT_PUBLIC_BASE_URL; - this.api_url = `${this.base_url}api/issues`; + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { + this.baseUrl = process.env.NEXT_PUBLIC_BASE_URL; + this.apiUrl = `${this.baseUrl}api/issues`; + this.httpClient = httpClient; } /** @@ -28,8 +31,8 @@ export class IssueApi { }; } - return httpClient.get({ - url: `${this.api_url}/${id}`, + return this.httpClient.get({ + url: `${this.apiUrl}/${id}`, }); }; @@ -38,8 +41,8 @@ export class IssueApi { * @returns An object containing an array of issue data, any error that occurred, and a loading state. */ getIssues = () => - httpClient.get({ - url: `${this.api_url}`, + this.httpClient.get({ + url: `${this.apiUrl}`, options: { refreshInterval: true }, }); @@ -50,8 +53,8 @@ export class IssueApi { * @returns An object containing any error that occurred and a loading state. */ createIssue = (issueData: IOpenIssueForm, shouldFetch = false) => - httpClient.post<{ id: string | number }>({ - url: `${this.api_url}`, + this.httpClient.post<{ id: string | number }>({ + url: `${this.apiUrl}`, body: { ...issueData, }, @@ -59,25 +62,25 @@ export class IssueApi { }); getInProgressIssues = () => - httpClient.get({ - url: `${this.api_url}/inProgress`, + this.httpClient.get({ + url: `${this.apiUrl}/inProgress`, }); closeIssue = (id: string, shouldFetcher = false) => - httpClient.post<{ id: string }>({ - url: `${this.api_url}/${id}/close`, + this.httpClient.post<{ id: string }>({ + url: `${this.apiUrl}/${id}/close`, shouldFetch: shouldFetcher, }); reopenIssue = (id: string, shouldFetcher = false) => - httpClient.post<{ id: string }>({ - url: `${this.api_url}/${id}/reopen`, + this.httpClient.post<{ id: string }>({ + url: `${this.apiUrl}/${id}/reopen`, shouldFetch: shouldFetcher, }); startIssue = (id: string, shouldFetcher = false) => - httpClient.post<{ id: string }>({ - url: `${this.api_url}/${id}/start`, + this.httpClient.post<{ id: string }>({ + url: `${this.apiUrl}/${id}/start`, shouldFetch: shouldFetcher, }); } diff --git a/src/utils/apis/Search.api.ts b/src/utils/apis/Search.api.ts index 38357238..2107413c 100644 --- a/src/utils/apis/Search.api.ts +++ b/src/utils/apis/Search.api.ts @@ -1,4 +1,4 @@ -import { httpClient } from "@/implementations/client"; +import { IHttpClient } from "@/implementations/client/interfaces"; import { TicketDto } from "@/types"; export type SearchResponse = { @@ -10,18 +10,21 @@ export type SearchResponse = { }; export class SearchApi { - private readonly base_url: string | undefined; + private readonly baseUrl: string | undefined; - private readonly api_url: string; + private readonly apiUrl: string; - constructor() { - this.base_url = process.env.NEXT_PUBLIC_BASE_URL; - this.api_url = `${this.base_url}api/search`; + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { + this.baseUrl = process.env.NEXT_PUBLIC_BASE_URL; + this.apiUrl = `${this.baseUrl}api/search`; + this.httpClient = httpClient; } getSearch = (searchTerm: string, shouldFetch: boolean) => - httpClient.get({ - url: `${this.api_url}?searchTerm=${searchTerm}`, + this.httpClient.get({ + url: `${this.apiUrl}?searchTerm=${searchTerm}`, shouldFetch, }); } diff --git a/src/utils/apis/Sector.api.ts b/src/utils/apis/Sector.api.ts index a084e155..54105aae 100644 --- a/src/utils/apis/Sector.api.ts +++ b/src/utils/apis/Sector.api.ts @@ -1,3 +1,27 @@ +import { IHttpClient } from "@/implementations/client/interfaces"; + export class SectorApi { - constructor(private readonly httpClient: HTTPClientImplementation) {} -} \ No newline at end of file + private readonly baseUrl: string | undefined; + + private readonly apiUrl: string; + + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { + this.baseUrl = process.env.NEXT_PUBLIC_BASE_URL; + this.apiUrl = `${this.baseUrl}api/sector`; + this.httpClient = httpClient; + } + + getSectors = () => { + return this.httpClient.get({ + url: `${this.apiUrl}`, + }); + }; + + getRolesBySector = (sectorId: string) => { + return this.httpClient.get({ + url: `${this.apiUrl}${sectorId}/roles`, + }); + }; +} diff --git a/src/utils/apis/Session.api.ts b/src/utils/apis/Session.api.ts index 56ac216e..981bbf33 100644 --- a/src/utils/apis/Session.api.ts +++ b/src/utils/apis/Session.api.ts @@ -1,12 +1,16 @@ +import { IHttpClient } from "@/implementations/client/interfaces"; import { IHttpResponse, ISessionResponse } from "@/types"; import { defaultHeaders } from "@/utils/constraints"; export class SessionApi { - private readonly api_url: string; + private readonly apiUrl: string; - constructor() { - // this.api_url = `${process.env.NEXT_PUBLIC_APIS_BASE_URL}sessions`; - this.api_url = `${process.env.NEXT_PUBLIC_BASE_URL}api/sessions`; + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { + // this.apiUrl = `${process.env.NEXT_PUBLIC_APIS_BASE_URL}sessions`; + this.apiUrl = `${process.env.NEXT_PUBLIC_BASE_URL}api/sessions`; + this.httpClient = httpClient; } createSession = async ( @@ -16,7 +20,7 @@ export class SessionApi { data?: ISessionResponse | null; error?: { message?: string; title?: string } | null; }> => { - const res = await fetch(this.api_url, { + const res = await fetch(this.apiUrl, { method: "POST", headers: { ...defaultHeaders, @@ -35,7 +39,7 @@ export class SessionApi { }; closeSession = async (accessToken: string) => { - const res = await fetch(this.api_url, { + const res = await fetch(this.apiUrl, { method: "DELETE", headers: { Authorization: `Bearer ${accessToken}`, diff --git a/src/utils/apis/Ticket.api.ts b/src/utils/apis/Ticket.api.ts index 4ac168d2..e86d256c 100644 --- a/src/utils/apis/Ticket.api.ts +++ b/src/utils/apis/Ticket.api.ts @@ -1,4 +1,4 @@ -import { httpClient } from "@/implementations/client"; +import { IHttpClient } from "@/implementations/client/interfaces"; import { ITicket, TicketDto } from "@/types"; type InitializeTicketType = { @@ -9,13 +9,16 @@ type InitializeTicketType = { * Represents the API for managing tickets, providing methods to fetch and initialize tickets. */ export class TicketApi { - private readonly base_url: string | undefined; + private readonly baseUrl: string | undefined; - private readonly api_url: string; + private readonly apiUrl: string; - constructor() { - this.base_url = process.env.NEXT_PUBLIC_BASE_URL; - this.api_url = `${this.base_url}api/tickets`; + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { + this.baseUrl = process.env.NEXT_PUBLIC_BASE_URL; + this.apiUrl = `${this.baseUrl}api/tickets`; + this.httpClient = httpClient; } /** @@ -24,8 +27,8 @@ export class TicketApi { * @returns An object containing the issue data, any error, and loading state. */ getTicket = (id: string) => { - const { data, error, isLoading } = httpClient.get({ - url: `${this.api_url}/${id}`, + const { data, error, isLoading } = this.httpClient.get({ + url: `${this.apiUrl}/${id}`, }); return { data, error, isLoading }; }; @@ -35,8 +38,8 @@ export class TicketApi { * @returns An object containing an array of issue data, any error, and loading state. */ getTickets = (shouldFetch: boolean) => - httpClient.get({ - url: `${this.api_url}`, + this.httpClient.get({ + url: `${this.apiUrl}`, shouldFetch, }); @@ -46,8 +49,8 @@ export class TicketApi { * @returns A promise resolving with the result of the initialization request. */ initializeTicket = (TicketData: InitializeTicketType) => - httpClient.put({ - url: `${this.api_url}`, + this.httpClient.put({ + url: `${this.apiUrl}`, body: { ...TicketData, }, diff --git a/src/utils/apis/User.api.ts b/src/utils/apis/User.api.ts index 2745593a..ef6be315 100644 --- a/src/utils/apis/User.api.ts +++ b/src/utils/apis/User.api.ts @@ -1,14 +1,17 @@ -import { httpClient } from "@/implementations/client"; +import { IHttpClient } from "@/implementations/client/interfaces"; import { IUser } from "@/types"; export class UserApi { private readonly base_url: string | undefined; - private readonly api_url: string; + private readonly apiUrl: string; - constructor() { + private readonly httpClient: IHttpClient; + + constructor(httpClient: IHttpClient) { this.base_url = process.env.NEXT_PUBLIC_BASE_URL; - this.api_url = `${this.base_url}api/user`; + this.apiUrl = `${this.base_url}api/user`; + this.httpClient = httpClient; } getUser = ({ @@ -18,8 +21,8 @@ export class UserApi { accessToken: string; shouldFetch?: boolean; }) => - httpClient.get({ - url: this.api_url, + this.httpClient.get({ + url: this.apiUrl, headers: { Authorization: `Bearer ${accessToken}`, }, diff --git a/src/utils/apis/index.ts b/src/utils/apis/index.ts index 63871a47..6aec37fc 100644 --- a/src/utils/apis/index.ts +++ b/src/utils/apis/index.ts @@ -1,11 +1,14 @@ +import { httpClient } from "@/implementations/client"; import { IssueApi } from "./Issue.api"; import { SearchApi } from "./Search.api"; +import { SectorApi } from "./Sector.api"; import { SessionApi } from "./Session.api"; import { TicketApi } from "./Ticket.api"; import { UserApi } from "./User.api"; -export const ticketApi = new TicketApi(); -export const issueApi = new IssueApi(); -export const searchApi = new SearchApi(); -export const userApi = new UserApi(); -export const sessionApi = new SessionApi(); +export const ticketApi = new TicketApi(httpClient); +export const issueApi = new IssueApi(httpClient); +export const searchApi = new SearchApi(httpClient); +export const userApi = new UserApi(httpClient); +export const sessionApi = new SessionApi(httpClient); +export const sectorApi = new SectorApi(httpClient);