Refactor
This commit is contained in:
parent
1fbd120404
commit
79dbeb6b7a
10 changed files with 52 additions and 27 deletions
|
|
@ -8,11 +8,11 @@ import {
|
|||
const fetchLoginApi = async (
|
||||
request: IFetchLoginRequest,
|
||||
): Promise<IFetchLoginResponse> => {
|
||||
return await callApi(HTTP.POST, `auth/login`, request);
|
||||
return await callApi(HTTP.POST, `auth/login`, request, true);
|
||||
};
|
||||
|
||||
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 };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
interface IFetchChannel {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
communityId: string;
|
||||
creationDate: number;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,19 @@
|
|||
interface IFetchCommunity {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
ownerId: string;
|
||||
creationDate: number;
|
||||
}
|
||||
|
||||
interface IFetchCommunityRequest {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IFetchCommunityResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
interface IFetchCommunityResponse extends IFetchCommunity {}
|
||||
|
||||
export { type IFetchCommunityRequest, type IFetchCommunityResponse };
|
||||
export {
|
||||
type IFetchCommunity,
|
||||
type IFetchCommunityRequest,
|
||||
type IFetchCommunityResponse,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
interface IFetchRole {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
communityId: string;
|
||||
permissions: string[];
|
||||
creationDate: number;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,33 +2,26 @@ import { state } from "../store/state";
|
|||
import config from "./config.json";
|
||||
|
||||
enum HTTP {
|
||||
GET_REFRESH,
|
||||
GET,
|
||||
POST,
|
||||
PATCH,
|
||||
DELETE,
|
||||
}
|
||||
|
||||
async function callApi(type: HTTP, path: string, body?: object) {
|
||||
async function callApi(
|
||||
type: HTTP,
|
||||
path: string,
|
||||
body?: object,
|
||||
includeCookies?: boolean,
|
||||
) {
|
||||
let response: Response;
|
||||
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:
|
||||
response = await fetch(
|
||||
`${config.schema}://${config.url}:${config.port}/${config.path}/${path}`,
|
||||
{
|
||||
method: "GET",
|
||||
credentials: includeCookies ? "include" : "omit",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
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}`,
|
||||
{
|
||||
method: "POST",
|
||||
credentials: includeCookies ? "include" : "omit",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
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}`,
|
||||
{
|
||||
method: "PATCH",
|
||||
credentials: includeCookies ? "include" : "omit",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
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}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
credentials: includeCookies ? "include" : "omit",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${state.auth.session?.token}`,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,13 @@
|
|||
interface IFetchUser {
|
||||
id: string;
|
||||
username: string;
|
||||
email?: string;
|
||||
description?: string;
|
||||
admin: boolean;
|
||||
registerDate: number;
|
||||
lastLogin: number;
|
||||
}
|
||||
|
||||
interface IFetchLoggedUserResponse {
|
||||
id: string;
|
||||
}
|
||||
|
|
@ -6,11 +16,7 @@ interface IFetchUserRequest {
|
|||
id: string;
|
||||
}
|
||||
|
||||
interface IFetchUserResponse {
|
||||
id: string;
|
||||
username: string;
|
||||
description: string;
|
||||
}
|
||||
interface IFetchUserResponse extends IFetchUser {}
|
||||
|
||||
interface IFetchUserCommunitiesRequest {
|
||||
id: string;
|
||||
|
|
@ -28,6 +34,7 @@ interface IFetchUserCommunity {
|
|||
}
|
||||
|
||||
export {
|
||||
type IFetchUser,
|
||||
type IFetchLoggedUserResponse,
|
||||
type IFetchUserRequest,
|
||||
type IFetchUserResponse,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue