Refactor types

This commit is contained in:
2025-05-22 21:24:39 +02:00
parent 2f181d0f7f
commit 32218e5595
20 changed files with 457 additions and 418 deletions

View File

@@ -1,15 +1,17 @@
import { apiCall } from './api';
import type { User } from '../types/authApi';
import { API_BASE_URL, isUser } from '../types/authApi';
import type {
CreateUserRequest,
SystemStats,
UpdateUserRequest} from '@/types/adminApi';
import {
isSystemStats
} from '@/types/adminApi';
import type { Workspace } from '@/types/workspace';
import { isWorkspace } from '@/types/workspace';
API_BASE_URL,
type CreateUserRequest,
type UpdateUserRequest,
} from '@/types/api';
import { apiCall } from './api';
import {
isSystemStats,
isUser,
isWorkspace,
type SystemStats,
type User,
type Workspace,
} from '@/types/models';
const ADMIN_BASE_URL = `${API_BASE_URL}/admin`;
@@ -22,7 +24,7 @@ const ADMIN_BASE_URL = `${API_BASE_URL}/admin`;
* */
export const getUsers = async (): Promise<User[]> => {
const response = await apiCall(`${ADMIN_BASE_URL}/users`);
const data = await response.json();
const data: unknown = await response.json();
if (!Array.isArray(data)) {
throw new Error('Invalid users response received from API');
@@ -49,7 +51,7 @@ export const createUser = async (
body: JSON.stringify(userData),
});
const data = await response.json();
const data: unknown = await response.json();
if (!isUser(data)) {
throw new Error('Invalid user object received from API');
}
@@ -88,7 +90,7 @@ export const updateUser = async (
body: JSON.stringify(userData),
});
const data = await response.json();
const data: unknown = await response.json();
if (!isUser(data)) {
throw new Error('Invalid user object received from API');
}
@@ -104,7 +106,7 @@ export const updateUser = async (
* */
export const getWorkspaces = async (): Promise<Workspace[]> => {
const response = await apiCall(`${ADMIN_BASE_URL}/workspaces`);
const data = await response.json();
const data: unknown = await response.json();
if (!Array.isArray(data)) {
throw new Error('Invalid workspaces response received from API');
}
@@ -125,7 +127,7 @@ export const getWorkspaces = async (): Promise<Workspace[]> => {
* */
export const getSystemStats = async (): Promise<SystemStats> => {
const response = await apiCall(`${ADMIN_BASE_URL}/stats`);
const data = await response.json();
const data: unknown = await response.json();
if (!isSystemStats(data)) {
throw new Error('Invalid system stats response received from API');
}

View File

@@ -1,6 +1,6 @@
import type { User, LoginRequest} from '../types/authApi';
import { API_BASE_URL, isUser } from '../types/authApi';
import { API_BASE_URL, isLoginResponse, type LoginRequest } from '@/types/api';
import { apiCall } from './api';
import { isUser, type User } from '@/types/models';
/**
* Logs in a user with email and password
@@ -17,8 +17,8 @@ export const login = async (email: string, password: string): Promise<User> => {
body: JSON.stringify(loginData),
});
const data = await response.json();
if (!('user' in data) || !isUser(data.user)) {
const data: unknown = await response.json();
if (!isLoginResponse(data)) {
throw new Error('Invalid login response from API');
}
@@ -52,7 +52,7 @@ export const refreshToken = async (): Promise<boolean> => {
});
return true;
} catch (error) {
} catch (_error) {
return false;
}
};
@@ -65,7 +65,7 @@ export const refreshToken = async (): Promise<boolean> => {
*/
export const getCurrentUser = async (): Promise<User> => {
const response = await apiCall(`${API_BASE_URL}/auth/me`);
const data = await response.json();
const data: unknown = await response.json();
if (!isUser(data)) {
throw new Error('Invalid user data received from API');

View File

@@ -1,14 +1,11 @@
import { API_BASE_URL } from '@/types/authApi';
import { isFileNode, type FileNode } from '@/types/models';
import { apiCall } from './api';
import type {
FileNode,
LookupResponse,
SaveFileResponse} from '@/types/fileApi';
import {
isFileNode,
API_BASE_URL,
isLookupResponse,
isSaveFileResponse
} from '@/types/fileApi';
isSaveFileResponse,
type SaveFileResponse,
} from '@/types/api';
/**
* listFiles fetches the list of files in a workspace
@@ -20,7 +17,7 @@ export const listFiles = async (workspaceName: string): Promise<FileNode[]> => {
const response = await apiCall(
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/files`
);
const data = await response.json();
const data: unknown = await response.json();
if (!Array.isArray(data)) {
throw new Error('Invalid files response received from API');
}
@@ -48,7 +45,7 @@ export const lookupFileByName = async (
workspaceName
)}/files/lookup?filename=${encodeURIComponent(filename)}`
);
const data = await response.json();
const data: unknown = await response.json();
if (!isLookupResponse(data)) {
throw new Error('Invalid lookup response received from API');
}
@@ -100,7 +97,7 @@ export const saveFile = async (
body: content,
}
);
const data = await response.json();
const data: unknown = await response.json();
if (!isSaveFileResponse(data)) {
throw new Error('Invalid save file response received from API');
}
@@ -136,11 +133,15 @@ export const getLastOpenedFile = async (
const response = await apiCall(
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/files/last`
);
const data = await response.json();
if (!('lastOpenedFilePath' in data)) {
const data: unknown = await response.json();
if (
typeof data !== 'object' ||
data === null ||
!('lastOpenedFilePath' in data)
) {
throw new Error('Invalid last opened file response received from API');
}
return data.lastOpenedFilePath;
return data.lastOpenedFilePath as string;
};
/**

View File

@@ -1,6 +1,6 @@
import { API_BASE_URL } from '@/types/authApi';
import { API_BASE_URL } from '@/types/api';
import { apiCall } from './api';
import type { CommitHash } from '@/types/git';
import type { CommitHash } from '@/types/models';
/**
* pullChanges fetches the latest changes from the remote repository
@@ -14,11 +14,11 @@ export const pullChanges = async (workspaceName: string): Promise<string> => {
method: 'POST',
}
);
const data = await response.json();
if (!('message' in data)) {
const data: unknown = await response.json();
if (typeof data !== 'object' || data === null || !('message' in data)) {
throw new Error('Invalid pull response received from API');
}
return data.message;
return data.message as string;
};
/**
@@ -42,8 +42,8 @@ export const commitAndPush = async (
body: JSON.stringify({ message }),
}
);
const data = await response.json();
if (!('commitHash' in data)) {
const data: unknown = await response.json();
if (typeof data !== 'object' || data === null || !('commitHash' in data)) {
throw new Error('Invalid commit response received from API');
}
return data.commitHash as CommitHash;

View File

@@ -1,7 +1,6 @@
import type { User } from '@/types/authApi';
import { API_BASE_URL, isUser } from '@/types/authApi';
import { API_BASE_URL, type UpdateProfileRequest } from '@/types/api';
import { isUser, type User } from '@/types/models';
import { apiCall } from './api';
import type { UpdateProfileRequest } from '@/types/userApi';
/**
* updateProfile updates the user's profile information.
@@ -16,12 +15,12 @@ export const updateProfile = async (
method: 'PUT',
body: JSON.stringify(updateRequest),
});
const data = response.json();
const data: unknown = response.json();
if (!isUser(data)) {
throw new Error('Invalid user data');
}
return data as User;
return data;
};
/**

View File

@@ -1,7 +1,6 @@
import { API_BASE_URL } from '@/types/authApi';
import { type Workspace, isWorkspace } from '@/types/models';
import { apiCall } from './api';
import type { Workspace } from '@/types/workspace';
import { isWorkspace } from '@/types/workspace';
import { API_BASE_URL } from '@/types/api';
/**
* listWorkspaces fetches the list of workspaces
@@ -10,7 +9,7 @@ import { isWorkspace } from '@/types/workspace';
*/
export const listWorkspaces = async (): Promise<Workspace[]> => {
const response = await apiCall(`${API_BASE_URL}/workspaces`);
const data = await response.json();
const data: unknown = await response.json();
if (!Array.isArray(data)) {
throw new Error('Invalid workspaces response received from API');
}
@@ -36,7 +35,7 @@ export const createWorkspace = async (name: string): Promise<Workspace> => {
},
body: JSON.stringify({ name }),
});
const data = await response.json();
const data: unknown = await response.json();
if (!isWorkspace(data)) {
throw new Error('Invalid workspace object received from API');
}
@@ -105,8 +104,12 @@ export const deleteWorkspace = async (
method: 'DELETE',
}
);
const data = await response.json();
if (!('nextWorkspaceName' in data)) {
const data: unknown = await response.json();
if (
typeof data !== 'object' ||
data === null ||
!('nextWorkspaceName' in data)
) {
throw new Error('Invalid delete workspace response received from API');
}
return data.nextWorkspaceName as string;
@@ -119,8 +122,12 @@ export const deleteWorkspace = async (
*/
export const getLastWorkspaceName = async (): Promise<string> => {
const response = await apiCall(`${API_BASE_URL}/workspaces/last`);
const data = await response.json();
if (!('lastWorkspaceName' in data)) {
const data: unknown = await response.json();
if (
typeof data !== 'object' ||
data === null ||
!('lastWorkspaceName' in data)
) {
throw new Error('Invalid last workspace name response received from API');
}
return data.lastWorkspaceName as string;