Skip to content

Commit

Permalink
feat: ✨ Adiciona novas interfaces e refatora APIs, incluindo a implem…
Browse files Browse the repository at this point in the history
…entação de SectorApi e ajustes na estrutura de URLs
  • Loading branch information
Wellington Braga committed Jan 26, 2025
1 parent 8c1ee2f commit cae25e2
Show file tree
Hide file tree
Showing 26 changed files with 228 additions and 162 deletions.
Empty file.
5 changes: 5 additions & 0 deletions src/app/api/public/register/getSectors/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextRequest } from "next/server";

export async function GET(req: NextRequest) {
return SearchController.searchTickets(req);
}
Empty file.
2 changes: 1 addition & 1 deletion src/app/api/tickets/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { ticketUrl } from "../../url";
import { ticketUrl } from "../../urls";

type Ticket = {
id: string;
Expand Down
6 changes: 0 additions & 6 deletions src/app/api/url/index.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/app/api/urls/index.ts
Original file line number Diff line number Diff line change
@@ -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/`;
42 changes: 8 additions & 34 deletions src/implementations/client/abstractions/httpClient/index.tsx
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<string, string | number | boolean>;
headers?: Record<string, string | number | boolean>;
shouldFetch?: boolean;
options?: HttpClientOptions;
};

type HttpClientResponse<T> = {
result?: {
data?: T;
error?: {
message?: string;
title?: string;
};
};
statusCode: number;
headers?: Record<string, string | number | boolean>;
};

export class HTTPClient {
export class HTTPClient implements IHttpClient {
get<T>(props: Omit<HttpClientProps, "type">) {
return this.httpClient<T>({
...props,
Expand Down Expand Up @@ -77,7 +51,7 @@ export class HTTPClient {
headers,
shouldFetch = true,
options,
}: HttpClientProps) {
}: HttpClientProps): IHttpResponse<T, unknown> {
const fetcher = async () => {
let res: AxiosResponse<T>;
switch (type) {
Expand All @@ -102,12 +76,12 @@ export class HTTPClient {
result: res.data,
statusCode: res.status,
headers: res.headers,
} as HttpClientResponse<T>;
} as IHttpClientResponse<T>;
};

const newRes =
!options?.dontRefresh || typeof window === "undefined"
? useSWR<HttpClientResponse<T>>(shouldFetch ? url : null, fetcher, {
? useSWR<IHttpClientResponse<T>>(shouldFetch ? url : null, fetcher, {
refreshInterval: options?.refreshInterval ? 1000 * 60 * 5 : 0,
revalidateOnFocus: true,
revalidateOnReconnect: true,
Expand All @@ -116,7 +90,7 @@ export class HTTPClient {
refreshWhenHidden: true,
refreshWhenOffline: true,
})
: useSWRImmutable<HttpClientResponse<T>>(
: useSWRImmutable<IHttpClientResponse<T>>(
shouldFetch ? url : null,
fetcher,
);
Expand All @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions src/implementations/client/interfaces/HttpClient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-unused-vars */
import { HttpClientProps, IHttpResponse } from "@/types";

export interface IHttpClientResponse<T> {
result?: {
data?: T;
error?: {
message?: string;
title?: string;
};
};
statusCode: number;
headers?: Record<string, string | number | boolean>;
}

export interface IHttpClient {
get<T>(props: Omit<HttpClientProps, "type">): IHttpResponse<T, unknown>;

post<T>(props: Omit<HttpClientProps, "type">): IHttpResponse<T, unknown>;

put<T>(props: Omit<HttpClientProps, "type">): IHttpResponse<T, unknown>;

delete<T>(props: Omit<HttpClientProps, "type">): IHttpResponse<T, unknown>;

// patch<T>(props: Omit<HttpClientProps, "type">): IHttpResponse<T, unknown>;
}
1 change: 1 addition & 0 deletions src/implementations/client/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./HttpClient";
5 changes: 5 additions & 0 deletions src/screens/Register/UI/components/Form/WhatYouDo/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Section
title="O que você faz?"
Expand Down
30 changes: 14 additions & 16 deletions src/server/controllers/Issue.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { startDBConnection } from "@/database";
import { TicketView } from "@/server";
import { getAuthToken } from "@/server/functions/getAuthToken";
import { getFormattedBody } from "@/server/functions/getFormattedBody";
import { IOpenIssueForm } from "@/types";
import { captureException } from "@sentry/nextjs";
import { NextRequest, NextResponse } from "next/server";
import { IssueServices } from "../services";

export class IssueController {
static async getAllIssues(req: NextRequest) {
try {
await startDBConnection();
const { isAuthenticated, userId } = await getAuthToken(req);

if (!isAuthenticated || !userId) {
Expand All @@ -25,7 +22,8 @@ export class IssueController {
// const params = req.nextUrl.searchParams;
// const statuses = params.getAll("status");

const tickets = await IssueServices.getAllIssues(userId);
// @todo call the api
const tickets = [];

if (!tickets.length) {
return NextResponse.json(
Expand All @@ -52,7 +50,6 @@ export class IssueController {

static async getIssueById(req: NextRequest, params: { id: string }) {
try {
await startDBConnection();
const ticketId = params.id;
const { userId, isAuthenticated } = await getAuthToken(req);

Expand All @@ -65,7 +62,8 @@ export class IssueController {
);
}

const ticket = (await IssueServices.getIssueById(userId, ticketId)).at(0);
// @todo call the api
const ticket = {};

if (!ticket) {
return NextResponse.json(
Expand All @@ -92,7 +90,6 @@ export class IssueController {

static async createIssue(req: NextRequest) {
try {
await startDBConnection();
const body = await getFormattedBody<IOpenIssueForm>(req);
const { userId, isAuthenticated } = await getAuthToken(req);

Expand All @@ -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,
Expand All @@ -128,7 +126,6 @@ export class IssueController {

static async getInProgressIssues(req: NextRequest) {
try {
await startDBConnection();
const { userId, isAuthenticated } = await getAuthToken(req);

if (!isAuthenticated || !userId) {
Expand All @@ -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(
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand Down
22 changes: 11 additions & 11 deletions src/server/controllers/Search.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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 });
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/server/controllers/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading

0 comments on commit cae25e2

Please sign in to comment.