first init
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL,
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
export type ApiPagination = {
|
||||
current_page: number;
|
||||
per_page: number;
|
||||
total: number;
|
||||
last_page: number;
|
||||
from: number | null;
|
||||
to: number | null;
|
||||
has_more_pages: boolean;
|
||||
};
|
||||
|
||||
export type PaginatedApiResponse<T> = {
|
||||
success: boolean;
|
||||
data: T[];
|
||||
pagination: ApiPagination;
|
||||
message?: string;
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import { type Icon } from '@/components/ui/lucide'
|
||||
|
||||
export interface Menu {
|
||||
icon?: Icon
|
||||
title?: string
|
||||
route_name?: string
|
||||
params?: Record<string, unknown>
|
||||
badge?: number
|
||||
sub_menu?: Menu[]
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import axios from 'axios'
|
||||
|
||||
type ApiErrorBody = {
|
||||
message?: string
|
||||
errors?: Record<string, string[]>
|
||||
}
|
||||
|
||||
export function getApiErrorMessage(
|
||||
error: unknown,
|
||||
fallback = 'Something went wrong. Please try again.',
|
||||
): string {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const data = error.response?.data as ApiErrorBody | undefined
|
||||
|
||||
if (data?.errors) {
|
||||
const firstError = Object.values(data.errors).flat()[0]
|
||||
if (firstError) return firstError
|
||||
}
|
||||
|
||||
if (data?.message) return data.message
|
||||
|
||||
if (error.message) return error.message
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user