first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { ApiError } from "@core/Models/Server.Error.model";
|
||||
|
||||
class ApiServerService {
|
||||
post<T>(endpoint: string, data: object): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const url = process.env.NEXT_PUBLIC_API_URL;
|
||||
fetch(`${url}${endpoint}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
cache: "no-store",
|
||||
})
|
||||
.then((response) => {
|
||||
resolve(response.json());
|
||||
})
|
||||
.catch((error) => {
|
||||
try {
|
||||
reject(new ApiError(error.response.data.code, error.response.data.desc));
|
||||
} catch {
|
||||
const errorMessage = process.env.VITE_API_UNAVAILABLE as string;
|
||||
reject(new ApiError(500, errorMessage));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const API_SERVER = new ApiServerService();
|
||||
|
||||
export default API_SERVER;
|
||||
@@ -0,0 +1,64 @@
|
||||
import { getTokenFromLocalStorage } from "@config/token";
|
||||
import { ApiError } from "@models/Server.Error.model";
|
||||
import axios from "@config/axios.config";
|
||||
|
||||
class ApiService {
|
||||
/**
|
||||
* Sends a HTTP POST request to the specified endpoint with the provided data
|
||||
* and returns a promise that resolves to the response data.
|
||||
*
|
||||
* @param {string} endpoint - The URL or path of the API endpoint to send the request to.
|
||||
* @param {object} data - The data to be sent with the request.
|
||||
* @return {Promise<T>} A promise that resolves to the response data of type T.
|
||||
*/
|
||||
post<T>(endpoint: string, data: object): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
axios()
|
||||
.post<T>(endpoint, data)
|
||||
.then((response) => {
|
||||
resolve(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
try {
|
||||
const data = error.response.data;
|
||||
const msg = data.desc || data.message || JSON.stringify(data.details || data);
|
||||
reject(new ApiError(data.code || error.response.status || 500, msg));
|
||||
} catch {
|
||||
const errorMessage = process.env.VITE_API_UNAVAILABLE as string;
|
||||
reject(new ApiError(500, errorMessage));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
postFile<T>(endpoint: string, data: FormData): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
axios()
|
||||
.post<T>(endpoint, data, {
|
||||
headers: {
|
||||
"content-type": "multipart/form-data",
|
||||
Authorization: getTokenFromLocalStorage(),
|
||||
},
|
||||
})
|
||||
.then((response) => {
|
||||
resolve(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
try {
|
||||
console.error("Full API Error:", error.response?.data);
|
||||
const data = error.response.data;
|
||||
const msg = data.desc || data.message || JSON.stringify(data.details || data);
|
||||
reject(new ApiError(data.code || error.response.status || 500, msg));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
const errorMessage = process.env.VITE_API_UNAVAILABLE as string;
|
||||
reject(new ApiError(500, errorMessage));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const API = new ApiService();
|
||||
|
||||
export default API;
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ApiVoidResult } from "@core/Models/Api.VoidResult.type";
|
||||
import ApiRequest from "@services/Api.Service";
|
||||
|
||||
export type ErrorReportParams = {
|
||||
message: string;
|
||||
url: string;
|
||||
userName: string;
|
||||
userEmail: string;
|
||||
userId: string;
|
||||
dateError: Date;
|
||||
};
|
||||
|
||||
export const reportError = async (data: ErrorReportParams): Promise<ApiVoidResult> => {
|
||||
return ApiRequest.post<ApiVoidResult>("error-reporting/report", data);
|
||||
};
|
||||
@@ -0,0 +1,89 @@
|
||||
import { ISession } from "@models/Session.model";
|
||||
import ApiRequest from "./Api.Service";
|
||||
import { ApiVoidResult } from "@core/Models/Api.VoidResult.type";
|
||||
import { CheckUserPhoneParams, RecoveryAccountParams } from "@core/Models/User.model";
|
||||
|
||||
/**
|
||||
* Logs in a user with the given email and password.
|
||||
* @param {string} email - The user's email.
|
||||
* @param {string} password - The user's password.
|
||||
* @returns {Promise<ISession>} - The session object containing the user's session details.
|
||||
*/
|
||||
export async function login(email: string, password: string): Promise<ISession> {
|
||||
// Send a POST request to the "users/login" endpoint with the email and password
|
||||
return ApiRequest.post<ISession>("users/login", {
|
||||
email: email,
|
||||
password: password,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in a user using a token.
|
||||
*
|
||||
* @param {string} token - The token to authenticate the user.
|
||||
* @returns {Promise<ISession>} - A promise that resolves to a session object if the login is successful, or null if it fails.
|
||||
*/
|
||||
export async function loginByToken(token: string): Promise<ISession> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
return ApiRequest.post<ISession>("users/loginbytoken", {
|
||||
token: token,
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkUserPhone(data: CheckUserPhoneParams): Promise<boolean> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
return ApiRequest.post<boolean>("users/check-phone", data);
|
||||
}
|
||||
|
||||
export async function loginByGoogle(token: string): Promise<ISession> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
return ApiRequest.post<ISession>("users/loginbygoogle", {
|
||||
token: token,
|
||||
});
|
||||
}
|
||||
|
||||
export async function loginByAndroid(token: string): Promise<ISession> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
return ApiRequest.post<ISession>("users/loginbyandroid", {
|
||||
token: token,
|
||||
});
|
||||
}
|
||||
|
||||
export async function loginAndVerificate(
|
||||
email: string,
|
||||
password: string,
|
||||
verificationCode: string
|
||||
): Promise<ISession> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
return ApiRequest.post<ISession>("users/verificate", {
|
||||
email,
|
||||
password,
|
||||
verificationCode,
|
||||
});
|
||||
}
|
||||
|
||||
export async function sendRecoveryCode(email: string): Promise<ApiVoidResult> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
const data: RecoveryAccountParams = {
|
||||
email,
|
||||
sendRecoveryCode: true,
|
||||
};
|
||||
|
||||
return ApiRequest.post<ApiVoidResult>("users/recovery", data);
|
||||
}
|
||||
|
||||
export async function recoveryAccountWithCode(
|
||||
email: string,
|
||||
newPassword: string,
|
||||
recoveryCode: string
|
||||
): Promise<ApiVoidResult> {
|
||||
// Make a POST request to the "users/loginbytoken" endpoint with the provided token
|
||||
const data: RecoveryAccountParams = {
|
||||
email,
|
||||
newPassword,
|
||||
recoveryCode: recoveryCode,
|
||||
sendRecoveryCode: false,
|
||||
};
|
||||
|
||||
return ApiRequest.post<ApiVoidResult>("users/recovery", data);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// socket.ts
|
||||
import { io, Socket } from "socket.io-client";
|
||||
|
||||
export let clientSocket: Socket | undefined;
|
||||
|
||||
export const getClientSocket = (userId: string) => {
|
||||
const serverUrl = process.env.NEXT_PUBLIC_CHAT_URL;
|
||||
clientSocket = io(serverUrl, {
|
||||
auth: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
|
||||
return clientSocket;
|
||||
};
|
||||
Reference in New Issue
Block a user