Skip to content

Commit

Permalink
feat: better type definition for global axios()
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Jun 29, 2023
1 parent 597f1f7 commit 4956d47
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
29 changes: 5 additions & 24 deletions resources/client/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { captureException } from '@sentry/vue'
import axios from 'axios'
import type { AxiosError, AxiosStatic } from 'axios'
import { format } from 'date-fns'
import { enUS as en, id } from 'date-fns/locale'

declare global {
const dateFormat: (date: Date, fmt: string) => string
const numberFormat: (num: number) => string

interface Window {
dateFormat: (date: Date, fmt: string) => string
numberFormat: (num: number) => string
axios: AxiosStatic
dateFormat: typeof dateFormat
numberFormat: typeof numberFormat
}
}

Expand All @@ -24,24 +23,6 @@ window.numberFormat = (num: number) => {
return new Intl.NumberFormat(document.documentElement.lang).format(num)
}

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.withCredentials = true
axios.interceptors.response.use(response => response, (error: AxiosError) => {
// if (error.response?.status === 401)
// window.location.replace('/login')

if (error.response?.status === 422)
return error.response

// whatever you want to do with the error
captureException(error)
})

if (import.meta.env.VITE_API_URL)
axios.defaults.baseURL = import.meta.env.VITE_API_URL

window.axios = axios

// const dataLayer = window.dataLayer || []
// const gtag = window.gtag = (...args) => dataLayer.push(args)

Expand Down
25 changes: 25 additions & 0 deletions resources/client/modules/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { captureException } from '@sentry/vue'
import axios from 'axios'
import type { AxiosError } from 'axios'

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.withCredentials = true
axios.interceptors.response.use(response => response, (error: AxiosError) => {
// if (error.response?.status === 401)
// window.location.replace('/login')

if (error.response?.status === 422)
return error.response

// whatever you want to do with the error
captureException(error)
})

if (import.meta.env.VITE_API_URL)
axios.defaults.baseURL = import.meta.env.VITE_API_URL

window.axios = axios

export const install: AppModuleInstall = ({ app }): void => {
app.config.globalProperties.$axios = axios
}
7 changes: 7 additions & 0 deletions resources/client/shim.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AxiosStatic } from 'axios'
import type Router from 'ziggy/Router'
import { Ziggy } from '~/ziggy.cjs'

Expand All @@ -6,12 +7,18 @@ export {}
type ZiggyRoutes = typeof Ziggy.routes

declare global {
const axios: AxiosStatic
function route(): Router
function route(name: keyof ZiggyRoutes, params?: Record<string, any>, absolute?: boolean): string

interface Window {
axios: AxiosStatic
}
}

declare module 'vue' {
interface ComponentCustomProperties {
$route: typeof route
$axios: AxiosStatic
}
}

0 comments on commit 4956d47

Please sign in to comment.