Sink/app/utils/api.ts
wudi 7cadbfe2b1 refactor: Migrate project files and directories to the app directory using the new compatibilityVersion: 4 feature
- Moved `app.config.ts` to the `app` directory
- Migrated `.vue` files and other assets to the `app` directory
- Updated import paths to reflect the new file locations
2025-03-24 11:05:59 +08:00

19 lines
553 B
TypeScript

import { defu } from 'defu'
import { toast } from 'vue-sonner'
export function useAPI(api: string, options?: object): Promise<unknown> {
return $fetch(api, defu(options || {}, {
headers: {
Authorization: `Bearer ${localStorage.getItem('SinkSiteToken') || ''}`,
},
})).catch((error) => {
if (error?.status === 401) {
localStorage.removeItem('SinkSiteToken')
navigateTo('/dashboard/login')
}
if (error?.data?.statusMessage) {
toast(error?.data?.statusMessage)
}
return Promise.reject(error)
})
}