chore: move api to api folder and seperate the types into another file

This commit is contained in:
Koosha Lahouti
2025-08-15 15:21:34 +03:30
parent 5a72b597c8
commit 0b42f4ccba
22 changed files with 845 additions and 1402 deletions

View File

@@ -1,22 +1,23 @@
import axios, { isAxiosError } from 'axios';
import apiClient from '@/lib/apiClient';
import {
type GetProfileApiResponse,
type SaveProfileApiResponse,
type TokenApiResponse,
type PhoneNumberApiResponse,
type ConfirmPhoneNumberApiResponse,
type SaveSettingsApiResponse,
type DeleteSessionsApiResponse,
type PasswordApiResponse,
type SendEmailCodeApiResponse,
type ConfirmEmailCodeApiResponse,
type ChangeEmailApiResponse,
} from '../types/settingsApiType';
import { type InfoRowData } from '../types/settingsType';
const storedToken = () => localStorage.getItem('authToken');
export async function fetchProfile(): Promise<{ data: GetProfileApiResponse }> {
const res = await apiClient.post<GetProfileApiResponse>(
'/Profile/GetProfile',
{},
{ headers: { Authorization: `Bearer ${storedToken()}` } },
);
return { data: res.data };
}
@@ -28,56 +29,13 @@ export async function saveProfile(payload?: {
if (!payload) {
throw new Error('Payload for saving profile is missing.');
}
const res = await apiClient.post<SaveProfileApiResponse>(
'/Profile/SaveProfilePersonalInforamtion',
payload,
{ headers: { Authorization: `Bearer ${storedToken()}` } },
);
return { data: res.data };
}
export async function getToken(): Promise<{
data: { success: boolean; message?: string };
}> {
const apiUrl = 'https://accounts.business-harmony.com';
const tokenEndpoint = `${apiUrl}/connect/token`;
try {
const body = new URLSearchParams();
body.set('grant_type', 'password');
body.set('username', 'zareian.1381@gmail.com');
body.set('password', '123@Qweasd');
body.set('client_id', 'harmony_identity');
body.set('scope', 'openid harmony_identity profile offline_access');
const response = await axios.post<TokenApiResponse>(
tokenEndpoint,
body.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
if (response.data?.access_token) {
localStorage.setItem('authToken', response.data.access_token);
return { data: { success: true } };
} else {
throw new Error('No access token in response');
}
} catch (error: unknown) {
let message = 'Failed to get token';
if (isAxiosError(error) && error.response) {
message = `Request failed with status ${error.response.status}`;
} else if (error instanceof Error) {
message = error.message;
}
throw new Error(message);
}
}
export async function sendVerificationCode(payload?: {
phoneNumber: string;
}): Promise<{ data: PhoneNumberApiResponse }> {
@@ -87,7 +45,6 @@ export async function sendVerificationCode(payload?: {
const res = await apiClient.post<PhoneNumberApiResponse>(
'/Profile/SendVerfiyPhoneNumberCode',
payload,
{ headers: { Authorization: `Bearer ${storedToken()}` } },
);
return { data: res.data };
}
@@ -102,7 +59,6 @@ export async function confirmPhoneNumberCode(payload?: {
const res = await apiClient.post<ConfirmPhoneNumberApiResponse>(
'/Profile/ConfirmPhoneNumberChangeCode',
payload,
{ headers: { Authorization: `Bearer ${storedToken()}` } },
);
return { data: res.data };
}
@@ -116,7 +72,102 @@ export async function changePhoneNumber(payload?: {
const res = await apiClient.post<PhoneNumberApiResponse>(
'/Profile/ChangePhoneNumber',
payload,
{ headers: { Authorization: `Bearer ${storedToken()}` } },
);
return { data: res.data };
}
export async function saveSettings(payload?: {
theme: number;
calendarType: number;
language: number;
}): Promise<{ data: SaveSettingsApiResponse }> {
if (!payload) {
throw new Error('Payload for saving settings is missing.');
}
const res = await apiClient.post<SaveSettingsApiResponse>(
'/Profile/SaveSetting',
payload,
);
return { data: res.data };
}
export async function deleteSessions(payload?: {
keys: string[];
}): Promise<{ data: DeleteSessionsApiResponse }> {
if (!payload || payload.keys.length === 0) {
throw new Error('Payload with session keys is missing or empty.');
}
const res = await apiClient.post<DeleteSessionsApiResponse>(
'/Profile/DeleteSessions',
payload,
);
return { data: res.data };
}
export async function addPassword(payload?: {
password: string;
}): Promise<{ data: PasswordApiResponse }> {
if (!payload) {
throw new Error('Payload for adding password is missing.');
}
const res = await apiClient.post<PasswordApiResponse>(
'/Profile/AddPassword',
payload,
);
return { data: res.data };
}
export async function changePassword(payload?: {
oldPassword: string;
newPassword: string;
}): Promise<{ data: PasswordApiResponse }> {
if (!payload) {
throw new Error('Payload for changing password is missing.');
}
const res = await apiClient.post<PasswordApiResponse>(
'/Profile/ChangePassword',
payload,
);
return { data: res.data };
}
// ✅ NEW FUNCTIONS
export async function sendEmailCode(payload?: {
email: string;
}): Promise<{ data: SendEmailCodeApiResponse }> {
if (!payload) {
throw new Error('Payload for sending email code is missing.');
}
const res = await apiClient.post<SendEmailCodeApiResponse>(
'Profile/SendEmailChangeCode',
payload,
);
return { data: res.data };
}
export async function confirmEmailCode(payload?: {
email: string;
verifyCode: string;
}): Promise<{ data: ConfirmEmailCodeApiResponse }> {
if (!payload) {
throw new Error('Payload for confirming email code is missing.');
}
const res = await apiClient.post<ConfirmEmailCodeApiResponse>(
'Profile/ConfirmEmailChangeCode',
payload,
);
return { data: res.data };
}
export async function changeEmail(payload?: {
email: string;
}): Promise<{ data: ChangeEmailApiResponse }> {
if (!payload) {
throw new Error('Payload for changing email is missing.');
}
const res = await apiClient.post<ChangeEmailApiResponse>(
'Profile/ChangeEmail',
payload,
);
return { data: res.data };
}