mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-12 18:22:16 +01:00
call api for organization create/update/delete and switch
This commit is contained in:
committed by
Constantin Graf
parent
e8f632f330
commit
6c0041d249
31
resources/js/utils/apiValidation.ts
Normal file
31
resources/js/utils/apiValidation.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import axios, { type AxiosError } from 'axios';
|
||||
|
||||
type ApiValidationResponse = {
|
||||
message?: string;
|
||||
errors?: Record<string, string[]>;
|
||||
};
|
||||
|
||||
export function isApiValidationError(error: unknown): error is AxiosError<ApiValidationResponse> {
|
||||
return axios.isAxiosError<ApiValidationResponse>(error) && error.response?.status === 422;
|
||||
}
|
||||
|
||||
export function getApiValidationFieldErrors(error: unknown): Record<string, string> {
|
||||
if (!isApiValidationError(error)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const fieldErrors: Record<string, string> = {};
|
||||
for (const [field, messages] of Object.entries(error.response?.data?.errors ?? {})) {
|
||||
if (Array.isArray(messages) && messages[0]) {
|
||||
fieldErrors[field] = messages[0];
|
||||
}
|
||||
}
|
||||
return fieldErrors;
|
||||
}
|
||||
|
||||
export function getApiValidationMessage(error: unknown, fallback: string): string {
|
||||
if (!isApiValidationError(error)) {
|
||||
return fallback;
|
||||
}
|
||||
return error.response?.data?.message ?? fallback;
|
||||
}
|
||||
Reference in New Issue
Block a user