This commit is contained in:
Aslan 2026-01-07 17:42:16 -05:00
parent 1fbd120404
commit 79dbeb6b7a
10 changed files with 52 additions and 27 deletions

View file

@ -8,11 +8,11 @@ import {
const fetchLoginApi = async ( const fetchLoginApi = async (
request: IFetchLoginRequest, request: IFetchLoginRequest,
): Promise<IFetchLoginResponse> => { ): Promise<IFetchLoginResponse> => {
return await callApi(HTTP.POST, `auth/login`, request); return await callApi(HTTP.POST, `auth/login`, request, true);
}; };
const fetchRefreshApi = async (): Promise<IFetchRefreshResponse> => { const fetchRefreshApi = async (): Promise<IFetchRefreshResponse> => {
return await callApi(HTTP.GET_REFRESH, `auth/refresh`); return await callApi(HTTP.GET, `auth/refresh`, undefined, true);
}; };
export { fetchLoginApi, fetchRefreshApi }; export { fetchLoginApi, fetchRefreshApi };

View file

@ -1,6 +1,7 @@
interface IFetchChannel { interface IFetchChannel {
id: string; id: string;
name: string; name: string;
description?: string;
communityId: string; communityId: string;
creationDate: number; creationDate: number;
} }

View file

@ -1,11 +1,19 @@
interface IFetchCommunity {
id: string;
name: string;
description?: string;
ownerId: string;
creationDate: number;
}
interface IFetchCommunityRequest { interface IFetchCommunityRequest {
id: string; id: string;
} }
interface IFetchCommunityResponse { interface IFetchCommunityResponse extends IFetchCommunity {}
id: string;
name: string;
description: string;
}
export { type IFetchCommunityRequest, type IFetchCommunityResponse }; export {
type IFetchCommunity,
type IFetchCommunityRequest,
type IFetchCommunityResponse,
};

View file

@ -1,7 +1,9 @@
interface IFetchRole { interface IFetchRole {
id: string; id: string;
name: string; name: string;
description?: string;
communityId: string; communityId: string;
permissions: string[];
creationDate: number; creationDate: number;
} }

View file

@ -2,33 +2,26 @@ import { state } from "../store/state";
import config from "./config.json"; import config from "./config.json";
enum HTTP { enum HTTP {
GET_REFRESH,
GET, GET,
POST, POST,
PATCH, PATCH,
DELETE, DELETE,
} }
async function callApi(type: HTTP, path: string, body?: object) { async function callApi(
type: HTTP,
path: string,
body?: object,
includeCookies?: boolean,
) {
let response: Response; let response: Response;
switch (type) { switch (type) {
case HTTP.GET_REFRESH:
response = await fetch(
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
},
);
break;
case HTTP.GET: case HTTP.GET:
response = await fetch( response = await fetch(
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`, `${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{ {
method: "GET", method: "GET",
credentials: includeCookies ? "include" : "omit",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`, Authorization: `Bearer ${state.auth.session?.token}`,
@ -41,6 +34,7 @@ async function callApi(type: HTTP, path: string, body?: object) {
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`, `${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{ {
method: "POST", method: "POST",
credentials: includeCookies ? "include" : "omit",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`, Authorization: `Bearer ${state.auth.session?.token}`,
@ -54,6 +48,7 @@ async function callApi(type: HTTP, path: string, body?: object) {
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`, `${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{ {
method: "PATCH", method: "PATCH",
credentials: includeCookies ? "include" : "omit",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`, Authorization: `Bearer ${state.auth.session?.token}`,
@ -67,6 +62,7 @@ async function callApi(type: HTTP, path: string, body?: object) {
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`, `${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
{ {
method: "DELETE", method: "DELETE",
credentials: includeCookies ? "include" : "omit",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${state.auth.session?.token}`, Authorization: `Bearer ${state.auth.session?.token}`,

View file

@ -1,3 +1,13 @@
interface IFetchUser {
id: string;
username: string;
email?: string;
description?: string;
admin: boolean;
registerDate: number;
lastLogin: number;
}
interface IFetchLoggedUserResponse { interface IFetchLoggedUserResponse {
id: string; id: string;
} }
@ -6,11 +16,7 @@ interface IFetchUserRequest {
id: string; id: string;
} }
interface IFetchUserResponse { interface IFetchUserResponse extends IFetchUser {}
id: string;
username: string;
description: string;
}
interface IFetchUserCommunitiesRequest { interface IFetchUserCommunitiesRequest {
id: string; id: string;
@ -28,6 +34,7 @@ interface IFetchUserCommunity {
} }
export { export {
type IFetchUser,
type IFetchLoggedUserResponse, type IFetchLoggedUserResponse,
type IFetchUserRequest, type IFetchUserRequest,
type IFetchUserResponse, type IFetchUserResponse,

View file

@ -5,7 +5,9 @@ interface IChannelState {
interface IChannel { interface IChannel {
id: string; id: string;
name?: string; name?: string;
description?: string;
communityId?: string; communityId?: string;
creationDate?: number;
} }
export { type IChannelState, type IChannel }; export { type IChannelState, type IChannel };

View file

@ -6,6 +6,8 @@ interface ICommunity {
id: string; id: string;
name?: string; name?: string;
description?: string; description?: string;
owner?: string;
creationDate?: number;
} }
export { type ICommunityState, type ICommunity }; export { type ICommunityState, type ICommunity };

View file

@ -5,7 +5,10 @@ interface IRoleState {
interface IRole { interface IRole {
id: string; id: string;
name?: string; name?: string;
description?: string;
communityId?: string; communityId?: string;
permissions?: string[];
creationDate?: number;
} }
export { type IRoleState, type IRole }; export { type IRoleState, type IRole };

View file

@ -6,7 +6,11 @@ interface IUserState {
interface IUser { interface IUser {
id: string; id: string;
username?: string; username?: string;
email?: string;
description?: string; description?: string;
admin?: boolean;
registerDate?: number;
lastLogin?: number;
communities?: string[]; communities?: string[];
} }