Skip to content

Commit 3015120

Browse files
committed
add axiosInstance option
1 parent 2b0c4a1 commit 3015120

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

packages/react/src/createInertiaApp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Page, PageProps, PageResolver, router, setupProgress } from '@inertiajs/core'
22
import { ComponentType, FunctionComponent, Key, ReactElement, ReactNode, createElement } from 'react'
33
import { renderToString } from 'react-dom/server'
4+
import { AxiosInstance } from 'axios'
45
import App from './App'
56

67
type ReactInstance = ReactElement
@@ -46,6 +47,7 @@ type InertiaAppOptionsForCSR<SharedProps extends PageProps> = BaseInertiaAppOpti
4647
showSpinner?: boolean
4748
}
4849
setup(options: SetupOptions<HTMLElement, SharedProps>): CreateInertiaAppSetupReturnType
50+
axiosInstance?: AxiosInstance
4951
}
5052

5153
type CreateInertiaAppSSRContent = { head: string[]; body: string }
@@ -55,6 +57,7 @@ type InertiaAppOptionsForSSR<SharedProps extends PageProps> = BaseInertiaAppOpti
5557
render: typeof renderToString
5658
progress?: undefined
5759
setup(options: SetupOptions<null, SharedProps>): ReactInstance
60+
axiosInstance?: undefined
5861
}
5962

6063
export default async function createInertiaApp<SharedProps extends PageProps = PageProps>(
@@ -71,6 +74,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
7174
progress = {},
7275
page,
7376
render,
77+
axiosInstance,
7478
}: InertiaAppOptionsForCSR<SharedProps> | InertiaAppOptionsForSSR<SharedProps>): Promise<
7579
CreateInertiaAppSetupReturnType | CreateInertiaAppSSRContent
7680
> {
@@ -82,6 +86,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
8286

8387
let head = []
8488

89+
if (!isServer && axiosInstance) {
90+
router.setAxiosInstance(axiosInstance)
91+
}
92+
8593
const reactApp = await Promise.all([
8694
resolveComponent(initialPage.component),
8795
router.decryptHistory().catch(() => {}),

packages/svelte/src/createInertiaApp.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import escape from 'html-escape'
33
import type { ComponentType } from 'svelte'
44
import App, { type InertiaAppProps } from './components/App.svelte'
55
import type { ComponentResolver } from './types'
6+
import { AxiosInstance } from 'axios'
67

78
type SvelteRenderResult = { html: string; head: string; css?: { code: string } }
89
type AppComponent = ComponentType<App> & { render: (props: InertiaAppProps) => SvelteRenderResult }
@@ -24,6 +25,7 @@ interface CreateInertiaAppProps {
2425
showSpinner?: boolean
2526
}
2627
page?: Page
28+
axiosInstance?: AxiosInstance
2729
}
2830

2931
export default async function createInertiaApp({
@@ -32,12 +34,17 @@ export default async function createInertiaApp({
3234
setup,
3335
progress = {},
3436
page,
37+
axiosInstance
3538
}: CreateInertiaAppProps): InertiaAppResponse {
3639
const isServer = typeof window === 'undefined'
3740
const el = isServer ? null : document.getElementById(id)
3841
const initialPage: Page = page || JSON.parse(el?.dataset.page || '{}')
3942
const resolveComponent = (name: string) => Promise.resolve(resolve(name))
40-
43+
44+
if (!isServer && axiosInstance) {
45+
router.setAxiosInstance(axiosInstance)
46+
}
47+
4148
const [initialComponent] = await Promise.all([
4249
resolveComponent(initialPage.component),
4350
router.decryptHistory().catch(() => {}),

packages/vue3/src/createInertiaApp.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Page, router, setupProgress } from '@inertiajs/core'
22
import { DefineComponent, Plugin, App as VueApp, createSSRApp, h } from 'vue'
33
import App, { InertiaApp, InertiaAppProps, plugin } from './app'
4+
import { AxiosInstance } from 'axios'
45

56
interface CreateInertiaAppProps {
67
id?: string
@@ -17,6 +18,7 @@ interface CreateInertiaAppProps {
1718
}
1819
page?: Page
1920
render?: (app: VueApp) => Promise<string>
21+
axiosInstance?: AxiosInstance
2022
}
2123

2224
export default async function createInertiaApp({
@@ -27,6 +29,7 @@ export default async function createInertiaApp({
2729
progress = {},
2830
page,
2931
render,
32+
axiosInstance,
3033
}: CreateInertiaAppProps): Promise<{ head: string[]; body: string }> {
3134
const isServer = typeof window === 'undefined'
3235
const el = isServer ? null : document.getElementById(id)
@@ -35,6 +38,10 @@ export default async function createInertiaApp({
3538

3639
let head = []
3740

41+
if (!isServer && axiosInstance) {
42+
router.setAxiosInstance(axiosInstance)
43+
}
44+
3845
const vueApp = await Promise.all([
3946
resolveComponent(initialPage.component),
4047
router.decryptHistory().catch(() => {}),

0 commit comments

Comments
 (0)