refactor(types): improve type safety and eliminate type assertions

- Replace type assertions with proper type guards in ProfileService
- Add isAxiosError type guard and improve error handling
- Clean up formatting and improve type safety in deepLinks service
- Remove type assertions in AccountViewView Vue component
- Improve code formatting and consistency across services
This commit is contained in:
Matthew Raymer
2025-08-19 03:37:20 +00:00
parent bc1214e9db
commit 9384f0083a
3 changed files with 84 additions and 41 deletions

View File

@@ -127,10 +127,10 @@ export class ProfileService {
logger.debug("Attempting to delete profile for DID:", activeDid);
logger.debug("Using partner API server:", this.partnerApiServer);
logger.debug("Request headers:", headers);
const url = `${this.partnerApiServer}/api/partner/userProfile`;
logger.debug("DELETE request URL:", url);
const response = await this.axios.delete(url, { headers });
if (response.status === 200 || response.status === 204) {
@@ -140,20 +140,22 @@ export class ProfileService {
logger.error("Unexpected response status when deleting profile:", {
status: response.status,
statusText: response.statusText,
data: response.data
data: response.data,
});
throw new Error(`Profile not deleted - HTTP ${response.status}: ${response.statusText}`);
throw new Error(
`Profile not deleted - HTTP ${response.status}: ${response.statusText}`,
);
}
} catch (error) {
if (this.isApiError(error) && error.response) {
const response = error.response as any; // Type assertion for error response
const response = error.response;
logger.error("API error deleting profile:", {
status: response.status,
statusText: response.statusText,
data: response.data,
url: (error as any).config?.url
url: this.getErrorUrl(error),
});
// Handle specific HTTP status codes
if (response.status === 204) {
logger.debug("Profile deleted successfully (204 No Content)");
@@ -163,7 +165,11 @@ export class ProfileService {
return true; // Consider this a success if profile doesn't exist
} else if (response.status === 400) {
logger.error("Bad request when deleting profile:", response.data);
throw new Error(`Profile deletion failed: ${response.data?.message || 'Bad request'}`);
const errorMessage =
typeof response.data === "string"
? response.data
: response.data?.message || "Bad request";
throw new Error(`Profile deletion failed: ${errorMessage}`);
} else if (response.status === 401) {
logger.error("Unauthorized to delete profile");
throw new Error("You are not authorized to delete this profile");
@@ -172,7 +178,7 @@ export class ProfileService {
throw new Error("You are not allowed to delete this profile");
}
}
logger.error("Error deleting profile:", errorStringForLog(error));
handleApiError(error as AxiosError, "/api/partner/userProfile");
return false;
@@ -244,11 +250,32 @@ export class ProfileService {
/**
* Type guard for API errors
*/
private isApiError(
error: unknown,
): error is { response?: { status?: number } } {
private isApiError(error: unknown): error is {
response?: {
status?: number;
statusText?: string;
data?: { message?: string } | string;
};
} {
return typeof error === "object" && error !== null && "response" in error;
}
/**
* Extract URL from AxiosError without type casting
*/
private getErrorUrl(error: unknown): string | undefined {
if (this.isAxiosError(error)) {
return error.config?.url;
}
return undefined;
}
/**
* Type guard for AxiosError
*/
private isAxiosError(error: unknown): error is AxiosError {
return error instanceof AxiosError;
}
}
/**